Skip to content
Snippets Groups Projects
Commit 100d7a59 authored by Jürgen Haas's avatar Jürgen Haas
Browse files

Issue #3502614: Add a service user to settings and an action to switch to it

parent c181c7b5
No related branches found
No related tags found
No related merge requests found
log_level: 3
documentation_domain: "https://ecaguide.org"
user: ''
service_user: ''
dependency_calculation:
- bundle
- field_storage
......
......@@ -11,6 +11,9 @@ eca.settings:
user:
type: string
label: 'Execute models with user'
service_user:
type: string
label: 'Service user'
dependency_calculation:
type: sequence
label: 'Config dependency calculation'
......
......@@ -201,3 +201,13 @@ function eca_update_8009(): void {
$storage = \Drupal::entityTypeManager()->getStorage('eca');
$storage->rebuildSubscribedEvents();
}
/**
* Update global configuration with new service user setting.
*/
function eca_update_8010(): void {
\Drupal::configFactory()
->getEditable('eca.settings')
->set('service_user', '')
->save();
}
......@@ -90,6 +90,13 @@ class Settings extends ConfigFormBase {
'#default_value' => $config->get('user'),
'#weight' => 0,
];
$form['service_user'] = [
'#type' => 'textfield',
'#title' => $this->t('Service account user'),
'#description' => $this->t('The service account is a Drupal use that ECA will switch to when the action "Switch to service user" will be executed in a model. <br/>Can be a numeric user ID (UID) or a valid UUID that identifies the user.'),
'#default_value' => $config->get('service_user'),
'#weight' => 5,
];
$form['token_browser'] = $this->tokenBrowser->getTokenBrowserMarkup();
$form['token_browser']['#weight'] = 10;
$form['dependency_calculation'] = [
......
<?php
namespace Drupal\eca_user\Plugin\Action;
use Drupal\Component\Uuid\Uuid;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Switch current account to the service user.
*
* @Action(
* id = "eca_switch_service_account",
* label = @Translation("User: switch to service user"),
* description = @Translation("Switch to the globally configured service account."),
* eca_version_introduced = "2.1.3"
* )
*/
class SwitchServiceAccount extends SwitchAccount {
/**
* The globally configured service user.
*
* @var string
*/
protected string $serviceUser;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->serviceUser = trim((string) $container->get('config.factory')->get('eca.settings')->get('service_user'));
return $instance;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration(): array {
$default = parent::defaultConfiguration();
unset($default['user_id']);
return $default;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
$form = parent::buildConfigurationForm($form, $form_state);
unset($form['user_id']);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void {
parent::submitConfigurationForm($form, $form_state);
unset($this->configuration['user_id']);
}
/**
* {@inheritdoc}
*/
public function execute(): void {
$user = NULL;
$storage = $this->entityTypeManager->getStorage('user');
if ($this->serviceUser === '') {
return;
}
if (ctype_digit($this->serviceUser)) {
$user = $storage->load($this->serviceUser);
}
elseif (Uuid::isValid($this->serviceUser)) {
$users = $storage->loadByProperties(['uuid' => $this->serviceUser]);
$user = reset($users);
}
if ($user !== NULL) {
$this->configuration['user_id'] = $user->id();
parent::execute();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment