Skip to content

Commit

Permalink
Fix format of global user roles array
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenoh committed Nov 26, 2023
1 parent c8b0cbc commit c611e42
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 21 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/Entity/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
27 changes: 14 additions & 13 deletions src/User/GlobalUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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)
Expand Down Expand Up @@ -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,
};
Expand Down
2 changes: 1 addition & 1 deletion src/functions/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion tests/data/menu_example/menu_example.module
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions tests/src/Integration/Routing/HookMenuRoutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit c611e42

Please sign in to comment.