Skip to content
Snippets Groups Projects
Commit 4ef3d7ea authored by Igor Biki's avatar Igor Biki
Browse files

Merge branch 'feature/ISTWCMS-4631-lkmorlan-toolbar' into '8.x-1.x'

ISTWCMS-4631, ISTWCMS-4636: Toolbar adjustments

See merge request !65
parents 0ac096b3 25d201bb
No related branches found
No related tags found
1 merge request!65ISTWCMS-4631, ISTWCMS-4636: Toolbar adjustments
...@@ -2,10 +2,11 @@ ...@@ -2,10 +2,11 @@
namespace Drupal\uw_cfg_common\Service; namespace Drupal\uw_cfg_common\Service;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\NodeInterface; use Drupal\node\NodeInterface;
use Drupal\Core\Database\Connection;
use Drupal\simplify_menu\MenuItems; use Drupal\simplify_menu\MenuItems;
/** /**
...@@ -417,4 +418,24 @@ class UWService implements UWServiceInterface { ...@@ -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);
}
} }
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\uw_cfg_common\Service\UWService; use Drupal\uw_cfg_common\Service\UWService;
use Drupal\webform\WebformInterface; use Drupal\webform\WebformInterface;
use Drupal\webform\WebformSubmissionStorageInterface; use Drupal\webform\WebformSubmissionStorageInterface;
...@@ -174,24 +175,44 @@ function uw_cfg_common_webform_create(WebformInterface $webform) { ...@@ -174,24 +175,44 @@ function uw_cfg_common_webform_create(WebformInterface $webform) {
* Remove the Manage link from the toolbar for authenticated users. * Remove the Manage link from the toolbar for authenticated users.
*/ */
function uw_cfg_common_toolbar_alter(&$items) { function uw_cfg_common_toolbar_alter(&$items) {
// Get the current user. // Get the current user.
$current_user = \Drupal::currentUser(); $current_user = \Drupal::currentUser();
// Ensure that the current user is not user1. // No changes for user 1.
if ($current_user->id() !== '1') { if ((int) $current_user->id() === 1) {
return;
// Get the roles of the user. }
$roles = $current_user->getRoles();
// If there is only 1 role and that first role is authenticated, remove the // Get the roles of the user.
// manage link. If there are multiple roles then we know that they will have $roles = $current_user->getRoles();
// 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. // If there is only 1 role and that first role is authenticated, remove the
unset($items['administration']); // 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,
];
}
} }
} }
} }
......
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