UserHasRole.php in Business Rules 8
File
src/Plugin/BusinessRulesCondition/UserHasRole.php
View source
<?php
namespace Drupal\business_rules\Plugin\BusinessRulesCondition;
use Drupal\business_rules\ConditionInterface;
use Drupal\business_rules\Events\BusinessRulesEvent;
use Drupal\business_rules\ItemInterface;
use Drupal\business_rules\Plugin\BusinessRulesConditionPlugin;
use Drupal\Core\Form\FormStateInterface;
class UserHasRole extends BusinessRulesConditionPlugin {
public function getSettingsForm(array &$form, FormStateInterface $form_state, ItemInterface $item) {
$settings['roles'] = [
'#type' => 'checkboxes',
'#title' => t('Roles'),
'#required' => TRUE,
'#options' => $this->util
->getUserRolesOptions(),
'#default_value' => is_array($item
->getSettings('roles')) ? $item
->getSettings('roles') : [],
];
$settings['criteria'] = [
'#type' => 'select',
'#title' => t('Match criteria'),
'#description' => t('The condition will check if current user has all selected roles or at least one role?'),
'#required' => TRUE,
'#default_value' => $item
->getSettings('criteria'),
'#options' => [
'all' => t('All roles'),
'one' => t('At least one role'),
],
];
return $settings;
}
public function processSettings(array $settings, ItemInterface $item) {
foreach ($settings['roles'] as $key => $role) {
if ($role === 0) {
unset($settings['roles'][$key]);
}
}
return $settings;
}
public function process(ConditionInterface $condition, BusinessRulesEvent $event) {
$roles = $condition
->getSettings('roles');
$criteria = $condition
->getSettings('criteria');
$current_user = $this->util->container
->get('current_user');
$user_roles = array_values($current_user
->getRoles());
$result = FALSE;
if (is_array($roles) && is_array($user_roles)) {
if ($criteria == 'all') {
$result = count(array_intersect($roles, $user_roles)) == count($roles);
}
elseif ($criteria == 'one') {
$result = !empty(array_intersect($user_roles, $roles));
}
}
return $result;
}
}