@@ 11-137 (lines=127) @@ | ||
8 | from permission.compat import is_authenticated |
|
9 | ||
10 | ||
11 | class CollaboratorsPermissionLogic(PermissionLogic): |
|
12 | """ |
|
13 | Permission logic class for collaborators based permission system |
|
14 | """ |
|
15 | def __init__(self, |
|
16 | field_name=None, |
|
17 | any_permission=None, |
|
18 | change_permission=None, |
|
19 | delete_permission=None): |
|
20 | """ |
|
21 | Constructor |
|
22 | ||
23 | Parameters |
|
24 | ---------- |
|
25 | field_name : string |
|
26 | A field name of object which store the collaborators as django |
|
27 | relational fields for django user model. |
|
28 | You can specify the related object with '__' like django queryset |
|
29 | filter. |
|
30 | Default value will be taken from |
|
31 | ``PERMISSION_DEFAULT_COLLABORATORS_PERMISSION_LOGIC_FIELD_NAME`` in |
|
32 | settings. |
|
33 | any_permission : boolean |
|
34 | True for give any permission of the specified object to the |
|
35 | collaborators. |
|
36 | Default value will be taken from |
|
37 | ``PERMISSION_DEFAULT_COLLABORATORS_PERMISSION_LOGIC_ANY_PERMISSION`` |
|
38 | in settings. |
|
39 | change_permission : boolean |
|
40 | True for give change permission of the specified object to the |
|
41 | collaborators. |
|
42 | It will be ignored if :attr:`any_permission` is True. |
|
43 | Default value will be taken from |
|
44 | ``PERMISSION_DEFAULT_COLLABORATORS_PERMISSION_LOGIC_CHANGE_PERMISSION`` |
|
45 | in settings. |
|
46 | delete_permission : boolean |
|
47 | True for give delete permission of the specified object to the |
|
48 | collaborators. |
|
49 | It will be ignored if :attr:`any_permission` is True. |
|
50 | Default value will be taken from |
|
51 | ``PERMISSION_DEFAULT_COLLABORATORS_PERMISSION_LOGIC_DELETE_PERMISSION`` |
|
52 | in settings. |
|
53 | """ |
|
54 | self.field_name = field_name |
|
55 | self.any_permission = any_permission |
|
56 | self.change_permission = change_permission |
|
57 | self.delete_permission = delete_permission |
|
58 | ||
59 | if self.field_name is None: |
|
60 | self.field_name = \ |
|
61 | settings.PERMISSION_DEFAULT_CPL_FIELD_NAME |
|
62 | if self.any_permission is None: |
|
63 | self.any_permission = \ |
|
64 | settings.PERMISSION_DEFAULT_CPL_ANY_PERMISSION |
|
65 | if self.change_permission is None: |
|
66 | self.change_permission = \ |
|
67 | settings.PERMISSION_DEFAULT_CPL_CHANGE_PERMISSION |
|
68 | if self.delete_permission is None: |
|
69 | self.delete_permission = \ |
|
70 | settings.PERMISSION_DEFAULT_CPL_DELETE_PERMISSION |
|
71 | ||
72 | def has_perm(self, user_obj, perm, obj=None): |
|
73 | """ |
|
74 | Check if user have permission (of object) |
|
75 | ||
76 | If the user_obj is not authenticated, it return ``False``. |
|
77 | ||
78 | If no object is specified, it return ``True`` when the corresponding |
|
79 | permission was specified to ``True`` (changed from v0.7.0). |
|
80 | This behavior is based on the django system. |
|
81 | https://code.djangoproject.com/wiki/RowLevelPermissions |
|
82 | ||
83 | ||
84 | If an object is specified, it will return ``True`` if the user is |
|
85 | found in ``field_name`` of the object (e.g. ``obj.collaborators``). |
|
86 | So once the object store the user as a collaborator in |
|
87 | ``field_name`` attribute (default: ``collaborators``), the collaborator |
|
88 | can change or delete the object (you can change this behavior to set |
|
89 | ``any_permission``, ``change_permission`` or ``delete_permission`` |
|
90 | attributes of this instance). |
|
91 | ||
92 | Parameters |
|
93 | ---------- |
|
94 | user_obj : django user model instance |
|
95 | A django user model instance which be checked |
|
96 | perm : string |
|
97 | `app_label.codename` formatted permission string |
|
98 | obj : None or django model instance |
|
99 | None or django model instance for object permission |
|
100 | ||
101 | Returns |
|
102 | ------- |
|
103 | boolean |
|
104 | Whether the specified user have specified permission (of specified |
|
105 | object). |
|
106 | """ |
|
107 | if not is_authenticated(user_obj): |
|
108 | return False |
|
109 | # construct the permission full name |
|
110 | change_permission = self.get_full_permission_string('change') |
|
111 | delete_permission = self.get_full_permission_string('delete') |
|
112 | if obj is None: |
|
113 | # object permission without obj should return True |
|
114 | # Ref: https://code.djangoproject.com/wiki/RowLevelPermissions |
|
115 | if self.any_permission: |
|
116 | return True |
|
117 | if self.change_permission and perm == change_permission: |
|
118 | return True |
|
119 | if self.delete_permission and perm == delete_permission: |
|
120 | return True |
|
121 | return False |
|
122 | elif user_obj.is_active: |
|
123 | # get collaborator queryset |
|
124 | collaborators = field_lookup(obj, self.field_name) |
|
125 | if hasattr(collaborators, 'all'): |
|
126 | collaborators = collaborators.all() |
|
127 | if user_obj in collaborators: |
|
128 | if self.any_permission: |
|
129 | # have any kind of permissions to the obj |
|
130 | return True |
|
131 | if (self.change_permission and |
|
132 | perm == change_permission): |
|
133 | return True |
|
134 | if (self.delete_permission and |
|
135 | perm == delete_permission): |
|
136 | return True |
|
137 | return False |
|
138 |
@@ 11-133 (lines=123) @@ | ||
8 | from permission.compat import is_authenticated |
|
9 | ||
10 | ||
11 | class AuthorPermissionLogic(PermissionLogic): |
|
12 | """ |
|
13 | Permission logic class for author based permission system |
|
14 | """ |
|
15 | def __init__(self, |
|
16 | field_name=None, |
|
17 | any_permission=None, |
|
18 | change_permission=None, |
|
19 | delete_permission=None): |
|
20 | """ |
|
21 | Constructor |
|
22 | ||
23 | Parameters |
|
24 | ---------- |
|
25 | field_name : string |
|
26 | A field name of object which store the author as django user model. |
|
27 | You can specify the related object with '__' like django queryset |
|
28 | filter. |
|
29 | Default value will be taken from |
|
30 | ``PERMISSION_DEFAULT_APL_FIELD_NAME`` in |
|
31 | settings. |
|
32 | any_permission : boolean |
|
33 | True for give any permission of the specified object to the author |
|
34 | Default value will be taken from |
|
35 | ``PERMISSION_DEFAULT_APL_ANY_PERMISSION`` in |
|
36 | settings. |
|
37 | change_permission : boolean |
|
38 | True for give change permission of the specified object to the |
|
39 | author. |
|
40 | It will be ignored if :attr:`any_permission` is True. |
|
41 | Default value will be taken from |
|
42 | ``PERMISSION_DEFAULT_APL_CHANGE_PERMISSION`` in |
|
43 | settings. |
|
44 | delete_permission : boolean |
|
45 | True for give delete permission of the specified object to the |
|
46 | author. |
|
47 | It will be ignored if :attr:`any_permission` is True. |
|
48 | Default value will be taken from |
|
49 | ``PERMISSION_DEFAULT_APL_DELETE_PERMISSION`` in |
|
50 | settings. |
|
51 | """ |
|
52 | self.field_name = field_name |
|
53 | self.any_permission = any_permission |
|
54 | self.change_permission = change_permission |
|
55 | self.delete_permission = delete_permission |
|
56 | ||
57 | if self.field_name is None: |
|
58 | self.field_name = \ |
|
59 | settings.PERMISSION_DEFAULT_APL_FIELD_NAME |
|
60 | if self.any_permission is None: |
|
61 | self.any_permission = \ |
|
62 | settings.PERMISSION_DEFAULT_APL_ANY_PERMISSION |
|
63 | if self.change_permission is None: |
|
64 | self.change_permission = \ |
|
65 | settings.PERMISSION_DEFAULT_APL_CHANGE_PERMISSION |
|
66 | if self.delete_permission is None: |
|
67 | self.delete_permission = \ |
|
68 | settings.PERMISSION_DEFAULT_APL_DELETE_PERMISSION |
|
69 | ||
70 | def has_perm(self, user_obj, perm, obj=None): |
|
71 | """ |
|
72 | Check if user have permission (of object) |
|
73 | ||
74 | If the user_obj is not authenticated, it return ``False``. |
|
75 | ||
76 | If no object is specified, it return ``True`` when the corresponding |
|
77 | permission was specified to ``True`` (changed from v0.7.0). |
|
78 | This behavior is based on the django system. |
|
79 | https://code.djangoproject.com/wiki/RowLevelPermissions |
|
80 | ||
81 | If an object is specified, it will return ``True`` if the user is |
|
82 | specified in ``field_name`` of the object (e.g. ``obj.author``). |
|
83 | So once user create an object and the object store who is the author in |
|
84 | ``field_name`` attribute (default: ``author``), the author can change |
|
85 | or delete the object (you can change this behavior to set |
|
86 | ``any_permission``, ``change_permissino`` or ``delete_permission`` |
|
87 | attributes of this instance). |
|
88 | ||
89 | Parameters |
|
90 | ---------- |
|
91 | user_obj : django user model instance |
|
92 | A django user model instance which be checked |
|
93 | perm : string |
|
94 | `app_label.codename` formatted permission string |
|
95 | obj : None or django model instance |
|
96 | None or django model instance for object permission |
|
97 | ||
98 | Returns |
|
99 | ------- |
|
100 | boolean |
|
101 | Whether the specified user have specified permission (of specified |
|
102 | object). |
|
103 | """ |
|
104 | if not is_authenticated(user_obj): |
|
105 | return False |
|
106 | # construct the permission full name |
|
107 | change_permission = self.get_full_permission_string('change') |
|
108 | delete_permission = self.get_full_permission_string('delete') |
|
109 | # check if the user is authenticated |
|
110 | if obj is None: |
|
111 | # object permission without obj should return True |
|
112 | # Ref: https://code.djangoproject.com/wiki/RowLevelPermissions |
|
113 | if self.any_permission: |
|
114 | return True |
|
115 | if self.change_permission and perm == change_permission: |
|
116 | return True |
|
117 | if self.delete_permission and perm == delete_permission: |
|
118 | return True |
|
119 | return False |
|
120 | elif user_obj.is_active: |
|
121 | # get author instance |
|
122 | author = field_lookup(obj, self.field_name) |
|
123 | if author == user_obj: |
|
124 | if self.any_permission: |
|
125 | # have any kind of permissions to the obj |
|
126 | return True |
|
127 | if (self.change_permission and |
|
128 | perm == change_permission): |
|
129 | return True |
|
130 | if (self.delete_permission and |
|
131 | perm == delete_permission): |
|
132 | return True |
|
133 | return False |
|
134 |