bbbf2d40 |
1 | <?php |
cee0901c |
2 | /** |
3 | * Capability session information format |
bbbf2d40 |
4 | * 2 x 2 array |
5 | * [context][capability] |
6 | * where context is the context id of the table 'context' |
7 | * and capability is a string defining the capability |
8 | * e.g. |
9 | * |
10 | * [Capabilities] => [26][mod/forum:viewpost] = 1 |
11 | * [26][mod/forum:startdiscussion] = -8990 |
12 | * [26][mod/forum:editallpost] = -1 |
13 | * [273][moodle:blahblah] = 1 |
14 | * [273][moodle:blahblahblah] = 2 |
15 | */ |
bbbf2d40 |
16 | |
17 | // permission definitions |
18 | define('CAP_ALLOW', 1); |
19 | define('CAP_PREVENT', -1); |
20 | define('CAP_PROHIBIT', -1000); |
21 | |
bbbf2d40 |
22 | // context definitions |
23 | define('CONTEXT_SYSTEM', 10); |
24 | define('CONTEXT_PERSONAL', 20); |
25 | define('CONTEXT_USERID', 30); |
26 | define('CONTEXT_COURSECAT', 40); |
27 | define('CONTEXT_COURSE', 50); |
28 | define('CONTEXT_GROUP', 60); |
29 | define('CONTEXT_MODULE', 70); |
30 | define('CONTEXT_BLOCK', 80); |
31 | |
340ea4e8 |
32 | $context_cache = array(); // Cache of all used context objects for performance (by level and instance) |
33 | $context_cache_id = array(); // Index to above cache by id |
bbbf2d40 |
34 | |
7700027f |
35 | |
e7876c1e |
36 | /** |
37 | * Loads the capabilities for the default guest role to the current user in a specific context |
38 | * @return object |
39 | */ |
40 | function load_guest_role($context=NULL) { |
41 | global $USER; |
42 | |
43 | static $guestrole; |
44 | |
45 | if (!isloggedin()) { |
46 | return false; |
47 | } |
48 | |
49 | if (!$sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID)) { |
50 | return false; |
51 | } |
52 | |
53 | if (empty($context)) { |
54 | $context = $sitecontext; |
55 | } |
56 | |
57 | if (empty($guestrole)) { |
58 | if ($roles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) { |
59 | $guestrole = array_shift($roles); // Pick the first one |
60 | } else { |
61 | return false; |
62 | } |
63 | } |
64 | |
65 | if ($capabilities = get_records_select('role_capabilities', |
66 | "roleid = $guestrole->id AND contextid = $sitecontext->id")) { |
67 | foreach ($capabilities as $capability) { |
68 | $USER->capabilities[$context->id][$capability->capability] = $capability->permission; |
69 | } |
70 | } |
71 | |
72 | return true; |
73 | } |
74 | |
7700027f |
75 | /** |
76 | * Load default not logged in role capabilities when user is not logged in |
77 | * @return bool |
78 | */ |
20aeb4b8 |
79 | function load_notloggedin_role() { |
80 | global $CFG, $USER; |
81 | |
7700027f |
82 | if (!$sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID)) { |
83 | return false; |
35a518c5 |
84 | } |
85 | |
7700027f |
86 | if (empty($CFG->notloggedinroleid)) { // Let's set the default to the guest role |
87 | if ($roles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) { |
88 | $role = array_shift($roles); // Pick the first one |
89 | set_config('notloggedinroleid', $role->id); |
90 | } else { |
91 | return false; |
92 | } |
93 | } |
20aeb4b8 |
94 | |
7700027f |
95 | if ($capabilities = get_records_select('role_capabilities', |
96 | "roleid = $CFG->notloggedinroleid AND contextid = $sitecontext->id")) { |
97 | foreach ($capabilities as $capability) { |
98 | $USER->capabilities[$sitecontext->id][$capability->capability] = $capability->permission; |
99 | } |
20aeb4b8 |
100 | } |
101 | |
102 | return true; |
103 | } |
cee0901c |
104 | |
bbbf2d40 |
105 | /** |
106 | * This functions get all the course categories in proper order |
0468976c |
107 | * @param int $context |
bbbf2d40 |
108 | * @param int $type |
109 | * @return array of contextids |
110 | */ |
0468976c |
111 | function get_parent_cats($context, $type) { |
98882637 |
112 | |
113 | $parents = array(); |
98882637 |
114 | |
c5ddc3fd |
115 | switch ($type) { |
98882637 |
116 | |
117 | case CONTEXT_COURSECAT: |
118 | |
c5ddc3fd |
119 | if (!$cat = get_record('course_categories','id',$context->instanceid)) { |
120 | break; |
121 | } |
122 | |
123 | while (!empty($cat->parent)) { |
124 | if (!$context = get_context_instance(CONTEXT_COURSECAT, $cat->parent)) { |
125 | break; |
126 | } |
98882637 |
127 | $parents[] = $context->id; |
128 | $cat = get_record('course_categories','id',$cat->parent); |
129 | } |
130 | |
131 | break; |
132 | |
133 | case CONTEXT_COURSE: |
134 | |
c5ddc3fd |
135 | if (!$course = get_record('course', 'id', $context->instanceid)) { |
136 | break; |
137 | } |
138 | if (!$catinstance = get_context_instance(CONTEXT_COURSECAT, $course->category)) { |
139 | break; |
140 | } |
141 | |
98882637 |
142 | $parents[] = $catinstance->id; |
c5ddc3fd |
143 | |
144 | if (!$cat = get_record('course_categories','id',$course->category)) { |
145 | break; |
146 | } |
147 | |
148 | while (!empty($cat->parent)) { |
149 | if (!$context = get_context_instance(CONTEXT_COURSECAT, $cat->parent)) { |
150 | break; |
151 | } |
98882637 |
152 | $parents[] = $context->id; |
153 | $cat = get_record('course_categories','id',$cat->parent); |
154 | } |
155 | break; |
156 | |
157 | default: |
158 | break; |
159 | |
160 | } |
161 | |
162 | return array_reverse($parents); |
bbbf2d40 |
163 | } |
164 | |
165 | |
cee0901c |
166 | |
167 | /************************************* |
168 | * Functions for Roles & Capabilites * |
169 | *************************************/ |
bbbf2d40 |
170 | |
171 | |
0468976c |
172 | /** |
173 | * This function checks for a capability assertion being true. If it isn't |
174 | * then the page is terminated neatly with a standard error message |
175 | * @param string $capability - name of the capability |
176 | * @param object $context - a context object (record from context table) |
177 | * @param integer $userid - a userid number |
178 | * @param string $errorstring - an errorstring |
179 | */ |
180 | function require_capability($capability, $context=NULL, $userid=NULL, $errormessage="nopermissions", $stringfile='') { |
a9e1c058 |
181 | |
182 | global $USER; |
183 | |
184 | /// If the current user is not logged in, then make sure they are |
185 | |
186 | if (empty($userid) and empty($USER->id)) { |
187 | if ($context && ($context->aggregatelevel == CONTEXT_COURSE)) { |
188 | require_login($context->instanceid); |
189 | } else { |
190 | require_login(); |
191 | } |
192 | } |
193 | |
194 | /// OK, if they still don't have the capability then print a nice error message |
195 | |
0468976c |
196 | if (!has_capability($capability, $context, $userid)) { |
197 | $capabilityname = get_capability_string($capability); |
198 | print_error($errormessage, $stringfile, '', $capabilityname); |
199 | } |
200 | } |
201 | |
202 | |
bbbf2d40 |
203 | /** |
204 | * This function returns whether the current user has the capability of performing a function |
205 | * For example, we can do has_capability('mod/forum:replypost',$cm) in forum |
206 | * only one of the 4 (moduleinstance, courseid, site, userid) would be set at 1 time |
207 | * This is a recursive funciton. |
bbbf2d40 |
208 | * @uses $USER |
209 | * @param string $capability - name of the capability |
0468976c |
210 | * @param object $context - a context object (record from context table) |
211 | * @param integer $userid - a userid number |
20aeb4b8 |
212 | * @param bool $doanything - if false, ignore do anything |
bbbf2d40 |
213 | * @return bool |
214 | */ |
20aeb4b8 |
215 | function has_capability($capability, $context=NULL, $userid=NULL, $doanything='true') { |
bbbf2d40 |
216 | |
20aeb4b8 |
217 | global $USER, $CONTEXT, $CFG; |
bbbf2d40 |
218 | |
dc411d1b |
219 | if (empty($userid) && !isloggedin() && !isset($USER->capabilities)) { |
20aeb4b8 |
220 | load_notloggedin_role(); |
221 | } |
222 | |
223 | if ($userid && $userid != $USER->id) { |
9425b25f |
224 | if (empty($USER->id) or ($userid != $USER->id)) { |
225 | $capabilities = load_user_capability($capability, $context, $userid); |
226 | } else { //$USER->id == $userid |
227 | $capabilities = empty($USER->capabilities) ? NULL : $USER->capabilities; |
228 | } |
229 | } else { // no userid |
230 | $capabilities = empty($USER->capabilities) ? NULL : $USER->capabilities; |
98882637 |
231 | } |
9425b25f |
232 | |
0468976c |
233 | if (empty($context)) { // Use default CONTEXT if none specified |
340ea4e8 |
234 | if (empty($CONTEXT)) { |
235 | return false; |
236 | } else { |
237 | $context = $CONTEXT; |
238 | } |
0468976c |
239 | } else { // A context was given to us |
240 | if (empty($CONTEXT)) { |
241 | $CONTEXT = $context; // Store FIRST used context in this global as future default |
242 | } |
340ea4e8 |
243 | } |
bbbf2d40 |
244 | |
20aeb4b8 |
245 | if ($doanything) { |
246 | // Check site |
247 | $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID); |
248 | if (isset($capabilities[$sitecontext->id]['moodle/site:doanything'])) { |
249 | return (0 < $capabilities[$sitecontext->id]['moodle/site:doanything']); |
250 | } |
98882637 |
251 | |
20aeb4b8 |
252 | switch ($context->aggregatelevel) { |
bbbf2d40 |
253 | |
20aeb4b8 |
254 | case CONTEXT_COURSECAT: |
255 | // Check parent cats. |
256 | $parentcats = get_parent_cats($context, CONTEXT_COURSECAT); |
257 | foreach ($parentcats as $parentcat) { |
258 | if (isset($capabilities[$parentcat]['moodle/site:doanything'])) { |
259 | return (0 < $capabilities[$parentcat]['moodle/site:doanything']); |
260 | } |
cee0901c |
261 | } |
20aeb4b8 |
262 | break; |
bbbf2d40 |
263 | |
20aeb4b8 |
264 | case CONTEXT_COURSE: |
265 | // Check parent cat. |
266 | $parentcats = get_parent_cats($context, CONTEXT_COURSE); |
98882637 |
267 | |
20aeb4b8 |
268 | foreach ($parentcats as $parentcat) { |
269 | if (isset($capabilities[$parentcat]['do_anything'])) { |
270 | return (0 < $capabilities[$parentcat]['do_anything']); |
271 | } |
9425b25f |
272 | } |
20aeb4b8 |
273 | break; |
bbbf2d40 |
274 | |
20aeb4b8 |
275 | case CONTEXT_GROUP: |
276 | // Find course. |
277 | $group = get_record('groups','id',$context->instanceid); |
278 | $courseinstance = get_context_instance(CONTEXT_COURSE, $group->courseid); |
9425b25f |
279 | |
20aeb4b8 |
280 | $parentcats = get_parent_cats($courseinstance, CONTEXT_COURSE); |
281 | foreach ($parentcats as $parentcat) { |
282 | if (isset($capabilities[$parentcat->id]['do_anything'])) { |
283 | return (0 < $capabilities[$parentcat->id]['do_anything']); |
284 | } |
9425b25f |
285 | } |
9425b25f |
286 | |
20aeb4b8 |
287 | $coursecontext = ''; |
288 | if (isset($capabilities[$courseinstance->id]['do_anything'])) { |
289 | return (0 < $capabilities[$courseinstance->id]['do_anything']); |
290 | } |
9425b25f |
291 | |
20aeb4b8 |
292 | break; |
bbbf2d40 |
293 | |
20aeb4b8 |
294 | case CONTEXT_MODULE: |
295 | // Find course. |
296 | $cm = get_record('course_modules', 'id', $context->instanceid); |
297 | $courseinstance = get_context_instance(CONTEXT_COURSE, $cm->course); |
9425b25f |
298 | |
20aeb4b8 |
299 | if ($parentcats = get_parent_cats($courseinstance, CONTEXT_COURSE)) { |
300 | foreach ($parentcats as $parentcat) { |
301 | if (isset($capabilities[$parentcat]['do_anything'])) { |
302 | return (0 < $capabilities[$parentcat]['do_anything']); |
303 | } |
cee0901c |
304 | } |
9425b25f |
305 | } |
98882637 |
306 | |
20aeb4b8 |
307 | if (isset($capabilities[$courseinstance->id]['do_anything'])) { |
308 | return (0 < $capabilities[$courseinstance->id]['do_anything']); |
309 | } |
bbbf2d40 |
310 | |
20aeb4b8 |
311 | break; |
bbbf2d40 |
312 | |
20aeb4b8 |
313 | case CONTEXT_BLOCK: |
314 | // 1 to 1 to course. |
315 | // Find course. |
316 | $block = get_record('block_instance','id',$context->instanceid); |
317 | $courseinstance = get_context_instance(CONTEXT_COURSE, $block->pageid); // needs check |
9425b25f |
318 | |
20aeb4b8 |
319 | $parentcats = get_parent_cats($courseinstance, CONTEXT_COURSE); |
320 | foreach ($parentcats as $parentcat) { |
321 | if (isset($capabilities[$parentcat]['do_anything'])) { |
322 | return (0 < $capabilities[$parentcat]['do_anything']); |
323 | } |
cee0901c |
324 | } |
9425b25f |
325 | |
20aeb4b8 |
326 | if (isset($capabilities[$courseinstance->id]['do_anything'])) { |
327 | return (0 < $capabilities[$courseinstance->id]['do_anything']); |
328 | } |
329 | break; |
bbbf2d40 |
330 | |
20aeb4b8 |
331 | default: |
332 | // CONTEXT_SYSTEM: CONTEXT_PERSONAL: CONTEXT_USERID: |
333 | // Do nothing. |
334 | break; |
335 | } |
bbbf2d40 |
336 | |
20aeb4b8 |
337 | // Last: check self. |
338 | if (isset($capabilities[$context->id]['do_anything'])) { |
339 | return (0 < $capabilities[$context->id]['do_anything']); |
340 | } |
98882637 |
341 | } |
98882637 |
342 | // do_anything has not been set, we now look for it the normal way. |
9425b25f |
343 | return (0 < capability_search($capability, $context, $capabilities)); |
bbbf2d40 |
344 | |
9425b25f |
345 | } |
bbbf2d40 |
346 | |
347 | |
348 | /** |
349 | * In a separate function so that we won't have to deal with do_anything. |
350 | * again. Used by function has_capability. |
351 | * @param $capability - capability string |
0468976c |
352 | * @param $context - the context object |
bbbf2d40 |
353 | * @param $capabilities - either $USER->capability or loaded array |
354 | * @return permission (int) |
355 | */ |
0468976c |
356 | function capability_search($capability, $context, $capabilities) { |
20aeb4b8 |
357 | |
bbbf2d40 |
358 | global $USER, $CFG; |
0468976c |
359 | |
0468976c |
360 | if (isset($capabilities[$context->id][$capability])) { |
9425b25f |
361 | if ($CFG->debug > 15) { |
362 | notify("Found $capability in context $context->id at level $context->aggregatelevel: ".$capabilities[$context->id][$capability], 'notifytiny'); |
363 | } |
0468976c |
364 | return ($capabilities[$context->id][$capability]); |
bbbf2d40 |
365 | } |
9425b25f |
366 | |
bbbf2d40 |
367 | /* Then, we check the cache recursively */ |
9425b25f |
368 | $permission = 0; |
369 | |
d140ad3f |
370 | switch ($context->aggregatelevel) { |
bbbf2d40 |
371 | |
372 | case CONTEXT_SYSTEM: // by now it's a definite an inherit |
373 | $permission = 0; |
374 | break; |
375 | |
376 | case CONTEXT_PERSONAL: |
0468976c |
377 | $parentcontext = get_context_instance(CONTEXT_SYSTEM, SITEID); |
378 | $permission = capability_search($capability, $parentcontext, $capabilities); |
bbbf2d40 |
379 | break; |
9425b25f |
380 | |
bbbf2d40 |
381 | case CONTEXT_USERID: |
0468976c |
382 | $parentcontext = get_context_instance(CONTEXT_SYSTEM, SITEID); |
383 | $permission = capability_search($capability, $parentcontext, $capabilities); |
bbbf2d40 |
384 | break; |
9425b25f |
385 | |
bbbf2d40 |
386 | case CONTEXT_COURSECAT: // Coursecat -> coursecat or site |
387 | $coursecat = get_record('course_categories','id',$context->instanceid); |
0468976c |
388 | if (!empty($coursecat->parent)) { // return parent value if it exists |
389 | $parentcontext = get_context_instance(CONTEXT_COURSECAT, $coursecat->parent); |
bbbf2d40 |
390 | } else { // else return site value |
0468976c |
391 | $parentcontext = get_context_instance(CONTEXT_SYSTEM, SITEID); |
bbbf2d40 |
392 | } |
0468976c |
393 | $permission = capability_search($capability, $parentcontext, $capabilities); |
bbbf2d40 |
394 | break; |
395 | |
396 | case CONTEXT_COURSE: // 1 to 1 to course cat |
397 | // find the course cat, and return its value |
398 | $course = get_record('course','id',$context->instanceid); |
0468976c |
399 | $parentcontext = get_context_instance(CONTEXT_COURSECAT, $course->category); |
400 | $permission = capability_search($capability, $parentcontext, $capabilities); |
bbbf2d40 |
401 | break; |
402 | |
403 | case CONTEXT_GROUP: // 1 to 1 to course |
404 | $group = get_record('groups','id',$context->instanceid); |
0468976c |
405 | $parentcontext = get_context_instance(CONTEXT_COURSE, $group->courseid); |
406 | $permission = capability_search($capability, $parentcontext, $capabilities); |
bbbf2d40 |
407 | break; |
408 | |
409 | case CONTEXT_MODULE: // 1 to 1 to course |
410 | $cm = get_record('course_modules','id',$context->instanceid); |
0468976c |
411 | $parentcontext = get_context_instance(CONTEXT_COURSE, $cm->course); |
412 | $permission = capability_search($capability, $parentcontext, $capabilities); |
bbbf2d40 |
413 | break; |
414 | |
415 | case CONTEXT_BLOCK: // 1 to 1 to course |
416 | $block = get_record('block_instance','id',$context->instanceid); |
0468976c |
417 | $parentcontext = get_context_instance(CONTEXT_COURSE, $block->pageid); // needs check |
418 | $permission = capability_search($capability, $parentcontext, $capabilities); |
bbbf2d40 |
419 | break; |
420 | |
421 | default: |
422 | error ('This is an unknown context!'); |
423 | return false; |
424 | } |
460a7a62 |
425 | if ($CFG->debug > 15) { |
9425b25f |
426 | notify("Found $capability recursively from context $context->id at level $context->aggregatelevel: $permission", 'notifytiny'); |
427 | } |
428 | |
98882637 |
429 | return $permission; |
bbbf2d40 |
430 | } |
431 | |
432 | |
433 | /** |
434 | * This function should be called immediately after a login, when $USER is set. |
435 | * It will build an array of all the capabilities at each level |
436 | * i.e. site/metacourse/course_category/course/moduleinstance |
437 | * Note we should only load capabilities if they are explicitly assigned already, |
438 | * we should not load all module's capability! |
439 | * @param $userid - the id of the user whose capabilities we want to load |
440 | * @return array |
441 | * possible just s simple 2D array with [contextid][capabilityname] |
442 | * [Capabilities] => [26][forum_post] = 1 |
443 | * [26][forum_start] = -8990 |
444 | * [26][forum_edit] = -1 |
445 | * [273][blah blah] = 1 |
446 | * [273][blah blah blah] = 2 |
447 | */ |
0468976c |
448 | function load_user_capability($capability='', $context ='', $userid='') { |
d140ad3f |
449 | |
98882637 |
450 | global $USER, $CFG; |
bbbf2d40 |
451 | |
20aeb4b8 |
452 | |
bbbf2d40 |
453 | if (empty($userid)) { |
dc411d1b |
454 | if (empty($USER->id)) { // We have no user to get capabilities for |
455 | return false; |
456 | } |
457 | if (!empty($USER->capabilities)) { // make sure it's cleaned when loaded (again) |
458 | unset($USER->capabilities); |
459 | } |
bbbf2d40 |
460 | $userid = $USER->id; |
dc411d1b |
461 | $otheruserid = false; |
bbbf2d40 |
462 | } else { |
9425b25f |
463 | $otheruserid = $userid; |
bbbf2d40 |
464 | } |
9425b25f |
465 | |
bbbf2d40 |
466 | if ($capability) { |
9425b25f |
467 | $capsearch = " AND rc.capability = '$capability' "; |
bbbf2d40 |
468 | } else { |
9425b25f |
469 | $capsearch =""; |
bbbf2d40 |
470 | } |
5f70bcc3 |
471 | |
472 | /// First we generate a list of all relevant contexts of the user |
473 | |
474 | $usercontexts = array(); |
bbbf2d40 |
475 | |
0468976c |
476 | if ($context) { // if context is specified |
477 | $usercontexts = get_parent_contexts($context); |
98882637 |
478 | } else { // else, we load everything |
5f70bcc3 |
479 | if ($userroles = get_records('role_assignments','userid',$userid)) { |
480 | foreach ($userroles as $userrole) { |
481 | $usercontexts[] = $userrole->contextid; |
482 | } |
98882637 |
483 | } |
5f70bcc3 |
484 | } |
485 | |
486 | /// Set up SQL fragments for searching contexts |
487 | |
488 | if ($usercontexts) { |
0468976c |
489 | $listofcontexts = '('.implode(',', $usercontexts).')'; |
5f70bcc3 |
490 | $searchcontexts1 = "c1.id IN $listofcontexts AND"; |
491 | $searchcontexts2 = "c2.id IN $listofcontexts AND"; |
492 | } else { |
493 | $listofcontexts = $searchcontexts1 = $searchcontexts2 = ''; |
bbbf2d40 |
494 | } |
0468976c |
495 | |
5f70bcc3 |
496 | /// Then we use 1 giant SQL to bring out all relevant capabilities. |
497 | /// The first part gets the capabilities of orginal role. |
498 | /// The second part gets the capabilities of overriden roles. |
bbbf2d40 |
499 | |
98882637 |
500 | $siteinstance = get_context_instance(CONTEXT_SYSTEM, SITEID); |
bbbf2d40 |
501 | |
75e84883 |
502 | $SQL = " SELECT rc.capability, c1.id, (c1.aggregatelevel * 100) AS aggrlevel, |
bbbf2d40 |
503 | SUM(rc.permission) AS sum |
504 | FROM |
42ac3ecf |
505 | {$CFG->prefix}role_assignments ra, |
506 | {$CFG->prefix}role_capabilities rc, |
507 | {$CFG->prefix}context c1 |
bbbf2d40 |
508 | WHERE |
d4649c76 |
509 | ra.contextid=c1.id AND |
510 | ra.roleid=rc.roleid AND |
bbbf2d40 |
511 | ra.userid=$userid AND |
5f70bcc3 |
512 | $searchcontexts1 |
bbbf2d40 |
513 | rc.contextid=$siteinstance->id |
98882637 |
514 | $capsearch |
bbbf2d40 |
515 | GROUP BY |
0be16c1d |
516 | rc.capability, (c1.aggregatelevel * 100), c1.id |
bbbf2d40 |
517 | HAVING |
41811960 |
518 | SUM(rc.permission) != 0 |
bbbf2d40 |
519 | UNION |
520 | |
75e84883 |
521 | SELECT rc.capability, c1.id, (c1.aggregatelevel * 100 + c2.aggregatelevel) AS aggrlevel, |
bbbf2d40 |
522 | SUM(rc.permission) AS sum |
523 | FROM |
42ac3ecf |
524 | {$CFG->prefix}role_assignments ra, |
525 | {$CFG->prefix}role_capabilities rc, |
526 | {$CFG->prefix}context c1, |
527 | {$CFG->prefix}context c2 |
bbbf2d40 |
528 | WHERE |
d4649c76 |
529 | ra.contextid=c1.id AND |
530 | ra.roleid=rc.roleid AND |
531 | ra.userid=$userid AND |
532 | rc.contextid=c2.id AND |
5f70bcc3 |
533 | $searchcontexts1 |
534 | $searchcontexts2 |
535 | rc.contextid != $siteinstance->id |
bbbf2d40 |
536 | $capsearch |
537 | |
538 | GROUP BY |
0be16c1d |
539 | rc.capability, (c1.aggregatelevel * 100 + c2.aggregatelevel), c1.id |
bbbf2d40 |
540 | HAVING |
41811960 |
541 | SUM(rc.permission) != 0 |
bbbf2d40 |
542 | ORDER BY |
75e84883 |
543 | aggrlevel ASC |
bbbf2d40 |
544 | "; |
545 | |
98882637 |
546 | $capabilities = array(); // Reinitialize. |
75e84883 |
547 | if (!$rs = get_recordset_sql($SQL)) { |
548 | error("Query failed in load_user_capability."); |
549 | } |
5cf38a57 |
550 | |
bbbf2d40 |
551 | if ($rs && $rs->RecordCount() > 0) { |
552 | while (!$rs->EOF) { |
75e84883 |
553 | $array = $rs->fields; |
554 | $temprecord = new object; |
98882637 |
555 | |
556 | foreach ($array as $key=>$val) { |
75e84883 |
557 | if ($key == 'aggrlevel') { |
558 | $temprecord->aggregatelevel = $val; |
559 | } else { |
560 | $temprecord->{$key} = $val; |
561 | } |
98882637 |
562 | } |
bbbf2d40 |
563 | $capabilities[] = $temprecord; |
564 | $rs->MoveNext(); |
565 | } |
566 | } |
d140ad3f |
567 | |
bbbf2d40 |
568 | /* so up to this point we should have somethign like this |
41811960 |
569 | * $capabilities[1] ->aggregatelevel = 1000 |
bbbf2d40 |
570 | ->module = SITEID |
571 | ->capability = do_anything |
572 | ->id = 1 (id is the context id) |
573 | ->sum = 0 |
574 | |
41811960 |
575 | * $capabilities[2] ->aggregatelevel = 1000 |
bbbf2d40 |
576 | ->module = SITEID |
577 | ->capability = post_messages |
578 | ->id = 1 |
579 | ->sum = -9000 |
580 | |
41811960 |
581 | * $capabilittes[3] ->aggregatelevel = 3000 |
bbbf2d40 |
582 | ->module = course |
583 | ->capability = view_course_activities |
584 | ->id = 25 |
585 | ->sum = 1 |
586 | |
41811960 |
587 | * $capabilittes[4] ->aggregatelevel = 3000 |
bbbf2d40 |
588 | ->module = course |
589 | ->capability = view_course_activities |
590 | ->id = 26 |
591 | ->sum = 0 (this is another course) |
592 | |
41811960 |
593 | * $capabilities[5] ->aggregatelevel = 3050 |
bbbf2d40 |
594 | ->module = course |
595 | ->capability = view_course_activities |
596 | ->id = 25 (override in course 25) |
597 | ->sum = -1 |
598 | * .... |
599 | * now we proceed to write the session array, going from top to bottom |
600 | * at anypoint, we need to go up and check parent to look for prohibit |
601 | */ |
602 | // print_object($capabilities); |
603 | |
604 | /* This is where we write to the actualy capabilities array |
605 | * what we need to do from here on is |
606 | * going down the array from lowest level to highest level |
607 | * 1) recursively check for prohibit, |
608 | * if any, we write prohibit |
609 | * else, we write the value |
610 | * 2) at an override level, we overwrite current level |
611 | * if it's not set to prohibit already, and if different |
612 | * ........ that should be it ........ |
613 | */ |
98882637 |
614 | $usercap = array(); // for other user's capabilities |
bbbf2d40 |
615 | foreach ($capabilities as $capability) { |
616 | |
0468976c |
617 | $context = get_context_instance_by_id($capability->id); |
618 | |
41811960 |
619 | if (!empty($otheruserid)) { // we are pulling out other user's capabilities, do not write to session |
98882637 |
620 | |
0468976c |
621 | if (capability_prohibits($capability->capability, $context, $capability->sum, $usercap)) { |
98882637 |
622 | $usercap[$capability->id][$capability->capability] = -9000; |
623 | continue; |
624 | } |
625 | |
626 | $usercap[$capability->id][$capability->capability] = $capability->sum; |
627 | |
628 | } else { |
629 | |
0468976c |
630 | if (capability_prohibits($capability->capability, $context, $capability->sum)) { // if any parent or parent's parent is set to prohibit |
98882637 |
631 | $USER->capabilities[$capability->id][$capability->capability] = -9000; |
632 | continue; |
633 | } |
634 | |
635 | // if no parental prohibit set |
636 | // just write to session, i am not sure this is correct yet |
637 | // since 3050 shows up after 3000, and 3070 shows up after 3050, |
638 | // it should be ok just to overwrite like this, provided that there's no |
639 | // parental prohibits |
640 | // no point writing 0, since 0 = inherit |
641 | // we need to write even if it's 0, because it could be an inherit override |
642 | $USER->capabilities[$capability->id][$capability->capability] = $capability->sum; |
643 | } |
bbbf2d40 |
644 | } |
645 | |
646 | // now we don't care about the huge array anymore, we can dispose it. |
647 | unset($capabilities); |
648 | |
dbe7e582 |
649 | if (!empty($otheruserid)) { |
98882637 |
650 | return $usercap; // return the array |
bbbf2d40 |
651 | } |
652 | // see array in session to see what it looks like |
653 | |
654 | } |
655 | |
656 | |
657 | /** |
658 | * This is a recursive function that checks whether the capability in this |
659 | * context, or the parent capabilities are set to prohibit. |
660 | * |
661 | * At this point, we can probably just use the values already set in the |
662 | * session variable, since we are going down the level. Any prohit set in |
663 | * parents would already reflect in the session. |
664 | * |
665 | * @param $capability - capability name |
666 | * @param $sum - sum of all capabilities values |
0468976c |
667 | * @param $context - the context object |
bbbf2d40 |
668 | * @param $array - when loading another user caps, their caps are not stored in session but an array |
669 | */ |
0468976c |
670 | function capability_prohibits($capability, $context, $sum='', $array='') { |
bbbf2d40 |
671 | global $USER; |
0468976c |
672 | |
bbbf2d40 |
673 | if ($sum < -8000) { |
674 | // If this capability is set to prohibit. |
675 | return true; |
676 | } |
677 | |
678 | if (isset($array)) { |
0468976c |
679 | if (isset($array[$context->id][$capability]) |
680 | && $array[$context->id][$capability] < -8000) { |
98882637 |
681 | return true; |
682 | } |
bbbf2d40 |
683 | } else { |
98882637 |
684 | // Else if set in session. |
0468976c |
685 | if (isset($USER->capabilities[$context->id][$capability]) |
686 | && $USER->capabilities[$context->id][$capability] < -8000) { |
98882637 |
687 | return true; |
688 | } |
bbbf2d40 |
689 | } |
d140ad3f |
690 | switch ($context->aggregatelevel) { |
bbbf2d40 |
691 | |
692 | case CONTEXT_SYSTEM: |
693 | // By now it's a definite an inherit. |
694 | return 0; |
695 | break; |
696 | |
697 | case CONTEXT_PERSONAL: |
698 | $parent = get_context_instance(CONTEXT_SYSTEM, SITEID); |
0468976c |
699 | return capability_prohibits($capability, $parent); |
bbbf2d40 |
700 | break; |
701 | |
702 | case CONTEXT_USERID: |
703 | $parent = get_context_instance(CONTEXT_SYSTEM, SITEID); |
0468976c |
704 | return capability_prohibits($capability, $parent); |
bbbf2d40 |
705 | break; |
706 | |
707 | case CONTEXT_COURSECAT: |
708 | // Coursecat -> coursecat or site. |
709 | $coursecat = get_record('course_categories','id',$context->instanceid); |
41811960 |
710 | if (!empty($coursecat->parent)) { |
bbbf2d40 |
711 | // return parent value if exist. |
712 | $parent = get_context_instance(CONTEXT_COURSECAT, $coursecat->parent); |
713 | } else { |
714 | // Return site value. |
715 | $parent = get_context_instance(CONTEXT_SYSTEM, SITEID); |
716 | } |
0468976c |
717 | return capability_prohibits($capability, $parent); |
bbbf2d40 |
718 | break; |
719 | |
720 | case CONTEXT_COURSE: |
721 | // 1 to 1 to course cat. |
722 | // Find the course cat, and return its value. |
723 | $course = get_record('course','id',$context->instanceid); |
724 | $parent = get_context_instance(CONTEXT_COURSECAT, $course->category); |
0468976c |
725 | return capability_prohibits($capability, $parent); |
bbbf2d40 |
726 | break; |
727 | |
728 | case CONTEXT_GROUP: |
729 | // 1 to 1 to course. |
730 | $group = get_record('groups','id',$context->instanceid); |
731 | $parent = get_context_instance(CONTEXT_COURSE, $group->courseid); |
0468976c |
732 | return capability_prohibits($capability, $parent); |
bbbf2d40 |
733 | break; |
734 | |
735 | case CONTEXT_MODULE: |
736 | // 1 to 1 to course. |
737 | $cm = get_record('course_modules','id',$context->instanceid); |
738 | $parent = get_context_instance(CONTEXT_COURSE, $cm->course); |
0468976c |
739 | return capability_prohibits($capability, $parent); |
bbbf2d40 |
740 | break; |
741 | |
742 | case CONTEXT_BLOCK: |
743 | // 1 to 1 to course. |
744 | $block = get_record('block_instance','id',$context->instanceid); |
745 | $parent = get_context_instance(CONTEXT_COURSE, $block->pageid); // needs check |
0468976c |
746 | return capability_prohibits($capability, $parent); |
bbbf2d40 |
747 | break; |
748 | |
749 | default: |
750 | error ('This is an unknown context!'); |
751 | return false; |
752 | } |
753 | } |
754 | |
755 | |
756 | /** |
757 | * A print form function. This should either grab all the capabilities from |
758 | * files or a central table for that particular module instance, then present |
759 | * them in check boxes. Only relevant capabilities should print for known |
760 | * context. |
761 | * @param $mod - module id of the mod |
762 | */ |
763 | function print_capabilities($modid=0) { |
764 | global $CFG; |
765 | |
766 | $capabilities = array(); |
767 | |
768 | if ($modid) { |
769 | // We are in a module specific context. |
770 | |
771 | // Get the mod's name. |
772 | // Call the function that grabs the file and parse. |
773 | $cm = get_record('course_modules', 'id', $modid); |
774 | $module = get_record('modules', 'id', $cm->module); |
775 | |
776 | } else { |
777 | // Print all capabilities. |
778 | foreach ($capabilities as $capability) { |
779 | // Prints the check box component. |
780 | } |
781 | } |
782 | } |
783 | |
784 | |
785 | /** |
1afecc03 |
786 | * Installs the roles system. |
787 | * This function runs on a fresh install as well as on an upgrade from the old |
788 | * hard-coded student/teacher/admin etc. roles to the new roles system. |
bbbf2d40 |
789 | */ |
1afecc03 |
790 | function moodle_install_roles() { |
bbbf2d40 |
791 | |
1afecc03 |
792 | global $CFG, $db; |
793 | |
bbbf2d40 |
794 | // Create a system wide context for assignemnt. |
795 | $systemcontext = $context = get_context_instance(CONTEXT_SYSTEM, SITEID); |
796 | |
1afecc03 |
797 | |
798 | // Create default/legacy roles and capabilities. |
799 | // (1 legacy capability per legacy role at system level). |
bbbf2d40 |
800 | $adminrole = create_role(get_string('administrator'), get_string('administratordescription'), 'moodle/legacy:admin'); |
98882637 |
801 | if (!assign_capability('moodle/site:doanything', CAP_ALLOW, $adminrole, $systemcontext->id)) { |
bbbf2d40 |
802 | error('Could not assign moodle/site:doanything to the admin role'); |
803 | } |
804 | $coursecreatorrole = create_role(get_string('coursecreators'), get_string('coursecreatorsdescription'), 'moodle/legacy:coursecreator'); |
98882637 |
805 | $noneditteacherrole = create_role(get_string('noneditingteacher'), get_string('noneditingteacherdescription'), 'moodle/legacy:teacher'); |
806 | $editteacherrole = create_role(get_string('defaultcourseteacher'), get_string('defaultcourseteacherdescription'), 'moodle/legacy:editingteacher'); |
807 | $studentrole = create_role(get_string('defaultcoursestudent'), get_string('defaultcoursestudentdescription'), 'moodle/legacy:student'); |
bbbf2d40 |
808 | $guestrole = create_role(get_string('guest'), get_string('guestdescription'), 'moodle/legacy:guest'); |
1afecc03 |
809 | |
810 | |
811 | // Look inside user_admin, user_creator, user_teachers, user_students and |
812 | // assign above new roles. If a user has both teacher and student role, |
813 | // only teacher role is assigned. The assignment should be system level. |
814 | $dbtables = $db->MetaTables('TABLES'); |
bbbf2d40 |
815 | |
1afecc03 |
816 | |
98882637 |
817 | /** |
bbbf2d40 |
818 | * Upgrade the admins. |
1afecc03 |
819 | * Sort using id ASC, first one is primary admin. |
bbbf2d40 |
820 | */ |
1afecc03 |
821 | if (in_array($CFG->prefix.'user_admins', $dbtables)) { |
822 | if ($useradmins = get_records_sql('SELECT * from '.$CFG->prefix.'user_admins ORDER BY ID ASC')) { |
823 | foreach ($useradmins as $admin) { |
824 | role_assign($adminrole, $admin->userid, 0, $systemcontext->id); |
825 | } |
826 | } |
827 | } else { |
828 | // This is a fresh install. |
bbbf2d40 |
829 | } |
1afecc03 |
830 | |
831 | |
bbbf2d40 |
832 | /** |
833 | * Upgrade course creators. |
834 | */ |
1afecc03 |
835 | if (in_array($CFG->prefix.'user_coursecreators', $dbtables)) { |
836 | if ($usercoursecreators = get_records('user_coursecreators')) { |
837 | foreach ($usercoursecreators as $coursecreator) { |
56b4d70d |
838 | role_assign($coursecreatorrole, $coursecreator->userid, 0, $systemcontext->id); |
1afecc03 |
839 | } |
840 | } |
bbbf2d40 |
841 | } |
842 | |
1afecc03 |
843 | |
bbbf2d40 |
844 | /** |
845 | * Upgrade editting teachers and non-editting teachers. |
846 | */ |
1afecc03 |
847 | if (in_array($CFG->prefix.'user_teachers', $dbtables)) { |
848 | if ($userteachers = get_records('user_teachers')) { |
849 | foreach ($userteachers as $teacher) { |
850 | $coursecontext = get_context_instance(CONTEXT_COURSE, $teacher->course); // needs cache |
851 | if ($teacher->editall) { // editting teacher |
852 | role_assign($editteacherrole, $teacher->userid, 0, $coursecontext->id); |
853 | } else { |
854 | role_assign($noneditteacherrole, $teacher->userid, 0, $coursecontext->id); |
855 | } |
856 | } |
bbbf2d40 |
857 | } |
858 | } |
1afecc03 |
859 | |
860 | |
bbbf2d40 |
861 | /** |
862 | * Upgrade students. |
863 | */ |
1afecc03 |
864 | if (in_array($CFG->prefix.'user_students', $dbtables)) { |
865 | if ($userstudents = get_records('user_students')) { |
866 | foreach ($userstudents as $student) { |
867 | $coursecontext = get_context_instance(CONTEXT_COURSE, $student->course); |
868 | role_assign($studentrole, $student->userid, 0, $coursecontext->id); |
869 | } |
870 | } |
bbbf2d40 |
871 | } |
1afecc03 |
872 | |
873 | |
bbbf2d40 |
874 | /** |
875 | * Upgrade guest (only 1 entry). |
876 | */ |
1afecc03 |
877 | if ($guestuser = get_record('user', 'username', 'guest')) { |
878 | role_assign($guestrole, $guestuser->id, 0, $systemcontext->id); |
879 | } |
880 | |
945f88ca |
881 | /** |
882 | * Insert the correct records for legacy roles |
883 | */ |
884 | allow_assign($adminrole, $adminrole); |
885 | allow_assign($adminrole, $coursecreatorrole); |
886 | allow_assign($adminrole, $noneditteacherrole); |
887 | allow_assign($adminrole, $editteacherrole); |
888 | allow_assign($adminrole, $studentrole); |
889 | allow_assign($adminrole, $guestrole); |
890 | |
891 | allow_assign($coursecreatorrole, $noneditteacherrole); |
892 | allow_assign($coursecreatorrole, $editteacherrole); |
893 | allow_assign($coursecreatorrole, $studentrole); |
894 | allow_assign($coursecreatorrole, $guestrole); |
895 | |
896 | allow_assign($editteacherrole, $noneditteacherrole); |
897 | allow_assign($editteacherrole, $studentrole); |
898 | allow_assign($editteacherrole, $guestrole); |
899 | |
900 | /// overrides |
901 | allow_override($adminrole, $adminrole); |
902 | allow_override($adminrole, $coursecreatorrole); |
903 | allow_override($adminrole, $noneditteacherrole); |
904 | allow_override($adminrole, $editteacherrole); |
905 | allow_override($adminrole, $studentrole); |
5769734f |
906 | allow_override($adminrole, $guestrole); |
1afecc03 |
907 | |
908 | // Should we delete the tables after we are done? Not yet. |
bbbf2d40 |
909 | } |
910 | |
bbbf2d40 |
911 | /** |
912 | * Assign the defaults found in this capabality definition to roles that have |
913 | * the corresponding legacy capabilities assigned to them. |
914 | * @param $legacyperms - an array in the format (example): |
915 | * 'guest' => CAP_PREVENT, |
916 | * 'student' => CAP_ALLOW, |
917 | * 'teacher' => CAP_ALLOW, |
918 | * 'editingteacher' => CAP_ALLOW, |
919 | * 'coursecreator' => CAP_ALLOW, |
920 | * 'admin' => CAP_ALLOW |
921 | * @return boolean - success or failure. |
922 | */ |
923 | function assign_legacy_capabilities($capability, $legacyperms) { |
924 | |
925 | foreach ($legacyperms as $type => $perm) { |
926 | |
927 | $systemcontext = get_context_instance(CONTEXT_SYSTEM, SITEID); |
928 | |
929 | // The legacy capabilities are: |
930 | // 'moodle/legacy:guest' |
931 | // 'moodle/legacy:student' |
932 | // 'moodle/legacy:teacher' |
933 | // 'moodle/legacy:editingteacher' |
934 | // 'moodle/legacy:coursecreator' |
935 | // 'moodle/legacy:admin' |
936 | |
937 | if (!$roles = get_roles_with_capability('moodle/legacy:'.$type, CAP_ALLOW)) { |
938 | return false; |
939 | } |
940 | |
941 | foreach ($roles as $role) { |
942 | // Assign a site level capability. |
e7876c1e |
943 | if (!assign_capability($capability, $perm, $role->id, $systemcontext->id)) { |
bbbf2d40 |
944 | return false; |
945 | } |
946 | } |
947 | } |
948 | return true; |
949 | } |
950 | |
951 | |
cee0901c |
952 | /** |
953 | * Checks to see if a capability is a legacy capability. |
954 | * @param $capabilityname |
955 | * @return boolean |
956 | */ |
bbbf2d40 |
957 | function islegacy($capabilityname) { |
98882637 |
958 | if (strstr($capabilityname, 'legacy') === false) { |
959 | return false; |
960 | } else { |
961 | return true; |
962 | } |
bbbf2d40 |
963 | } |
964 | |
cee0901c |
965 | |
966 | |
967 | /********************************** |
bbbf2d40 |
968 | * Context Manipulation functions * |
969 | **********************************/ |
970 | |
bbbf2d40 |
971 | /** |
972 | * This should be called prolly everytime a user, group, module, course, |
973 | * coursecat or site is set up maybe? |
974 | * @param $level |
975 | * @param $instanceid |
976 | */ |
d140ad3f |
977 | function create_context($aggregatelevel, $instanceid) { |
978 | if (!get_record('context','aggregatelevel',$aggregatelevel,'instanceid',$instanceid)) { |
bbbf2d40 |
979 | $context = new object; |
d140ad3f |
980 | $context->aggregatelevel = $aggregatelevel; |
bbbf2d40 |
981 | $context->instanceid = $instanceid; |
982 | return insert_record('context',$context); |
983 | } |
984 | } |
985 | |
986 | |
987 | /** |
988 | * Get the context instance as an object. This function will create the |
989 | * context instance if it does not exist yet. |
990 | * @param $level |
991 | * @param $instance |
992 | */ |
d140ad3f |
993 | function get_context_instance($aggregatelevel=NULL, $instance=SITEID) { |
e5605780 |
994 | |
51195e6f |
995 | global $context_cache, $context_cache_id, $CONTEXT; |
d9a35e12 |
996 | |
340ea4e8 |
997 | /// If no level is supplied then return the current global context if there is one |
d140ad3f |
998 | if (empty($aggregatelevel)) { |
340ea4e8 |
999 | if (empty($CONTEXT)) { |
1000 | if ($CFG->debug > 7) { |
1001 | notify("Error: get_context_instance() called without a context"); |
1002 | } |
1003 | } else { |
1004 | return $CONTEXT; |
1005 | } |
e5605780 |
1006 | } |
1007 | |
340ea4e8 |
1008 | /// Check the cache |
d140ad3f |
1009 | if (isset($context_cache[$aggregatelevel][$instance])) { // Already cached |
1010 | return $context_cache[$aggregatelevel][$instance]; |
e5605780 |
1011 | } |
1012 | |
340ea4e8 |
1013 | /// Get it from the database, or create it |
d140ad3f |
1014 | if (!$context = get_record('context', 'aggregatelevel', $aggregatelevel, 'instanceid', $instance)) { |
1015 | create_context($aggregatelevel, $instance); |
1016 | $context = get_record('context', 'aggregatelevel', $aggregatelevel, 'instanceid', $instance); |
e5605780 |
1017 | } |
1018 | |
ccfc5ecc |
1019 | /// Only add to cache if context isn't empty. |
1020 | if (!empty($context)) { |
1021 | $context_cache[$aggregatelevel][$instance] = $context; // Cache it for later |
1022 | $context_cache_id[$context->id] = $context; // Cache it for later |
1023 | } |
0468976c |
1024 | |
bbbf2d40 |
1025 | return $context; |
1026 | } |
1027 | |
cee0901c |
1028 | |
340ea4e8 |
1029 | /** |
1030 | * Get a context instance as an object, from a given id. |
1031 | * @param $id |
1032 | */ |
1033 | function get_context_instance_by_id($id) { |
1034 | |
d9a35e12 |
1035 | global $context_cache, $context_cache_id; |
1036 | |
340ea4e8 |
1037 | if (isset($context_cache_id[$id])) { // Already cached |
75e84883 |
1038 | return $context_cache_id[$id]; |
340ea4e8 |
1039 | } |
1040 | |
1041 | if ($context = get_record('context', 'id', $id)) { // Update the cache and return |
d140ad3f |
1042 | $context_cache[$context->aggregatelevel][$context->instanceid] = $context; |
340ea4e8 |
1043 | $context_cache_id[$context->id] = $context; |
1044 | return $context; |
1045 | } |
1046 | |
1047 | return false; |
1048 | } |
1049 | |
bbbf2d40 |
1050 | |
8737be58 |
1051 | /** |
1052 | * Get the local override (if any) for a given capability in a role in a context |
1053 | * @param $roleid |
0468976c |
1054 | * @param $contextid |
1055 | * @param $capability |
8737be58 |
1056 | */ |
1057 | function get_local_override($roleid, $contextid, $capability) { |
1058 | return get_record('role_capabilities', 'roleid', $roleid, 'capability', $capability, 'contextid', $contextid); |
1059 | } |
1060 | |
1061 | |
bbbf2d40 |
1062 | |
1063 | /************************************ |
1064 | * DB TABLE RELATED FUNCTIONS * |
1065 | ************************************/ |
1066 | |
cee0901c |
1067 | /** |
bbbf2d40 |
1068 | * function that creates a role |
1069 | * @param name - role name |
1070 | * @param description - role description |
1071 | * @param legacy - optional legacy capability |
1072 | * @return id or false |
1073 | */ |
1074 | function create_role($name, $description, $legacy='') { |
98882637 |
1075 | |
1076 | // check for duplicate role name |
1077 | |
1078 | if ($role = get_record('role','name', $name)) { |
98882637 |
1079 | error('there is already a role with this name!'); |
1080 | } |
1081 | |
1082 | $role->name = $name; |
1083 | $role->description = $description; |
ec7a8b79 |
1084 | |
1085 | $context = get_context_instance(CONTEXT_SYSTEM, SITEID); |
1086 | |
98882637 |
1087 | if ($id = insert_record('role', $role)) { |
ec7a8b79 |
1088 | if ($legacy) { |
1afecc03 |
1089 | assign_capability($legacy, CAP_ALLOW, $id, $context->id); |
98882637 |
1090 | } |
ec7a8b79 |
1091 | |
1092 | /// By default, users with role:manage at site level |
1093 | /// should be able to assign users to this new role, and override this new role's capabilities |
1094 | |
1095 | // find all admin roles |
e46c0987 |
1096 | if ($adminroles = get_roles_with_capability('moodle/role:manage', CAP_ALLOW, $context)) { |
1097 | // foreach admin role |
1098 | foreach ($adminroles as $arole) { |
1099 | // write allow_assign and allow_overrid |
1100 | allow_assign($arole->id, $id); |
1101 | allow_override($arole->id, $id); |
1102 | } |
ec7a8b79 |
1103 | } |
1104 | |
98882637 |
1105 | return $id; |
1106 | } else { |
1107 | return false; |
1108 | } |
bbbf2d40 |
1109 | |
1110 | } |
1111 | |
1112 | /** |
1113 | * Function to write context specific overrides, or default capabilities. |
1114 | * @param module - string name |
1115 | * @param capability - string name |
1116 | * @param contextid - context id |
1117 | * @param roleid - role id |
1118 | * @param permission - int 1,-1 or -1000 |
1119 | */ |
e7876c1e |
1120 | function assign_capability($capability, $permission, $roleid, $contextid, $overwrite=false) { |
98882637 |
1121 | |
1122 | global $USER; |
1123 | |
1124 | if (empty($permission) || $permission == 0) { // if permission is not set |
1125 | unassign_capability($capability, $roleid, $contextid); |
1126 | } |
bbbf2d40 |
1127 | |
e7876c1e |
1128 | $existing = get_record('role_capabilities', 'contextid', $contextid, 'roleid', $capability, $capability); |
1129 | |
1130 | if ($existing and !$overwrite) { // We want to keep whatever is there already |
1131 | return true; |
1132 | } |
1133 | |
bbbf2d40 |
1134 | $cap = new object; |
1135 | $cap->contextid = $contextid; |
1136 | $cap->roleid = $roleid; |
1137 | $cap->capability = $capability; |
1138 | $cap->permission = $permission; |
1139 | $cap->timemodified = time(); |
9db12da7 |
1140 | $cap->modifierid = empty($USER->id) ? 0 : $USER->id; |
e7876c1e |
1141 | |
1142 | if ($existing) { |
1143 | $cap->id = $existing->id; |
1144 | return update_record('role_capabilities', $cap); |
1145 | } else { |
1146 | return insert_record('role_capabilities', $cap); |
1147 | } |
bbbf2d40 |
1148 | } |
1149 | |
1150 | |
1151 | /** |
1152 | * Unassign a capability from a role. |
1153 | * @param $roleid - the role id |
1154 | * @param $capability - the name of the capability |
1155 | * @return boolean - success or failure |
1156 | */ |
1157 | function unassign_capability($capability, $roleid, $contextid=NULL) { |
98882637 |
1158 | |
1159 | if (isset($contextid)) { |
1160 | $status = delete_records('role_capabilities', 'capability', $capability, |
1161 | 'roleid', $roleid, 'contextid', $contextid); |
1162 | } else { |
1163 | $status = delete_records('role_capabilities', 'capability', $capability, |
1164 | 'roleid', $roleid); |
1165 | } |
1166 | return $status; |
bbbf2d40 |
1167 | } |
1168 | |
1169 | |
1170 | /** |
1171 | * Get the roles that have a given capability. |
1172 | * @param $capability - capability name (string) |
1173 | * @param $permission - optional, the permission defined for this capability |
1174 | * either CAP_ALLOW, CAP_PREVENT or CAP_PROHIBIT |
1175 | * @return array or role objects |
1176 | */ |
ec7a8b79 |
1177 | function get_roles_with_capability($capability, $permission=NULL, $context='') { |
1178 | |
bbbf2d40 |
1179 | global $CFG; |
1180 | |
ec7a8b79 |
1181 | if ($context) { |
1182 | if ($contexts = get_parent_contexts($context)) { |
1183 | $listofcontexts = '('.implode(',', $contexts).')'; |
1184 | } else { |
1185 | $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID); |
1186 | $listofcontexts = '('.$sitecontext->id.')'; // must be site |
1187 | } |
42ac3ecf |
1188 | $contextstr = "AND (rc.contextid = '$context->id' OR rc.contextid IN $listofcontexts)"; |
ec7a8b79 |
1189 | } else { |
1190 | $contextstr = ''; |
1191 | } |
1192 | |
bbbf2d40 |
1193 | $selectroles = "SELECT r.* |
42ac3ecf |
1194 | FROM {$CFG->prefix}role r, |
1195 | {$CFG->prefix}role_capabilities rc |
bbbf2d40 |
1196 | WHERE rc.capability = '$capability' |
ec7a8b79 |
1197 | AND rc.roleid = r.id $contextstr"; |
bbbf2d40 |
1198 | |
1199 | if (isset($permission)) { |
1200 | $selectroles .= " AND rc.permission = '$permission'"; |
1201 | } |
1202 | return get_records_sql($selectroles); |
1203 | } |
1204 | |
1205 | |
1206 | /** |
a9e1c058 |
1207 | * This function makes a role-assignment (a role for a user or group in a particular context) |
bbbf2d40 |
1208 | * @param $roleid - the role of the id |
1209 | * @param $userid - userid |
1210 | * @param $groupid - group id |
1211 | * @param $contextid - id of the context |
1212 | * @param $timestart - time this assignment becomes effective |
1213 | * @param $timeend - time this assignemnt ceases to be effective |
1214 | * @uses $USER |
1215 | * @return id - new id of the assigment |
1216 | */ |
f44152f4 |
1217 | function role_assign($roleid, $userid, $groupid, $contextid, $timestart=0, $timeend=0, $hidden=0, $enrol='manual') { |
aa311411 |
1218 | global $USER, $CFG; |
bbbf2d40 |
1219 | |
218564ac |
1220 | if ($CFG->debug > 7) { |
98882637 |
1221 | notify("Assign roleid $roleid userid $userid contextid $contextid", 'notifytiny'); |
aa311411 |
1222 | } |
bbbf2d40 |
1223 | |
a9e1c058 |
1224 | /// Do some data validation |
1225 | |
bbbf2d40 |
1226 | if (empty($roleid)) { |
a9e1c058 |
1227 | notify('Role ID not provided'); |
1228 | return false; |
bbbf2d40 |
1229 | } |
1230 | |
1231 | if (empty($userid) && empty($groupid)) { |
a9e1c058 |
1232 | notify('Either userid or groupid must be provided'); |
1233 | return false; |
bbbf2d40 |
1234 | } |
7700027f |
1235 | |
1236 | if ($userid && !record_exists('user', 'id', $userid)) { |
1237 | notify('User does not exist!'); |
1238 | return false; |
1239 | } |
bbbf2d40 |
1240 | |
dc411d1b |
1241 | if ($groupid && !record_exists('groups', 'id', $groupid)) { |
1242 | notify('Group does not exist!'); |
1243 | return false; |
1244 | } |
1245 | |
7700027f |
1246 | if (!$context = get_context_instance_by_id($contextid)) { |
a9e1c058 |
1247 | notify('A valid context must be provided'); |
1248 | return false; |
bbbf2d40 |
1249 | } |
1250 | |
a9e1c058 |
1251 | if (($timestart and $timeend) and ($timestart > $timeend)) { |
7700027f |
1252 | notify('The end time can not be earlier than the start time'); |
a9e1c058 |
1253 | return false; |
1254 | } |
1255 | |
7700027f |
1256 | |
a9e1c058 |
1257 | /// Check for existing entry |
1258 | if ($userid) { |
7700027f |
1259 | $ra = get_record('role_assignments', 'roleid', $roleid, 'contextid', $context->id, 'userid', $userid); |
a9e1c058 |
1260 | } else { |
7700027f |
1261 | $ra = get_record('role_assignments', 'roleid', $roleid, 'contextid', $context->id, 'groupid', $groupid); |
a9e1c058 |
1262 | } |
1263 | |
9ebcb4d2 |
1264 | |
a9e1c058 |
1265 | $newra = new object; |
bbbf2d40 |
1266 | |
a9e1c058 |
1267 | if (empty($ra)) { // Create a new entry |
1268 | $newra->roleid = $roleid; |
7700027f |
1269 | $newra->contextid = $context->id; |
a9e1c058 |
1270 | $newra->userid = $userid; |
1271 | $newra->groupid = $groupid; |
1272 | |
1273 | $newra->hidden = $hidden; |
f44152f4 |
1274 | $newra->enrol = $enrol; |
a9e1c058 |
1275 | $newra->timestart = $timestart; |
1276 | $newra->timeend = $timeend; |
1277 | $newra->timemodified = time(); |
1278 | $newra->modifier = empty($USER->id) ? 0 : $USER->id; |
1279 | |
9ebcb4d2 |
1280 | $success = insert_record('role_assignments', $newra); |
a9e1c058 |
1281 | |
1282 | } else { // We already have one, just update it |
1283 | |
1284 | $newra->id = $ra->id; |
1285 | $newra->hidden = $hidden; |
f44152f4 |
1286 | $newra->enrol = $enrol; |
a9e1c058 |
1287 | $newra->timestart = $timestart; |
1288 | $newra->timeend = $timeend; |
1289 | $newra->timemodified = time(); |
1290 | $newra->modifier = empty($USER->id) ? 0 : $USER->id; |
1291 | |
9ebcb4d2 |
1292 | $success = update_record('role_assignments', $newra); |
1293 | } |
1294 | |
7700027f |
1295 | if ($success) { /// Role was assigned, so do some other things |
1296 | |
1297 | /// If the user is the current user, then reload the capabilities too. |
1298 | if (!empty($USER->id) && $USER->id == $userid) { |
1299 | load_user_capability(); |
1300 | } |
1301 | |
1302 | /// Make sure the user is subscribed to any appropriate forums in this context |
1303 | require_once($CFG->dirroot.'/mod/forum/lib.php'); |
1911105f |
1304 | forum_add_user_default_subscriptions($userid, $context); |
a9e1c058 |
1305 | } |
6eb4f823 |
1306 | |
1307 | return $success; |
bbbf2d40 |
1308 | } |
1309 | |
1310 | |
1311 | /** |
1dc1f037 |
1312 | * Deletes one or more role assignments. You must specify at least one parameter. |
bbbf2d40 |
1313 | * @param $roleid |
1314 | * @param $userid |
1315 | * @param $groupid |
1316 | * @param $contextid |
1317 | * @return boolean - success or failure |
1318 | */ |
1dc1f037 |
1319 | function role_unassign($roleid=0, $userid=0, $groupid=0, $contextid=0) { |
1320 | $args = array('roleid', 'userid', 'groupid', 'contextid'); |
1321 | $select = array(); |
1322 | foreach ($args as $arg) { |
1323 | if ($$arg) { |
1324 | $select[] = $arg.' = '.$$arg; |
1325 | } |
1326 | } |
1327 | if ($select) { |
1328 | return delete_records_select('role_assignments', implode(' AND ', $select)); |
1329 | } |
1330 | return false; |
bbbf2d40 |
1331 | } |
1332 | |
1333 | |
1334 | /** |
1335 | * Loads the capability definitions for the component (from file). If no |
1336 | * capabilities are defined for the component, we simply return an empty array. |
1337 | * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results' |
1338 | * @return array of capabilities |
1339 | */ |
1340 | function load_capability_def($component) { |
1341 | global $CFG; |
1342 | |
1343 | if ($component == 'moodle') { |
1344 | $defpath = $CFG->libdir.'/db/access.php'; |
1345 | $varprefix = 'moodle'; |
1346 | } else { |
1347 | $defpath = $CFG->dirroot.'/'.$component.'/db/access.php'; |
1348 | $varprefix = str_replace('/', '_', $component); |
1349 | } |
1350 | $capabilities = array(); |
1351 | |
1352 | if (file_exists($defpath)) { |
1353 | require_once($defpath); |
1354 | $capabilities = ${$varprefix.'_capabilities'}; |
1355 | } |
1356 | return $capabilities; |
1357 | } |
1358 | |
1359 | |
1360 | /** |
1361 | * Gets the capabilities that have been cached in the database for this |
1362 | * component. |
1363 | * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results' |
1364 | * @return array of capabilities |
1365 | */ |
1366 | function get_cached_capabilities($component='moodle') { |
1367 | if ($component == 'moodle') { |
1368 | $storedcaps = get_records_select('capabilities', |
1369 | "name LIKE 'moodle/%:%'"); |
1370 | } else { |
1371 | $storedcaps = get_records_select('capabilities', |
1372 | "name LIKE '$component:%'"); |
1373 | } |
1374 | return $storedcaps; |
1375 | } |
1376 | |
1377 | |
1378 | /** |
1379 | * Updates the capabilities table with the component capability definitions. |
1380 | * If no parameters are given, the function updates the core moodle |
1381 | * capabilities. |
1382 | * |
1383 | * Note that the absence of the db/access.php capabilities definition file |
1384 | * will cause any stored capabilities for the component to be removed from |
1385 | * the database. |
1386 | * |
1387 | * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results' |
1388 | * @return boolean |
1389 | */ |
1390 | function update_capabilities($component='moodle') { |
1391 | |
1392 | $storedcaps = array(); |
be4486da |
1393 | |
1394 | $filecaps = load_capability_def($component); |
bbbf2d40 |
1395 | $cachedcaps = get_cached_capabilities($component); |
1396 | if ($cachedcaps) { |
1397 | foreach ($cachedcaps as $cachedcap) { |
1398 | array_push($storedcaps, $cachedcap->name); |
be4486da |
1399 | // update risk bitmasks in existing capabilitites if needed |
1400 | if (array_key_exists($cachedcap->name, $filecaps)) { |
1401 | if (!array_key_exists('riskbitmask', $filecaps[$cachedcap->name])) { |
2b531945 |
1402 | $filecaps[$cachedcap->name]['riskbitmask'] = 0; // no risk if not specified |
be4486da |
1403 | } |
1404 | if ($cachedcap->riskbitmask != $filecaps[$cachedcap->name]['riskbitmask']) { |
1405 | $updatecap = new object; |
1406 | $updatecap->id = $cachedcap->id; |
1407 | $updatecap->riskbitmask = $filecaps[$cachedcap->name]['riskbitmask']; |
1408 | if (!update_record('capabilities', $updatecap)) { |
1409 | return false; |
1410 | } |
1411 | } |
1412 | } |
bbbf2d40 |
1413 | } |
1414 | } |
be4486da |
1415 | |
bbbf2d40 |
1416 | // Are there new capabilities in the file definition? |
1417 | $newcaps = array(); |
1418 | |
1419 | foreach ($filecaps as $filecap => $def) { |
1420 | if (!$storedcaps || |
1421 | ($storedcaps && in_array($filecap, $storedcaps) === false)) { |
2b531945 |
1422 | if (!array_key_exists('riskbitmask', $def)) { |
1423 | $def['riskbitmask'] = 0; // no risk if not specified |
1424 | } |
bbbf2d40 |
1425 | $newcaps[$filecap] = $def; |
1426 | } |
1427 | } |
1428 | // Add new capabilities to the stored definition. |
1429 | foreach ($newcaps as $capname => $capdef) { |
1430 | $capability = new object; |
1431 | $capability->name = $capname; |
1432 | $capability->captype = $capdef['captype']; |
1433 | $capability->contextlevel = $capdef['contextlevel']; |
1434 | $capability->component = $component; |
be4486da |
1435 | $capability->riskbitmask = $capdef['riskbitmask']; |
bbbf2d40 |
1436 | |
1437 | if (!insert_record('capabilities', $capability, false, 'id')) { |
1438 | return false; |
1439 | } |
1440 | // Do we need to assign the new capabilities to roles that have the |
1441 | // legacy capabilities moodle/legacy:* as well? |
1442 | if (isset($capdef['legacy']) && is_array($capdef['legacy']) && |
1443 | !assign_legacy_capabilities($capname, $capdef['legacy'])) { |
1444 | error('Could not assign legacy capabilities'); |
1445 | return false; |
1446 | } |
1447 | } |
1448 | // Are there any capabilities that have been removed from the file |
1449 | // definition that we need to delete from the stored capabilities and |
1450 | // role assignments? |
1451 | capabilities_cleanup($component, $filecaps); |
1452 | |
1453 | return true; |
1454 | } |
1455 | |
1456 | |
1457 | /** |
1458 | * Deletes cached capabilities that are no longer needed by the component. |
1459 | * Also unassigns these capabilities from any roles that have them. |
1460 | * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results' |
1461 | * @param $newcapdef - array of the new capability definitions that will be |
1462 | * compared with the cached capabilities |
1463 | * @return int - number of deprecated capabilities that have been removed |
1464 | */ |
1465 | function capabilities_cleanup($component, $newcapdef=NULL) { |
1466 | |
1467 | $removedcount = 0; |
1468 | |
1469 | if ($cachedcaps = get_cached_capabilities($component)) { |
1470 | foreach ($cachedcaps as $cachedcap) { |
1471 | if (empty($newcapdef) || |
1472 | array_key_exists($cachedcap->name, $newcapdef) === false) { |
1473 | |
1474 | // Remove from capabilities cache. |
1475 | if (!delete_records('capabilities', 'name', $cachedcap->name)) { |
1476 | error('Could not delete deprecated capability '.$cachedcap->name); |
1477 | } else { |
1478 | $removedcount++; |
1479 | } |
1480 | // Delete from roles. |
1481 | if($roles = get_roles_with_capability($cachedcap->name)) { |
1482 | foreach($roles as $role) { |
46943f7b |
1483 | if (!unassign_capability($cachedcap->name, $role->id)) { |
bbbf2d40 |
1484 | error('Could not unassign deprecated capability '. |
1485 | $cachedcap->name.' from role '.$role->name); |
1486 | } |
1487 | } |
1488 | } |
1489 | } // End if. |
1490 | } |
1491 | } |
1492 | return $removedcount; |
1493 | } |
1494 | |
1495 | |
1496 | |
cee0901c |
1497 | /**************** |
1498 | * UI FUNCTIONS * |
1499 | ****************/ |
bbbf2d40 |
1500 | |
1501 | |
1502 | /** |
1503 | * prints human readable context identifier. |
1504 | */ |
0468976c |
1505 | function print_context_name($context) { |
340ea4e8 |
1506 | |
ec0810ee |
1507 | $name = ''; |
d140ad3f |
1508 | switch ($context->aggregatelevel) { |
ec0810ee |
1509 | |
bbbf2d40 |
1510 | case CONTEXT_SYSTEM: // by now it's a definite an inherit |
ec0810ee |
1511 | $name = get_string('site'); |
340ea4e8 |
1512 | break; |
bbbf2d40 |
1513 | |
1514 | case CONTEXT_PERSONAL: |
ec0810ee |
1515 | $name = get_string('personal'); |
340ea4e8 |
1516 | break; |
1517 | |
bbbf2d40 |
1518 | case CONTEXT_USERID: |
ec0810ee |
1519 | if ($user = get_record('user', 'id', $context->instanceid)) { |
1520 | $name = get_string('user').': '.fullname($user); |
1521 | } |
340ea4e8 |
1522 | break; |
1523 | |
bbbf2d40 |
1524 | case CONTEXT_COURSECAT: // Coursecat -> coursecat or site |
ec0810ee |
1525 | if ($category = get_record('course_categories', 'id', $context->instanceid)) { |
1526 | $name = get_string('category').': '.$category->name; |
1527 | } |
340ea4e8 |
1528 | break; |
bbbf2d40 |
1529 | |
1530 | case CONTEXT_COURSE: // 1 to 1 to course cat |
ec0810ee |
1531 | if ($course = get_record('course', 'id', $context->instanceid)) { |
1532 | $name = get_string('course').': '.$course->fullname; |
1533 | } |
340ea4e8 |
1534 | break; |
bbbf2d40 |
1535 | |
1536 | case CONTEXT_GROUP: // 1 to 1 to course |
ec0810ee |
1537 | if ($group = get_record('groups', 'id', $context->instanceid)) { |
1538 | $name = get_string('group').': '.$group->name; |
1539 | } |
340ea4e8 |
1540 | break; |
bbbf2d40 |
1541 | |
1542 | case CONTEXT_MODULE: // 1 to 1 to course |
98882637 |
1543 | if ($cm = get_record('course_modules','id',$context->instanceid)) { |
1544 | if ($module = get_record('modules','id',$cm->module)) { |
1545 | if ($mod = get_record($module->name, 'id', $cm->instance)) { |
ec0810ee |
1546 | $name = get_string('activitymodule').': '.$mod->name; |
98882637 |
1547 | } |
ec0810ee |
1548 | } |
1549 | } |
340ea4e8 |
1550 | break; |
bbbf2d40 |
1551 | |
1552 | case CONTEXT_BLOCK: // 1 to 1 to course |
98882637 |
1553 | if ($blockinstance = get_record('block_instance','id',$context->instanceid)) { |
1554 | if ($block = get_record('block','id',$blockinstance->blockid)) { |
91be52d7 |
1555 | global $CFG; |
1556 | require_once("$CFG->dirroot/blocks/moodleblock.class.php"); |
1557 | require_once("$CFG->dirroot/blocks/$block->name/block_$block->name.php"); |
1558 | $blockname = "block_$block->name"; |
1559 | if ($blockobject = new $blockname()) { |
1560 | $name = $blockobject->title.' ('.get_string('block').')'; |
1561 | } |
ec0810ee |
1562 | } |
1563 | } |
340ea4e8 |
1564 | break; |
bbbf2d40 |
1565 | |
1566 | default: |
1567 | error ('This is an unknown context!'); |
340ea4e8 |
1568 | return false; |
1569 | |
1570 | } |
340ea4e8 |
1571 | return $name; |
bbbf2d40 |
1572 | } |
1573 | |
1574 | |
1575 | /** |
1576 | * Extracts the relevant capabilities given a contextid. |
1577 | * All case based, example an instance of forum context. |
1578 | * Will fetch all forum related capabilities, while course contexts |
1579 | * Will fetch all capabilities |
0468976c |
1580 | * @param object context |
bbbf2d40 |
1581 | * @return array(); |
1582 | * |
1583 | * capabilities |
1584 | * `name` varchar(150) NOT NULL, |
1585 | * `captype` varchar(50) NOT NULL, |
1586 | * `contextlevel` int(10) NOT NULL, |
1587 | * `component` varchar(100) NOT NULL, |
1588 | */ |
0468976c |
1589 | function fetch_context_capabilities($context) { |
98882637 |
1590 | |
1591 | global $CFG; |
bbbf2d40 |
1592 | |
1593 | $sort = 'ORDER BY contextlevel,component,id'; // To group them sensibly for display |
98882637 |
1594 | |
d140ad3f |
1595 | switch ($context->aggregatelevel) { |
bbbf2d40 |
1596 | |
98882637 |
1597 | case CONTEXT_SYSTEM: // all |
1598 | $SQL = "select * from {$CFG->prefix}capabilities"; |
bbbf2d40 |
1599 | break; |
1600 | |
1601 | case CONTEXT_PERSONAL: |
0a8a95c9 |
1602 | $SQL = "select * from {$CFG->prefix}capabilities where contextlevel = ".CONTEXT_PERSONAL; |
bbbf2d40 |
1603 | break; |
1604 | |
1605 | case CONTEXT_USERID: |
0a8a95c9 |
1606 | $SQL = "select * from {$CFG->prefix}capabilities where contextlevel = ".CONTEXT_USERID; |
bbbf2d40 |
1607 | break; |
1608 | |
1609 | case CONTEXT_COURSECAT: // all |
98882637 |
1610 | $SQL = "select * from {$CFG->prefix}capabilities"; |
bbbf2d40 |
1611 | break; |
1612 | |
1613 | case CONTEXT_COURSE: // all |
98882637 |
1614 | $SQL = "select * from {$CFG->prefix}capabilities"; |
bbbf2d40 |
1615 | break; |
1616 | |
1617 | case CONTEXT_GROUP: // group caps |
1618 | break; |
1619 | |
1620 | case CONTEXT_MODULE: // mod caps |
98882637 |
1621 | $cm = get_record('course_modules', 'id', $context->instanceid); |
1622 | $module = get_record('modules', 'id', $cm->module); |
bbbf2d40 |
1623 | |
98882637 |
1624 | $SQL = "select * from {$CFG->prefix}capabilities where contextlevel = ".CONTEXT_MODULE." |
1625 | and component = 'mod/$module->name'"; |
bbbf2d40 |
1626 | break; |
1627 | |
1628 | case CONTEXT_BLOCK: // block caps |
98882637 |
1629 | $cb = get_record('block_instance', 'id', $context->instanceid); |
1630 | $block = get_record('block', 'id', $cb->blockid); |
bbbf2d40 |
1631 | |
98882637 |
1632 | $SQL = "select * from {$CFG->prefix}capabilities where contextlevel = ".CONTEXT_BLOCK." |
1633 | and component = 'block/$block->name'"; |
bbbf2d40 |
1634 | break; |
1635 | |
1636 | default: |
1637 | return false; |
1638 | } |
1639 | |
1640 | $records = get_records_sql($SQL.' '.$sort); |
1641 | return $records; |
1642 | |
1643 | } |
1644 | |
1645 | |
1646 | /** |
1647 | * This function pulls out all the resolved capabilities (overrides and |
1648 | * defaults) of a role used in capability overrieds in contexts at a given |
1649 | * context. |
0a8a95c9 |
1650 | * @param obj $context |
bbbf2d40 |
1651 | * @param int $roleid |
1652 | * @return array |
1653 | */ |
1648afb2 |
1654 | function role_context_capabilities($roleid, $context, $cap='') { |
98882637 |
1655 | global $CFG; |
1656 | |
1657 | $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID); |
0468976c |
1658 | if ($sitecontext->id == $context->id) { |
20aeb4b8 |
1659 | $contexts = array($sitecontext->id); |
1660 | } else { |
1661 | // first of all, figure out all parental contexts |
1662 | $contexts = array_reverse(get_parent_contexts($context)); |
98882637 |
1663 | } |
98882637 |
1664 | $contexts = '('.implode(',', $contexts).')'; |
1665 | |
1648afb2 |
1666 | if ($cap) { |
e4697bf7 |
1667 | $search = " AND rc.capability = '$cap' "; |
1648afb2 |
1668 | } else { |
1669 | $search = ''; |
1670 | } |
1671 | |
98882637 |
1672 | $SQL = "SELECT rc.* FROM {$CFG->prefix}role_capabilities rc, {$CFG->prefix}context c |
1673 | where rc.contextid in $contexts |
1674 | and rc.roleid = $roleid |
1648afb2 |
1675 | and rc.contextid = c.id $search |
d140ad3f |
1676 | ORDER BY c.aggregatelevel DESC, rc.capability DESC"; |
1648afb2 |
1677 | |
98882637 |
1678 | $capabilities = array(); |
1679 | |
4729012f |
1680 | if ($records = get_records_sql($SQL)) { |
1681 | // We are traversing via reverse order. |
1682 | foreach ($records as $record) { |
1683 | // If not set yet (i.e. inherit or not set at all), or currently we have a prohibit |
1684 | if (!isset($capabilities[$record->capability]) || $record->permission<-500) { |
1685 | $capabilities[$record->capability] = $record->permission; |
1686 | } |
1687 | } |
98882637 |
1688 | } |
1689 | return $capabilities; |
bbbf2d40 |
1690 | } |
1691 | |
1692 | |
1693 | /** |
0468976c |
1694 | * Recursive function which, given a context, find all parent context ids, |
bbbf2d40 |
1695 | * and return the array in reverse order, i.e. parent first, then grand |
1696 | * parent, etc. |
1697 | * @param object $context |
1698 | * @return array() |
1699 | */ |
bbbf2d40 |
1700 | function get_parent_contexts($context) { |
1701 | |
d140ad3f |
1702 | switch ($context->aggregatelevel) { |
bbbf2d40 |
1703 | |
1704 | case CONTEXT_SYSTEM: // no parent |
957861f7 |
1705 | return array(); |
bbbf2d40 |
1706 | break; |
1707 | |
1708 | case CONTEXT_PERSONAL: |
957861f7 |
1709 | if (!$parent = get_context_instance(CONTEXT_SYSTEM, SITEID)) { |
1710 | return array(); |
1711 | } else { |
1712 | return array($parent->id); |
1713 | } |
bbbf2d40 |
1714 | break; |
1715 | |
1716 | case CONTEXT_USERID: |
957861f7 |
1717 | if (!$parent = get_context_instance(CONTEXT_SYSTEM, SITEID)) { |
1718 | return array(); |
1719 | } else { |
1720 | return array($parent->id); |
1721 | } |
bbbf2d40 |
1722 | break; |
1723 | |
1724 | case CONTEXT_COURSECAT: // Coursecat -> coursecat or site |
957861f7 |
1725 | if (!$coursecat = get_record('course_categories','id',$context->instanceid)) { |
1726 | return array(); |
1727 | } |
c5ddc3fd |
1728 | if (!empty($coursecat->parent)) { // return parent value if exist |
bbbf2d40 |
1729 | $parent = get_context_instance(CONTEXT_COURSECAT, $coursecat->parent); |
1730 | return array_merge(array($parent->id), get_parent_contexts($parent)); |
1731 | } else { // else return site value |
1732 | $parent = get_context_instance(CONTEXT_SYSTEM, SITEID); |
1733 | return array($parent->id); |
1734 | } |
1735 | break; |
1736 | |
1737 | case CONTEXT_COURSE: // 1 to 1 to course cat |
957861f7 |
1738 | if (!$course = get_record('course','id',$context->instanceid)) { |
1739 | return array(); |
1740 | } |
1741 | if (!empty($course->category)) { |
1742 | $parent = get_context_instance(CONTEXT_COURSECAT, $course->category); |
1743 | return array_merge(array($parent->id), get_parent_contexts($parent)); |
1744 | } else { |
1745 | return array(); |
1746 | } |
bbbf2d40 |
1747 | break; |
1748 | |
1749 | case CONTEXT_GROUP: // 1 to 1 to course |
957861f7 |
1750 | if (!$group = get_record('groups','id',$context->instanceid)) { |
1751 | return array(); |
1752 | } |
1753 | if ($parent = get_context_instance(CONTEXT_COURSE, $group->courseid)) { |
1754 | return array_merge(array($parent->id), get_parent_contexts($parent)); |
1755 | } else { |
1756 | return array(); |
1757 | } |
bbbf2d40 |
1758 | break; |
1759 | |
1760 | case CONTEXT_MODULE: // 1 to 1 to course |
957861f7 |
1761 | if (!$cm = get_record('course_modules','id',$context->instanceid)) { |
1762 | return array(); |
1763 | } |
1764 | if ($parent = get_context_instance(CONTEXT_COURSE, $cm->course)) { |
1765 | return array_merge(array($parent->id), get_parent_contexts($parent)); |
1766 | } else { |
1767 | return array(); |
1768 | } |
bbbf2d40 |
1769 | break; |
1770 | |
1771 | case CONTEXT_BLOCK: // 1 to 1 to course |
957861f7 |
1772 | if (!$block = get_record('block_instance','id',$context->instanceid)) { |
1773 | return array(); |
1774 | } |
1775 | if ($parent = get_context_instance(CONTEXT_COURSE, $block->pageid)) { |
1776 | return array_merge(array($parent->id), get_parent_contexts($parent)); |
1777 | } else { |
1778 | return array(); |
1779 | } |
bbbf2d40 |
1780 | break; |
1781 | |
1782 | default: |
957861f7 |
1783 | error('This is an unknown context!'); |
bbbf2d40 |
1784 | return false; |
1785 | } |
bbbf2d40 |
1786 | } |
1787 | |
1788 | |
1789 | /** |
1790 | * This function gets the capability of a role in a given context. |
1791 | * It is needed when printing override forms. |
1792 | * @param int $contextid |
bbbf2d40 |
1793 | * @param string $capability |
1794 | * @param array $capabilities - array loaded using role_context_capabilities |
1795 | * @return int (allow, prevent, prohibit, inherit) |
1796 | */ |
bbbf2d40 |
1797 | function get_role_context_capability($contextid, $capability, $capabilities) { |
98882637 |
1798 | return $capabilities[$contextid][$capability]; |
bbbf2d40 |
1799 | } |
1800 | |
1801 | |
cee0901c |
1802 | /** |
1803 | * Returns the human-readable, translated version of the capability. |
1804 | * Basically a big switch statement. |
1805 | * @param $capabilityname - e.g. mod/choice:readresponses |
1806 | */ |
ceb83c70 |
1807 | function get_capability_string($capabilityname) { |
bbbf2d40 |
1808 | |
cee0901c |
1809 | // Typical capabilityname is mod/choice:readresponses |
ceb83c70 |
1810 | |
1811 | $names = split('/', $capabilityname); |
1812 | $stringname = $names[1]; // choice:readresponses |
1813 | $components = split(':', $stringname); |
1814 | $componentname = $components[0]; // choice |
98882637 |
1815 | |
1816 | switch ($names[0]) { |
1817 | case 'mod': |
ceb83c70 |
1818 | $string = get_string($stringname, $componentname); |
98882637 |
1819 | break; |
1820 | |
1821 | case 'block': |
ceb83c70 |
1822 | $string = get_string($stringname, 'block_'.$componentname); |
98882637 |
1823 | break; |
ceb83c70 |
1824 | |
98882637 |
1825 | case 'moodle': |
ceb83c70 |
1826 | $string = get_string($stringname, 'role'); |
98882637 |
1827 | break; |
1828 | |
1829 | case 'enrol': |
ceb83c70 |
1830 | $string = get_string($stringname, 'enrol_'.$componentname); |
1831 | break; |
98882637 |
1832 | |
1833 | default: |
ceb83c70 |
1834 | $string = get_string($stringname); |
98882637 |
1835 | break; |
98882637 |
1836 | |
1837 | } |
ceb83c70 |
1838 | return $string; |
bbbf2d40 |
1839 | } |
1840 | |
1841 | |
cee0901c |
1842 | /** |
1843 | * This gets the mod/block/course/core etc strings. |
1844 | * @param $component |
1845 | * @param $contextlevel |
1846 | */ |
bbbf2d40 |
1847 | function get_component_string($component, $contextlevel) { |
1848 | |
98882637 |
1849 | switch ($contextlevel) { |
bbbf2d40 |
1850 | |
98882637 |
1851 | case CONTEXT_SYSTEM: |
ceb83c70 |
1852 | $string = get_string('coresystem'); |
bbbf2d40 |
1853 | break; |
1854 | |
1855 | case CONTEXT_PERSONAL: |
98882637 |
1856 | $string = get_string('personal'); |
bbbf2d40 |
1857 | break; |
1858 | |
1859 | case CONTEXT_USERID: |
98882637 |
1860 | $string = get_string('users'); |
bbbf2d40 |
1861 | break; |
1862 | |
1863 | case CONTEXT_COURSECAT: |
98882637 |
1864 | $string = get_string('categories'); |
bbbf2d40 |
1865 | break; |
1866 | |
1867 | case CONTEXT_COURSE: |
98882637 |
1868 | $string = get_string('course'); |
bbbf2d40 |
1869 | break; |
1870 | |
1871 | case CONTEXT_GROUP: |
98882637 |
1872 | $string = get_string('group'); |
bbbf2d40 |
1873 | break; |
1874 | |
1875 | case CONTEXT_MODULE: |
98882637 |
1876 | $string = get_string('modulename', basename($component)); |
bbbf2d40 |
1877 | break; |
1878 | |
1879 | case CONTEXT_BLOCK: |
98882637 |
1880 | $string = get_string('blockname', 'block_'.$component.'.php'); |
bbbf2d40 |
1881 | break; |
1882 | |
1883 | default: |
1884 | error ('This is an unknown context!'); |
1885 | return false; |
98882637 |
1886 | |
1887 | } |
98882637 |
1888 | return $string; |
bbbf2d40 |
1889 | } |
cee0901c |
1890 | |
945f88ca |
1891 | /** gets the list of roles assigned to this context |
1892 | * @param object $context |
1893 | * @return array |
1894 | */ |
e4dd3222 |
1895 | function get_roles_used_in_context($context) { |
1896 | |
1897 | global $CFG; |
1898 | |
1899 | return get_records_sql('SELECT distinct r.id, r.name |
1900 | FROM '.$CFG->prefix.'role_assignments ra, |
1901 | '.$CFG->prefix.'role r |
1902 | WHERE r.id = ra.roleid |
1903 | AND ra.contextid = '.$context->id.' |
1904 | ORDER BY r.sortorder ASC'); |
1905 | } |
1906 | |
945f88ca |
1907 | /** this function is used to print roles column in user profile page. |
1908 | * @param int userid |
1909 | * @param int contextid |
1910 | * @return string |
1911 | */ |
0a8a95c9 |
1912 | function get_user_roles_in_context($userid, $contextid){ |
1913 | global $CFG; |
1914 | |
1915 | $rolestring = ''; |
1916 | $SQL = 'select * from '.$CFG->prefix.'role_assignments ra, '.$CFG->prefix.'role r where ra.userid='.$userid.' and ra.contextid='.$contextid.' and ra.roleid = r.id'; |
1917 | if ($roles = get_records_sql($SQL)) { |
1918 | foreach ($roles as $userrole) { |
1919 | $rolestring .= '<a href="'.$CFG->wwwroot.'/user/index.php?contextid='.$userrole->contextid.'&roleid='.$userrole->roleid.'">'.$userrole->name.'</a>, '; |
1920 | } |
1921 | |
1922 | } |
1923 | return rtrim($rolestring, ', '); |
1924 | } |
68c52526 |
1925 | |
1926 | |
945f88ca |
1927 | /** |
1928 | * Checks if a user can override capabilities of a particular role in this context |
1929 | * @param object $context |
1930 | * @param int targetroleid - the id of the role you want to override |
1931 | * @return boolean |
1932 | */ |
68c52526 |
1933 | function user_can_override($context, $targetroleid) { |
1934 | // first check if user has override capability |
1935 | // if not return false; |
1936 | if (!has_capability('moodle/role:override', $context)) { |
1937 | return false; |
1938 | } |
1939 | // pull out all active roles of this user from this context(or above) |
c0614051 |
1940 | if ($userroles = get_user_roles($context)) { |
1941 | foreach ($userroles as $userrole) { |
1942 | // if any in the role_allow_override table, then it's ok |
1943 | if (get_record('role_allow_override', 'roleid', $userrole->roleid, 'allowoverride', $targetroleid)) { |
1944 | return true; |
1945 | } |
68c52526 |
1946 | } |
1947 | } |
1948 | |
1949 | return false; |
1950 | |
1951 | } |
1952 | |
945f88ca |
1953 | /** |
1954 | * Checks if a user can assign users to a particular role in this context |
1955 | * @param object $context |
1956 | * @param int targetroleid - the id of the role you want to assign users to |
1957 | * @return boolean |
1958 | */ |
68c52526 |
1959 | function user_can_assign($context, $targetroleid) { |
1960 | |
1961 | // first check if user has override capability |
1962 | // if not return false; |
1963 | if (!has_capability('moodle/role:assign', $context)) { |
1964 | return false; |
1965 | } |
1966 | // pull out all active roles of this user from this context(or above) |
c0614051 |
1967 | if ($userroles = get_user_roles($context)) { |
1968 | foreach ($userroles as $userrole) { |
1969 | // if any in the role_allow_override table, then it's ok |
1970 | if (get_record('role_allow_assign', 'roleid', $userrole->roleid, 'allowassign', $targetroleid)) { |
1971 | return true; |
1972 | } |
68c52526 |
1973 | } |
1974 | } |
1975 | |
1976 | return false; |
1977 | } |
1978 | |
945f88ca |
1979 | /** |
1980 | * gets all the user roles assigned in this context, or higher contexts |
1981 | * this is mainly used when checking if a user can assign a role, or overriding a role |
1982 | * i.e. we need to know what this user holds, in order to verify against allow_assign and |
1983 | * allow_override tables |
1984 | * @param object $context |
1985 | * @param int $userid |
1986 | * @return array |
1987 | */ |
c0614051 |
1988 | function get_user_roles($context, $userid=0) { |
68c52526 |
1989 | |
1990 | global $USER, $CFG, $db; |
c0614051 |
1991 | |
1992 | if (empty($userid)) { |
1993 | if (empty($USER->id)) { |
1994 | return array(); |
1995 | } |
1996 | $userid = $USER->id; |
1997 | } |
1998 | |
1999 | if ($parents = get_parent_contexts($context)) { |
2000 | $contexts = ' AND ra.contextid IN ('.implode(',' , $parents).')'; |
2001 | } else { |
2002 | $contexts = ' AND ra.contextid = \''.$context->id.'\''; |
2003 | } |
2004 | |
68c52526 |
2005 | return get_records_sql('SELECT * |
2006 | FROM '.$CFG->prefix.'role_assignments ra |
c0614051 |
2007 | WHERE ra.userid = '.$userid. |
2008 | $contexts); |
68c52526 |
2009 | } |
2010 | |
945f88ca |
2011 | /** |
2012 | * Creates a record in the allow_override table |
2013 | * @param int sroleid - source roleid |
2014 | * @param int troleid - target roleid |
2015 | * @return int - id or false |
2016 | */ |
2017 | function allow_override($sroleid, $troleid) { |
2018 | $record->roleid = $sroleid; |
2019 | $record->allowoverride = $troleid; |
2020 | return insert_record('role_allow_override', $record); |
2021 | } |
2022 | |
2023 | /** |
2024 | * Creates a record in the allow_assign table |
2025 | * @param int sroleid - source roleid |
2026 | * @param int troleid - target roleid |
2027 | * @return int - id or false |
2028 | */ |
2029 | function allow_assign($sroleid, $troleid) { |
2030 | $record->roleid = $sroleid; |
2031 | $record->allowassign = $troleid; |
2032 | return insert_record('role_allow_assign', $record); |
2033 | } |
2034 | |
2035 | /** |
2036 | * gets a list of roles assignalbe in this context for this user |
2037 | * @param object $context |
2038 | * @return array |
2039 | */ |
2040 | function get_assignable_roles ($context) { |
2041 | |
2042 | $role = get_records('role'); |
2043 | $options = array(); |
2044 | foreach ($role as $rolex) { |
2045 | if (user_can_assign($context, $rolex->id)) { |
2046 | $options[$rolex->id] = $rolex->name; |
2047 | } |
2048 | } |
2049 | return $options; |
2050 | } |
2051 | |
2052 | /** |
2053 | * gets a list of roles that can be overriden in this context by this user |
2054 | * @param object $context |
2055 | * @return array |
2056 | */ |
2057 | function get_overridable_roles ($context) { |
2058 | |
2059 | $role = get_records('role'); |
2060 | $options = array(); |
2061 | foreach ($role as $rolex) { |
2062 | if (user_can_override($context, $rolex->id)) { |
2063 | $options[$rolex->id] = $rolex->name; |
2064 | } |
2065 | } |
2066 | |
2067 | return $options; |
2068 | |
2069 | } |
1648afb2 |
2070 | |
2071 | |
2072 | /** |
2073 | * who has this capability in this context |
2074 | * does not handling user level resolving!!! |
2075 | * i.e 1 person has 2 roles 1 allow, 1 prevent, this will not work properly |
2076 | * @param $context - object |
2077 | * @param $capability - string capability |
2078 | * @param $fields - fields to be pulled |
2079 | * @param $sort - the sort order |
04417640 |
2080 | * @param $limitfrom - number of records to skip (offset) |
2081 | * @param $limitnum - number of records to fetch |
1c45e42e |
2082 | * @param $groups - single group or array of groups - group(s) user is in |
1648afb2 |
2083 | */ |
1c45e42e |
2084 | function get_users_by_capability($context, $capability, $fields='u.*', $sort='', $limitfrom='', $limitnum='', $groups='') { |
1648afb2 |
2085 | |
2086 | global $CFG; |
2087 | |
1c45e42e |
2088 | if ($groups) { |
2089 | |
2090 | $groupjoin = 'LEFT JOIN '.$CFG->prefix.'groups_members gm ON gm.userid = ra.userid'; |
2091 | |
2092 | if (is_array($groups)) { |
2093 | $groupsql = 'AND gm.id IN ('.implode(',', $groups).')'; |
2094 | } else { |
2095 | $groupsql = 'AND gm.id = '.$groups; |
2096 | } |
2097 | } else { |
2098 | $groupjoin = ''; |
2099 | $groupsql = ''; |
2100 | } |
2101 | |
1648afb2 |
2102 | // first get all roles with this capability in this context, or above |
ec7a8b79 |
2103 | $possibleroles = get_roles_with_capability($capability, CAP_ALLOW, $context); |
1648afb2 |
2104 | $validroleids = array(); |
2105 | foreach ($possibleroles as $prole) { |
2106 | $caps = role_context_capabilities($prole->id, $context, $capability); // resolved list |
2107 | if ($caps[$capability] > 0) { // resolved capability > 0 |
2108 | $validroleids[] = $prole->id; |
2109 | } |
2110 | } |
2111 | |
ec7a8b79 |
2112 | /// the following few lines may not be needed |
1648afb2 |
2113 | if ($usercontexts = get_parent_contexts($context)) { |
2114 | $listofcontexts = '('.implode(',', $usercontexts).')'; |
2115 | } else { |
2116 | $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID); |
2117 | $listofcontexts = '('.$sitecontext->id.')'; // must be site |
2118 | } |
2119 | |
2120 | $roleids = '('.implode(',', $validroleids).')'; |
2121 | |
2122 | $select = ' SELECT '.$fields; |
1c45e42e |
2123 | $from = ' FROM '.$CFG->prefix.'user u LEFT JOIN '.$CFG->prefix.'role_assignments ra ON ra.userid = u.id '.$groupjoin; |
2124 | $where = ' WHERE (ra.contextid = '.$context->id.' OR ra.contextid in '.$listofcontexts.') AND u.deleted = 0 AND ra.roleid in '.$roleids.' '.$groupsql; |
1648afb2 |
2125 | |
04417640 |
2126 | return get_records_sql($select.$from.$where.$sort, $limitfrom, $limitnum); |
1648afb2 |
2127 | |
2128 | } |
7700027f |
2129 | |
1dc1f037 |
2130 | ?> |