From c0fd6686718362bd7340243f29bbd922faf6e78c Mon Sep 17 00:00:00 2001
From: Jan-Hendrik Willms <tleilax+studip@gmail.com>
Date: Fri, 18 Nov 2022 10:04:27 +0000
Subject: [PATCH] group roles in administration, fixes #1793

Closes #1793

Merge request studip/studip!1173
---
 app/controllers/admin/role.php              |  4 ++--
 app/views/admin/role/assign_plugin_role.php | 22 +++++++++++++++------
 app/views/admin/role/assign_role.php        | 14 +++++++++++--
 lib/plugins/db/RolePersistence.class.php    | 14 +++++++++++--
 4 files changed, 42 insertions(+), 12 deletions(-)

diff --git a/app/controllers/admin/role.php b/app/controllers/admin/role.php
index f932b0db3cd..6d1a82a43f0 100644
--- a/app/controllers/admin/role.php
+++ b/app/controllers/admin/role.php
@@ -177,7 +177,7 @@ class Admin_RoleController extends AuthenticatedController
             $this->currentuser = $this->users[$usersel];
             $this->assignedroles = $this->currentuser->getRoles();
             $this->all_userroles = $this->currentuser->getRoles(true);
-            $this->roles = RolePersistence::getAllRoles();
+            $this->roles = RolePersistence::getAllRoles(true);
             foreach ($this->assignedroles as $role) {
                 $institutes = SimpleCollection::createFromArray(Institute::findMany(RolePersistence::getAssignedRoleInstitutes($usersel, $role->getRoleid())));
                 $this->assignedroles_institutes[$role->getRoleid()] = $institutes->orderBy('name')->pluck('name');
@@ -227,7 +227,7 @@ class Admin_RoleController extends AuthenticatedController
 
         $this->plugins = PluginManager::getInstance()->getPluginInfos();
         $this->assigned = RolePersistence::getAssignedPluginRoles($pluginid);
-        $this->roles = RolePersistence::getAllRoles();
+        $this->roles = RolePersistence::getAllRoles(true);
         $this->pluginid = $pluginid;
     }
 
diff --git a/app/views/admin/role/assign_plugin_role.php b/app/views/admin/role/assign_plugin_role.php
index 18ff2cc0592..471a087388d 100644
--- a/app/views/admin/role/assign_plugin_role.php
+++ b/app/views/admin/role/assign_plugin_role.php
@@ -63,12 +63,22 @@ use Studip\Button;
                 </td>
                 <td>
                     <select multiple name="rolesel[]" size="10" style="width: 300px;">
-                        <? foreach ($roles as $role): ?>
-                            <option value="<?= $role->getRoleid() ?>">
-                                <?= htmlReady($role->getRolename()) ?>
-                                <? if ($role->getSystemtype()): ?>[<?= _('Systemrolle') ?>]<? endif ?>
-                            </option>
-                        <? endforeach ?>
+                        <optgroup label="<?= _('Systemrollen') ?>">
+                            <? foreach ($roles['system'] as $role): ?>
+                                <option value="<?= $role->getRoleid() ?>">
+                                    <?= htmlReady($role->getRolename()) ?>
+                                </option>
+                            <? endforeach ?>
+                        </optgroup>
+                    <? if (count($roles['other']) > 0): ?>
+                        <optgroup label="<?= _('Weitere Rollen') ?>">
+                            <? foreach ($roles['other'] as $role): ?>
+                                <option value="<?= $role->getRoleid() ?>">
+                                    <?= htmlReady($role->getRolename()) ?>
+                                </option>
+                            <? endforeach ?>
+                        </optgroup>
+                    <? endif; ?>
                     </select>
                 </td>
             </tr>
diff --git a/app/views/admin/role/assign_role.php b/app/views/admin/role/assign_role.php
index b82b4514219..67c8b1e6042 100644
--- a/app/views/admin/role/assign_role.php
+++ b/app/views/admin/role/assign_role.php
@@ -82,12 +82,22 @@ use Studip\Button, Studip\LinkButton;
                 </td>
                 <td>
                     <select size="10" name="rolesel[]" multiple style="width: 300px;">
-                        <? foreach ($roles as $role): ?>
+                        <optgroup label="<?= _('Systemrollen') ?>">
+                        <? foreach ($roles['system'] as $role): ?>
                             <option value="<?= $role->getRoleid() ?>">
                                 <?= htmlReady($role->getRolename()) ?>
-                                <? if ($role->getSystemtype()): ?>[<?= _('Systemrolle') ?>]<? endif ?>
                             </option>
                         <? endforeach ?>
+                        </optgroup>
+                    <? if (count($roles['other']) > 0): ?>
+                        <optgroup label="<?= _('Weitere Rollen') ?>">
+                        <? foreach ($roles['other'] as $role): ?>
+                            <option value="<?= $role->getRoleid() ?>">
+                                <?= htmlReady($role->getRolename()) ?>
+                            </option>
+                        <? endforeach ?>
+                        </optgroup>
+                    <? endif; ?>
                     </select>
                 </td>
             </tr>
diff --git a/lib/plugins/db/RolePersistence.class.php b/lib/plugins/db/RolePersistence.class.php
index d24c2a6298f..1b03a0132c9 100644
--- a/lib/plugins/db/RolePersistence.class.php
+++ b/lib/plugins/db/RolePersistence.class.php
@@ -22,7 +22,7 @@ class RolePersistence
      *
      * @return array Roles
      */
-    public static function getAllRoles(): array
+    public static function getAllRoles(bool $grouped = false): array
     {
         // read cache
         $cache = StudipCacheFactory::getCache();
@@ -44,7 +44,17 @@ class RolePersistence
             $cache->write(self::ROLES_CACHE_KEY, $roles);
         }
 
-        return $roles;
+        if (!$grouped) {
+            return $roles;
+        }
+
+        $groups = ['system' => [], 'other' => []];
+        foreach ($roles as $id => $role) {
+            $index = $role->getSystemtype() ? 'system' : 'other';
+            $groups[$index][$id] = $role;
+        }
+
+        return $groups;
     }
 
     public static function getRoleIdByName($name)
-- 
GitLab