| Línea 1478... |
Línea 1478... |
| 1478 |
* Unassign a capability from a role.
|
1478 |
* Unassign a capability from a role.
|
| 1479 |
*
|
1479 |
*
|
| 1480 |
* @param string $capability the name of the capability
|
1480 |
* @param string $capability the name of the capability
|
| 1481 |
* @param int $roleid the role id
|
1481 |
* @param int $roleid the role id
|
| 1482 |
* @param int|context $contextid null means all contexts
|
1482 |
* @param int|context $contextid null means all contexts
|
| - |
|
1483 |
* @param bool $showdebug if true, will show debugging messages
|
| 1483 |
* @return boolean true or exception
|
1484 |
* @return boolean true or exception
|
| 1484 |
*/
|
1485 |
*/
|
| 1485 |
function unassign_capability($capability, $roleid, $contextid = null) {
|
1486 |
function unassign_capability($capability, $roleid, $contextid = null, bool $showdebug = true) {
|
| 1486 |
global $DB, $USER;
|
1487 |
global $DB, $USER;
|
| Línea 1487... |
Línea 1488... |
| 1487 |
|
1488 |
|
| 1488 |
// Capability must exist.
|
1489 |
// Capability must exist.
|
| 1489 |
if (!$capinfo = get_capability_info($capability)) {
|
1490 |
if (!get_capability_info($capability, $showdebug)) {
|
| 1490 |
throw new coding_exception("Capability '{$capability}' was not found! This has to be fixed in code.");
|
1491 |
throw new coding_exception("Capability '{$capability}' was not found! This has to be fixed in code.");
|
| Línea 1491... |
Línea 1492... |
| 1491 |
}
|
1492 |
}
|
| 1492 |
|
1493 |
|
| Línea 2458... |
Línea 2459... |
| 2458 |
array_key_exists($cachedcap->name, $newcapdef) === false) {
|
2459 |
array_key_exists($cachedcap->name, $newcapdef) === false) {
|
| Línea 2459... |
Línea 2460... |
| 2459 |
|
2460 |
|
| 2460 |
// Delete from roles.
|
2461 |
// Delete from roles.
|
| 2461 |
if ($roles = get_roles_with_capability($cachedcap->name)) {
|
2462 |
if ($roles = get_roles_with_capability($cachedcap->name)) {
|
| - |
|
2463 |
foreach ($roles as $role) {
|
| 2462 |
foreach ($roles as $role) {
|
2464 |
if (!unassign_capability(
|
| - |
|
2465 |
capability: $cachedcap->name,
|
| - |
|
2466 |
roleid: $role->id,
|
| - |
|
2467 |
showdebug: false, // Suppress debugging messages in the get_capability_info().
|
| 2463 |
if (!unassign_capability($cachedcap->name, $role->id)) {
|
2468 |
)) {
|
| 2464 |
throw new \moodle_exception('cannotunassigncap', 'error', '',
|
2469 |
throw new \moodle_exception('cannotunassigncap', 'error', '',
|
| 2465 |
(object)array('cap' => $cachedcap->name, 'role' => $role->name));
|
2470 |
(object)array('cap' => $cachedcap->name, 'role' => $role->name));
|
| 2466 |
}
|
2471 |
}
|
| 2467 |
}
|
2472 |
}
|
| Línea 2593... |
Línea 2598... |
| 2593 |
}
|
2598 |
}
|
| Línea 2594... |
Línea 2599... |
| 2594 |
|
2599 |
|
| 2595 |
/**
|
2600 |
/**
|
| 2596 |
* Returns capability information (cached)
|
2601 |
* Returns capability information (cached)
|
| 2597 |
*
|
2602 |
*
|
| - |
|
2603 |
* @param string $capabilityname the capability name.
|
| 2598 |
* @param string $capabilityname
|
2604 |
* @param bool $showdebug if true, will show debugging messages.
|
| 2599 |
* @return ?stdClass object or null if capability not found
|
2605 |
* @return ?stdClass object or null if capability not found
|
| 2600 |
*/
|
2606 |
*/
|
| 2601 |
function get_capability_info($capabilityname) {
|
2607 |
function get_capability_info(string $capabilityname, bool $showdebug = true): ?stdClass {
|
| Línea 2602... |
Línea 2608... |
| 2602 |
$caps = get_all_capabilities();
|
2608 |
$caps = get_all_capabilities();
|
| 2603 |
|
2609 |
|
| 2604 |
// Check for deprecated capability.
|
2610 |
// Check for deprecated capability.
|
| 2605 |
if ($deprecatedinfo = get_deprecated_capability_info($capabilityname)) {
|
2611 |
if ($deprecatedinfo = get_deprecated_capability_info($capabilityname)) {
|
| 2606 |
if (!empty($deprecatedinfo['replacement'])) {
|
2612 |
if (!empty($deprecatedinfo['replacement'])) {
|
| 2607 |
// Let's try again with this capability if it exists.
|
2613 |
// Let's try again with this capability if it exists.
|
| 2608 |
if (isset($caps[$deprecatedinfo['replacement']])) {
|
2614 |
if (isset($caps[$deprecatedinfo['replacement']])) {
|
| - |
|
2615 |
$capabilityname = $deprecatedinfo['replacement'];
|
| 2609 |
$capabilityname = $deprecatedinfo['replacement'];
|
2616 |
} else {
|
| 2610 |
} else {
|
2617 |
if ($showdebug) {
|
| - |
|
2618 |
debugging("Capability '{$capabilityname}' was supposed to be replaced with ".
|
| 2611 |
debugging("Capability '{$capabilityname}' was supposed to be replaced with ".
|
2619 |
"'{$deprecatedinfo['replacement']}', which does not exist !");
|
| 2612 |
"'{$deprecatedinfo['replacement']}', which does not exist !");
|
2620 |
}
|
| - |
|
2621 |
}
|
| - |
|
2622 |
}
|
| 2613 |
}
|
2623 |
|
| 2614 |
}
|
2624 |
if ($showdebug) {
|
| - |
|
2625 |
$fullmessage = $deprecatedinfo['fullmessage'];
|
| 2615 |
$fullmessage = $deprecatedinfo['fullmessage'];
|
2626 |
debugging($fullmessage, DEBUG_DEVELOPER);
|
| 2616 |
debugging($fullmessage, DEBUG_DEVELOPER);
|
2627 |
}
|
| 2617 |
}
|
2628 |
}
|
| 2618 |
if (!isset($caps[$capabilityname])) {
|
2629 |
if (!isset($caps[$capabilityname])) {
|
| Línea 2639... |
Línea 2650... |
| 2639 |
$components = [];
|
2650 |
$components = [];
|
| 2640 |
$alldeprecatedcaps = [];
|
2651 |
$alldeprecatedcaps = [];
|
| 2641 |
foreach ($allcaps as $cap) {
|
2652 |
foreach ($allcaps as $cap) {
|
| 2642 |
if (!in_array($cap['component'], $components)) {
|
2653 |
if (!in_array($cap['component'], $components)) {
|
| 2643 |
$components[] = $cap['component'];
|
2654 |
$components[] = $cap['component'];
|
| - |
|
2655 |
|
| 2644 |
$defpath = core_component::get_component_directory($cap['component']).'/db/access.php';
|
2656 |
$componentdir = core_component::get_component_directory($cap['component']);
|
| - |
|
2657 |
if ($componentdir === null) {
|
| - |
|
2658 |
continue;
|
| - |
|
2659 |
}
|
| - |
|
2660 |
|
| - |
|
2661 |
$defpath = "{$componentdir}/db/access.php";
|
| 2645 |
if (file_exists($defpath)) {
|
2662 |
if (file_exists($defpath)) {
|
| 2646 |
$deprecatedcapabilities = [];
|
2663 |
$deprecatedcapabilities = [];
|
| 2647 |
require($defpath);
|
2664 |
require($defpath);
|
| 2648 |
if (!empty($deprecatedcapabilities)) {
|
2665 |
if (!empty($deprecatedcapabilities)) {
|
| 2649 |
foreach ($deprecatedcapabilities as $cname => $cdef) {
|
2666 |
foreach ($deprecatedcapabilities as $cname => $cdef) {
|
| Línea 2964... |
Línea 2981... |
| 2964 |
* use role_get_names() if you need role names.
|
2981 |
* use role_get_names() if you need role names.
|
| 2965 |
*
|
2982 |
*
|
| 2966 |
* @param context $context optional context for course role name aliases
|
2983 |
* @param context $context optional context for course role name aliases
|
| 2967 |
* @return array of role records with optional coursealias property
|
2984 |
* @return array of role records with optional coursealias property
|
| 2968 |
*/
|
2985 |
*/
|
| 2969 |
function get_all_roles(context $context = null) {
|
2986 |
function get_all_roles(?context $context = null) {
|
| 2970 |
global $DB;
|
2987 |
global $DB;
|
| Línea 2971... |
Línea 2988... |
| 2971 |
|
2988 |
|
| 2972 |
if (!$context or !$coursecontext = $context->get_course_context(false)) {
|
2989 |
if (!$context or !$coursecontext = $context->get_course_context(false)) {
|
| 2973 |
$coursecontext = null;
|
2990 |
$coursecontext = null;
|
| Línea 4403... |
Línea 4420... |
| 4403 |
|
4420 |
|
| 4404 |
if (!isset($USER->access)) {
|
4421 |
if (!isset($USER->access)) {
|
| 4405 |
load_all_capabilities();
|
4422 |
load_all_capabilities();
|
| Línea 4406... |
Línea -... |
| 4406 |
}
|
- |
|
| 4407 |
|
- |
|
| 4408 |
// Make sure that course index is refreshed.
|
- |
|
| 4409 |
if ($coursecontext = $context->get_course_context()) {
|
- |
|
| 4410 |
core_courseformat\base::session_cache_reset(get_course($coursecontext->instanceid));
|
- |
|
| 4411 |
}
|
4423 |
}
|
| 4412 |
|
4424 |
|
| 4413 |
// Add the switch RA
|
4425 |
// Add the switch RA
|
| 4414 |
if ($roleid == 0) {
|
4426 |
if ($roleid == 0) {
|
| 4415 |
unset($USER->access['rsw'][$context->path]);
|
4427 |
unset($USER->access['rsw'][$context->path]);
|
| Línea 4416... |
Línea 4428... |
| 4416 |
return true;
|
4428 |
return true;
|
| Línea -... |
Línea 4429... |
| - |
|
4429 |
}
|
| - |
|
4430 |
|
| - |
|
4431 |
$USER->access['rsw'][$context->path] = $roleid;
|
| - |
|
4432 |
|
| - |
|
4433 |
// Dispatch the hook for post user switch.
|
| - |
|
4434 |
$hook = new \core\hook\access\after_role_switched(
|
| 4417 |
}
|
4435 |
context: $context,
|
| 4418 |
|
4436 |
roleid: $roleid
|
| Línea 4419... |
Línea 4437... |
| 4419 |
$USER->access['rsw'][$context->path] = $roleid;
|
4437 |
);
|
| 4420 |
|
4438 |
\core\di::get(\core\hook\manager::class)->dispatch($hook);
|
| Línea 4639... |
Línea 4657... |
| 4639 |
* @param array of role objects with a ->localname field containing the context-specific role name.
|
4657 |
* @param array of role objects with a ->localname field containing the context-specific role name.
|
| 4640 |
* @param int $rolenamedisplay
|
4658 |
* @param int $rolenamedisplay
|
| 4641 |
* @param bool $returnmenu true means id=>localname, false means id=>rolerecord
|
4659 |
* @param bool $returnmenu true means id=>localname, false means id=>rolerecord
|
| 4642 |
* @return array Array of context-specific role names, or role objects with a ->localname field added.
|
4660 |
* @return array Array of context-specific role names, or role objects with a ->localname field added.
|
| 4643 |
*/
|
4661 |
*/
|
| 4644 |
function role_get_names(context $context = null, $rolenamedisplay = ROLENAME_ALIAS, $returnmenu = null) {
|
4662 |
function role_get_names(?context $context = null, $rolenamedisplay = ROLENAME_ALIAS, $returnmenu = null) {
|
| 4645 |
return role_fix_names(get_all_roles($context), $context, $rolenamedisplay, $returnmenu);
|
4663 |
return role_fix_names(get_all_roles($context), $context, $rolenamedisplay, $returnmenu);
|
| 4646 |
}
|
4664 |
}
|
| Línea 4647... |
Línea 4665... |
| 4647 |
|
4665 |
|
| 4648 |
/**
|
4666 |
/**
|
| Línea 4652... |
Línea 4670... |
| 4652 |
* @param context $context the context, null means system context
|
4670 |
* @param context $context the context, null means system context
|
| 4653 |
* @param int $rolenamedisplay
|
4671 |
* @param int $rolenamedisplay
|
| 4654 |
* @param bool $returnmenu null means keep the same format as $roleoptions, true means id=>localname, false means id=>rolerecord
|
4672 |
* @param bool $returnmenu null means keep the same format as $roleoptions, true means id=>localname, false means id=>rolerecord
|
| 4655 |
* @return array Array of context-specific role names, or role objects with a ->localname field added.
|
4673 |
* @return array Array of context-specific role names, or role objects with a ->localname field added.
|
| 4656 |
*/
|
4674 |
*/
|
| 4657 |
function role_fix_names($roleoptions, context $context = null, $rolenamedisplay = ROLENAME_ALIAS, $returnmenu = null) {
|
4675 |
function role_fix_names($roleoptions, ?context $context = null, $rolenamedisplay = ROLENAME_ALIAS, $returnmenu = null) {
|
| 4658 |
global $DB;
|
4676 |
global $DB;
|
| Línea 4659... |
Línea 4677... |
| 4659 |
|
4677 |
|
| 4660 |
if (empty($roleoptions)) {
|
4678 |
if (empty($roleoptions)) {
|
| 4661 |
return array();
|
4679 |
return array();
|