MDL-78551 core_access: Add support for hooks api

This commit is contained in:
Safat
2024-03-26 13:53:39 +11:00
parent 968897b653
commit fc7127e867
3 changed files with 101 additions and 25 deletions
+13 -25
View File
@@ -1645,18 +1645,6 @@ function role_assign($roleid, $userid, $contextid, $component = '', $itemid = 0,
core_course_category::role_assignment_changed($roleid, $context);
// Update the room membership and power levels when the user role changes.
if (\core_communication\api::is_available() && $coursecontext = $context->get_course_context(false)) {
$communication = \core_communication\api::load_by_instance(
$coursecontext,
'core_course',
'coursecommunication',
$coursecontext->instanceid,
);
$communication->update_room_membership([$userid]);
}
$event = \core\event\role_assigned::create(array(
'context' => $context,
'objectid' => $ra->roleid,
@@ -1670,6 +1658,13 @@ function role_assign($roleid, $userid, $contextid, $component = '', $itemid = 0,
$event->add_record_snapshot('role_assignments', $ra);
$event->trigger();
// Dispatch the hook for post role assignment actions.
$hook = new \core\hook\access\after_role_assigned(
context: $context,
userid: $userid,
);
\core\di::get(\core\hook\manager::class)->dispatch($hook);
return $ra->id;
}
@@ -1763,19 +1758,12 @@ function role_unassign_all(array $params, $subcontexts = false, $includemanual =
$event->trigger();
core_course_category::role_assignment_changed($ra->roleid, $context);
// Update the room membership and power levels when the user role changes.
if (\core_communication\api::is_available() && $coursecontext = $context->get_course_context(false)) {
$communication = \core_communication\api::load_by_instance(
$coursecontext,
'core_course',
'coursecommunication',
$coursecontext->instanceid,
);
$communication->update_room_membership([$ra->userid]);
}
// Dispatch the hook for post role assignment actions.
$hook = new \core\hook\access\after_role_unassigned(
context: $context,
userid: $ra->userid,
);
\core\di::get(\core\hook\manager::class)->dispatch($hook);
}
}
unset($ras);
@@ -0,0 +1,44 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\hook\access;
use context;
/**
* Hook after a role is assigned.
*
* @package core
* @copyright 2024 Safat Shahin <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
#[\core\attribute\label('Allows plugins or features to perform actions after a role is assigned to a user.')]
#[\core\attribute\tags('role,', 'user')]
class after_role_assigned {
/**
* Constructor for the hook.
*
* @param context $context The context of the role assignment.
* @param int $userid The user id of the user.
*
*/
public function __construct(
public readonly context $context,
public readonly int $userid,
) {
}
}
@@ -0,0 +1,44 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\hook\access;
use context;
/**
* Hook after a role is unassigned.
*
* @package core
* @copyright 2024 Safat Shahin <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
#[\core\attribute\label('Allows plugins or features to perform actions after a role is unassigned for a user.')]
#[\core\attribute\tags('role,', 'user')]
class after_role_unassigned {
/**
* Constructor for the hook.
*
* @param context $context The context of the role assignment.
* @param int $userid The user id of the user.
*
*/
public function __construct(
public readonly context $context,
public readonly int $userid,
) {
}
}