class UserHasRole
Provides a 'User has roles(s)' condition.
@todo Add access callback information from Drupal 7.
Plugin annotation
@Condition(
id = "rules_user_has_role",
label = @Translation("User has role(s)"),
category = @Translation("User"),
context_definitions = {
"user" = @ContextDefinition("entity:user",
label = @Translation("User"),
description = @Translation("Specifies the user account to check."),
),
"roles" = @ContextDefinition("entity:user_role",
label = @Translation("Roles"),
description = @Translation("Specifies the roles to check for."),
multiple = TRUE,
options_provider = "\Drupal\rules\TypedData\Options\RolesOptions"
),
"operation" = @ContextDefinition("string",
label = @Translation("Matching multiple roles"),
description = @Translation("Specify if the user must have <em>all</em> the roles selected or <em>any</em> of the roles selected."),
assignment_restriction = "input",
default_value = "AND",
options_provider = "\Drupal\rules\TypedData\Options\AndOrOptions",
required = FALSE
),
}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements \Drupal\Component\Plugin\PluginInspectionInterface, \Drupal\Component\Plugin\DerivativeInspectionInterface
- class \Drupal\Core\Plugin\PluginBase extends \Drupal\Component\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait
- class \Drupal\Core\Executable\ExecutablePluginBase extends \Drupal\Core\Plugin\PluginBase implements \Drupal\Core\Executable\ExecutableInterface, \Drupal\Core\Cache\CacheableDependencyInterface, \Drupal\Core\Plugin\ContextAwarePluginInterface uses \Drupal\Core\Plugin\ContextAwarePluginTrait
- class \Drupal\Core\Condition\ConditionPluginBase extends \Drupal\Core\Executable\ExecutablePluginBase implements \Drupal\Core\Condition\ConditionInterface uses \Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait
- class \Drupal\rules\Core\RulesConditionBase extends \Drupal\Core\Condition\ConditionPluginBase implements \Drupal\rules\Core\RulesConditionInterface uses \Drupal\rules\Context\ContextProviderTrait, \Drupal\rules\Core\ExecutablePluginTrait, \Drupal\rules\Core\ConfigurationAccessControlTrait
- class \Drupal\rules\Plugin\Condition\UserHasRole extends \Drupal\rules\Core\RulesConditionBase
- class \Drupal\rules\Core\RulesConditionBase extends \Drupal\Core\Condition\ConditionPluginBase implements \Drupal\rules\Core\RulesConditionInterface uses \Drupal\rules\Context\ContextProviderTrait, \Drupal\rules\Core\ExecutablePluginTrait, \Drupal\rules\Core\ConfigurationAccessControlTrait
- class \Drupal\Core\Condition\ConditionPluginBase extends \Drupal\Core\Executable\ExecutablePluginBase implements \Drupal\Core\Condition\ConditionInterface uses \Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait
- class \Drupal\Core\Executable\ExecutablePluginBase extends \Drupal\Core\Plugin\PluginBase implements \Drupal\Core\Executable\ExecutableInterface, \Drupal\Core\Cache\CacheableDependencyInterface, \Drupal\Core\Plugin\ContextAwarePluginInterface uses \Drupal\Core\Plugin\ContextAwarePluginTrait
- class \Drupal\Core\Plugin\PluginBase extends \Drupal\Component\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait
Expanded class hierarchy of UserHasRole
File
-
src/
Plugin/ Condition/ UserHasRole.php, line 40
Namespace
Drupal\rules\Plugin\ConditionView source
class UserHasRole extends RulesConditionBase {
/**
* Evaluate if user has role(s).
*
* @param \Drupal\user\UserInterface $user
* The account to check.
* @param \Drupal\user\RoleInterface[] $roles
* Array of user roles.
* @param string $operation
* Either "AND": user has all of roles.
* Or "OR": user has at least one of all roles.
* Defaults to "AND".
*
* @return bool
* TRUE if the user has the role(s).
*/
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.');
}
}
}