-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUserWrapperCommands.php
167 lines (149 loc) · 4.46 KB
/
UserWrapperCommands.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace Drupal\islandora_drush_utils\Drush\Commands;
use Consolidation\AnnotatedCommand\AnnotationData;
use Consolidation\AnnotatedCommand\CommandData;
use Consolidation\AnnotatedCommand\CommandError;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AccountSwitcherInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Command wrapper to perform user switching.
*
* Prior to Drush 9, there was a "--user" option which could be used to run
* commands as other users... this command wrapper introduces the
* "@islandora_drush_utils-user-wrap" annotation to use it where we want
* to.
*/
class UserWrapperCommands implements LoggerAwareInterface, ContainerInjectionInterface {
use LoggerAwareTrait;
/**
* The user to which we will switch.
*
* Either some form of account object, or boolean FALSE.
*
* @var \Drupal\Core\Session\AccountInterface|false
*/
protected AccountInterface|false $user = FALSE;
/**
* Constructor.
*/
public function __construct(
protected AccountSwitcherInterface $switcher,
protected EntityTypeManagerInterface $entityTypeManager,
protected $debug = FALSE,
) {
}
/**
* {@inheritDoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('account_switcher'),
$container->get('entity_type.manager'),
FALSE,
);
}
/**
* Command validation callback; if `--user` is supported, is it provided?
*
* @hook validate @islandora-drush-utils-user-wrap
*/
public function userCheck(CommandData $commandData) {
if (!$commandData->input()->getOption('user')) {
$this->logger->info('Command supports "--user"; however, it was not passed to the command.');
}
}
/**
* Add the option to the command.
*
* @hook option @islandora-drush-utils-user-wrap
*/
public function userOption(Command $command, AnnotationData $annotationData) {
$command->addOption(
'user',
'u',
InputOption::VALUE_REQUIRED,
'The Drupal user as whom to run the command.'
);
}
/**
* Logging helper; only log debug messages when so-instructed.
*
* @param string $message
* The message to log.
*/
protected function logDebug($message) {
if ($this->debug) {
$this->logger->debug($message);
}
}
/**
* Ensure the user provided is valid.
*
* @hook validate @islandora-drush-utils-user-wrap
*/
public function userExists(CommandData $commandData) {
$input = $commandData->input();
$user = $input->getOption('user');
if (!isset($user)) {
$this->logDebug('"user" option does not appear to be set');
return NULL;
}
$user_storage = $this->entityTypeManager->getStorage('user');
if (is_numeric($user)) {
$this->logDebug('"user" appears to be numeric; loading as-is');
$this->user = $user_storage->load($user);
}
else {
$this->logDebug('"user" is non-numeric; assuming it is a name');
$candidates = $user_storage->loadByProperties(['name' => $user]);
if (count($candidates) > 1) {
return new CommandError(
\dt('Too many candidates for user name: @spec', [
'@spec' => $user,
])
);
}
$this->user = reset($candidates);
}
if (!$this->user) {
return new CommandError(\dt('Failed to load the user: @spec', [
'@spec' => $user,
]));
}
}
/**
* Perform the swap before running the command.
*
* @hook pre-command @islandora-drush-utils-user-wrap
*/
public function switchUser(CommandData $commandData) {
$this->logDebug('pre-command');
if ($this->user) {
$this->logDebug('switching user');
$this->switcher->switchTo($this->user);
$this->logDebug('switched user');
}
}
/**
* Swap back after running the command.
*
* @hook post-command @islandora-drush-utils-user-wrap
*/
public function unswitch($result, CommandData $commandData) {
$this->logDebug('post-command');
if ($this->user) {
$this->logDebug('to switch back');
$this->switcher->switchBack();
$this->logDebug('switched back');
$this->user = FALSE;
}
return $result;
}
}