MDL-40044 Roles: Added role_deleted event and replaced related add_to_log

This commit is contained in:
Rajesh Taneja 2013-08-06 09:37:55 +08:00
parent 50ff861263
commit a7524e354b
5 changed files with 114 additions and 5 deletions

View File

@ -90,7 +90,6 @@ switch ($action) {
}
// Deleted a role sitewide...
$systemcontext->mark_dirty();
add_to_log(SITEID, 'role', 'delete', 'admin/roles/manage.php', $roles[$roleid]->localname, '', $USER->id);
redirect($baseurl);
break;

View File

@ -182,6 +182,7 @@ $string['errorbadroleshortname'] = 'Incorrect role short name';
$string['errorexistsrolename'] = 'Role name already exists';
$string['errorexistsroleshortname'] = 'Role name already exists';
$string['eventroleassigned'] = 'Role assigned';
$string['eventroledeleted'] = 'Role deleted';
$string['eventroleunassigned'] = 'Role unassigned';
$string['existingadmins'] = 'Current site administrators';
$string['existingusers'] = '{$a} existing users';

View File

@ -1515,13 +1515,28 @@ function delete_role($roleid) {
$DB->delete_records('role_names', array('roleid'=>$roleid));
$DB->delete_records('role_context_levels', array('roleid'=>$roleid));
// finally delete the role itself
// get this before the name is gone for logging
$rolename = $DB->get_field('role', 'name', array('id'=>$roleid));
// Get role record before it's deleted.
$role = $DB->get_record('role', array('id'=>$roleid));
// Finally delete the role itself.
$DB->delete_records('role', array('id'=>$roleid));
add_to_log(SITEID, 'role', 'delete', 'admin/roles/action=delete&roleid='.$roleid, $rolename, '');
// Trigger event.
$event = \core\event\role_deleted::create(
array(
'context' => context_system::instance(),
'objectid' => $roleid,
'other' =>
array(
'name' => $role->name,
'shortname' => $role->shortname,
'description' => $role->description,
'archetype' => $role->archetype
)
)
);
$event->add_record_snapshot('role', $role);
$event->trigger();
return true;
}

View File

@ -0,0 +1,73 @@
<?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\event;
/**
* Role assigned event.
*
* @package core_event
* @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class role_deleted extends base {
/**
* Initialise event parameters.
*/
protected function init() {
$this->data['objecttable'] = 'role';
$this->data['crud'] = 'd';
// TODO: MDL-41040 set level.
$this->data['level'] = 50;
}
/**
* Returns localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventroledeleted', 'role');
}
/**
* Returns non-localised event description with id's for admin use only.
*
* @return string
*/
public function get_description() {
return 'Role ' . $this->objectid . ' is deleted by user ' . $this->userid;
}
/**
* Returns relevant URL.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/admin/roles/manage.php');
}
/**
* Returns array of parameters to be passed to legacy add_to_log() function.
*
* @return array
*/
protected function get_legacy_logdata() {
return array(SITEID, 'role', 'delete', 'admin/roles/manage.php?action=delete&roleid='.$this->objectid, $this->other['shortname'], '');
}
}

View File

@ -644,7 +644,13 @@ class core_accesslib_testcase extends advanced_testcase {
$this->assertTrue($DB->record_exists('role_allow_override', array('roleid'=>$role->id)));
$this->assertTrue($DB->record_exists('role_allow_override', array('allowoverride'=>$role->id)));
// Delete role and get event.
$sink = $this->redirectEvents();
$result = delete_role($role->id);
$events = $sink->get_events();
$sink->close();
$event = array_pop($events);
$this->assertTrue($result);
$this->assertFalse($DB->record_exists('role', array('id'=>$role->id)));
$this->assertFalse($DB->record_exists('role_assignments', array('roleid'=>$role->id)));
@ -655,6 +661,21 @@ class core_accesslib_testcase extends advanced_testcase {
$this->assertFalse($DB->record_exists('role_allow_assign', array('allowassign'=>$role->id)));
$this->assertFalse($DB->record_exists('role_allow_override', array('roleid'=>$role->id)));
$this->assertFalse($DB->record_exists('role_allow_override', array('allowoverride'=>$role->id)));
// Test triggered event.
$this->assertInstanceOf('\core\event\role_deleted', $event);
$this->assertSame('role', $event->target);
$this->assertSame('role', $event->objecttable);
$this->assertSame($role->id, $event->objectid);
$this->assertEquals(context_system::instance(), $event->get_context());
$this->assertSame($role->name, $event->other['name']);
$this->assertSame($role->shortname, $event->other['shortname']);
$this->assertSame($role->description, $event->other['description']);
$this->assertSame($role->archetype, $event->other['archetype']);
$expectedlegacylog = array(SITEID, 'role', 'delete', 'admin/roles/manage.php?action=delete&roleid='.$role->id,
$role->shortname, '');
$this->assertEventLegacyLogData($expectedlegacylog, $event);
}
/**