You are here

public function UserVariableHasRole::process in Business Rules 8

Same name and namespace in other branches
  1. 2.x src/Plugin/BusinessRulesCondition/UserVariableHasRole.php \Drupal\business_rules\Plugin\BusinessRulesCondition\UserVariableHasRole::process()

Process the condition.

Parameters

\Drupal\business_rules\ConditionInterface $condition: The configured condition.

\Drupal\business_rules\Events\BusinessRulesEvent $event: The event that has triggered the condition.

Return value

bool Boolean value that indicates if the condition is true.

Overrides BusinessRulesConditionPlugin::process

File

src/Plugin/BusinessRulesCondition/UserVariableHasRole.php, line 105

Class

UserVariableHasRole
Class UserVariableHasRole.

Namespace

Drupal\business_rules\Plugin\BusinessRulesCondition

Code

public function process(ConditionInterface $condition, BusinessRulesEvent $event) {
  $roles = $condition
    ->getSettings('roles');
  $criteria = $condition
    ->getSettings('criteria');
  $user_variable_id = $condition
    ->getSettings('user_variable');

  /** @var \Drupal\business_rules\VariablesSet $event_variables */
  $event_variables = $event
    ->getArgument('variables');

  /** @var \Drupal\user\Entity\User $user_variable */
  $user_variable = $event_variables
    ->getVariable($user_variable_id)
    ->getValue();

  // Check if $user_variable is defined.
  if (!$user_variable instanceof User) {
    $this->util->logger
      ->error('Trying to check user roles in a null user object. Condition: %condition', [
      '%condition' => $condition
        ->label() . ' [' . $condition
        ->id() . ']',
    ]);
    return FALSE;
  }
  $user_roles = array_values($user_variable
    ->getRoles());
  $result = FALSE;
  if ($criteria == 'all') {
    $result = count(array_intersect($roles, $user_roles)) == count($roles);
  }
  elseif ($criteria == 'one') {
    $result = !empty(array_intersect($user_roles, $roles));
  }
  return $result;
}