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