Skip to content
Snippets Groups Projects
Commit d79463e9 authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms
Browse files

implement RolePersistence::getUsersWithRoleByName() and...

implement RolePersistence::getUsersWithRoleByName() and RolePersistence::getUsersWithRoleById(), fixes #2014

Closes #2014

Merge request studip/studip!1311
parent d8fe98cc
No related branches found
No related tags found
No related merge requests found
...@@ -288,18 +288,17 @@ class Admin_RoleController extends AuthenticatedController ...@@ -288,18 +288,17 @@ class Admin_RoleController extends AuthenticatedController
$this->roleid = ''; $this->roleid = '';
if ($roleid) { if ($roleid) {
$sql = "SELECT DISTINCT Vorname,Nachname,user_id,username,perms $this->users = RolePersistence::getUsersWithRoleById($roleid);
FROM auth_user_md5
JOIN roles_user ON userid = user_id $this->user_institutes = [];
WHERE roleid = ? foreach ($this->users as $user) {
ORDER BY Nachname, Vorname"; $this->user_institutes[$user->id] = Institute::findAndMapMany(
$statement = DBManager::get()->prepare($sql); function (Institute $institute) {
$statement->execute([$roleid]); return $institute->name;
},
$users = $statement->fetchAll(PDO::FETCH_ASSOC); RolePersistence::getAssignedRoleInstitutes($user['user_id'], $roleid),
foreach ($users as $key => $user) { 'ORDER BY name'
$institutes = new SimpleCollection(Institute::findMany(RolePersistence::getAssignedRoleInstitutes($user['user_id'], $roleid))); );
$users[$key]['institutes'] = $institutes->orderBy('name')->pluck('name');
} }
$plugins = PluginManager::getInstance()->getPluginInfos(); $plugins = PluginManager::getInstance()->getPluginInfos();
...@@ -311,7 +310,6 @@ class Admin_RoleController extends AuthenticatedController ...@@ -311,7 +310,6 @@ class Admin_RoleController extends AuthenticatedController
$this->implicit_count = RolePersistence::countImplicitUsers($roleid); $this->implicit_count = RolePersistence::countImplicitUsers($roleid);
$this->users = $users;
$this->plugins = $plugins; $this->plugins = $plugins;
$this->role = self::getRole($roleid); $this->role = self::getRole($roleid);
$this->roleid = $roleid; $this->roleid = $roleid;
......
...@@ -5,7 +5,8 @@ ...@@ -5,7 +5,8 @@
* @var string $roleid * @var string $roleid
* @var Role[] $roles * @var Role[] $roles
* @var QuickSearch $mps * @var QuickSearch $mps
* @var array $users * @var User[] $users
* @var array $user_institutes
* @var array $plugins * @var array $plugins
* @var int $implicit_count * @var int $implicit_count
*/ */
...@@ -90,29 +91,30 @@ use Studip\Button; ...@@ -90,29 +91,30 @@ use Studip\Button;
<? foreach (array_values($users) as $index => $user): ?> <? foreach (array_values($users) as $index => $user): ?>
<tr> <tr>
<td> <td>
<input type="checkbox" name="ids[]" value="<?= $user['user_id'] ?>"> <input type="checkbox" name="ids[]" value="<?= htmlReady($user->id) ?>">
</td> </td>
<td style="text-align: right;"> <td style="text-align: right;">
<?= $index + 1 ?>. <?= $index + 1 ?>.
</td> </td>
<td> <td>
<a href="<?= $controller->url_for('admin/role/assign_role', $user['user_id']) ?>"> <a href="<?= $controller->link_for('admin/role/assign_role', $user->id) ?>">
<?= htmlReady(sprintf('%s %s (%s)', $user['Vorname'], $user['Nachname'], $user['username'])) ?> <?= htmlReady(sprintf('%s %s (%s)', $user->vorname, $user->nachname, $user->username)) ?>
</a> </a>
</td> </td>
<td><?= $user['perms'] ?></td> <td><?= htmlReady($user->perms) ?></td>
<td> <td>
<? $institutes = join(', ', $user['institutes']); ?> <? $institutes = join(', ', $user_institutes[$user->id]); ?>
<?= htmlReady(mb_substr($institutes, 0, 60)) ?> <?= htmlReady(mb_substr($institutes, 0, 60)) ?>
<? if (mb_strlen($institutes) > 60): ?> <? if (mb_strlen($institutes) > 60): ?>
...<?= tooltipIcon(join("\n", $user['institutes']))?> ...<?= tooltipIcon(join("\n", $user_institutes[$user->id]))?>
<? endif ?> <? endif ?>
</td> </td>
<td class="actions"> <td class="actions">
<?= Icon::create('trash', 'clickable', ['title' => _('Rolle entziehen')]) <?= Icon::create('trash')->asInput([
->asInput([ 'title' => _('Rolle entziehen'),
"data-confirm" => _('Soll dieser Person wirklich die Rolle entzogen werden?'), 'data-confirm' => _('Soll dieser Person wirklich die Rolle entzogen werden?'),
"formaction" => $controller->url_for('admin/role/remove_user/'.$roleid.'/'.$user['user_id'])]) ?> 'formaction' => $controller->url_for('admin/role/remove_user', $roleid, $user->id),
]) ?>
</td> </td>
</tr> </tr>
<? endforeach; ?> <? endforeach; ?>
......
...@@ -525,6 +525,62 @@ class RolePersistence ...@@ -525,6 +525,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: * Returns statistic values for each role:
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment