Skip to content

Commit

Permalink
[TASK] Don't re-fetch user data on user refresh (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
buchmarv authored Sep 9, 2024
1 parent e326265 commit 2c5fe4f
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 18 deletions.
1 change: 0 additions & 1 deletion src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ services:
keycloak.typo3.com.user.provider:
class: '%t3g_keycloak.keycloak.user_provider_class%'
arguments:
$tokenService: '@keycloak.typo3.com.token_service'
$roleMapping: '%t3g_keycloak.keycloak.role_mapping%'
$defaultRoles: '%t3g_keycloak.keycloak.default_roles%'

Expand Down
6 changes: 3 additions & 3 deletions src/Security/KeyCloakUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class KeyCloakUser implements UserInterface
{
private string $identifier;
private array $roles;
private ?string $fullName = null;
private ?string $email = null;
private bool $fresh = false;
private ?string $fullName;
private ?string $email;
private bool $fresh;

public function __construct(string $identifier, array $roles, ?string $email = null, ?string $fullName = null, bool $fresh = false)
{
Expand Down
16 changes: 2 additions & 14 deletions src/Security/KeyCloakUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use T3G\Bundle\Keycloak\Service\TokenService;

class KeyCloakUserProvider implements UserProviderInterface
{
private TokenService $tokenService;
private array $roleMapping;
private array $defaultRoles;

public function __construct(TokenService $tokenService, array $roleMapping, array $defaultRoles = ['ROLE_USER', 'ROLE_OAUTH_USER'])
public function __construct(array $roleMapping, array $defaultRoles = ['ROLE_USER', 'ROLE_OAUTH_USER'])
{
$this->tokenService = $tokenService;
$this->roleMapping = $roleMapping;
$this->defaultRoles = $defaultRoles;
}
Expand Down Expand Up @@ -83,16 +80,7 @@ public function refreshUser(UserInterface $user): KeyCloakUser
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user)));
}

$userData = $this->tokenService->fetchUserData();

return $this->loadUserByIdentifier(
$userData['preferred_username'],
$userData['realm_access']['roles'] ?? [],
$this->tokenService->getScopes(),
$userData['email'] ?? null,
$userData['name'] ?? null,
true
);
return new KeyCloakUser($user->getUsername(), $user->getRoles(), $user->getEmail(), $user->getFullName(), false);
}

/**
Expand Down

0 comments on commit 2c5fe4f

Please sign in to comment.