-
Notifications
You must be signed in to change notification settings - Fork 1
/
permissions_api.module
354 lines (332 loc) · 12.9 KB
/
permissions_api.module
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
// $Id: permissions_api.module,v 1.14 2010/03/09 14:47:22 ebeyrent Exp $
/**
* @file
* Provides functions for granting and revoking permissions for given roles
*
* @ingroup permissions_api
*/
/**
* Helper function to grant permissions to a given role.
*
* @param
* A string that represents a role name in the database. Ex: 'site administrator'
* @param
* An array of strings representing permissions to be granted to the role specified in the first parameter.
* - Ex. array('administer users', 'administer content types');
* @return
* boolean to indicate success or failure
* @see hook_perm()
* @see permissions_get_role()
* @see permissions_get_permissions_for_role()
*/
function permissions_grant_permissions($role_name, $new_permissions) {
$permissions = array();
$updated_permissions = '';
$role = permissions_get_role($role_name);
if($role->rid > 0) {
if (count($new_permissions) > 0) {
// Fetch the permissions string for the given role id
$permissions = permissions_get_permissions_for_role($role->name);
// Check to see if there are existing permissions
if(count($permissions) > 0) {
// Add the new permissions if the role doesn't already have the permission
foreach($new_permissions as $permission) {
if(! in_array($permission, $permissions)) {
$permissions[] = trim($permission);
}
}
// rebuild the permission string
$updated_permissions = join(', ', $permissions);
db_query("UPDATE {permission} SET perm = '%s' WHERE rid = %d", $updated_permissions, $role->rid);
return true;
}
else {
// No permissions have been set for this role, so we need to insert some
foreach($new_permissions as $permission){
$permissions[] = trim($permission);
}
// rebuild the permission string
$updated_permissions = join(', ', $permissions);
db_query('DELETE FROM {permission} WHERE rid = %d', $role->rid);
db_query("INSERT INTO {permission} (rid, perm, tid) VALUES(%d,'%s',%d)",$role->rid,$updated_permissions,0);
return true;
}
}
watchdog('permissions_api', 'permissions_grant_permissions: permissions to grant is empty, expecting array', null, WATCHDOG_ERROR);
return false;
}
watchdog('permissions_api', 'permissions_grant_permissions: failed to get role %rolename.', array('%rolename' => $role->name), WATCHDOG_ERROR);
return false;
}
/**
* Helper function to revoke permissions from a role.
*
* @param
* A string that represents a role name in the database. Ex: 'site administrator'
* @param
* An array of strings representing permissions to be revoked from the role specified in the first parameter.
* - Ex. array('administer users', 'administer content types');
* @return
* boolean to indicate success or failure
* @see hook_perm()
* @see permissions_get_role()
* @see permissions_get_permissions_for_role()
*/
function permissions_revoke_permissions($role_name, $new_permissions) {
$permissions = array();
$updated_permissions = '';
$role = permissions_get_role($role_name);
if($role->rid > 0) {
if (! empty($new_permissions)) {
$permissions = permissions_get_permissions_for_role($role->name);
if (count($permissions) > 0) {
// Unset the permission from the array
foreach($new_permissions as $permission){
$index = array_search($permission, $permissions);
if($index !== false){
unset($permissions[$index]);
}
}
// rebuild the permission string
$updated_permissions = join(', ', $permissions);
db_query("UPDATE {permission} SET perm = '%s' WHERE rid = %d", $updated_permissions, $role->rid);
return true;
}
watchdog('permissions_api', 'permissions_revoke_permissions: role %rolename has no permissions defined.', array('%rolename' => $role->name), WATCHDOG_WARNING);
return false;
}
watchdog('permissions_api', 'permissions_revoke_permissions: permissions to revoke is empty, expecting array', null, WATCHDOG_ERROR);
return false;
}
watchdog('permissions_api', 'permissions_revoke_permissions: failed to get role %rolename.', array('%rolename' => $role->name), WATCHDOG_ERROR);
return false;
}
/**
* Helper function to grant all defined permissions to a given role
*
* @param
* A string that represents a role name in the database. Ex: 'site administrator'
* @return
* boolean to indicate success or failure
* @see hook_perm()
* @see permissions_get_role()
*/
function permissions_grant_all_permissions($role_name) {
$updated_permissions = '';
$all_permissions = array();
$role = permissions_get_role($role_name);
if($role->rid > 0){
// Build a list of all the permissions defined by hook_perm
foreach (module_list(FALSE, FALSE, TRUE) as $module) {
if ($permissions = module_invoke($module, 'perm')) {
asort($permissions);
foreach ($permissions as $perm) {
$all_permissions[] = $perm;
}
}
}
// Build the permissions string
$updated_permissions = join(', ', $all_permissions);
$perm_string = db_result(db_query("SELECT perm FROM {permission} WHERE rid = %d", $role->rid));
// Check to see if there are existing permissions
if (strlen($perm_string) > 0) {
db_query("UPDATE {permission} SET perm = '%s' WHERE rid = %d", $updated_permissions, $role->rid);
}
else{
db_query("INSERT INTO {permission} (rid, perm, tid) VALUES(%d,'%s',%d)",$role->rid,$updated_permissions,0);
}
return true;
}
watchdog('permissions_api', 'permissions_grant_all_permissions: failed to get role %rolename.', array('%rolename' => $role->name), WATCHDOG_ERROR);
return false;
}
/**
* Helper function to grant all permissions defined by a given module to a given role
*
* @param
* A string that represents a role name in the database. Ex: 'site administrator'
* @param
* A string that represents the name of a module. Ex: 'panels'
* @return
* boolean to indicate success or failure
* @see hook_perm()
* @see permissions_get_role()
* @see permissions_get_permissions_for_role()
*/
function permissions_grant_all_permissions_by_module($role_name, $module) {
$updated_permissions = '';
$permissions = array();
$module_permissions = array();
$role = permissions_get_role($role_name);
if($role->rid > 0){
// Fetch the permissions string for the given role id
$permissions = permissions_get_permissions_for_role($role->name);
// Check to see if there are existing permissions
if (count($permissions) > 0) {
// Build a list of all the permissions defined by hook_perm
if ($module_permissions = module_invoke($module, 'perm')) {
asort($module_permissions);
foreach ($module_permissions as $perm) {
if(! in_array($perm, $permissions)) {
$permissions[] = $perm;
}
}
}
// Build the permissions string
$updated_permissions = join(', ', $permissions);
db_query("UPDATE {permission} SET perm = '%s' WHERE rid = %d", $updated_permissions, $role->rid);
return true;
}
else {
// Build a list of all the permissions defined by hook_perm
if ($module_permissions = module_invoke($module, 'perm')) {
asort($module_permissions);
foreach ($module_permissions as $perm) {
$permissions[] = $perm;
}
}
// Build the permissions string
$updated_permissions = join(', ', $permissions);
db_query("INSERT INTO {permission} (rid, perm, tid) VALUES(%d,'%s',%d)",$role->rid,$updated_permissions,0);
return true;
}
}
watchdog('permissions_api', 'permissions_grant_all_permissions_by_module: failed to get role %rolename.', array('%rolename' => $role->name), WATCHDOG_ERROR);
return false;
}
/**
* Helper function to revoke all permissions defined by a given module from a given role
*
* @param
* A string that represents a role name in the database. Ex: 'site administrator'
* @param
* A string that represents the name of a module. Ex: 'panels'
* @return
* boolean to indicate success or failure
* @see hook_perm()
* @see permissions_get_role()
* @see permissions_get_permissions_for_role()
*/
function permissions_revoke_all_permissions_by_module($role_name, $module) {
$updated_permissions = '';
$permissions = array();
$module_permissions = array();
$role = permissions_get_role($role_name);
if($role->rid > 0){
// Fetch the permissions string for the given role id
$permissions = permissions_get_permissions_for_role($role->name);
// Check to see if there are existing permissions
if (count($permissions) > 0) {
// Build a list of all the permissions defined by hook_perm
if ($module_permissions = module_invoke($module, 'perm')) {
asort($module_permissions);
foreach ($module_permissions as $perm) {
$index = array_search($perm, $permissions);
if($index >= 0){
unset($permissions[$index]);
}
}
}
// Build the permissions string
$updated_permissions = join(', ', $permissions);
db_query("UPDATE {permission} SET perm = '%s' WHERE rid = %d", $updated_permissions, $role->rid);
return true;
}
}
watchdog('permissions_api', 'permissions_revoke_all_permissions_by_module: failed to get role %rolename.', array('%rolename' => $role->name), WATCHDOG_ERROR);
return false;
}
/**
* Helper function to return an array of permissions for a given role
*
* @param
* A string that represents a role name in the database. Ex: 'site administrator'
* @return
* Array an strings representing permissions in the database for the given role
* @see hook_perm()
* @see permissions_get_role()
*/
function permissions_get_permissions_for_role($role_name){
$role = permissions_get_role($role_name);
$permissions = array();
if($role->rid > 0){
$perm_string = db_result(db_query("SELECT perm FROM {permission} WHERE rid = %d", $role->rid));
if (strlen($perm_string) > 0) {
$permissions = explode(',', $perm_string);
foreach($permissions as $index => $perm){
$permissions[$index] = trim($perm);
}
}
}
return $permissions;
}
/**
* Helper function to look up the role by name
*
* @param
* A string that represents a role name in the database. Ex: 'site administrator'
* @return
* Object representing a role
*/
function permissions_get_role($role_name) {
$role = db_fetch_object(db_query("SELECT * FROM {role} r WHERE r.name = '%s'", $role_name));
return $role;
}
/**
* Helper function to create a new role in the database
*
* @param
* String representing the name of the role to be created. Ex: 'site administrator'
* @return
* Returns either an object representing the newly created (or previously existing) role, or FALSE if there was an error
*/
function permissions_create_role($role_name) {
// Look up the role to see if it exists already
$role = permissions_get_role($role_name);
if(!$role) {
db_query("INSERT INTO {role} VALUES('', '%s')", $role_name);
$role = permissions_get_role($role_name);
if($role->rid > 0) {
return $role;
}
watchdog('permissions_api', 'permissions_create_role: failed to create role %rolename', array('%rolename' => $role->name), WATCHDOG_ERROR);
return false;
}
watchdog('permissions_api', 'permissions_create_role: role %rolename already exists, returning existing role', array('%rolename' => $role->name), WATCHDOG_WARNING);
return $role;
}
/**
* This function allows a role to inherit either all the permissions of another role
*
* @param
* A string that represents a role name in the database. Ex: 'site administrator'
* @param
* A string that represents another role name in the database. Ex: 'site editor'
* @return
* boolean to indicate success or failure
* @see hook_perm()
* @see permissions_get_role()
* @see permissions_get_permissions_for_role()
*/
function permissions_role_inherit($new_role, $orig_role) {
$role = permissions_get_role($orig_role);
if(! $role) {
watchdog('permissions_api', 'permissions_role_inherit: unable to inherit permissions from %rolename; role doesn\'t exist', array('%rolename' => $orig_role), WATCHDOG_ERROR);
return false;
}
$clone = permissions_get_role($new_role);
if(! $clone) {
watchdog('permissions_api', 'permissions_role_inherit: unable to inherit permissions into %new_role; role doesn\'t exist', array('%new_role' => $new_role), WATCHDOG_ERROR);
return false;
}
// Inherit all of the permissions
$role_permissions = permissions_get_permissions_for_role($role->name);
if(! empty($role_permissions)) {
permissions_grant_permissions($clone->name, $role_permissions);
return true;
}
watchdog('permissions_api', 'permissions_role_inherit: failed to retrieve permissions for role %rolename.', array('%rolename' => $role->name), WATCHDOG_ERROR);
return false;
}
?>