You are here

public function DrupalRolesConsumer::revokeGrants in Authorization 8

Revoke all previously applied and no longer valid grants.

Parameters

\Drupal\user\UserInterface $user: The user to act upon.

array $context: Grants applied during this procedure.

Overrides ConsumerInterface::revokeGrants

File

authorization_drupal_roles/src/Plugin/authorization/Consumer/DrupalRolesConsumer.php, line 137

Class

DrupalRolesConsumer
Provides a consumer for Drupal roles.

Namespace

Drupal\authorization_drupal_roles\Plugin\authorization\Consumer

Code

public function revokeGrants(UserInterface $user, array $context) : void {
  foreach ($context as $key => $mapping) {
    $context[$key] = $this
      ->sanitizeRoleId($mapping);
  }
  $previousRoles = [];
  $savedRoles = $user
    ->get('authorization_drupal_roles_roles')
    ->getValue();
  foreach ($savedRoles as $savedRole) {
    $previousRoles[] = $savedRole['value'];
  }
  foreach ($previousRoles as $key => $value) {
    if (!in_array($value, $context, TRUE)) {
      $user
        ->removeRole($value);
      unset($previousRoles[$key]);
    }
  }
  $user
    ->set('authorization_drupal_roles_roles', $previousRoles);
}