1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
use Elgg\Roles\Api;
|
4
|
|
|
|
5
|
|
|
/**
|
6
|
|
|
* Roles library functions
|
7
|
|
|
*
|
8
|
|
|
* @package Roles
|
9
|
|
|
* @author Andras Szepeshazi
|
10
|
|
|
* @copyright Arck Interactive, LLC 2012
|
11
|
|
|
* @link http://www.arckinteractive.com/
|
12
|
|
|
*/
|
13
|
|
|
|
14
|
|
|
/**
|
15
|
|
|
* Obtains the role of a given user
|
16
|
|
|
*
|
17
|
|
|
* @param ElggUser $user User entity
|
18
|
|
|
* @return ElggRole The role the user belongs to
|
19
|
|
|
*/
|
20
|
|
|
function roles_get_role($user = null) {
|
21
|
|
|
$user = isset($user) ? $user : elgg_get_logged_in_user_entity();
|
22
|
|
|
if ($user instanceof ElggUser) {
|
23
|
|
|
return roles()->getRole($user);
|
24
|
|
|
}
|
25
|
|
|
return roles()->getRoleByName(roles()->filterName(Api::NO_ROLE));
|
26
|
|
|
}
|
27
|
|
|
|
28
|
|
|
/**
|
29
|
|
|
* Checks if the user has a specific role
|
30
|
|
|
*
|
31
|
|
|
* @param ElggUser $user
|
32
|
|
|
* @return bool True if the user belongs to the passed role, false otherwise
|
33
|
|
|
*/
|
34
|
|
|
function roles_has_role($user = null, $role_name = DEFAULT_ROLE) {
|
35
|
|
|
return roles()->hasRole($user, $role_name);
|
36
|
|
|
}
|
37
|
|
|
|
38
|
|
|
/**
|
39
|
|
|
* Assigns a role to a particular user
|
40
|
|
|
*
|
41
|
|
|
* @param ElggRole $role The role to be assigned
|
42
|
|
|
* @param ElggUser $user The user the role needs to be assigned to
|
43
|
|
|
* @return bool|void True if the role change was successful, false if could not update user role, and null if there was no change in user role
|
44
|
|
|
*/
|
45
|
|
|
function roles_set_role($role, $user = null) {
|
46
|
|
|
$user = isset($user) ? $user : elgg_get_logged_in_user_entity();
|
47
|
|
|
if ($user instanceof ElggUser && $role instanceof ElggRole) {
|
48
|
|
|
return roles()->setRole($user, $role);
|
49
|
|
|
}
|
50
|
|
|
return false;
|
51
|
|
|
}
|
52
|
|
|
|
53
|
|
|
/**
|
54
|
|
|
* Gets all role objects
|
55
|
|
|
* @return ElggRole[]|false An array of ElggRole objects defined in the system, or false if none found
|
56
|
|
|
*/
|
57
|
|
|
function roles_get_all_roles() {
|
58
|
|
|
return roles()->getAll();
|
59
|
|
|
}
|
60
|
|
|
|
61
|
|
|
/**
|
62
|
|
|
* Gets all non-default role objects
|
63
|
|
|
*
|
64
|
|
|
* This is used by the role selector view. Default roles (VISITOR_ROLE, ADMIN_ROLE, DEFAULT_ROLE) need to be omitted from
|
65
|
|
|
* the list of selectable roles - as default roles are automatically assigned to users based on their Elgg membership type
|
66
|
|
|
*
|
67
|
|
|
* @return ElggRole[]|false An array of non-default ElggRole objects defined in the system, or false if none found
|
68
|
|
|
*/
|
69
|
|
|
function roles_get_all_selectable_roles() {
|
70
|
|
|
return roles()->getSelectable();
|
71
|
|
|
}
|
72
|
|
|
|
73
|
|
|
/**
|
74
|
|
|
* Obtains a list of permissions associated with a particular role object
|
75
|
|
|
*
|
76
|
|
|
* @param ElggRole $role The role to check for permissions
|
77
|
|
|
* @param string $permission_type The section from the configuration array ('actions', 'menus', 'views', etc.)
|
78
|
|
|
* @return array The permission rules for the given role and permission type
|
79
|
|
|
*/
|
80
|
|
|
function roles_get_role_permissions($role = null, $permission_type = null) {
|
81
|
|
|
$role = isset($role) ? $role : roles_get_role();
|
82
|
|
|
return roles()->getPermissions($role, $permission_type);
|
83
|
|
|
}
|
84
|
|
|
|
85
|
|
|
/**
|
86
|
|
|
* Caches permissions associated with a role object. Also resolves all role extensions.
|
87
|
|
|
*
|
88
|
|
|
* @param ElggRole $role The role to cache permissions for
|
89
|
|
|
* @return void
|
90
|
|
|
*/
|
91
|
|
|
function roles_cache_permissions($role) {
|
92
|
|
|
return roles()->cachePermissions($role);
|
93
|
|
|
}
|
94
|
|
|
|
95
|
|
|
/**
|
96
|
|
|
* Gets a role object based on it's name
|
97
|
|
|
*
|
98
|
|
|
* @param string $role_name The name of the role
|
99
|
|
|
* @return ElggRole|false An ElggRole object if it could be found based on the name, false otherwise
|
100
|
|
|
*/
|
101
|
|
|
function roles_get_role_by_name($role_name) {
|
102
|
|
|
return roles()->getRoleByName($role_name);
|
103
|
|
|
}
|
104
|
|
|
|
105
|
|
|
/**
|
106
|
|
|
* Resolves the default role for specified or currently logged in user
|
107
|
|
|
*
|
108
|
|
|
* @param string $role_name The name of the user's role
|
109
|
|
|
* @param int $user_guid GUID of the user whose default role needs to be resolved
|
110
|
|
|
* @return string
|
111
|
|
|
*/
|
112
|
|
|
function roles_filter_role_name($role_name, $user_guid = null) {
|
113
|
|
|
if (!isset($user_guid)) {
|
114
|
|
|
$user_guid = elgg_get_logged_in_user_guid();
|
115
|
|
|
}
|
116
|
|
|
return roles()->filterName($role_name, get_entity($user_guid) ? : null);
|
117
|
|
|
}
|
118
|
|
|
|
119
|
|
|
/**
|
120
|
|
|
* Processes the configuration files and generates the appropriate ElggRole objects.
|
121
|
|
|
*
|
122
|
|
|
* If, for any role definition, there is an already existing role with the same name,
|
123
|
|
|
* the role permissions will be updated for the given role object.
|
124
|
|
|
* If there is no previously existing, corresponding role object, it will be created now.
|
125
|
|
|
*
|
126
|
|
|
* @param array $roles_array The roles configuration array
|
127
|
|
|
* @return void
|
128
|
|
|
*/
|
129
|
|
|
function roles_create_from_config($roles_array) {
|
130
|
|
|
return roles()->createFromConfig($roles_array);
|
131
|
|
|
}
|
132
|
|
|
|
133
|
|
|
/**
|
134
|
|
|
* Checks if the configuration array has been updated and updates role objects accordingly if needed
|
135
|
|
|
* @return void
|
136
|
|
|
*/
|
137
|
|
|
function roles_check_update() {
|
138
|
|
|
return roles()->checkUpdate();
|
139
|
|
|
}
|
140
|
|
|
|
141
|
|
|
/**
|
142
|
|
|
* @deprecated 2.0
|
143
|
|
|
*/
|
144
|
|
|
function roles_unregister_menu_item() {
|
145
|
|
|
|
146
|
|
|
}
|
147
|
|
|
|
148
|
|
|
/**
|
149
|
|
|
* @deprecated 2.0
|
150
|
|
|
*/
|
151
|
|
|
function roles_replace_menu_item() {
|
152
|
|
|
|
153
|
|
|
}
|
154
|
|
|
|
155
|
|
|
/**
|
156
|
|
|
* @deprecated 2.0
|
157
|
|
|
*/
|
158
|
|
|
function roles_unregister_menu_item_recursive() {
|
159
|
|
|
|
160
|
|
|
}
|
161
|
|
|
|
162
|
|
|
/**
|
163
|
|
|
* @deprecated 2.0
|
164
|
|
|
*/
|
165
|
|
|
function roles_replace_menu_item_recursive() {
|
166
|
|
|
|
167
|
|
|
}
|
168
|
|
|
|
169
|
|
|
/**
|
170
|
|
|
* @deprecated 2.0
|
171
|
|
|
*/
|
172
|
|
|
function roles_find_menu_index() {
|
173
|
|
|
|
174
|
|
|
}
|
175
|
|
|
|
176
|
|
|
/**
|
177
|
|
|
* Substitutes dynamic parts of a menu's target URL
|
178
|
|
|
*
|
179
|
|
|
* @param array $vars An associative array holding the menu permissions
|
180
|
|
|
* @return The substituted menu permission array
|
181
|
|
|
*/
|
182
|
|
|
function roles_prepare_menu_vars($vars) {
|
183
|
|
|
return roles()->prepareMenuVars($vars);
|
184
|
|
|
}
|
185
|
|
|
|
186
|
|
|
/**
|
187
|
|
|
* @deprecated 2.0
|
188
|
|
|
*/
|
189
|
|
|
function roles_get_menu($menu_name) {
|
|
|
|
|
190
|
|
|
|
191
|
|
|
}
|
192
|
|
|
|
193
|
|
|
/**
|
194
|
|
|
* Replaces certain parts of path and URL type definitions with dynamic values
|
195
|
|
|
*
|
196
|
|
|
* @param string $str The string to operate on
|
197
|
|
|
* @return string The updated, substituted string
|
198
|
|
|
*/
|
199
|
|
|
function roles_replace_dynamic_paths($str) {
|
200
|
|
|
return roles()->replaceDynamicPaths($str);
|
201
|
|
|
}
|
202
|
|
|
|
203
|
|
|
/**
|
204
|
|
|
* Checks if a path or URL type rule matches a given path. Also processes regular expressions
|
205
|
|
|
*
|
206
|
|
|
* @param string $rule The permission rule to check
|
207
|
|
|
* @param string $path The path to match against
|
208
|
|
|
* @return boole True if the rule matches the path, false otherwise
|
209
|
|
|
*/
|
210
|
|
|
function roles_path_match($rule, $path) {
|
211
|
|
|
return roles()->matchPath($rule, $path);
|
212
|
|
|
}
|
213
|
|
|
|
214
|
|
|
/**
|
215
|
|
|
* Checks if a permission rule should be executed for the current context
|
216
|
|
|
*
|
217
|
|
|
* @param string $permission_details The permission rule configuration
|
218
|
|
|
* @param boolean $strict If strict context matching should be used.
|
219
|
|
|
* If true, only the last context will be checked for the rule matching.
|
220
|
|
|
* If false, any context value in the context stack will be considered.
|
221
|
|
|
* @return bool True if the rule should be executed, false otherwise
|
222
|
|
|
*/
|
223
|
|
|
function roles_check_context($permission_details, $strict = false) {
|
224
|
|
|
return roles()->checkContext($permission_details, $strict);
|
225
|
|
|
}
|
226
|
|
|
|
227
|
|
|
/**
|
228
|
|
|
* Updates roles objects to 1.0.1 version
|
229
|
|
|
* @return bool
|
230
|
|
|
*/
|
231
|
|
|
function roles_update_100_to_101() {
|
232
|
|
|
|
233
|
|
|
// Remove all 'roles_hash' values from plugin settings
|
234
|
|
|
// This will force new storage of the configuration array hash
|
235
|
|
|
$dbprefix = elgg_get_config('dbprefix');
|
236
|
|
|
$statement = "DELETE from {$dbprefix}private_settings WHERE name = 'roles_hash'";
|
237
|
|
|
return delete_data($statement);
|
238
|
|
|
}
|
239
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.