9 | | An example of where this would be useful is a forum or message board. With the current permission system, a user is capable of editing all the posts or unable to edit any posts. After implementing a row level permission, it can be modified so a user is capable of editing only their own personal posts. |
10 | | |
11 | | == Using Row Level Permissions == |
12 | | |
13 | | === Basic Idea === |
14 | | |
15 | | There are a few things you need to know about row level permissions before working with them: |
16 | | |
17 | | * Row level permissions use the permissions table to determine an object's possible permissions, you need to create permissions in the permissions table before using them in row level permissions. |
18 | | * Row level permissions can be negative, this is determined by an attribute called "negative". |
19 | | * The order of checking permissions will work in the following order: User Row Level Permission -> Group Row Level Permission -> User Model Level Permission -> Group Model Level Permission. The checking will stop either at the first positive or negative, and if no permission is found will return a negative (or false). |
20 | | |
21 | | === Enabling Row Level Permissions === |
22 | | |
23 | | Enabling row level permissions is done by using the Meta class, you enable row level permissions by setting the "row_level_permissions" attribute to true. By default, row level permissions are assumed to be disabled. |
24 | | |
25 | | Example: To enable row level permissions for the mineral model, the model would look like: |
| 11 | With the current permission system, a user of Django's admin interface is capable either of editing *all* objects of a certain type or editing *none* of the objects of a certain type. After implementing a row level permission, the user can be capable of editing only certain objects of that type -- e.g., just the objects he created himself. |
| 12 | |
| 13 | === Status in Django === |
| 14 | |
| 15 | Row-level permissions are being implemented in a branch -- the [http://code.djangoproject.com/browser/django/branches/per-object-permissions per-object-permissions branch]. This branch needs your help in testing. To get the code, use this Subversion command: |
| 16 | |
| 17 | {{{ |
| 18 | svn co http://code.djangoproject.com/svn/django/branches/per-object-permissions |
| 19 | }}} |
| 20 | |
| 21 | == Using row-level permissions == |
| 22 | |
| 23 | === The basics === |
| 24 | |
| 25 | There are a few things you need to know about row-level permissions before working with them: |
| 26 | |
| 27 | * Row-level permissions use the permissions table to determine an object's possible permissions. You need to create permissions in the permissions table before using them in row-level permissions. |
| 28 | * Row-level permissions can be negative. This is determined by an attribute called "negative." |
| 29 | * Django checks permissions in the following order: |
| 30 | * User row-level permission |
| 31 | * Group row-level permission |
| 32 | * User model-level permission |
| 33 | * Group model-level permission |
| 34 | The checking stops either at the first positive or negative. If no permission is found, it will return a negative (or false). |
| 35 | |
| 36 | === Enabling row-level permissions === |
| 37 | |
| 38 | To enable row-level permissions for a model, add a {{{row_level_permissions = True}}} to the model's {{{class Meta}}}. By default, row-level permissions are disabled. |
| 39 | |
| 40 | For example: |
| 41 | |
43 | | You do not need the ''class Admin'' but it does create the default change, add and delete permissions for the object which are useful to have. |
44 | | |
45 | | === Creating a Row Level Permission === |
46 | | |
47 | | There are two helper methods to create row level permissions. These can be accessed by using the Row Level Permissions manager (e.g. RowLevelPermission.objects) |
48 | | |
49 | | The first is create_row_level_permission: |
| 58 | The {{{class Admin}}} is not required, but its presence does create the default add/change/delete model-level permissions, which are useful to have. |
| 59 | |
| 60 | === Creating a row-level permission === |
| 61 | |
| 62 | Django has two helper methods for use in creating row-level permissions. They both live on the {{{RowLevelPermission}}} model manager (i.e., {{{RowLevelPermission.objects}}}). |
| 63 | |
| 64 | The first helper function is {{{create_row_level_permission}}}: |
| 65 | |
61 | | ... |
62 | | }}} |
63 | | This will set up a row level permission with the default permissions set up for an object. The default permissions are: add, change and delete. |
64 | | |
65 | | An example of its use, this creates a change row level permission on the quartz object: |
66 | | {{{ |
67 | | #!python |
68 | | ... |
69 | | RowLevelPermissions.objects.create_default_row_level_permissions(quartz, user, delete=False) |
70 | | ... |
71 | | }}} |
72 | | |
73 | | === Checking Permissions === |
74 | | |
75 | | GenericAuthorization and row level permissions will eventually be merged. Therefore, I have shown two different methods of checking for permissions, one using the generic authorization and the currently implemented technique. |
76 | | |
77 | | The current method uses the has_perm method in the User model. Note: The object parameter is optional, this is to allow backwards compatibility, so if you do not want to check for row level permissions do not include the object parameter. |
78 | | |
79 | | Example: |
80 | | {{{ |
81 | | #!python |
82 | | ... |
| 79 | # ... |
| 80 | }}} |
| 81 | |
| 82 | This will set up a row-level permission with the default permissions set up for an object. The default permissions are: add, change and delete. |
| 83 | |
| 84 | For example, this creates a change row-level permission on the {{{quartz}}} object: |
| 85 | |
| 86 | {{{ |
| 87 | #!python |
| 88 | RowLevelPermission.objects.create_default_row_level_permissions(quartz, user, delete=False) |
| 89 | }}} |
| 90 | |
| 91 | === Checking permissions === |
| 92 | |
| 93 | To check whether a user has permission to edit an object, use the {{{has_perm()}}} method on the {{{User}}} object: |
| 94 | |
| 95 | {{{ |
| 96 | #!python |
84 | | ... |
85 | | }}} |
86 | | |
87 | | This will return either True or False depending on if the user has the correct permission. It will return false if the user has a negative row level permission on the object. |
88 | | |
89 | | This will also check group row level permissions. If the user is in two groups, the first having a positive row level permission and the second having a negative row level permission, it will take the positive row level permission over the negative. |
90 | | |
91 | | '''Second method using GenericAuthorization will be written after the merge. It will follow the documentation written on GenericAuthorization''' |
92 | | |
93 | | ==== Has Row Level Permission ==== |
94 | | |
95 | | The method contains_permission checks if the user has the given permission on a model (not an instance of a model but the model). This checks if there is exists the given row level permission on any of the instances of the model. It is used in the admin interface to determine if the change list should be shown to the user. |
96 | | |
97 | | E.g. If user A has change permission on article 234, and you did: |
98 | | {{{ |
99 | | #!python |
100 | | ... |
101 | | userA.contains_permission("mine.change_mineral", Mineral) |
| 98 | }}} |
| 99 | |
| 100 | The {{{object}}} parameter is optional for backwards-compatibility. If you don't want to check row-level permissions, exclude the {{{object}}} parameter. |
| 101 | |
| 102 | {{{has_perm()}}} returns either {{{True}}} or {{{False}}}. It returns {{{False}}} if the user has a negative row-level permission on the object. It also checks group row-level permissions. If the user is in two groups, the first having a positive row-level permission and the second having a negative row-level permission, it will take the positive permission over the negative. |
| 103 | |
| 104 | Note that the GenericAuthorization branch (yet another of Django's many current branches) implements a different way of checking permissions. See the GenericAuthorization page for how row-level permissions fit in with that scheme. |
| 105 | |
| 106 | ==== Has row-level permission ==== |
| 107 | |
| 108 | The method {{{contains_permission()}}} checks whether the user has the given permission on a model -- not an *instance* of a model but the model itself. This checks if there is exists the given row level permission on any of the instances of the model. It is used in the admin interface to determine if the change list should be shown to the user. |
| 109 | |
| 110 | E.g. If {{{some_user}}} has change permission on article 234, and you did: |
| 111 | {{{ |
| 112 | #!python |
| 113 | ... |
| 114 | some_user.contains_permission("mine.change_mineral", Mineral) |