Legend:

An example of row level permissions would be: "User A has read-access to article 234" or "User D has read, write access to article 234".

5

=== What are row-level permissions? ===

6

7

Some examples of row-level permissions: "User A has read-access to article 234" or "User D has read, write access to article 234."

6

8

7

9

=== Why do we need this? ===

8

10

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:

The permission parameter can either be the codename of the permission or a permission instance. The negative parameter is optional and will default to false. You must pass an instance of the object and owner to this method.

56

57

The second is create_default_row_permissions:

69

# ...

70

}}}

71

72

The permission parameter can either be the codename of the permission or a permission instance. The negative parameter is optional and defaults to {{{False}}}. You must pass an instance of the object and owner to this method.

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:

To check whether a user has permission to edit an object, use the {{{has_perm()}}} method on the {{{User}}} object:

94

95

{{{

96

#!python

83

97

user.has_perm("mine.can_mine", object=mineral)

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: