AuthWordpress.php (mrbs-1.9.4) | : | AuthWordpress.php (mrbs-1.10.0) | ||
---|---|---|---|---|
skipping to change at line 26 | skipping to change at line 26 | |||
* | * | |||
* Checks if the specified username/password pair are valid | * Checks if the specified username/password pair are valid | |||
* | * | |||
* $user - The user name | * $user - The user name | |||
* $pass - The password | * $pass - The password | |||
* | * | |||
* Returns: | * Returns: | |||
* false - The pair are invalid or do not exist | * false - The pair are invalid or do not exist | |||
* string - The validated username | * string - The validated username | |||
*/ | */ | |||
public function validateUser($user, $pass) | public function validateUser(?string $user, ?string $pass) | |||
{ | { | |||
return (is_wp_error(wp_authenticate($user, $pass))) ? false : $user; | return (is_wp_error(wp_authenticate($user, $pass))) ? false : $user; | |||
} | } | |||
public function getUser($username) | public function getUser(string $username) : ?User | |||
{ | { | |||
$wp_user = get_user_by('login', $username); | $wp_user = get_user_by('login', $username); | |||
if ($wp_user === false) | if ($wp_user === false) | |||
{ | { | |||
return null; | return null; | |||
} | } | |||
$user = new User($username); | $user = new User($username); | |||
$user->display_name = $wp_user->display_name; | $user->display_name = $wp_user->display_name; | |||
$user->email = $wp_user->user_email; | $user->email = $wp_user->user_email; | |||
$user->level = self::getUserLevel($wp_user); | $user->level = self::getUserLevel($wp_user); | |||
return $user; | return $user; | |||
} | } | |||
// Checks whether validation of a user by email address is possible and | // Checks whether validation of a user by email address is possible and | |||
// allowed. In the case of WordPress, wp_authenticate() accepts either | // allowed. In the case of WordPress, wp_authenticate() accepts either | |||
// a username or email address and so this function always returns true. | // a username or email address and so this function always returns true. | |||
public function canValidateByEmail() | public function canValidateByEmail() : bool | |||
{ | { | |||
return true; | return true; | |||
} | } | |||
// Return an array of MRBS users, indexed by 'username' and 'display_name' | // Return an array of MRBS users, indexed by 'username' and 'display_name' | |||
public function getUsernames() | public function getUsernames() : array | |||
{ | { | |||
global $auth; | global $auth; | |||
$result = array(); | $result = array(); | |||
// We are only interested in MRBS users and admins | // We are only interested in MRBS users and admins | |||
$mrbs_roles = array_merge((array)$auth['wordpress']['admin_roles'], | $mrbs_roles = array_merge((array)$auth['wordpress']['admin_roles'], | |||
(array)$auth['wordpress']['user_roles']); | (array)$auth['wordpress']['user_roles']); | |||
// The 'role__in' argument to get_users() is only supported in Wordpress >= 4.4. | // The 'role__in' argument to get_users() is only supported in Wordpress >= 4.4. | |||
skipping to change at line 100 | skipping to change at line 100 | |||
// Remove duplicate users | // Remove duplicate users | |||
$users = array_map('unserialize', array_unique(array_map('serialize', $use rs))); | $users = array_map('unserialize', array_unique(array_map('serialize', $use rs))); | |||
} | } | |||
foreach ($users as $user) | foreach ($users as $user) | |||
{ | { | |||
$result[] = array('username' => $user->user_login, | $result[] = array('username' => $user->user_login, | |||
'display_name' => $user->display_name); | 'display_name' => $user->display_name); | |||
} | } | |||
if (!$can_use_role__in) | // Although the users are probably already sorted, we sort them again becaus | |||
{ | e MRBS | |||
// We need to sort the users in this case as we've only got an array of me | // offers an option for sorting by first or last name. | |||
rged | self::sortUsers($result); | |||
// sorted arrays. So the small arrays are sorted but the merged array is | ||||
not. | ||||
self::sortUsers($result); | ||||
} | ||||
return $result; | return $result; | |||
} | } | |||
private static function getUserLevel(\WP_User $wp_user) | private static function getUserLevel(\WP_User $wp_user) : int | |||
{ | { | |||
global $auth; | global $auth; | |||
// cache the user levels for performance | // cache the user levels for performance | |||
static $user_levels = array(); | static $user_levels = array(); | |||
// User not logged in, user level '0' | // User not logged in, user level '0' | |||
// Shouldn't get here anyway because the type hint won't allow it, | // Shouldn't get here anyway because the type hint won't allow it, | |||
// but we'll check anyway for completeness | // but we'll check anyway for completeness | |||
if(!isset($wp_user) || ($wp_user === false)) | if(!isset($wp_user) || ($wp_user === false)) | |||
skipping to change at line 157 | skipping to change at line 154 | |||
{ | { | |||
$user_levels[$wp_user->login] = 0; | $user_levels[$wp_user->login] = 0; | |||
} | } | |||
} | } | |||
return $user_levels[$wp_user->login]; | return $user_levels[$wp_user->login]; | |||
} | } | |||
// Checks to see whether any of the user's roles are contained in $mrbs_roles, which can be a | // Checks to see whether any of the user's roles are contained in $mrbs_roles, which can be a | |||
// string or an array of strings. | // string or an array of strings. | |||
private static function check_roles(\WP_User $wp_user, $mrbs_roles) | private static function check_roles(\WP_User $wp_user, $mrbs_roles) : bool | |||
{ | { | |||
if (!isset($mrbs_roles)) | if (!isset($mrbs_roles)) | |||
{ | { | |||
return false; | return false; | |||
} | } | |||
// Turn $mrbs_roles into an array if it isn't already | // Turn $mrbs_roles into an array if it isn't already | |||
$mrbs_roles = (array)$mrbs_roles; | $mrbs_roles = (array)$mrbs_roles; | |||
// Put the roles into the standard WordPress format | // Put the roles into the standard WordPress format | |||
$mrbs_roles = array_map('self::standardise_role_name', $mrbs_roles); | $mrbs_roles = array_map('self::standardise_role_name', $mrbs_roles); | |||
return (count(array_intersect($wp_user->roles, $mrbs_roles)) > 0); | return (count(array_intersect($wp_user->roles, $mrbs_roles)) > 0); | |||
} | } | |||
// Convert a WordPress role name to lowercase and replace spaces by underscore s. | // Convert a WordPress role name to lowercase and replace spaces by underscore s. | |||
// Example "MRBS Admin" -> "mrbs_admin" | // Example "MRBS Admin" -> "mrbs_admin" | |||
private static function standardise_role_name($role) | private static function standardise_role_name(string $role) : string | |||
{ | { | |||
return str_replace(' ', '_', \MRBS\utf8_strtolower($role)); | return str_replace(' ', '_', \MRBS\utf8_strtolower($role)); | |||
} | } | |||
} | } | |||
End of changes. 8 change blocks. | ||||
15 lines changed or deleted | 11 lines changed or added |