diff --git a/src/Service/UWService.php b/src/Service/UWService.php index f2589f87350d8c9cdf641b44cecbfdc29129f93a..3e0ffbbb5d97858b1cd94181fdc9d9e8dfcf9cf7 100644 --- a/src/Service/UWService.php +++ b/src/Service/UWService.php @@ -5,6 +5,7 @@ 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\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); + } + }