示例#1
0
 /**
  * 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 removeRole($rid)
 {
     $this->subject->removeRole($rid);
 }