From d9f9198b45545296cec68260688d85d3d2f49bb3 Mon Sep 17 00:00:00 2001 From: Marco Date: Sat, 10 Mar 2018 03:03:57 +0100 Subject: [PATCH] Implement method 'getRolesForUserById' in class 'Administration' --- src/Administration.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/Administration.php b/src/Administration.php index 561ad46..2e85afe 100644 --- a/src/Administration.php +++ b/src/Administration.php @@ -286,6 +286,36 @@ final class Administration extends UserManager { return ($rolesBitmask & $role) === $role; } + /** + * Returns the roles of the user with the given ID, mapping the numerical values to their descriptive names + * + * @param int $userId the ID of the user to return the roles for + * @return array + * @throws UnknownIdException if no user with the specified ID has been found + * + * @see Role + */ + public function getRolesForUserById($userId) { + $userId = (int) $userId; + + $rolesBitmask = $this->db->selectValue( + 'SELECT roles_mask FROM ' . $this->dbTablePrefix . 'users WHERE id = ?', + [ $userId ] + ); + + if ($rolesBitmask === null) { + throw new UnknownIdException(); + } + + return \array_filter( + Role::getMap(), + function ($each) use ($rolesBitmask) { + return ($rolesBitmask & $each) === $each; + }, + \ARRAY_FILTER_USE_KEY + ); + } + /** * Signs in as the user with the specified ID *