forked from adamlaska/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
148 lines (109 loc) · 4.71 KB
/
__init__.py
File metadata and controls
148 lines (109 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import permissions.utils
class PermissionBase(object):
"""Mix-in class for permissions.
"""
def grant_permission(self, role, permission):
"""Grants passed permission to passed role. Returns True if the
permission was able to be added, otherwise False.
**Parameters:**
role
The role for which the permission should be granted.
permission
The permission which should be granted. Either a permission
object or the codename of a permission.
"""
return permissions.utils.grant_permission(self, role, permission)
def remove_permission(self, role, permission):
"""Removes passed permission from passed role. Returns True if the
permission has been removed.
**Parameters:**
role
The role for which a permission should be removed.
permission
The permission which should be removed. Either a permission object
or the codename of a permission.
"""
return permissions.utils.remove_permission(self, role, permission)
def has_permission(self, user, permission, roles=[]):
"""Returns True if the passed user has passed permission for this
instance. Otherwise False.
**Parameters:**
permission
The permission's codename which should be checked. Must be a
string with a valid codename.
user
The user for which the permission should be checked.
roles
If passed, these roles will be assigned to the user temporarily
before the permissions are checked.
"""
return permissions.utils.has_permission(self, user, permission, roles)
def check_permission(self, user, permission, roles=[]):
"""Raise Unauthorized if the the passed user hasn't passed permission
for this instance.
**Parameters:**
permission
The permission's codename which should be checked. Must be a
string with a valid codename.
user
The user for which the permission should be checked.
roles
If passed, these roles will be assigned to the user temporarily
before the permissions are checked.
"""
if not self.has_permission(user, permission, roles):
raise Unauthorized("User %s doesn't have permission %s for object %s" % (user, permission, obj.slug))
def add_inheritance_block(self, permission):
"""Adds an inheritance block for the passed permission.
**Parameters:**
permission
The permission for which an inheritance block should be added.
Either a permission object or the codename of a permission.
"""
return permissions.utils.add_inheritance_block(self, permission)
def remove_inheritance_block(self, permission):
"""Removes a inheritance block for the passed permission.
**Parameters:**
permission
The permission for which an inheritance block should be removed.
Either a permission object or the codename of a permission.
"""
return permissions.utils.remove_inheritance_block(self, permission)
def is_inherited(self, codename):
"""Returns True if the passed permission is inherited.
**Parameters:**
codename
The permission which should be checked. Must be the codename of
the permission.
"""
return permissions.utils.is_inherited(self, codename)
def add_role(self, principal, role):
"""Adds a local role for the principal.
**Parameters:**
principal
The principal (user or group) which gets the role.
role
The role which is assigned.
"""
return permissions.utils.add_local_role(self, principal, role)
def get_roles(self, principal):
"""Returns *direct* local roles for passed principal (user or group).
"""
return permissions.utils.get_local_roles(self, principal)
def remove_role(self, principal, role):
"""Adds a local role for the principal to the object.
**Parameters:**
principal
The principal (user or group) from which the role is removed.
role
The role which is removed.
"""
return permissions.utils.remove_local_role(self, principal, role)
def remove_roles(self, principal):
"""Removes all local roles for the passed principal from the object.
**Parameters:**
principal
The principal (user or group) from which all local roles are
removed.
"""
return permissions.utils.remove_local_roles(self, principal)