1
|
|
|
# coding=utf-8 |
2
|
|
|
""" |
3
|
|
|
Permission logic module to manage users' self-modifications |
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 OneselfPermissionLogic(PermissionLogic): |
11
|
|
|
""" |
12
|
|
|
Permission logic class to manage users' self-modifications |
13
|
|
|
|
14
|
|
|
Written by quasiyoke. |
15
|
|
|
https://github.com/lambdalisue/django-permission/pull/27 |
16
|
|
|
""" |
17
|
|
|
def __init__(self, |
18
|
|
|
any_permission=None, |
19
|
|
|
change_permission=None, |
20
|
|
|
delete_permission=None): |
21
|
|
|
""" |
22
|
|
|
Constructor |
23
|
|
|
|
24
|
|
|
Parameters |
25
|
|
|
---------- |
26
|
|
|
any_permission : boolean |
27
|
|
|
True for give any permission of the user to himself. |
28
|
|
|
Default value will be taken from |
29
|
|
|
``PERMISSION_DEFAULT_OSPL_ANY_PERMISSION`` in |
30
|
|
|
settings. |
31
|
|
|
change_permission : boolean |
32
|
|
|
True for give change permission of the user to himself. |
33
|
|
|
It will be ignored if :attr:`any_permission` is True. |
34
|
|
|
Default value will be taken from |
35
|
|
|
``PERMISSION_DEFAULT_OSPL_CHANGE_PERMISSION`` in |
36
|
|
|
settings. |
37
|
|
|
delete_permission : boolean |
38
|
|
|
True for give delete permission of the user to himself. |
39
|
|
|
It will be ignored if :attr:`any_permission` is True. |
40
|
|
|
Default value will be taken from |
41
|
|
|
``PERMISSION_DEFAULT_OSPL_DELETE_PERMISSION`` in |
42
|
|
|
settings. |
43
|
|
|
""" |
44
|
|
|
self.any_permission = any_permission |
45
|
|
|
self.change_permission = change_permission |
46
|
|
|
self.delete_permission = delete_permission |
47
|
|
|
|
48
|
|
|
if self.any_permission is None: |
49
|
|
|
self.any_permission = \ |
50
|
|
|
settings.PERMISSION_DEFAULT_OSPL_ANY_PERMISSION |
51
|
|
|
if self.change_permission is None: |
52
|
|
|
self.change_permission = \ |
53
|
|
|
settings.PERMISSION_DEFAULT_OSPL_CHANGE_PERMISSION |
54
|
|
|
if self.delete_permission is None: |
55
|
|
|
self.delete_permission = \ |
56
|
|
|
settings.PERMISSION_DEFAULT_OSPL_DELETE_PERMISSION |
57
|
|
|
|
58
|
|
|
def has_perm(self, user_obj, perm, obj=None): |
59
|
|
|
""" |
60
|
|
|
Check if user have permission of himself |
61
|
|
|
|
62
|
|
|
If the user_obj is not authenticated, it return ``False``. |
63
|
|
|
|
64
|
|
|
If no object is specified, it return ``True`` when the corresponding |
65
|
|
|
permission was specified to ``True`` (changed from v0.7.0). |
66
|
|
|
This behavior is based on the django system. |
67
|
|
|
https://code.djangoproject.com/wiki/RowLevelPermissions |
68
|
|
|
|
69
|
|
|
If an object is specified, it will return ``True`` if the object is the |
70
|
|
|
user. |
71
|
|
|
So users can change or delete themselves (you can change this behavior |
72
|
|
|
to set ``any_permission``, ``change_permissino`` or |
73
|
|
|
``delete_permission`` attributes of this instance). |
74
|
|
|
|
75
|
|
|
Parameters |
76
|
|
|
---------- |
77
|
|
|
user_obj : django user model instance |
78
|
|
|
A django user model instance which be checked |
79
|
|
|
perm : string |
80
|
|
|
`app_label.codename` formatted permission string |
81
|
|
|
obj : None or django model instance |
82
|
|
|
None or django model instance for object permission |
83
|
|
|
|
84
|
|
|
Returns |
85
|
|
|
------- |
86
|
|
|
boolean |
87
|
|
|
Whether the specified user have specified permission (of specified |
88
|
|
|
object). |
89
|
|
|
""" |
90
|
|
|
if not is_authenticated(user_obj): |
91
|
|
|
return False |
92
|
|
|
# construct the permission full name |
93
|
|
|
change_permission = self.get_full_permission_string('change') |
94
|
|
|
delete_permission = self.get_full_permission_string('delete') |
95
|
|
|
# check if the user is authenticated |
96
|
|
|
if obj is None: |
97
|
|
|
# object permission without obj should return True |
98
|
|
|
# Ref: https://code.djangoproject.com/wiki/RowLevelPermissions |
99
|
|
|
if self.any_permission: |
100
|
|
|
return True |
101
|
|
|
if self.change_permission and perm == change_permission: |
102
|
|
|
return True |
103
|
|
|
if self.delete_permission and perm == delete_permission: |
104
|
|
|
return True |
105
|
|
|
return False |
106
|
|
|
elif user_obj.is_active: |
107
|
|
|
# check if the user trying to interact with himself |
108
|
|
|
if obj == user_obj: |
109
|
|
|
if self.any_permission: |
110
|
|
|
# have any kind of permissions to himself |
111
|
|
|
return True |
112
|
|
|
if (self.change_permission and |
113
|
|
|
perm == change_permission): |
114
|
|
|
return True |
115
|
|
|
if (self.delete_permission and |
116
|
|
|
perm == delete_permission): |
117
|
|
|
return True |
118
|
|
|
return False |
119
|
|
|
|