MDL-41170 events: User_updated event should be triggered on password updates

This commit is contained in:
Ankit Agarwal 2013-10-07 17:27:45 +08:00
parent 835a006b07
commit 6c679d1532
3 changed files with 58 additions and 0 deletions

View file

@ -4644,6 +4644,14 @@ function update_internal_user_password($user, $password) {
if ($passwordchanged || $algorithmchanged) { if ($passwordchanged || $algorithmchanged) {
$DB->set_field('user', 'password', $hashedpassword, array('id' => $user->id)); $DB->set_field('user', 'password', $hashedpassword, array('id' => $user->id));
$user->password = $hashedpassword; $user->password = $hashedpassword;
// Trigger event.
$event = \core\event\user_updated::create(array(
'objectid' => $user->id,
'context' => context_user::instance($user->id)
));
$event->add_record_snapshot('user', $user);
$event->trigger();
} }
return true; return true;
@ -5861,6 +5869,15 @@ function setnew_password_and_mail($user, $fasthash = false) {
$hashedpassword = hash_internal_user_password($newpassword, $fasthash); $hashedpassword = hash_internal_user_password($newpassword, $fasthash);
$DB->set_field('user', 'password', $hashedpassword, array('id' => $user->id)); $DB->set_field('user', 'password', $hashedpassword, array('id' => $user->id));
$user->password = $hashedpassword;
// Trigger event.
$event = \core\event\user_updated::create(array(
'objectid' => $user->id,
'context' => context_user::instance($user->id)
));
$event->add_record_snapshot('user', $user);
$event->trigger();
$a = new stdClass(); $a = new stdClass();
$a->firstname = fullname($user, true); $a->firstname = fullname($user, true);

View file

@ -2517,4 +2517,44 @@ class core_moodlelib_testcase extends advanced_testcase {
$this->assertSame($user2->email, $result[1]->to); $this->assertSame($user2->email, $result[1]->to);
$this->assertSame($user1->email, $result[1]->from); $this->assertSame($user1->email, $result[1]->from);
} }
/**
* Test user_updated event trigger by various apis.
*/
public function test_user_updated_event() {
global $DB, $CFG;
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
// Set config to allow email_to_user() to be called.
$CFG->noemailever = false;
// Update user password.
$sink = $this->redirectEvents();
$sink2 = $this->redirectEmails(); // Make sure we are redirecting emails.
setnew_password_and_mail($user);
update_internal_user_password($user, 'randompass');
$events = $sink->get_events();
$sink->close();
$sink2->close();
// Test updated value.
$dbuser = $DB->get_record('user', array('id' => $user->id));
$this->assertSame($user->firstname, $dbuser->firstname);
$this->assertNotSame('M00dLe@T', $dbuser->password);
// Test event.
foreach ($events as $event) {
$this->assertInstanceOf('\core\event\user_updated', $event);
$this->assertSame($user->id, $event->objectid);
$this->assertSame('user_updated', $event->get_legacy_eventname());
$this->assertEventLegacyData($user, $event);
$this->assertEquals(context_user::instance($user->id), $event->get_context());
$expectedlogdata = array(SITEID, 'user', 'update', 'view.php?id='.$user->id, '');
$this->assertEventLegacyLogData($expectedlogdata, $event);
}
}
} }

View file

@ -47,6 +47,7 @@ information provided here is intended especially for developers.
backups which supports both compression formats; get_file_packer('application/vnd.moodle.backup'). backups which supports both compression formats; get_file_packer('application/vnd.moodle.backup').
* New optional parameter to stored_file::get_content_file_handle to open file handle with 'gzopen' instead * New optional parameter to stored_file::get_content_file_handle to open file handle with 'gzopen' instead
of 'fopen' to read gzip-compressed files if required. of 'fopen' to read gzip-compressed files if required.
* update_internal_user_password() and setnew_password_and_mail() now trigger user_updated event.
DEPRECATIONS: DEPRECATIONS:
Various previously deprecated functions have now been altered to throw DEBUG_DEVELOPER debugging notices Various previously deprecated functions have now been altered to throw DEBUG_DEVELOPER debugging notices