diff options
| author | Jan-Hendrik Willms <tleilax+studip@gmail.com> | 2023-03-22 12:27:53 +0000 |
|---|---|---|
| committer | Jan-Hendrik Willms <tleilax+studip@gmail.com> | 2023-03-22 12:27:53 +0000 |
| commit | d79463e9605fcb1bef250ab3e9cf717227fe04ed (patch) | |
| tree | 41217c17318b2b0969099c9bd5806fb00d2668d9 /lib/plugins/db/RolePersistence.class.php | |
| parent | d8fe98cc4178a6f49355633c0441e6a45c83945b (diff) | |
implement RolePersistence::getUsersWithRoleByName() and RolePersistence::getUsersWithRoleById(), fixes #2014
Closes #2014
Merge request studip/studip!1311
Diffstat (limited to 'lib/plugins/db/RolePersistence.class.php')
| -rw-r--r-- | lib/plugins/db/RolePersistence.class.php | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/lib/plugins/db/RolePersistence.class.php b/lib/plugins/db/RolePersistence.class.php index 3514ca6..85f47a2 100644 --- a/lib/plugins/db/RolePersistence.class.php +++ b/lib/plugins/db/RolePersistence.class.php @@ -526,6 +526,62 @@ class RolePersistence } /** + * Returns all users that have a specific role - given by it's name. + * + * @param string $role_name Name of the role + * @param bool $only_explicit Only select explicit assignments from table + * `roles_user` if true, otherwise also select + * by perm defined in table `roles_studipperms` + * + * @return User[] + */ + public static function getUsersWithRoleByName(string $role_name, bool $only_explicit = true): array + { + $role_id = self::getRoleIdByName($role_name); + if ($role_id === false) { + throw new Exception("Unknown role name {$role_name}"); + } + + return self::getUsersWithRoleById($role_id, $only_explicit); + } + + /** + * Returns all users that have a specific role - given by it's id. + * + * @param int $role_id Id of the role + * @param bool $only_explicit Only select explicit assignments from table + * `roles_user` if true, otherwise also select + * by perm defined in table `roles_studipperms` + * + * @return User[] + */ + public static function getUsersWithRoleById(int $role_id, bool $only_explicit = true): array + { + $query = "SELECT `userid` AS `user_id` + FROM `roles_user` + WHERE `roleid` = :role_id"; + + if (!$only_explicit) { + $query = "SELECT DISTINCT `user_id` + FROM ( + {$query} + + UNION ALL + + SELECT `user_id` + FROM `roles_studipperms` AS `rsp` + JOIN `auth_user_md5` AS `aum` + ON (`rsp`.`permname` = `aum`.`perms`) + WHERE `rsp`.`roleid` = :role_id + ) AS tmp"; + } + + $user_ids = DBManager::get()->fetchFirst($query, [':role_id' => $role_id]); + + return User::findMany($user_ids); + } + + /** * Returns statistic values for each role: * * - number of explicitely assigned users |
