diff --git a/src/Service/UWService.php b/src/Service/UWService.php index 1f021d59cfbb5f3d975c8baf674639a7be9c2cbd..3e0ffbbb5d97858b1cd94181fdc9d9e8dfcf9cf7 100644 --- a/src/Service/UWService.php +++ b/src/Service/UWService.php @@ -2,10 +2,11 @@ namespace Drupal\uw_cfg_common\Service; +use Drupal\Core\Database\Connection; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Session\AccountInterface; use Drupal\node\NodeInterface; -use Drupal\Core\Database\Connection; use Drupal\simplify_menu\MenuItems; /** @@ -417,4 +418,24 @@ class UWService implements UWServiceInterface { } } + /** + * Determine whether the user is in an administrator group. + * + * @param \Drupal\Core\Session\AccountInterface $user + * The user object. + * + * @return bool + * TRUE if the user is in an administrator group, FALSE otherwise. + */ + public static function userIsAdmin(AccountInterface $user): bool { + $user_roles = $user->getRoles(); + + // Based on core/modules/user/src/AccountSettingsForm.php. + $admin_roles = \Drupal::service('entity_type.manager')->getStorage('user_role')->getQuery() + ->condition('is_admin', TRUE) + ->execute(); + + return (bool) array_intersect($user_roles, $admin_roles); + } + } diff --git a/uw_cfg_common.module b/uw_cfg_common.module index 3695fbdbcd7cb21b60a73c2fad19231415446ca9..8741e9eb48e3321770e99fbfb59f26d149bd2b90 100644 --- a/uw_cfg_common.module +++ b/uw_cfg_common.module @@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Url; use Drupal\uw_cfg_common\Service\UWService; use Drupal\webform\WebformInterface; use Drupal\webform\WebformSubmissionStorageInterface; @@ -174,24 +175,44 @@ function uw_cfg_common_webform_create(WebformInterface $webform) { * Remove the Manage link from the toolbar for authenticated users. */ function uw_cfg_common_toolbar_alter(&$items) { - // Get the current user. $current_user = \Drupal::currentUser(); - // Ensure that the current user is not user1. - if ($current_user->id() !== '1') { - - // Get the roles of the user. - $roles = $current_user->getRoles(); + // No changes for user 1. + if ((int) $current_user->id() === 1) { + return; + } - // If there is only 1 role and that first role is authenticated, remove the - // manage link. If there are multiple roles then we know that they will have - // the Manage link, we are only removing the manage link for strictly - // authenticated users only. - if (count($roles) == 1 && $roles[0] == 'authenticated') { + // Get the roles of the user. + $roles = $current_user->getRoles(); - // Remove the manage link. - unset($items['administration']); + // If there is only 1 role and that first role is authenticated, remove the + // manage link. If there are multiple roles then we know that they will have + // the Manage link, we are only removing the manage link for strictly + // authenticated users only. + if (count($roles) == 1 && $roles[0] == 'authenticated') { + // Remove the manage link. + unset($items['administration']); + } + // Adjust toolbar for non-admin users. + elseif (!UWService::userIsAdmin($current_user)) { + // Remove "Manage" toolbar item. + unset($items['administration']); + // Add links to "Workbench". 'dashboards' is renamed in + // uw_dashboard_toolbar_alter(). + $links = [ + 'entity.user.collection' => t('People'), + 'system.admin_reports' => t('Reports'), + ]; + foreach ($links as $route => $title) { + $url = Url::fromRoute($route); + if ($url->access()) { + $items['dashboards']['tray']['dashboards']['#items'][] = [ + '#type' => 'link', + '#title' => $title, + '#url' => $url, + ]; + } } } }