class UserClaimsNormalizer in Simple OAuth (OAuth2) & OpenID Connect 5.x
Normalizes a user entity to extract the claims.
Hierarchy
- class \Drupal\serialization\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\SerializerAwareInterface, CacheableNormalizerInterface uses \Symfony\Component\Serializer\SerializerAwareTrait
- class \Drupal\simple_oauth\Normalizer\UserClaimsNormalizer implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface
Expanded class hierarchy of UserClaimsNormalizer
1 string reference to 'UserClaimsNormalizer'
1 service uses UserClaimsNormalizer
File
- src/
Normalizer/ UserClaimsNormalizer.php, line 16
Namespace
Drupal\simple_oauth\NormalizerView source
class UserClaimsNormalizer extends NormalizerBase implements NormalizerInterface {
protected $supportedInterfaceOrClass = UserEntityWithClaims::class;
protected $format = 'json';
/**
* The user storage.
*
* @var \Drupal\user\UserStorageInterface
*/
protected $userStorage;
/**
* The claims.
*
* @var string[]
*/
protected $claims;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* UserClaimsNormalizer constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param string[] $claims
* The list of claims being selected.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, array $claims, ModuleHandlerInterface $module_handler) {
$this->userStorage = $entity_type_manager
->getStorage('user');
$this->claims = $claims;
$this->moduleHandler = $module_handler;
}
/**
* {@inheritdoc}
*/
public function normalize($user_entity, $format = NULL, array $context = []) {
assert($user_entity instanceof UserEntityWithClaims);
$identifier = $user_entity
->getIdentifier();
// Check if the account is in $context. If not, load it from the database.
$account = $context[$identifier] instanceof AccountInterface ? $context[$identifier] : $this->userStorage
->load($identifier);
assert($account instanceof AccountInterface);
return $this
->getClaimsFromAccount($account);
}
/**
* Gets the claims for a given user.
*
* @param \Drupal\Core\Session\AccountInterface $account
* The user account.
*
* @return array
* The claims key/values.
*/
private function getClaimsFromAccount(AccountInterface $account) {
$profile_url = $account
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString();
$claim_values = [
'sub' => $account
->id(),
'name' => $account
->getDisplayName(),
'preferred_username' => $account
->getAccountName(),
'email' => $account
->getEmail(),
'email_verified' => TRUE,
'profile' => $profile_url,
'locale' => $account
->getPreferredLangcode(),
'zoneinfo' => $account
->getTimeZone(),
];
if ($account instanceof EntityChangedInterface) {
$claim_values['updated_at'] = $account
->getChangedTime();
}
$context = [
'account' => $account,
'claims' => $this->claims,
];
$this->moduleHandler
->alter('simple_oauth_oidc_claims', $claim_values, $context);
return array_intersect_key($claim_values, array_flip($this->claims));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheableNormalizerInterface:: |
constant | Name of key for bubbling cacheability metadata via serialization context. | ||
NormalizerBase:: |
protected | function | Adds cacheability if applicable. | |
NormalizerBase:: |
protected | function | Checks if the provided format is supported by this normalizer. | 1 |
NormalizerBase:: |
public | function | Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() | 1 |
NormalizerBase:: |
public | function | 1 | |
UserClaimsNormalizer:: |
protected | property | The claims. | |
UserClaimsNormalizer:: |
protected | property |
List of formats which supports (de-)normalization. Overrides NormalizerBase:: |
|
UserClaimsNormalizer:: |
protected | property | The module handler. | |
UserClaimsNormalizer:: |
protected | property |
The interface or class that this Normalizer supports. Overrides NormalizerBase:: |
|
UserClaimsNormalizer:: |
protected | property | The user storage. | |
UserClaimsNormalizer:: |
private | function | Gets the claims for a given user. | |
UserClaimsNormalizer:: |
public | function | ||
UserClaimsNormalizer:: |
public | function | UserClaimsNormalizer constructor. |