You are here

class RemoveUserRole in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/BusinessRulesAction/RemoveUserRole.php \Drupal\business_rules\Plugin\BusinessRulesAction\RemoveUserRole

Class RemoveUserRole.

@package Drupal\business_rules\Plugin\BusinessRulesAction

Plugin annotation


@BusinessRulesAction(
  id = "remove_user_role",
  label = @Translation("Remove role from user"),
  group = @Translation("User"),
  description = @Translation("Remove role(s) from user."),
)

Hierarchy

Expanded class hierarchy of RemoveUserRole

File

src/Plugin/BusinessRulesAction/RemoveUserRole.php, line 25

Namespace

Drupal\business_rules\Plugin\BusinessRulesAction
View source
class RemoveUserRole extends BusinessRulesActionPlugin {

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm(array &$form, FormStateInterface $form_state, ItemInterface $item) {
    $settings['user_container'] = [
      '#type' => 'select',
      '#title' => t('User container'),
      '#default_value' => $item
        ->getSettings('user_container'),
      '#options' => [
        'current' => t('Current user'),
        'by_id' => t('By user id'),
        'variable' => t('User variable'),
      ],
    ];
    $settings['uid'] = [
      '#type' => 'textfield',
      '#title' => t('User id'),
      '#default_value' => $item
        ->getSettings('uid'),
      '#description' => t('You can use variables here.'),
      '#states' => [
        'visible' => [
          'select[name="user_container"]' => [
            'value' => 'by_id',
          ],
        ],
      ],
    ];
    $settings['variable'] = [
      '#type' => 'select',
      '#title' => t('User variable'),
      '#default_option' => $item
        ->getSettings('variable'),
      '#description' => t('The variable containing the user. Only variables type: "User variable".'),
      '#options' => $this->util
        ->getVariablesOptions([
        'user_variable',
      ]),
      '#states' => [
        'visible' => [
          'select[name="user_container"]' => [
            'value' => 'variable',
          ],
        ],
      ],
    ];
    $settings['roles'] = [
      '#type' => 'checkboxes',
      '#title' => t('Roles'),
      '#required' => TRUE,
      '#options' => $this->util
        ->getUserRolesOptions(),
      '#default_value' => is_array($item
        ->getSettings('roles')) ? $item
        ->getSettings('roles') : [],
      '#description' => t('Roles to remove.'),
    ];
    return $settings;
  }

  /**
   * {@inheritdoc}
   */
  public function getVariables(ItemInterface $item) {
    $variableSet = parent::getVariables($item);
    if ($item
      ->getSettings('user_container') == 'variable') {
      $variableObj = new VariableObject($item
        ->getSettings('variable'), NULL, $item
        ->getType());
      $variableSet
        ->append($variableObj);
    }
    return $variableSet;
  }

  /**
   * {@inheritdoc}
   */
  public function processSettings(array $settings, ItemInterface $item) {
    $roles = $settings['roles'];
    foreach ($roles as $key => $role) {
      if ($key !== $role) {
        unset($roles[$key]);
      }
    }
    $settings['roles'] = $roles;
    switch ($settings['user_container']) {
      case 'current':
        unset($settings['variable']);
        unset($settings['uid']);
        break;
      case 'by_id':
        unset($settings['variable']);
        break;
      case 'variable':
        unset($settings['uid']);
        break;
    }
    return $settings;
  }

  /**
   * {@inheritdoc}
   */
  public function execute(ActionInterface $action, BusinessRulesEvent $event) {

    /** @var \Drupal\business_rules\VariablesSet $event_variables */
    $event_variables = $event
      ->getArgument('variables');
    $user_container = $action
      ->getSettings('user_container');
    $uid = $action
      ->getSettings('uid');
    $uid = $this
      ->processVariables($uid, $event_variables);
    $variable = $action
      ->getSettings('variable');
    $roles = $action
      ->getSettings('roles');
    switch ($user_container) {
      case 'current':

        /** @var \Drupal\Core\Session\AccountProxyInterface $account */

        /** @var \Drupal\user\Entity\User $user */
        $account = $this->util->container
          ->get('current_user');
        $user = User::load($account
          ->id());
        break;
      case 'by_id':
        $user = User::load($uid);
        break;
      case 'variable':
        $user = $event_variables
          ->getVariable($variable);
        break;
    }
    foreach ($roles as $role) {
      $user
        ->removeRole($role);
    }
    $user
      ->save();
    $result = [
      '#type' => 'markup',
      '#markup' => t('User: %user<br>Roles removed: %roles', [
        '%user' => $user
          ->getAccountName(),
        '%roles' => implode(', ', $roles),
      ]),
    ];
    return $result;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BusinessRulesItemPluginBase::$processor protected property The business rules processor.
BusinessRulesItemPluginBase::$util protected property The business rules util.
BusinessRulesItemPluginBase::buildForm public function Form constructor. Overrides BusinessRulesItemPluginInterface::buildForm 11
BusinessRulesItemPluginBase::getDescription public function Provide a description of the item. Overrides BusinessRulesItemPluginInterface::getDescription
BusinessRulesItemPluginBase::getEditUrl public function Get the redirect url for the item edit-form route. Overrides BusinessRulesItemPluginInterface::getEditUrl
BusinessRulesItemPluginBase::getGroup public function Provide the group of the item. Overrides BusinessRulesItemPluginInterface::getGroup
BusinessRulesItemPluginBase::getRedirectUrl public function Get the redirect url for the item collection route. Overrides BusinessRulesItemPluginInterface::getRedirectUrl
BusinessRulesItemPluginBase::pregMatch public function Extract the variables from the plugin settings. Overrides BusinessRulesItemPluginInterface::pregMatch
BusinessRulesItemPluginBase::processTokenArraySetting private function Helper function to process tokens if the setting is an array.
BusinessRulesItemPluginBase::processTokens public function Process the tokens on the settings property for the item. Overrides BusinessRulesItemPluginInterface::processTokens
BusinessRulesItemPluginBase::processVariables public function Process the item replacing the variables by it's values. Overrides BusinessRulesItemPluginInterface::processVariables 1
BusinessRulesItemPluginBase::validateForm public function Plugin form validator. Overrides BusinessRulesItemPluginInterface::validateForm 11
BusinessRulesItemPluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 11
BusinessRulesItemPluginInterface::VARIABLE_REGEX constant
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
RemoveUserRole::execute public function Execute the action. Overrides BusinessRulesActionPlugin::execute
RemoveUserRole::getSettingsForm public function Return the form array. Overrides BusinessRulesItemPluginBase::getSettingsForm
RemoveUserRole::getVariables public function Return a variable set with all used variables on the item. Overrides BusinessRulesItemPluginBase::getVariables
RemoveUserRole::processSettings public function Process the item settings before it's saved. Overrides BusinessRulesItemPluginBase::processSettings