Created
August 23, 2017 14:47
-
-
Save mohessaid/8980c2bc0c01b891f37825a4195f562a to your computer and use it in GitHub Desktop.
Moodle check_user_existance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Return description of check_user_existance paramters. | |
* | |
* @return external_function_paramaters | |
* @since Moodle 3.2 | |
*/ | |
public static function check_user_existence_parameters(){ | |
return new external_function_parameters( | |
array( | |
'username' => new external_value(core_user::get_property_type('username'), 'Username of the user'), | |
'password' => new external_value(core_user::get_property_type('password'), 'User password'), | |
) | |
); | |
} | |
public static function check_user_existence($username, $password){ | |
global $CFG, $DB; | |
$params = self::validate_parameters(self::check_user_existence_parameters(), | |
array('username' => $username, 'password' => $password)); | |
$validated_username = $params['username']; | |
$validated_password = $params['password']; | |
$valid = false; | |
$messages = []; | |
$data = []; | |
// Check if data are provided. | |
$datacheck = ['username' => !empty($validated_username), 'password' => !empty($validated_password)]; | |
if ($datacheck['username'] && $datacheck['password']){ | |
if ($user = $DB->get_record('user', array('username'=>$validated_username))) { | |
$valid = validate_internal_user_password($user, $validated_password); | |
} | |
} else { | |
$valid = false; | |
if (!$datacheck['username']){ | |
$messages[] = [ | |
'key' => "username", | |
'value' => "Username must be a valid name and not empty" | |
]; | |
} | |
if (!$datacheck['password']){ | |
$messages[] = [ | |
'key' => "password", | |
'value' => "Password must be a valid password and not empty" | |
]; | |
} | |
} | |
if (!$valid){ | |
$messages[] = [ | |
'key' => "error", | |
'value' => "Username or Password are wrong, please try with correct data" | |
]; | |
} | |
return array('valid' => $valid, 'data' => $data,'messages' => $messages); | |
} | |
/** | |
* Returns description of check_user_existance | |
* | |
* @return external_description | |
* @since Moodle 3.2 | |
*/ | |
public static function check_user_existence_returns() { | |
return new external_single_structure( | |
array( | |
'valid' => new external_value(PARAM_BOOL, 'Are the provided credentials valid or not'), | |
'data' => new external_multiple_structure( | |
new external_single_structure( | |
array( | |
'key' => new external_value(PARAM_ALPHANUMEXT, 'The key of the data'), | |
'value' => new external_value(PARAM_RAW, 'The value of the data') | |
) | |
) | |
), | |
'messages' => new external_multiple_structure( | |
new external_single_structure( | |
array( | |
'key' => new external_value(PARAM_ALPHANUMEXT, 'The key of the message'), | |
'value' => new external_value(PARAM_TEXT, 'The content of the message') | |
) | |
) | |
) | |
) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment