From 9b5d77cf6018c0c19b28db7f5e96becabdf99ba3 Mon Sep 17 00:00:00 2001
From: Liam Morland <lkmorlan@uwaterloo.ca>
Date: Tue, 9 Mar 2021 15:22:34 -0500
Subject: [PATCH] ISTWCMS-4631: Create UWService::userIsAdmin()

---
 src/Service/UWService.php | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/src/Service/UWService.php b/src/Service/UWService.php
index f2589f87..3e0ffbbb 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);
+  }
+
 }
-- 
GitLab