Skip to content

Commit

Permalink
MDL-58716 auth: New ajax WS core_auth_request_password_reset
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Jul 4, 2017
1 parent cca01dd commit b6f70a3
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
94 changes: 94 additions & 0 deletions auth/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,98 @@ public static function confirm_user_returns() {
)
);
}

/**
* Describes the parameters for request_password_reset.
*
* @return external_function_parameters
* @since Moodle 3.4
*/
public static function request_password_reset_parameters() {
return new external_function_parameters(
array(
'username' => new external_value(core_user::get_property_type('username'), 'User name', VALUE_DEFAULT, ''),
'email' => new external_value(core_user::get_property_type('email'), 'User email', VALUE_DEFAULT, ''),
)
);
}

/**
* Requests a password reset.
*
* @param string $username user name
* @param string $email user email
* @return array warnings and success status (including notices and errors while processing)
* @since Moodle 3.4
* @throws moodle_exception
*/
public static function request_password_reset($username = '', $email = '') {
global $CFG, $PAGE;
require_once($CFG->dirroot . '/login/lib.php');

$warnings = array();
$params = self::validate_parameters(
self::request_password_reset_parameters(),
array(
'username' => $username,
'email' => $email,
)
);

$context = context_system::instance();
$PAGE->set_context($context); // Needed by format_string calls.

// Check if an alternate forgotten password method is set.
if (!empty($CFG->forgottenpasswordurl)) {
throw new moodle_exception('cannotmailconfirm');
}

$errors = core_login_validate_forgot_password_data($params);
if (!empty($errors)) {
$status = 'dataerror';
$notice = '';

foreach ($errors as $itemname => $message) {
$warnings[] = array(
'item' => $itemname,
'itemid' => 0,
'warningcode' => 'fielderror',
'message' => s($message)
);
}
} else {
list($status, $notice, $url) = core_login_process_password_reset($params['username'], $params['email']);
}

return array(
'status' => $status,
'notice' => $notice,
'warnings' => $warnings,
);
}

/**
* Describes the request_password_reset return value.
*
* @return external_single_structure
* @since Moodle 3.4
*/
public static function request_password_reset_returns() {

return new external_single_structure(
array(
'status' => new external_value(PARAM_ALPHANUMEXT, 'The returned status of the process:
dataerror: Error in the sent data (username or email). More information in warnings field.
emailpasswordconfirmmaybesent: Email sent or not (depends on user found in database).
emailpasswordconfirmnotsent: Failure, user not found.
emailpasswordconfirmnoemail: Failure, email not found.
emailalreadysent: Email already sent.
emailpasswordconfirmsent: User pending confirmation.
emailresetconfirmsent: Email sent.
'),
'notice' => new external_value(PARAM_RAW, 'Important information for the user about the process.'),
'warnings' => new external_warnings(),
)
);
}
}
8 changes: 8 additions & 0 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
'ajax' => true,
'loginrequired' => false,
),
'core_auth_request_password_reset' => array(
'classname' => 'core_auth_external',
'methodname' => 'request_password_reset',
'description' => 'Requests a password reset.',
'type' => 'write',
'ajax' => true,
'loginrequired' => false,
),
'core_badges_get_user_badges' => array(
'classname' => 'core_badges_external',
'methodname' => 'get_user_badges',
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

defined('MOODLE_INTERNAL') || die();

$version = 2017062900.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2017062900.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.

Expand Down

0 comments on commit b6f70a3

Please sign in to comment.