/
UserService.php
867 lines (770 loc) · 29 KB
/
UserService.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
<?php
namespace Neos\Neos\Domain\Service;
/*
* 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\Flow\Persistence\Exception\IllegalObjectTypeException;
use Neos\Flow\Persistence\PersistenceManagerInterface;
use Neos\Flow\Persistence\QueryResultInterface;
use Neos\Flow\Security\Account;
use Neos\Flow\Security\AccountFactory;
use Neos\Flow\Security\AccountRepository;
use Neos\Flow\Security\Authentication\AuthenticationManagerInterface;
use Neos\Flow\Security\Authentication\Token\UsernamePassword;
use Neos\Flow\Security\Authentication\TokenAndProviderFactoryInterface;
use Neos\Flow\Security\Authentication\TokenInterface;
use Neos\Flow\Security\Authorization\PrivilegeManagerInterface;
use Neos\Flow\Security\Context;
use Neos\Flow\Security\Cryptography\HashService;
use Neos\Flow\Security\Exception\NoSuchRoleException;
use Neos\Flow\Security\Policy\PolicyService;
use Neos\Flow\Security\Policy\Role;
use Neos\Flow\Utility\Now;
use Neos\Neos\Domain\Exception;
use Neos\Neos\Domain\Model\User;
use Neos\Neos\Domain\Repository\UserRepository;
use Neos\Neos\Service\PublishingService;
use Neos\Party\Domain\Model\AbstractParty;
use Neos\Party\Domain\Model\PersonName;
use Neos\Party\Domain\Repository\PartyRepository;
use Neos\Party\Domain\Service\PartyService;
use Neos\ContentRepository\Domain\Model\Workspace;
use Neos\ContentRepository\Domain\Repository\WorkspaceRepository;
use Neos\Neos\Utility\User as UserUtility;
/**
* A service for managing users
*
* @Flow\Scope("singleton")
* @api
*/
class UserService
{
/**
* Might be configurable in the future, for now centralising this as a "constant"
*
* @var string
*/
protected $defaultAuthenticationProviderName = 'Neos.Neos:Backend';
/**
* @Flow\Inject
* @var WorkspaceRepository
*/
protected $workspaceRepository;
/**
* @Flow\Inject
* @var PublishingService
*/
protected $publishingService;
/**
* @Flow\Inject
* @var PartyRepository
*/
protected $partyRepository;
/**
* @Flow\Inject
* @var UserRepository
*/
protected $userRepository;
/**
* @Flow\Inject
* @var PartyService
*/
protected $partyService;
/**
* @Flow\Inject
* @var AccountFactory
*/
protected $accountFactory;
/**
* @Flow\Inject
* @var AccountRepository
*/
protected $accountRepository;
/**
* @Flow\Inject
* @var PolicyService
*/
protected $policyService;
/**
* @Flow\Inject
* @var AuthenticationManagerInterface
*/
protected $authenticationManager;
/**
* @Flow\Inject
* @var PrivilegeManagerInterface
*/
protected $privilegeManager;
/**
* @Flow\Inject
* @var Context
*/
protected $securityContext;
/**
* @Flow\Inject
* @var TokenAndProviderFactoryInterface
*/
protected $tokenAndProviderFactory;
/**
* @Flow\Inject
* @var HashService
*/
protected $hashService;
/**
* @Flow\Inject
* @var PersistenceManagerInterface
*/
protected $persistenceManager;
/**
* @Flow\Inject(lazy = false)
* @var Now
*/
protected $now;
/**
* @var array
*/
protected $runtimeUserCache = [];
/**
* Retrieves a list of all existing users
*
* @return QueryResultInterface The users
* @api
*/
public function getUsers(): QueryResultInterface
{
return $this->userRepository->findAllOrderedByUsername();
}
/**
* @param string $searchTerm
* @return QueryResultInterface
*/
public function searchUsers(string $searchTerm): QueryResultInterface
{
return $this->userRepository->findBySearchTerm($searchTerm);
}
/**
* Retrieves an existing user by the given username
*
* @param string $username The username
* @param string $authenticationProviderName Name of the authentication provider to use. Example: "Neos.Neos:Backend"
* @return User The user, or null if the user does not exist
* @throws Exception
* @api
*/
public function getUser($username, $authenticationProviderName = null)
{
$authenticationProviderName = $authenticationProviderName ?: $this->defaultAuthenticationProviderName;
$cacheIdentifier = $authenticationProviderName . '~' . $username;
if (array_key_exists($cacheIdentifier, $this->runtimeUserCache)) {
$userIdentifier = $this->runtimeUserCache[$cacheIdentifier];
return $this->partyRepository->findByIdentifier($userIdentifier);
}
$user = $this->findUserForAccount($username, $authenticationProviderName);
if ($user instanceof AbstractParty) {
$userIdentifier = $this->persistenceManager->getIdentifierByObject($user);
}
if (isset($userIdentifier) && (string)$userIdentifier !== '') {
$this->runtimeUserCache[$cacheIdentifier] = $userIdentifier;
return $this->partyRepository->findByIdentifier($userIdentifier);
}
return null;
}
/**
* Returns the username of the given user
*
* Technically, this method will look for the user's backend account (or, if authenticationProviderName is specified,
* for the account matching the given authentication provider) and return the account's identifier.
*
* @param User $user
* @param string $authenticationProviderName
* @return string The username or null if the given user does not have a backend account
*/
public function getUsername(User $user, $authenticationProviderName = null)
{
$authenticationProviderName = $authenticationProviderName ?: $this->defaultAuthenticationProviderName;
foreach ($user->getAccounts() as $account) {
/** @var Account $account */
if ((string) $account->getAuthenticationProviderName() === $authenticationProviderName) {
return $account->getAccountIdentifier();
}
}
return null;
}
/**
* Returns the currently logged in user, if any
*
* @return User The currently logged in user, or null
* @api
*/
public function getCurrentUser()
{
if ($this->securityContext->canBeInitialized() === true) {
$account = $this->securityContext->getAccount();
if ($account !== null) {
return $this->getUser($account->getAccountIdentifier());
}
}
return null;
}
/**
* Creates a user based on the given information
*
* The created user and account are automatically added to their respective repositories and thus be persisted.
*
* @param string $username The username of the user to be created.
* @param string $password Password of the user to be created
* @param string $firstName First name of the user to be created
* @param string $lastName Last name of the user to be created
* @param array $roleIdentifiers A list of role identifiers to assign
* @param string $authenticationProviderName Name of the authentication provider to use. Example: "Neos.Neos:Backend"
* @return User The created user instance
* @api
*/
public function createUser($username, $password, $firstName, $lastName, array $roleIdentifiers = null, $authenticationProviderName = null)
{
$user = new User();
$name = new PersonName('', $firstName, '', $lastName, '', $username);
$user->setName($name);
return $this->addUser($username, $password, $user, $roleIdentifiers, $authenticationProviderName);
}
/**
* Adds a user whose User object has been created elsewhere
*
* This method basically "creates" a user like createUser() would, except that it does not create the User
* object itself. If you need to create the User object elsewhere, for example in your ActionController, make sure
* to call this method for registering the new user instead of adding it to the PartyRepository manually.
*
* This method also creates a new user workspace for the given user if no such workspace exist.
*
* @param string $username The username of the user to be created.
* @param string $password Password of the user to be created
* @param User $user The pre-built user object to start with
* @param array $roleIdentifiers A list of role identifiers to assign
* @param string $authenticationProviderName Name of the authentication provider to use. Example: "Neos.Neos:Backend"
* @return User The same user object
* @api
*/
public function addUser($username, $password, User $user, array $roleIdentifiers = null, $authenticationProviderName = null)
{
if ($roleIdentifiers === null) {
$roleIdentifiers = ['Neos.Neos:Editor'];
}
$roleIdentifiers = $this->normalizeRoleIdentifiers($roleIdentifiers);
$account = $this->accountFactory->createAccountWithPassword($username, $password, $roleIdentifiers, $authenticationProviderName ?: $this->defaultAuthenticationProviderName);
$this->partyService->assignAccountToParty($account, $user);
$this->partyRepository->add($user);
$this->accountRepository->add($account);
$this->createPersonalWorkspace($user, $account);
$this->emitUserCreated($user);
return $user;
}
/**
* Signals that a new user, including a new account has been created.
*
* @param User $user The created user
* @return void
* @Flow\Signal
* @api
*/
public function emitUserCreated(User $user)
{
}
/**
* Deletes the specified user and all remaining content in his personal workspaces
*
* @param User $user The user to delete
* @return void
* @throws Exception
* @api
*/
public function deleteUser(User $user)
{
foreach ($user->getAccounts() as $account) {
$this->securityContext->withoutAuthorizationChecks(function () use ($account) {
$this->deletePersonalWorkspace($account->getAccountIdentifier());
});
$this->accountRepository->remove($account);
}
$this->removeOwnerFromUsersWorkspaces($user);
$this->partyRepository->remove($user);
$this->emitUserDeleted($user);
}
/**
* Signals that the given user has been deleted.
*
* @param User $user The created user
* @return void
* @Flow\Signal
* @api
*/
public function emitUserDeleted(User $user)
{
}
/**
* Sets a new password for the given user
*
* This method will iterate over all accounts owned by the given user and, if the account uses a UsernamePasswordToken,
* sets a new password accordingly.
*
* @param User $user The user to set the password for
* @param string $password A new password
* @return void
* @api
*/
public function setUserPassword(User $user, $password)
{
$tokens = $this->tokenAndProviderFactory->getTokens();
$indexedTokens = [];
foreach ($tokens as $token) {
/** @var TokenInterface $token */
$indexedTokens[$token->getAuthenticationProviderName()] = $token;
}
foreach ($user->getAccounts() as $account) {
/** @var Account $account */
$authenticationProviderName = (string) $account->getAuthenticationProviderName();
if (isset($indexedTokens[$authenticationProviderName]) && $indexedTokens[$authenticationProviderName] instanceof UsernamePassword) {
$account->setCredentialsSource($this->hashService->hashPassword($password));
$this->accountRepository->update($account);
}
}
}
/**
* Updates the given user in the respective repository and potentially executes further actions depending on what
* has been changed.
*
* Note: changes to the user's account will not be committed for persistence. Please use addRoleToAccount(), removeRoleFromAccount(),
* setRolesForAccount() and setUserPassword() for changing account properties.
*
* @param User $user The modified user
* @return void
* @api
*/
public function updateUser(User $user)
{
$this->partyRepository->update($user);
$this->emitUserUpdated($user);
}
/**
* Adds the specified role to all accounts of the given user and potentially carries out further actions which are needed to
* properly reflect these changes.
*
* @param User $user The user to add roles to
* @param string $roleIdentifier A fully qualified role identifier, or a role identifier relative to the Neos.Neos namespace
* @return integer How often this role has been added to accounts owned by the user
* @api
*/
public function addRoleToUser(User $user, $roleIdentifier)
{
$counter = 0;
foreach ($user->getAccounts() as $account) {
$counter += $this->addRoleToAccount($account, $roleIdentifier);
}
return $counter;
}
/**
* Removes the specified role from all accounts of the given user and potentially carries out further actions which are needed to
* properly reflect these changes.
*
* @param User $user The user to remove roles from
* @param string $roleIdentifier A fully qualified role identifier, or a role identifier relative to the Neos.Neos namespace
* @return integer How often this role has been removed from accounts owned by the user
* @api
*/
public function removeRoleFromUser(User $user, $roleIdentifier)
{
$counter = 0;
foreach ($user->getAccounts() as $account) {
$counter += $this->removeRoleFromAccount($account, $roleIdentifier);
}
return $counter;
}
/**
* Signals that the given user data has been updated.
*
* @param User $user The created user
* @return void
* @Flow\Signal
* @api
*/
public function emitUserUpdated(User $user)
{
}
/**
* Overrides any assigned roles of the given account and potentially carries out further actions which are needed
* to properly reflect these changes.
*
* @param Account $account The account to assign the roles to
* @param array $newRoleIdentifiers A list of fully qualified role identifiers, or role identifiers relative to the Neos.Neos namespace
* @return void
* @api
*/
public function setRolesForAccount(Account $account, array $newRoleIdentifiers)
{
$currentRoles = $account->getRoles();
/** @var Role $role */
foreach ($currentRoles as $role) {
$roleIdentifier = $this->normalizeRoleIdentifier((string) $role);
if (!in_array($roleIdentifier, $newRoleIdentifiers)) {
$this->removeRoleFromAccount($account, $roleIdentifier);
}
}
foreach ($newRoleIdentifiers as $roleIdentifier) {
if (!$currentRoles->has(new Role($roleIdentifier))) {
$this->addRoleToAccount($account, $roleIdentifier);
}
}
}
/**
* Adds the specified role to the given account and potentially carries out further actions which are needed to
* properly reflect these changes.
*
* @param Account $account The account to add roles to
* @param string $roleIdentifier A fully qualified role identifier, or a role identifier relative to the Neos.Neos namespace
* @return integer How often this role has been added to the given account (effectively can be 1 or 0)
* @api
*/
public function addRoleToAccount(Account $account, $roleIdentifier)
{
$roleIdentifier = $this->normalizeRoleIdentifier($roleIdentifier);
$role = $this->policyService->getRole($roleIdentifier);
if (!$account->hasRole($role)) {
$account->addRole($role);
$this->accountRepository->update($account);
$this->emitRolesAdded($account, [$role]);
return 1;
}
return 0;
}
/**
* Signals that new roles have been assigned to the given account
*
* @param Account $account The account
* @param array<Role> An array of Role objects which have been added for that account
* @return void
* @Flow\Signal
* @api
*/
public function emitRolesAdded(Account $account, array $roles)
{
}
/**
* Removes the specified role from the given account and potentially carries out further actions which are needed to
* properly reflect these changes.
*
* @param Account $account The account to remove roles from
* @param string $roleIdentifier A fully qualified role identifier, or a role identifier relative to the Neos.Neos namespace
* @return integer How often this role has been removed from the given account (effectively can be 1 or 0)
* @api
*/
public function removeRoleFromAccount(Account $account, $roleIdentifier)
{
$roleIdentifier = $this->normalizeRoleIdentifier($roleIdentifier);
$role = $this->policyService->getRole($roleIdentifier);
/** @var Account $account */
if ($account->hasRole($role)) {
$account->removeRole($role);
$this->accountRepository->update($account);
$this->emitRolesRemoved($account, [$role]);
return 1;
}
return 0;
}
/**
* Signals that roles have been removed to the given account
*
* @param Account $account The account
* @param array<Role> An array of Role objects which have been removed
* @return void
* @Flow\Signal
* @api
*/
public function emitRolesRemoved(Account $account, array $roles)
{
}
/**
* Reactivates the given user
*
* @param User $user The user to activate
* @return void
* @api
*/
public function activateUser(User $user)
{
foreach ($user->getAccounts() as $account) {
/** @var Account $account */
$account->setExpirationDate(null);
$this->accountRepository->update($account);
}
$this->emitUserActivated($user);
}
/**
* Signals that the given user has been activated
*
* @param User $user The user
* @return void
* @Flow\Signal
* @api
*/
public function emitUserActivated(User $user)
{
}
/**
* Deactivates the given user
*
* @param User $user The user to deactivate
* @return void
* @api
*/
public function deactivateUser(User $user)
{
/** @var Account $account */
foreach ($user->getAccounts() as $account) {
$account->setExpirationDate(
\DateTime::createFromFormat(\DateTimeInterface::ATOM, $this->now->format(\DateTimeInterface::ATOM))
);
$this->accountRepository->update($account);
}
$this->emitUserDeactivated($user);
}
/**
* Checks if the current user may publish to the given workspace according to one the roles of the user's accounts
*
* In future versions, this logic may be implemented in Neos in a more generic way (for example, by means of an
* ACL object), but for now, this method exists in order to at least centralize and encapsulate the required logic.
*
* @param Workspace $workspace The workspace
* @return boolean
*/
public function currentUserCanPublishToWorkspace(Workspace $workspace)
{
if ($workspace->getName() === 'live') {
return $this->securityContext->hasRole('Neos.Neos:LivePublisher');
}
if ($workspace->getOwner() === $this->getCurrentUser() || $workspace->getOwner() === null) {
return true;
}
return false;
}
/**
* Checks if the current user may read the given workspace according to one the roles of the user's accounts
*
* In future versions, this logic may be implemented in Neos in a more generic way (for example, by means of an
* ACL object), but for now, this method exists in order to at least centralize and encapsulate the required logic.
*
* @param Workspace $workspace The workspace
* @return boolean
*/
public function currentUserCanReadWorkspace(Workspace $workspace)
{
if ($workspace->getName() === 'live') {
return true;
}
if ($workspace->getOwner() === $this->getCurrentUser() || $workspace->getOwner() === null) {
return true;
}
return false;
}
/**
* Checks if the current user may manage the given workspace according to one the roles of the user's accounts
*
* In future versions, this logic may be implemented in Neos in a more generic way (for example, by means of an
* ACL object), but for now, this method exists in order to at least centralize and encapsulate the required logic.
*
* @param Workspace $workspace The workspace
* @return boolean
*/
public function currentUserCanManageWorkspace(Workspace $workspace)
{
if ($workspace->isPersonalWorkspace()) {
return false;
}
if ($workspace->isInternalWorkspace()) {
return $this->privilegeManager->isPrivilegeTargetGranted('Neos.Neos:Backend.Module.Management.Workspaces.ManageInternalWorkspaces');
}
if ($workspace->isPrivateWorkspace() && $workspace->getOwner() === $this->getCurrentUser()) {
return $this->privilegeManager->isPrivilegeTargetGranted('Neos.Neos:Backend.Module.Management.Workspaces.ManageOwnWorkspaces');
}
if ($workspace->isPrivateWorkspace() && $workspace->getOwner() !== $this->getCurrentUser()) {
return $this->privilegeManager->isPrivilegeTargetGranted('Neos.Neos:Backend.Module.Management.Workspaces.ManageAllPrivateWorkspaces');
}
return false;
}
/**
* Checks if the current user may transfer ownership of the given workspace
*
* In future versions, this logic may be implemented in Neos in a more generic way (for example, by means of an
* ACL object), but for now, this method exists in order to at least centralize and encapsulate the required logic.
*
* @param Workspace $workspace The workspace
* @return boolean
*/
public function currentUserCanTransferOwnershipOfWorkspace(Workspace $workspace)
{
if ($workspace->isPersonalWorkspace()) {
return false;
}
// The privilege to manage shared workspaces is needed, because regular editors should not change ownerships
// of their internal workspaces, even if it was technically possible, because they wouldn't be able to change
// ownership back to themselves.
return $this->privilegeManager->isPrivilegeTargetGranted('Neos.Neos:Backend.Module.Management.Workspaces.ManageInternalWorkspaces');
}
/**
* Returns the default authentication provider name
*
* @return string
* @api
*/
public function getDefaultAuthenticationProviderName()
{
return $this->defaultAuthenticationProviderName;
}
/**
* Signals that the given user has been activated
*
* @param User $user The user
* @return void
* @Flow\Signal
* @api
*/
public function emitUserDeactivated(User $user)
{
}
/**
* Replaces role identifiers not containing a "." into fully qualified role identifiers from the Neos.Neos namespace.
*
* @param array $roleIdentifiers
* @return array
*/
protected function normalizeRoleIdentifiers(array $roleIdentifiers)
{
foreach ($roleIdentifiers as &$roleIdentifier) {
$roleIdentifier = $this->normalizeRoleIdentifier($roleIdentifier);
}
return $roleIdentifiers;
}
/**
* Replaces a role identifier not containing a "." into fully qualified role identifier from the Neos.Neos namespace.
*
* @param string $roleIdentifier
* @return string
* @throws NoSuchRoleException
*/
protected function normalizeRoleIdentifier($roleIdentifier)
{
if (strpos($roleIdentifier, ':') === false) {
$roleIdentifier = 'Neos.Neos:' . $roleIdentifier;
}
if (!$this->policyService->hasRole($roleIdentifier)) {
throw new NoSuchRoleException(sprintf('The role %s does not exist.', $roleIdentifier), 1422540184);
}
return $roleIdentifier;
}
/**
* Returns an array with all roles of a user's accounts, including parent roles, the "Everybody" role and the
* "AuthenticatedUser" role, assuming that the user is logged in.
*
* @param User $user The user
* @return array
*/
protected function getAllRoles(User $user)
{
$roles = [
'Neos.Flow:Everybody' => $this->policyService->getRole('Neos.Flow:Everybody'),
'Neos.Flow:AuthenticatedUser' => $this->policyService->getRole('Neos.Flow:AuthenticatedUser')
];
/** @var Account $account */
foreach ($user->getAccounts() as $account) {
$accountRoles = $account->getRoles();
/** @var $currentRole Role */
foreach ($accountRoles as $currentRole) {
if (!in_array($currentRole, $roles)) {
$roles[$currentRole->getIdentifier()] = $currentRole;
}
/** @var $currentParentRole Role */
foreach ($currentRole->getAllParentRoles() as $currentParentRole) {
if (!in_array($currentParentRole, $roles)) {
$roles[$currentParentRole->getIdentifier()] = $currentParentRole;
}
}
}
}
return $roles;
}
/**
* Creates a personal workspace for the given user's account if it does not exist already.
*
* @param User $user The new user to create a workspace for
* @param Account $account The user's backend account
* @throws IllegalObjectTypeException
*/
protected function createPersonalWorkspace(User $user, Account $account)
{
$userWorkspaceName = UserUtility::getPersonalWorkspaceNameForUsername($account->getAccountIdentifier());
$userWorkspace = $this->workspaceRepository->findByIdentifier($userWorkspaceName);
if ($userWorkspace === null) {
$liveWorkspace = $this->workspaceRepository->findByIdentifier('live');
if (!($liveWorkspace instanceof Workspace)) {
$liveWorkspace = new Workspace('live');
$liveWorkspace->setTitle('Live');
$this->workspaceRepository->add($liveWorkspace);
}
$userWorkspace = new Workspace($userWorkspaceName, $liveWorkspace, $user);
$userWorkspace->setTitle((string)$user->getName());
$this->workspaceRepository->add($userWorkspace);
}
}
/**
* Removes all personal workspaces of the given user's account if these workspaces exist. Also removes
* all possibly existing content of these workspaces.
*
* @param string $accountIdentifier Identifier of the user's account
* @return void
*/
protected function deletePersonalWorkspace($accountIdentifier)
{
$userWorkspace = $this->workspaceRepository->findByIdentifier(UserUtility::getPersonalWorkspaceNameForUsername($accountIdentifier));
if ($userWorkspace instanceof Workspace) {
$this->publishingService->discardAllNodes($userWorkspace);
$this->workspaceRepository->remove($userWorkspace);
}
}
/**
* Removes ownership of all workspaces currently owned by the given user
*
* @param User $user The user currently owning workspaces
* @return void
*/
protected function removeOwnerFromUsersWorkspaces(User $user)
{
/** @var Workspace $workspace */
foreach ($this->workspaceRepository->findByOwner($user) as $workspace) {
$workspace->setOwner(null);
$this->workspaceRepository->update($workspace);
}
}
/**
* @param string $username
* @param string $authenticationProviderName
* @return \Neos\Party\Domain\Model\AbstractParty|null
* @throws Exception
*/
protected function findUserForAccount($username, $authenticationProviderName)
{
$account = $this->accountRepository->findByAccountIdentifierAndAuthenticationProviderName($username, $authenticationProviderName ?: $this->defaultAuthenticationProviderName);
if ($account === null) {
return null;
}
$user = $this->partyService->getAssignedPartyOfAccount($account);
if (!$user instanceof User) {
throw new Exception(sprintf('Unexpected user type "%s". An account with the identifier "%s" exists, but the corresponding party is not a Neos User.', get_class($user), $username), 1422270948);
}
return $user;
}
}