mirror of
https://github.com/delight-im/PHP-Auth.git
synced 2025-07-11 19:46:22 +02:00
Provide read access to user's roles via 'Auth' interface
This commit is contained in:
55
src/Auth.php
55
src/Auth.php
@ -1135,6 +1135,61 @@ final class Auth extends UserManager {
|
|||||||
return $this->getStatus() === Status::SUSPENDED;
|
return $this->getStatus() === Status::SUSPENDED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether the currently signed-in user has the specified role
|
||||||
|
*
|
||||||
|
* @param int $role the role as one of the constants from the {@see Role} class
|
||||||
|
* @return bool
|
||||||
|
*
|
||||||
|
* @see Role
|
||||||
|
*/
|
||||||
|
public function hasRole($role) {
|
||||||
|
$role = (int) $role;
|
||||||
|
|
||||||
|
if (isset($_SESSION) && isset($_SESSION[self::SESSION_FIELD_ROLES])) {
|
||||||
|
return (((int) $_SESSION[self::SESSION_FIELD_ROLES]) & $role) === $role;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether the currently signed-in user has *any* of the specified roles
|
||||||
|
*
|
||||||
|
* @param int[] ...$roles the roles as constants from the {@see Role} class
|
||||||
|
* @return bool
|
||||||
|
*
|
||||||
|
* @see Role
|
||||||
|
*/
|
||||||
|
public function hasAnyRole(...$roles) {
|
||||||
|
foreach ($roles as $role) {
|
||||||
|
if ($this->hasRole($role)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether the currently signed-in user has *all* of the specified roles
|
||||||
|
*
|
||||||
|
* @param int[] ...$roles the roles as constants from the {@see Role} class
|
||||||
|
* @return bool
|
||||||
|
*
|
||||||
|
* @see Role
|
||||||
|
*/
|
||||||
|
public function hasAllRoles(...$roles) {
|
||||||
|
foreach ($roles as $role) {
|
||||||
|
if (!$this->hasRole($role)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets whether the currently signed-in user has been remembered by a long-lived cookie
|
* Sets whether the currently signed-in user has been remembered by a long-lived cookie
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user