You are here

public function DrupalRolesConsumer::grantSingleAuthorization in Authorization 8

Grant one individual proposal.

Parameters

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

mixed $mapping: What to grant.

Overrides ConsumerInterface::grantSingleAuthorization

File

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

Class

DrupalRolesConsumer
Provides a consumer for Drupal roles.

Namespace

Drupal\authorization_drupal_roles\Plugin\authorization\Consumer

Code

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