From bc44a08b1b3a71ca5fb356d0036fbcb68660bd93 Mon Sep 17 00:00:00 2001 From: Marco Date: Sat, 29 Jul 2017 20:24:24 +0200 Subject: [PATCH] Allow for roles to be checked for users via 'Administration' class --- src/Administration.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/Administration.php b/src/Administration.php index d6c9349..10435cc 100644 --- a/src/Administration.php +++ b/src/Administration.php @@ -259,6 +259,32 @@ final class Administration extends UserManager { ); } + /** + * Returns whether the user with the given ID has the specified role + * + * @param int $userId the ID of the user to check the roles for + * @param int $role the role as one of the constants from the {@see Role} class + * @return bool + * @throws UnknownIdException if no user with the specified ID has been found + * + * @see Role + */ + public function doesUserHaveRole($userId, $role) { + $userId = (int) $userId; + $role = (int) $role; + + $rolesBitmask = $this->db->selectValue( + 'SELECT roles_mask FROM users WHERE id = ?', + [ $userId ] + ); + + if ($rolesBitmask === null) { + throw new UnknownIdException(); + } + + return ($rolesBitmask & $role) === $role; + } + protected function throttle($actionType, $customSelector = null) { // do nothing }