|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Flowpack\Neos\FrontendLogin\Command; |
| 5 | + |
| 6 | +/* |
| 7 | + * This file is part of the Flowpack.Neos.FrontendLogin package. |
| 8 | + * |
| 9 | + * (c) Contributors of the Neos Project - www.neos.io |
| 10 | + * |
| 11 | + * This package is Open Source Software. For the full copyright and license |
| 12 | + * information, please view the LICENSE file which was distributed with this |
| 13 | + * source code. |
| 14 | + */ |
| 15 | + |
| 16 | +use Neos\Flow\Annotations as Flow; |
| 17 | +use Neos\Flow\Cli\CommandController; |
| 18 | +use Neos\Flow\Security\Account; |
| 19 | +use Neos\Neos\Domain\Exception as NeosDomainException; |
| 20 | +use Neos\Neos\Domain\Model\User; |
| 21 | +use Neos\Neos\Domain\Service\UserService; |
| 22 | + |
| 23 | +/** |
| 24 | + * The User Command Controller |
| 25 | + * |
| 26 | + * @Flow\Scope("singleton") |
| 27 | + */ |
| 28 | +class UserCommandController extends CommandController |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @var string |
| 32 | + */ |
| 33 | + static protected $authenticationProviderName = 'Flowpack.Neos.FrontendLogin:Frontend'; |
| 34 | + |
| 35 | + /** |
| 36 | + * @Flow\Inject |
| 37 | + * @var UserService |
| 38 | + */ |
| 39 | + protected $userService; |
| 40 | + |
| 41 | + /** |
| 42 | + * Delete expired (inactive) users having only an account with the Flowpack.Neos.FrontendLogin:Frontend provider |
| 43 | + * |
| 44 | + * @return void |
| 45 | + * @throws NeosDomainException |
| 46 | + */ |
| 47 | + public function deleteExpiredCommand(): void |
| 48 | + { |
| 49 | + $users = $this->findExpiredUsers(); |
| 50 | + if (empty($users)) { |
| 51 | + $this->outputLine('No expired users found.'); |
| 52 | + $this->quit(0); |
| 53 | + } |
| 54 | + |
| 55 | + foreach ($users as $user) { |
| 56 | + $username = $this->userService->getUsername($user, self::$authenticationProviderName); |
| 57 | + |
| 58 | + $this->userService->deleteUser($user); |
| 59 | + $this->outputLine('Deleted user "%s".', [$username]); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Find all expired users |
| 65 | + * |
| 66 | + * @return array<User> |
| 67 | + */ |
| 68 | + protected function findExpiredUsers(): array |
| 69 | + { |
| 70 | + return array_filter( |
| 71 | + $this->userService->getUsers()->toArray(), |
| 72 | + static function (User $user) { |
| 73 | + $accounts = $user->getAccounts(); |
| 74 | + if (count($accounts) === 1) { |
| 75 | + /** @var Account $account */ |
| 76 | + $account = $accounts->first(); |
| 77 | + return $account->isActive() === false && $account->getAuthenticationProviderName() === self::$authenticationProviderName; |
| 78 | + } |
| 79 | + return false; |
| 80 | + } |
| 81 | + ); |
| 82 | + } |
| 83 | +} |
0 commit comments