/
UsersController.php
347 lines (319 loc) · 13.4 KB
/
UsersController.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php
declare(strict_types=1);
namespace Neos\Neos\Controller\Module\Administration;
/*
* This file is part of the Neos.Neos package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\Annotations as Flow;
use Neos\Error\Messages\Message;
use Neos\Flow\I18n\EelHelper\TranslationHelper;
use Neos\Flow\Mvc\Exception\ForwardException;
use Neos\Flow\Mvc\Exception\NoSuchArgumentException;
use Neos\Flow\Mvc\Exception\StopActionException;
use Neos\Flow\Property\PropertyMappingConfiguration;
use Neos\Flow\Property\TypeConverter\PersistentObjectConverter;
use Neos\Flow\Security\Account;
use Neos\Flow\Security\Authentication\TokenAndProviderFactoryInterface;
use Neos\Flow\Security\Authorization\PrivilegeManagerInterface;
use Neos\Flow\Security\Exception\NoSuchRoleException;
use Neos\Flow\Security\Policy\PolicyService;
use Neos\Neos\Controller\Module\AbstractModuleController;
use Neos\Neos\Domain\Exception;
use Neos\Neos\Domain\Model\User;
use Neos\Neos\Domain\Service\UserService;
use Neos\Party\Domain\Model\ElectronicAddress;
/**
* The Neos User Admin module controller that allows for managing Neos users
*/
class UsersController extends AbstractModuleController
{
/**
* @Flow\Inject
* @var PrivilegeManagerInterface
*/
protected $privilegeManager;
/**
* @Flow\Inject
* @var PolicyService
*/
protected $policyService;
/**
* @Flow\Inject
* @var UserService
*/
protected $userService;
/**
* @var User
*/
protected $currentUser;
/**
* @Flow\Inject
* @var TokenAndProviderFactoryInterface
*/
protected $tokenAndProviderFactory;
/**
* @return void
* @throws NoSuchArgumentException
*/
protected function initializeAction()
{
parent::initializeAction();
$translationHelper = new TranslationHelper();
$this->setTitle($translationHelper->translate($this->moduleConfiguration['label']) . ' :: ' . $translationHelper->translate(str_replace('label', 'action.', $this->moduleConfiguration['label']) . $this->request->getControllerActionName()));
if ($this->arguments->hasArgument('user')) {
$propertyMappingConfigurationForUser = $this->arguments->getArgument('user')->getPropertyMappingConfiguration();
$propertyMappingConfigurationForUserName = $propertyMappingConfigurationForUser->forProperty('user.name');
$propertyMappingConfigurationForPrimaryAccount = $propertyMappingConfigurationForUser->forProperty('user.primaryAccount');
$propertyMappingConfigurationForPrimaryAccount->setTypeConverterOption(PersistentObjectConverter::class, PersistentObjectConverter::CONFIGURATION_TARGET_TYPE, Account::class);
/** @var PropertyMappingConfiguration $propertyMappingConfiguration */
foreach ([$propertyMappingConfigurationForUser, $propertyMappingConfigurationForUserName, $propertyMappingConfigurationForPrimaryAccount] as $propertyMappingConfiguration) {
$propertyMappingConfiguration->setTypeConverterOption(PersistentObjectConverter::class, PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED, true);
$propertyMappingConfiguration->setTypeConverterOption(PersistentObjectConverter::class, PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED, true);
}
}
$this->currentUser = $this->userService->getCurrentUser();
}
/**
* Shows a list of all users
*
* @param string $searchTerm
* @return void
*/
public function indexAction(string $searchTerm = '')
{
if (empty($searchTerm)) {
$users = $this->userService->getUsers();
} else {
$users = $this->userService->searchUsers($searchTerm);
}
$this->view->assignMultiple([
'currentUser' => $this->currentUser,
'users' => $users,
'searchTerm' => $searchTerm
]);
}
/**
* Shows details for the specified user
*
* @param User $user
* @return void
*/
public function showAction(User $user): void
{
$this->view->assignMultiple([
'currentUser' => $this->currentUser,
'user' => $user
]);
}
/**
* Renders a form for creating a new user
*
* @param User $user
* @return void
*/
public function newAction(User $user = null): void
{
$this->view->assignMultiple([
'currentUser' => $this->currentUser,
'user' => $user,
'roles' => $this->policyService->getRoles(),
'providers' => $this->getAuthenticationProviders()
]);
}
/**
* Create a new user
*
* @param string $username The user name (ie. account identifier) of the new user
* @param array $password Expects an array in the format array('<password>', '<password confirmation>')
* @param User $user The user to create
* @param array $roleIdentifiers A list of roles (role identifiers) to assign to the new user
* @param string $authenticationProviderName Optional name of the authentication provider. If not provided the user server uses the default authentication provider
* @Flow\Validate(argumentName="username", type="\Neos\Flow\Validation\Validator\NotEmptyValidator")
* @Flow\Validate(argumentName="username", type="\Neos\Neos\Validation\Validator\UserDoesNotExistValidator")
* @Flow\Validate(argumentName="password", type="\Neos\Neos\Validation\Validator\PasswordValidator", options={ "allowEmpty"=0, "minimum"=1, "maximum"=255 })
* @return void
* @throws StopActionException
*/
public function createAction(string $username, array $password, User $user, array $roleIdentifiers, string $authenticationProviderName = null): void
{
$this->userService->addUser($username, $password[0], $user, $roleIdentifiers, $authenticationProviderName);
$this->addFlashMessage('The user "%s" has been created.', 'User created', Message::SEVERITY_OK, [htmlspecialchars($username)], 1416225561);
$this->redirect('index');
}
/**
* Edit an existing user
*
* @param User $user
* @return void
*/
public function editAction(User $user): void
{
$this->assignElectronicAddressOptions();
$this->view->assignMultiple([
'currentUser' => $this->currentUser,
'user' => $user,
'availableRoles' => $this->policyService->getRoles()
]);
}
/**
* Update a given user
*
* @param User $user The user to update, including updated data already (name, email address etc)
* @return void
* @throws StopActionException
*/
public function updateAction(User $user): void
{
$this->userService->updateUser($user);
$this->addFlashMessage('The user "%s" has been updated.', 'User updated', Message::SEVERITY_OK, [$user->getName()->getFullName()], 1412374498);
$this->redirect('index');
}
/**
* Delete the given user
*
* @param User $user
* @return void
* @throws Exception
* @throws StopActionException
*/
public function deleteAction(User $user): void
{
if ($user === $this->currentUser) {
$this->addFlashMessage('You can not delete the currently logged in user', 'Current user can\'t be deleted', Message::SEVERITY_WARNING, [], 1412374546);
$this->redirect('index');
}
$this->userService->deleteUser($user);
$this->addFlashMessage('The user "%s" has been deleted.', 'User deleted', Message::SEVERITY_NOTICE, [htmlspecialchars($user->getName()->getFullName())], 1412374546);
$this->redirect('index');
}
/**
* Edit the given account
*
* @param Account $account
* @return void
* @throws Exception
*/
public function editAccountAction(Account $account): void
{
$this->view->assignMultiple([
'account' => $account,
'user' => $this->userService->getUser($account->getAccountIdentifier(), (string) $account->getAuthenticationProviderName()),
'availableRoles' => $this->policyService->getRoles()
]);
}
/**
* Update a given account
*
* @param Account $account The account to update
* @param array $roleIdentifiers A possibly updated list of roles for the user's primary account
* @param array $password Expects an array in the format array('<password>', '<password confirmation>')
* @return void
* @throws StopActionException
* @throws ForwardException
* @throws NoSuchRoleException
* @throws Exception
* @Flow\Validate(argumentName="password", type="\Neos\Neos\Validation\Validator\PasswordValidator", options={ "allowEmpty"=1, "minimum"=1, "maximum"=255 })
*/
public function updateAccountAction(Account $account, array $roleIdentifiers, array $password = []): void
{
$user = $this->userService->getUser($account->getAccountIdentifier(), (string) $account->getAuthenticationProviderName());
if ($user === $this->currentUser) {
$roles = [];
foreach ($roleIdentifiers as $roleIdentifier) {
$roles[$roleIdentifier] = $this->policyService->getRole($roleIdentifier);
}
if (!$this->privilegeManager->isPrivilegeTargetGrantedForRoles($roles, 'Neos.Neos:Backend.Module.Administration.Users')) {
$this->addFlashMessage('With the selected roles the currently logged in user wouldn\'t have access to this module any longer. Please adjust the assigned roles!', 'Don\'t lock yourself out', Message::SEVERITY_WARNING, [], 1416501197);
$this->forward('edit', null, null, ['user' => $this->currentUser]);
}
}
$password = array_shift($password);
if (strlen(trim(strval($password))) > 0) {
$this->userService->setUserPassword($user, $password);
}
$this->userService->setRolesForAccount($account, $roleIdentifiers);
$this->addFlashMessage('The account has been updated.', 'Account updated', Message::SEVERITY_OK);
$this->redirect('edit', null, null, ['user' => $user]);
}
/**
* The add new electronic address action
*
* @param User $user
* @Flow\IgnoreValidation("$user")
* @return void
*/
public function newElectronicAddressAction(User $user): void
{
$this->assignElectronicAddressOptions();
$this->view->assign('user', $user);
}
/**
* Create an new electronic address
*
* @param User $user
* @param ElectronicAddress $electronicAddress
* @return void
* @throws StopActionException
*/
public function createElectronicAddressAction(User $user, ElectronicAddress $electronicAddress): void
{
/** @var User $user */
$user->addElectronicAddress($electronicAddress);
$this->userService->updateUser($user);
$this->addFlashMessage('An electronic address "%s" (%s) has been added.', 'Electronic address added', Message::SEVERITY_OK, [htmlspecialchars($electronicAddress->getIdentifier()), htmlspecialchars($electronicAddress->getType())], 1412374814);
$this->redirect('edit', null, null, ['user' => $user]);
}
/**
* Delete an electronic address action
*
* @param User $user
* @param ElectronicAddress $electronicAddress
* @return void
* @throws StopActionException
*/
public function deleteElectronicAddressAction(User $user, ElectronicAddress $electronicAddress): void
{
$user->removeElectronicAddress($electronicAddress);
$this->userService->updateUser($user);
$this->addFlashMessage('The electronic address "%s" (%s) has been deleted for "%s".', 'Electronic address removed', Message::SEVERITY_NOTICE, [htmlspecialchars($electronicAddress->getIdentifier()), htmlspecialchars($electronicAddress->getType()), htmlspecialchars($user->getName())], 1412374678);
$this->redirect('edit', null, null, ['user' => $user]);
}
/**
* @return void
*/
protected function assignElectronicAddressOptions(): void
{
$electronicAddress = new ElectronicAddress();
$electronicAddressTypes = [];
foreach ($electronicAddress->getAvailableElectronicAddressTypes() as $type) {
$electronicAddressTypes[$type] = $type;
}
$electronicAddressUsageTypes = [];
$translationHelper = new TranslationHelper();
foreach ($electronicAddress->getAvailableUsageTypes() as $type) {
$electronicAddressUsageTypes[$type] = $translationHelper->translate('users.electronicAddress.usage.type.' . $type, $type, [], 'Modules', 'Neos.Neos');
}
array_unshift($electronicAddressUsageTypes, '');
$this->view->assignMultiple([
'electronicAddressTypes' => $electronicAddressTypes,
'electronicAddressUsageTypes' => $electronicAddressUsageTypes
]);
}
/**
* Returns sorted list of auth providers by name.
*
* @return string[]
*/
protected function getAuthenticationProviders(): array
{
$providerNames = array_keys($this->tokenAndProviderFactory->getProviders());
sort($providerNames);
return array_combine($providerNames, $providerNames);
}
}