From c611e427aa15118aeaa4a0bd915613627f336686 Mon Sep 17 00:00:00 2001 From: Darren Oh <2293701+darrenoh@users.noreply.github.com> Date: Sun, 26 Nov 2023 12:18:57 -0500 Subject: [PATCH] Fix format of global user roles array --- phpstan-baseline.neon | 5 ---- src/Entity/Role.php | 4 ++- src/User/GlobalUser.php | 27 ++++++++++--------- src/functions/user.php | 2 +- tests/data/menu_example/menu_example.module | 2 +- .../Routing/HookMenuRoutesTest.php | 1 + 6 files changed, 20 insertions(+), 21 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index ad372b4..2e0a2d5 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -555,11 +555,6 @@ parameters: count: 1 path: src/Theme/HookPreprocess.php - - - message: "#^Method Retrofit\\\\Drupal\\\\User\\\\GlobalUser\\:\\:__get\\(\\) has no return type specified\\.$#" - count: 1 - path: src/User/GlobalUser.php - - message: "#^Method Retrofit\\\\Drupal\\\\User\\\\GlobalUser\\:\\:__set\\(\\) has parameter \\$value with no type specified\\.$#" count: 1 diff --git a/src/Entity/Role.php b/src/Entity/Role.php index 82ce02b..8de030d 100644 --- a/src/Entity/Role.php +++ b/src/Entity/Role.php @@ -4,7 +4,9 @@ namespace Retrofit\Drupal\Entity; -class Role extends \Drupal\user\Entity\Role +use Drupal\user\Entity\Role as CoreRole; + +class Role extends CoreRole { public function __toString() { diff --git a/src/User/GlobalUser.php b/src/User/GlobalUser.php index c2dd1c1..73483d5 100644 --- a/src/User/GlobalUser.php +++ b/src/User/GlobalUser.php @@ -5,7 +5,7 @@ namespace Retrofit\Drupal\User; use Drupal\Core\Session\AccountInterface; -use Drupal\user\Entity\Role; +use Retrofit\Drupal\Entity\Role; final class GlobalUser implements AccountInterface { @@ -21,16 +21,7 @@ public function id() public function getRoles($exclude_locked_roles = false) { - $roles = $this->inner->getRoles($exclude_locked_roles); - // Drupal 7 appended `user` to locked roles, so we need to do the same. - if (!$exclude_locked_roles) { - if ($this->isAuthenticated()) { - $roles[] = 'authenticated user'; - } else { - $roles[] = 'anonymous user'; - } - } - return $roles; + return $this->inner->getRoles($exclude_locked_roles); } public function hasPermission($permission) @@ -83,10 +74,20 @@ public function getLastAccessedTime() return $this->inner->getLastAccessedTime(); } - public function __get(string $name) + public function __get(string $name): mixed { return match ($name) { - 'roles' => array_combine($this->getRoles(), array_map([Role::class, 'load'], $this->getRoles())), + 'roles' => array_map( + fn($role) => match ($role->id()) { + 'anonymous', + 'authenticated' => strtolower((string) $role->label()), + default => (string) $role->label(), + }, + array_filter(array_map( + [Role::class, 'load'], + array_combine($this->getRoles(), $this->getRoles()) + )) + ), 'name' => $this->getAccountName(), default => null, }; diff --git a/src/functions/user.php b/src/functions/user.php index 6081567..08d2376 100644 --- a/src/functions/user.php +++ b/src/functions/user.php @@ -3,8 +3,8 @@ declare(strict_types=1); use Drupal\Core\Session\AccountInterface; -use Drupal\user\Entity\Role; use Drupal\user\RoleInterface; +use Retrofit\Drupal\Entity\Role; function user_access(string $string, ?AccountInterface $account = null): bool { diff --git a/tests/data/menu_example/menu_example.module b/tests/data/menu_example/menu_example.module index da29f95..6d23a86 100644 --- a/tests/data/menu_example/menu_example.module +++ b/tests/data/menu_example/menu_example.module @@ -377,7 +377,7 @@ function menu_example_permission() { * True if the acting user has the role specified. */ function menu_example_custom_access($role_name) { - $access_granted = array_key_exists($role_name, $GLOBALS['user']->roles); + $access_granted = in_array($role_name, $GLOBALS['user']->roles); return $access_granted; } diff --git a/tests/src/Integration/Routing/HookMenuRoutesTest.php b/tests/src/Integration/Routing/HookMenuRoutesTest.php index ab5e26a..a729f54 100644 --- a/tests/src/Integration/Routing/HookMenuRoutesTest.php +++ b/tests/src/Integration/Routing/HookMenuRoutesTest.php @@ -37,6 +37,7 @@ public function register(ContainerBuilder $container): void protected function setUp(): void { parent::setUp(); + $this->installConfig(['user']); $this->config('system.site') ->set('name', 'Drupal') ->save();