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