From 256be6d2ed505fd531cb0bb3937807c4a1a8cac5 Mon Sep 17 00:00:00 2001 From: Moritz Strohm <strohm@data-quest.de> Date: Wed, 5 Jul 2023 12:30:37 +0000 Subject: [PATCH] resources/admin/user_permissions: added bulk actions to delete permissions, closes #2009 Closes #2009 Merge request studip/studip!1308 --- app/controllers/resources/admin.php | 67 ++++ .../resources/admin/user_permissions.php | 341 ++++++++++++------ 2 files changed, 292 insertions(+), 116 deletions(-) diff --git a/app/controllers/resources/admin.php b/app/controllers/resources/admin.php index 393cacb3770..2f6e34edf9f 100644 --- a/app/controllers/resources/admin.php +++ b/app/controllers/resources/admin.php @@ -1226,4 +1226,71 @@ class Resources_AdminController extends AuthenticatedController $this->export_bookingtypes_default = $this->config->RESOURCES_EXPORT_BOOKINGTYPES_DEFAULT; } + + /** + * This action is called from the resource permission overview page. + * It is designed to be only called via HTTP POST. + */ + public function delete_permissions_action() + { + CSRFProtection::verifyUnsafeRequest(); + $type = Request::get('permission_type'); + if (!$type) { + return; + } + if ($type === 'permanent' || $type === 'temporary') { + $user_id = Request::option('user_id'); + $resource_ids = Request::optionArray('resource_ids'); + $deleted = 0; + if ($type === 'permanent') { + $deleted = ResourcePermission::deleteBySQL( + '`user_id` = :user_id AND `resource_id` IN ( :resource_ids )', + [ + 'user_id' => $user_id, + 'resource_ids' => $resource_ids + ] + ); + } elseif ($type === 'temporary') { + $deleted = ResourceTemporaryPermission::deleteBySQL( + '`user_id` = :user_id AND `resource_id` IN ( :resource_ids )', + [ + 'user_id' => $user_id, + 'resource_ids' => $resource_ids + ] + ); + } + if ($deleted > 0) { + PageLayout::postSuccess(sprintf( + ngettext( + '%u Berechtigung wurde gelöscht.', + '%u Berechtigungen wurden gelöscht.', + $deleted + ), + $deleted + )); + } + $this->redirect('resources/admin/user_permissions', ['user_id' => $user_id]); + } elseif ($type === 'all_from_users') { + $user_ids = Request::optionArray('user_ids'); + $deleted = ResourcePermission::deleteBySql( + '`user_id` IN ( :user_ids )', + ['user_ids' => $user_ids] + ); + $deleted += ResourceTemporaryPermission::deleteBySql( + '`user_id` IN ( :user_ids )', + ['user_ids' => $user_ids] + ); + if ($deleted > 0) { + PageLayout::postSuccess(sprintf( + ngettext( + 'Die Berechtigungen von einer Person wurden gelöscht.', + 'Die Berechtigungen von %u Personen wurden gelöscht.', + count($user_ids) + ), + count($user_ids) + )); + } + $this->redirect('resources/admin/user_permissions'); + } + } } diff --git a/app/views/resources/admin/user_permissions.php b/app/views/resources/admin/user_permissions.php index 143f2ad5371..ec7af93876e 100644 --- a/app/views/resources/admin/user_permissions.php +++ b/app/views/resources/admin/user_permissions.php @@ -65,113 +65,241 @@ <? endif ?> </div> <? if ($temporary_permissions): ?> - <table class="default resources_permissions-table sortable-table" - data-sortlist="[[0, 0]]"> - <caption> - <?= _('Temporäre Berechtigungen') ?> - </caption> - <thead> - <tr> - <th data-sort="text"><?= _('Name der Ressource') ?></th> - <th data-sort="1"><?= _('Berechtigung') ?></th> - <th><?= _('Gültigkeit') ?></th> - <th class="actions"><?= _('Aktionen') ?></th> - </tr> - </thead> - <tbody> - <? foreach ($temporary_permissions as $permission): ?> + <form class="default" id="permissions_temporary" method="post" action="<?= $controller->link_for('resources/admin/delete_permissions') ?>"> + <?= CSRFProtection::tokenTag() ?> + <input type="hidden" name="permission_type" value="temporary"> + <input type="hidden" name="user_id" value="<?= htmlReady($user->id) ?>"> + <table class="default resources_permissions-table sortable-table" + data-sortlist="[[0, 0]]"> + <caption> + <?= _('Temporäre Berechtigungen') ?> + </caption> + <colspan> + <col class="checkbox"> + <col> + <col> + <col> + <col> + </colspan> + <thead> <tr> - <td> - <?= htmlReady($permission->resource->getDerivedClassInstance()) ?> - </td> - <td> - <?= htmlReady($permission->perms) ?> - </td> - <td> - <?= date('d.m.Y H:i', $permission->begin) ?> - - - <?= date('d.m.Y H:i', $permission->end) ?> - </td> - <td class="actions"> - <a href="<?= $permission->resource->getActionLink( - 'temporary_permissions', - [ - 'user_id' => $permission->user_id - ] - ) ?>" data-dialog> - <?= Icon::create('edit')->asImg( - '20px', + <th> + <input type="checkbox" data-proxyfor="#permissions_temporary input[name='resource_ids[]']" + data-activates="#permissions_temporary tfoot .button" + title="<?= htmlReady(sprintf( + _('Alle Berechtigungen von %s auswählen'), $user->getFullName() + )) ?>"> + </th> + <th data-sort="text"><?= _('Name der Ressource') ?></th> + <th data-sort="1"><?= _('Berechtigung') ?></th> + <th><?= _('Gültigkeit') ?></th> + <th class="actions"><?= _('Aktionen') ?></th> + </tr> + </thead> + <tbody> + <? foreach ($temporary_permissions as $permission): ?> + <? + $resource = $permission->resource->getDerivedClassInstance(); + ?> + <tr> + <td> + <input type="checkbox" name="resource_ids[]" + value="<?= htmlReady($permission->resource_id) ?>" + title="<?= htmlReady(sprintf(_('Berechtigung für %s auswählen'), $resource)) ?>"> + </td> + <td> + <?= htmlReady($resource) ?> + </td> + <td> + <?= htmlReady($permission->perms) ?> + </td> + <td> + <?= date('d.m.Y H:i', $permission->begin) ?> + - + <?= date('d.m.Y H:i', $permission->end) ?> + </td> + <td class="actions"> + <a href="<?= $permission->resource->getActionLink( + 'temporary_permissions', [ - 'class' => 'text-bottom', - 'title' => _('Berechtigung bearbeiten') + 'user_id' => $permission->user_id ] - ) ?> - </a> - <a href="<?= URLHelper::getLink( - 'dispatch.php/resources/admin/booking_log/' - . $user->id - . '/' - . $permission->resource_id - ) ?>" data-dialog> - <?= Icon::create('log')->asImg( + ) ?>" data-dialog> + <?= Icon::create('edit')->asImg( + '20px', + [ + 'class' => 'text-bottom', + 'title' => _('Berechtigung bearbeiten') + ] + ) ?> + </a> + <a href="<?= URLHelper::getLink( + 'dispatch.php/resources/admin/booking_log/' + . $user->id + . '/' + . $permission->resource_id + ) ?>" data-dialog> + <?= Icon::create('log')->asImg( + [ + 'class' => 'text-bottom', + 'title' => 'Liste mit Buchungen anzeigen' + ] + ) ?> + </a> + </td> + </tr> + <? endforeach ?> + </tbody> + <tfoot> + <tr> + <td colspan="5"> + <?= \Studip\Button::create(_('Löschen'), 'delete', + [ + 'data-confirm' => sprintf( + _('Sollen die gewählten Berechtigungen von %s wirklich gelöscht werden?'), + $user->getFullName() + ) + ] + ) ?> + </td> + </tr> + </tfoot> + </table> + </form> + <? endif ?> + <? if ($permissions): ?> + <form class="default" id="permissions_permanent" method="post" + action="<?= $controller->link_for('resources/admin/delete_permissions') ?>"> + <?= CSRFProtection::tokenTag() ?> + <input type="hidden" name="permission_type" value="permanent"> + <input type="hidden" name="user_id" value="<?= htmlReady($user->id) ?>"> + <table class="default resources_permissions-table sortable-table" + data-sortlist="[[0, 0]]"> + <caption> + <?= _('Dauerhafte Berechtigungen') ?> + </caption> + <colspan> + <col class="checkbox"> + <col> + <col> + <col> + </colspan> + <thead> + <tr> + <th> + <input type="checkbox" data-proxyfor="#permissions_permanent input[name='resource_ids[]']" + data-activates="#permissions_permanent tfoot .button" + title="<?= htmlReady(sprintf( + _('Alle Berechtigungen von %s auswählen'), $user->getFullName() + )) ?>"> + </th> + <th data-sort="text"><?= _('Name der Ressource') ?></th> + <th data-sort="text"><?= _('Berechtigung') ?></th> + <th class="actions"><?= _('Aktionen') ?></th> + </tr> + </thead> + <tbody> + <? foreach ($permissions as $permission): ?> + <tr> + <td> + <input type="checkbox" name="resource_ids[]" + value="<?= htmlReady($permission->resource_id) ?>" + title="<?= htmlReady(sprintf(_('Berechtigung für %s auswählen'), $resource)) ?>"> + </td> + <td> + <?= htmlReady($permission->resource->getDerivedClassInstance()) ?> + </td> + <td> + <?= htmlReady($permission->perms) ?> + </td> + <td class="actions"> + <a href="<?= $permission->resource->getActionLink( + 'permissions', [ - 'class' => 'text-bottom', - 'title' => 'Liste mit Buchungen anzeigen' + 'user_id' => $permission->user_id ] - ) ?> - </a> + ) ?>" data-dialog> + <?= Icon::create('edit')->asImg( + [ + 'class' => 'text-bottom', + 'title' => _('Berechtigung bearbeiten') + ] + ) ?> + </a> + <a href="<?= URLHelper::getLink( + 'dispatch.php/resources/admin/booking_log/' + . $user->id + . '/' + . $permission->resource_id + ) ?>" data-dialog> + <?= Icon::create('log')->asImg( + [ + 'class' => 'text-bottom', + 'title' => _('Liste mit Buchungen anzeigen') + ] + ) ?> + </a> + </td> + </tr> + <? endforeach ?> + </tbody> + <tfoot> + <tr> + <td colspan="5"> + <?= Studip\Button::create(_('Löschen'), 'delete', + [ + 'data-confirm' => sprintf( + _('Sollen die gewählten Berechtigungen von %s wirklich gelöscht werden?'), + $user->getFullName() + ) + ] + ) ?> </td> </tr> - <? endforeach ?> - </tbody> - </table> + </tfoot> + </table> + </form> <? endif ?> - <? if ($permissions): ?> - <table class="default resources_permissions-table sortable-table" - data-sortlist="[[0, 0]]"> +<? elseif ($users) : ?> + <form class="default" id="permissions_all_from_user" method="post" + action="<?= $controller->link_for('resources/admin/delete_permissions') ?>"> + <?= CSRFProtection::tokenTag() ?> + <input type="hidden" name="permission_type" value="all_from_users"> + <table class="default sortable-table" data-sortlist="[[0, 0]]"> <caption> - <?= _('Dauerhafte Berechtigungen') ?> + <?= _('Personen mit Berechtigungen an der Raumverwaltung') ?> </caption> + <colspan> + <col class="checkbox"> + <col> + </colspan> <thead> <tr> - <th data-sort="text"><?= _('Name der Ressource') ?></th> - <th data-sort="text"><?= _('Berechtigung') ?></th> - <th class="actions"><?= _('Aktionen') ?></th> + <th> + <input type="checkbox" data-proxyfor="#permissions_all_from_user input[name='user_ids[]']" + data-activates="#permissions_all_from_user tfoot .button" + title="<?= _('Alle Berechtigungen auswählen') ?>"> + </th> + <th data-sort="text"><?= _('Nachname, Vorname') ?></th> </tr> </thead> <tbody> - <? foreach ($permissions as $permission): ?> + <? foreach ($users as $user) : ?> <tr> <td> - <?= htmlReady($permission->resource->getDerivedClassInstance()) ?> + <input type="checkbox" name="user_ids[]" + value="<?= htmlReady($user->id) ?>" + title="<?= htmlReady(sprintf(_('%s auswählen'), $user->getFullName())) ?>"> </td> <td> - <?= htmlReady($permission->perms) ?> - </td> - <td class="actions"> - <a href="<?= $permission->resource->getActionLink( - 'permissions', - [ - 'user_id' => $permission->user_id - ] - ) ?>" data-dialog> - <?= Icon::create('edit')->asImg( + <a href="<?= $controller->link_for( + 'resources/admin/user_permissions', + ['user_id' => $user->id] + ) ?>"> + <?= htmlReady($user->getFullName('full_rev')) ?> + <?= Icon::create('link-intern')->asImg( [ - 'class' => 'text-bottom', - 'title' => _('Berechtigung bearbeiten') - ] - ) ?> - </a> - <a href="<?= URLHelper::getLink( - 'dispatch.php/resources/admin/booking_log/' - . $user->id - . '/' - . $permission->resource_id - ) ?>" data-dialog> - <?= Icon::create('log')->asImg( - [ - 'class' => 'text-bottom', - 'title' => _('Liste mit Buchungen anzeigen') + 'class' => 'text-bottom' ] ) ?> </a> @@ -179,36 +307,17 @@ </tr> <? endforeach ?> </tbody> - </table> - <? endif ?> -<? elseif ($users) : ?> - <table class="default sortable-table" data-sortlist="[[0, 0]]"> - <caption> - <?= _('Personen mit Berechtigungen an der Raumverwaltung') ?> - </caption> - <thead> - <tr> - <th data-sort="text"><?= _('Nachname, Vorname') ?></th> - </tr> - </thead> - <tbody> - <? foreach ($users as $user) : ?> + <tfoot> <tr> - <td> - <a href="<?= $controller->link_for( - 'resources/admin/user_permissions', - ['user_id' => $user->id] - ) ?>"> - <?= htmlReady($user->getFullName('full_rev')) ?> - <?= Icon::create('link-intern')->asImg( - [ - 'class' => 'text-bottom' - ] - ) ?> - </a> + <td colspan="2"> + <?= Studip\Button::create(_('Löschen'),'delete', + [ + 'data-confirm' => _('Sollen alle Berechtigungen der ausgewählten Personen wirklich gelöscht werden?') + ] + ) ?> </td> </tr> - <? endforeach ?> - </tbody> - </table> + </tfoot> + </table> + </form> <? endif ?> -- GitLab