Commit a2a28c5b authored by mxh's avatar mxh Committed by Jürgen Haas
Browse files

Issue #3318472: Add a global setting for switching to a user when running ECA configurations

parent 3cc78e6d
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
log_level: 3
documentation_domain: "https://ecaguide.org"
user: ''
+3 −0
Original line number Diff line number Diff line
@@ -8,6 +8,9 @@ eca.settings:
    documentation_domain:
      type: string
      label: 'Documentation domain'
    user:
      type: string
      label: 'Execute models with user'

eca.eca.*:
  type: config_entity
+10 −0
Original line number Diff line number Diff line
@@ -161,3 +161,13 @@ function eca_update_8006() {
      ->save();
  }
}

/**
 * Update global configuration with new user setting.
 */
function eca_update_8007() {
  \Drupal::configFactory()
    ->getEditable('eca.settings')
    ->set('user', '')
    ->save();
}
+8 −0
Original line number Diff line number Diff line
@@ -94,6 +94,14 @@ services:
    parent: eca.default_event_subscriber
    tags:
      - { name: event_subscriber }
  eca.execution.switch_account_subscriber:
    class: Drupal\eca\EventSubscriber\EcaExecutionSwitchAccountSubscriber
    parent: eca.default_event_subscriber
    calls:
      - [setAccountSwitcher, ['@account_switcher']]
      - [initializeUser, ['@config.factory', '@current_user']]
    tags:
      - { name: event_subscriber }

  eca.token_data.current_user:
    class: Drupal\eca\Token\CurrentUserDataProvider
+51 −0
Original line number Diff line number Diff line
@@ -2,9 +2,12 @@

namespace Drupal\eca_ui\Form;

use Drupal\Component\Uuid\Uuid;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\eca_ui\Service\TokenBrowserService;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
@@ -19,12 +22,28 @@ class Settings extends ConfigFormBase {
   */
  protected ?string $defaultDocumentationDomain;

  /**
   * Token browser.
   *
   * @var \Drupal\eca_ui\Service\TokenBrowserService
   */
  protected TokenBrowserService $tokenBrowser;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected EntityTypeManagerInterface $entityTypeManager;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $instance = parent::create($container);
    $instance->defaultDocumentationDomain = $container->getParameter('eca.default_documentation_domain');
    $instance->tokenBrowser = $container->get('eca_ui.service.token_browser');
    $instance->entityTypeManager = $container->get('entity_type.manager');
    return $instance;
  }

@@ -63,9 +82,40 @@ class Settings extends ConfigFormBase {
        '#weight' => -10,
      ];
    }
    $form['user'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Execute models with user'),
      '#description' => $this->t('Specify here, which user ECA should always switch to when executing models. Leave empty to let ECA always execute models with the current user. <br/>Can be a numeric user ID (UID) or a valid UUID that identifies the user. This field supports tokens.<br/>When a user is specified here, you will have access to the original ID of the session user with the <em>[session_user:uid]</em> token.'),
      '#default_value' => $config->get('user'),
      '#weight' => 0,
    ];
    $form['token_browser'] = $this->tokenBrowser->getTokenBrowserMarkup();
    $form['token_browser']['#weight'] = 10;
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    if (($uid = trim($form_state->getValue('user'))) !== '') {
      if (ctype_digit(strval($uid))) {
        if (!$this->entityTypeManager->getStorage('user')->load($uid)) {
          $form_state->setErrorByName('user', $this->t('There is no user with the specified user ID %id.', ['%id' => $uid]));
        }
      }
      elseif (Uuid::isValid($uid)) {
        if (!$this->entityTypeManager->getStorage('user')->loadByProperties(['uuid' => $uid])) {
          $form_state->setErrorByName('user', $this->t('There is no user with the specified UUID %id.', ['%id' => $uid]));
        }
      }
      elseif (mb_strpos($uid, '[') !== 0 && !mb_strpos($uid, ']')) {
        $form_state->setErrorByName('user', $this->t('The given input is not a valid user ID, UUID or token.'));
      }
    }
    parent::validateForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
@@ -76,6 +126,7 @@ class Settings extends ConfigFormBase {
      if ($this->defaultDocumentationDomain) {
        $config->set('documentation_domain', $form_state->getValue('documentation_domain'));
      }
      $config->set('user', trim($form_state->getValue('user')));
      $config->save();
    }
    parent::submitForm($form, $form_state);
Loading