function UserHasRole::doEvaluate
Evaluate if user has role(s).
Parameters
\Drupal\user\UserInterface $user: The account to check.
\Drupal\user\RoleInterface[] $roles: Array of user roles.
string $operation: Either "AND": user has all of roles. Or "OR": user has at least one of all roles. Defaults to "AND".
Return value
bool TRUE if the user has the role(s).
File
-
src/
Plugin/ Condition/ UserHasRole.php, line 57
Class
- UserHasRole
- Provides a 'User has roles(s)' condition.
Namespace
Drupal\rules\Plugin\ConditionCode
protected function doEvaluate(UserInterface $user, array $roles, $operation = 'AND') {
$rids = array_map(function ($role) {
return $role->id();
}, $roles);
switch (strtoupper($operation)) {
case 'OR':
return (bool) array_intersect($rids, $user->getRoles());
case 'AND':
return (bool) (!array_diff($rids, $user->getRoles()));
default:
throw new InvalidArgumentException('Either use "AND" or "OR". Leave empty for default "AND" behavior.');
}
}