Skip to content

Commit

Permalink
MDL-59298 auth: Added get_password_change_info to auth plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
rezaies committed Apr 8, 2019
1 parent 280cfdf commit 206ec32
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 23 deletions.
5 changes: 5 additions & 0 deletions auth/upgrade.txt
@@ -1,6 +1,11 @@
This files describes API changes in /auth/* - plugins,
information provided here is intended especially for developers.

=== 3.7 ===

* get_password_change_info() method is added to the base class and returns an array containing the subject and body of the message
to the user that contains instructions on how to change their password. Authentication plugins can override this method if needed.

=== 3.6 ===

* Login forms generated from Moodle must include a login token to protect automated logins. See \core\session\manager::get_login_token().
Expand Down
39 changes: 39 additions & 0 deletions lib/authlib.php
Expand Up @@ -758,6 +758,45 @@ public static function prepare_identity_providers_for_output($identityproviders,
}
return $data;
}

/**
* Returns information on how the specified user can change their password.
*
* @param stdClass $user A user object
* @return string[] An array of strings with keys subject and message
*/
public function get_password_change_info(stdClass $user) : array {
$site = get_site();
$systemcontext = context_system::instance();

$data = new stdClass();
$data->firstname = $user->firstname;
$data->lastname = $user->lastname;
$data->username = $user->username;
$data->sitename = format_string($site->fullname);
$data->admin = generate_email_signoff();

if ($this->can_change_password() and $this->change_password_url()) {
// We have some external url for password changing.
$data->link = $this->change_password_url();
} else {
// No way to change password, sorry.
$data->link = '';
}

if (!empty($data->link) and has_capability('moodle/user:changeownpassword', $systemcontext, $user->id)) {
$subject = get_string('emailpasswordchangeinfosubject', '', format_string($site->fullname));
$message = get_string('emailpasswordchangeinfo', '', $data);
} else {
$subject = get_string('emailpasswordchangeinfosubject', '', format_string($site->fullname));
$message = get_string('emailpasswordchangeinfofail', '', $data);
}

return [
'subject' => $subject,
'message' => $message
];
}
}

/**
Expand Down
26 changes: 3 additions & 23 deletions lib/moodlelib.php
Expand Up @@ -6406,17 +6406,14 @@ function send_password_change_confirmation_email($user, $resetrecord) {
}

/**
* Sends an email containinginformation on how to change your password.
* Sends an email containing information on how to change your password.
*
* @param stdClass $user A {@link $USER} object
* @return bool Returns true if mail was sent OK and false if there was an error.
*/
function send_password_change_info($user) {
global $CFG;

$site = get_site();
$supportuser = core_user::get_support_user();
$systemcontext = context_system::instance();

$data = new stdClass();
$data->firstname = $user->firstname;
Expand All @@ -6425,35 +6422,18 @@ function send_password_change_info($user) {
$data->sitename = format_string($site->fullname);
$data->admin = generate_email_signoff();

$userauth = get_auth_plugin($user->auth);

if (!is_enabled_auth($user->auth) or $user->auth == 'nologin') {
$message = get_string('emailpasswordchangeinfodisabled', '', $data);
$subject = get_string('emailpasswordchangeinfosubject', '', format_string($site->fullname));
// Directly email rather than using the messaging system to ensure its not routed to a popup or jabber.
return email_to_user($user, $supportuser, $subject, $message);
}

if ($userauth->can_change_password() and $userauth->change_password_url()) {
// We have some external url for password changing.
$data->link .= $userauth->change_password_url();

} else {
// No way to change password, sorry.
$data->link = '';
}

if (!empty($data->link) and has_capability('moodle/user:changeownpassword', $systemcontext, $user->id)) {
$message = get_string('emailpasswordchangeinfo', '', $data);
$subject = get_string('emailpasswordchangeinfosubject', '', format_string($site->fullname));
} else {
$message = get_string('emailpasswordchangeinfofail', '', $data);
$subject = get_string('emailpasswordchangeinfosubject', '', format_string($site->fullname));
}
$userauth = get_auth_plugin($user->auth);
['subject' => $subject, 'message' => $message] = $userauth->get_password_change_info($user);

// Directly email rather than using the messaging system to ensure its not routed to a popup or jabber.
return email_to_user($user, $supportuser, $subject, $message);

}

/**
Expand Down
16 changes: 16 additions & 0 deletions lib/tests/moodlelib_test.php
Expand Up @@ -4354,4 +4354,20 @@ public function test_get_complete_user_data($field, $value, $success) {
$this->assertFalse($fetcheduser);
}
}

/**
* Test for send_password_change_().
*/
public function test_send_password_change_info() {
$this->resetAfterTest();

$user = $this->getDataGenerator()->create_user();

$sink = $this->redirectEmails(); // Make sure we are redirecting emails.
send_password_change_info($user);
$result = $sink->get_messages();
$sink->close();

$this->assertContains('passwords cannot be reset on this site', $result[0]->body);
}
}

0 comments on commit 206ec32

Please sign in to comment.