MDL-58716 auth: New ajax WS core_auth_request_password_reset

This commit is contained in:
Juan Leyva 2017-06-14 14:18:06 +01:00
parent cca01ddace
commit b6f70a3745
3 changed files with 103 additions and 1 deletions

View File

@ -121,4 +121,98 @@ class core_auth_external extends external_api {
)
);
}
/**
* 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(),
)
);
}
}

View File

@ -42,6 +42,14 @@ $functions = array(
'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',

View File

@ -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.