/** * Remove role from a user. * * @param \Drupal\user\UserInterface $account * User object the roles should be removed from. * @param \Drupal\user\RoleInterface[] $roles * Array of user roles. */ protected function doExecute(UserInterface $account, array $roles) { foreach ($roles as $role) { // Check if user has role. if ($account->hasRole($role->id())) { // If you try to add anonymous or authenticated role to user, Drupal // will throw an \InvalidArgumentException. Anonymous or authenticated // role ID must not be assigned manually. $account->removeRole($role->id()); // Set flag that indicates if the entity should be auto-saved later. $this->saveLater = TRUE; } } }
/** * {@inheritdoc} */ public function appliesTo(UserInterface $user) { switch ($this->protectedEntityTypeId) { case 'user': return $this->protectedEntityId == $user->id(); case 'user_role': return $user->hasRole($this->protectedEntityId); } return FALSE; }
/** * {@inheritdoc} */ public function hasRole($rid) { return $this->subject->hasRole($rid); }