1 | # coding=utf-8 |
||
2 | """ |
||
3 | Permission logic module for author based permission system |
||
4 | """ |
||
5 | from permission.conf import settings |
||
6 | from permission.logics.base import PermissionLogic |
||
7 | from permission.compat import is_authenticated |
||
8 | |||
9 | |||
10 | class StaffPermissionLogic(PermissionLogic): |
||
11 | """ |
||
12 | Permission logic class for is_staff authority based permission system |
||
13 | """ |
||
14 | def __init__(self, |
||
15 | any_permission=None, |
||
16 | add_permission=None, |
||
17 | change_permission=None, |
||
18 | delete_permission=None): |
||
19 | """ |
||
20 | Constructor |
||
21 | |||
22 | Parameters |
||
23 | ---------- |
||
24 | any_permission : boolean |
||
25 | True for give any permission of the specified object to the staff |
||
26 | user. Default value will be taken from |
||
27 | ``PERMISSION_DEFAULT_SPL_ANY_PERMISSION`` in |
||
28 | settings. |
||
29 | add_permission : boolean |
||
30 | True for give change permission of the specified object to the |
||
31 | staff user. |
||
32 | It will be ignored if :attr:`any_permission` is True. |
||
33 | Default value will be taken from |
||
34 | ``PERMISSION_DEFAULT_SPL_ADD_PERMISSION`` in |
||
35 | settings. |
||
36 | change_permission : boolean |
||
37 | True for give change permission of the specified object to the |
||
38 | staff user. |
||
39 | It will be ignored if :attr:`any_permission` is True. |
||
40 | Default value will be taken from |
||
41 | ``PERMISSION_DEFAULT_SPL_CHANGE_PERMISSION`` in |
||
42 | settings. |
||
43 | delete_permission : boolean |
||
44 | True for give delete permission of the specified object to the |
||
45 | staff user. |
||
46 | It will be ignored if :attr:`any_permission` is True. |
||
47 | Default value will be taken from |
||
48 | ``PERMISSION_DEFAULT_SPL_DELETE_PERMISSION`` in |
||
49 | settings. |
||
50 | """ |
||
51 | self.any_permission = any_permission |
||
52 | self.add_permission = add_permission |
||
53 | self.change_permission = change_permission |
||
54 | self.delete_permission = delete_permission |
||
55 | |||
56 | if self.any_permission is None: |
||
57 | self.any_permission = \ |
||
58 | settings.PERMISSION_DEFAULT_SPL_ANY_PERMISSION |
||
59 | if self.add_permission is None: |
||
60 | self.add_permission = \ |
||
61 | settings.PERMISSION_DEFAULT_SPL_ADD_PERMISSION |
||
62 | if self.change_permission is None: |
||
63 | self.change_permission = \ |
||
64 | settings.PERMISSION_DEFAULT_SPL_CHANGE_PERMISSION |
||
65 | if self.delete_permission is None: |
||
66 | self.delete_permission = \ |
||
67 | settings.PERMISSION_DEFAULT_SPL_DELETE_PERMISSION |
||
68 | |||
69 | View Code Duplication | def has_perm(self, user_obj, perm, obj=None): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
70 | """ |
||
71 | Check if user have permission (of object) |
||
72 | |||
73 | If the user_obj is not authenticated, it return ``False``. |
||
74 | |||
75 | If no object is specified, it return ``True`` when the corresponding |
||
76 | permission was specified to ``True`` (changed from v0.7.0). |
||
77 | This behavior is based on the django system. |
||
78 | https://code.djangoproject.com/wiki/RowLevelPermissions |
||
79 | |||
80 | If an object is specified, it will return ``True`` if the user is |
||
81 | staff. The staff can add, change or delete the object (you can change |
||
82 | this behavior to set ``any_permission``, ``add_permission``, |
||
83 | ``change_permission``, or ``delete_permission`` attributes of this |
||
84 | instance). |
||
85 | |||
86 | Parameters |
||
87 | ---------- |
||
88 | user_obj : django user model instance |
||
89 | A django user model instance which be checked |
||
90 | perm : string |
||
91 | `app_label.codename` formatted permission string |
||
92 | obj : None or django model instance |
||
93 | None or django model instance for object permission |
||
94 | |||
95 | Returns |
||
96 | ------- |
||
97 | boolean |
||
98 | Weather the specified user have specified permission (of specified |
||
99 | object). |
||
100 | """ |
||
101 | if not is_authenticated(user_obj): |
||
102 | return False |
||
103 | # construct the permission full name |
||
104 | add_permission = self.get_full_permission_string('add') |
||
105 | change_permission = self.get_full_permission_string('change') |
||
106 | delete_permission = self.get_full_permission_string('delete') |
||
107 | if obj is None: |
||
108 | if user_obj.is_staff: |
||
109 | if self.add_permission and perm == add_permission: |
||
110 | return True |
||
111 | if self.change_permission and perm == change_permission: |
||
112 | return True |
||
113 | if self.delete_permission and perm == delete_permission: |
||
114 | return True |
||
115 | return self.any_permission |
||
116 | return False |
||
117 | elif user_obj.is_active: |
||
118 | if user_obj.is_staff: |
||
119 | if self.any_permission: |
||
120 | # have any kind of permissions to the obj |
||
121 | return True |
||
122 | if (self.add_permission and |
||
123 | perm == add_permission): |
||
124 | return True |
||
125 | if (self.change_permission and |
||
126 | perm == change_permission): |
||
127 | return True |
||
128 | if (self.delete_permission and |
||
129 | perm == delete_permission): |
||
130 | return True |
||
131 | return False |
||
132 |