From 7e2f25d790571795d048e4fd4b1043f8516256ce Mon Sep 17 00:00:00 2001 From: Florian Wessels Date: Wed, 19 Dec 2018 18:47:17 +0100 Subject: [PATCH] [TASK] Migrate CommandController to Symfony Command --- ...mmandController.php => CleanUpCommand.php} | 180 +++++++++++------- Configuration/Commands.php | 8 + ext_localconf.php | 9 +- 3 files changed, 116 insertions(+), 81 deletions(-) rename Classes/Command/{CleanUpCommandController.php => CleanUpCommand.php} (55%) create mode 100644 Configuration/Commands.php diff --git a/Classes/Command/CleanUpCommandController.php b/Classes/Command/CleanUpCommand.php similarity index 55% rename from Classes/Command/CleanUpCommandController.php rename to Classes/Command/CleanUpCommand.php index 90ae7b25..8ada44e8 100644 --- a/Classes/Command/CleanUpCommandController.php +++ b/Classes/Command/CleanUpCommand.php @@ -18,13 +18,16 @@ use Bitmotion\Auth0\Domain\Model\Dto\EmAuth0Configuration; use Bitmotion\Auth0\Exception\InvalidApplicationException; use Bitmotion\Auth0\Utility\ApplicationUtility; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; use TYPO3\CMS\Core\Database\ConnectionPool; +use TYPO3\CMS\Core\Database\Query\QueryBuilder; use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction; use TYPO3\CMS\Core\Utility\GeneralUtility; -use TYPO3\CMS\Extbase\Mvc\Controller\CommandController; -use TYPO3\CMS\Extbase\Mvc\Exception\CommandException; -class CleanUpCommandController extends CommandController +class CleanUpCommand extends Command { /** * @var array @@ -38,7 +41,10 @@ class CleanUpCommandController extends CommandController /** * @var array */ - protected $tableNames = []; + protected $tableNames = [ + 'users' => 'be_users', + 'sessions' => 'be_sessions', + ]; /** * @var array @@ -48,7 +54,7 @@ class CleanUpCommandController extends CommandController /** * @var Application */ - protected $application = null; + protected $application; /** * @var string @@ -56,93 +62,102 @@ class CleanUpCommandController extends CommandController protected $method = ''; /** - * @throws CommandException + * @var OutputInterface */ - protected function initialize(string $method) + protected $output; + + /** + * @var EmAuth0Configuration + */ + protected $configuration; + + protected function configure() { - // Unknown method - if (!in_array($method, $this->allowedMethods)) { - $message = 'Unknown method: %s'; - $this->outputLine( - '' . $message . '', - [$method] - ); - throw new CommandException(sprintf($message, $method)); + $this->addArgument('method', InputArgument::REQUIRED, '"disable", "delete" or "deleteIrrevocable"'); + } + + /** + * @throws \TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException + * @throws \TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationPathDoesNotExistException + * @throws \Exception + * @return int|void|null + */ + public function execute(InputInterface $input, OutputInterface $output) + { + $this->output = $output; + + if (!$this->isInputValid($input)) { + $output->writeln(sprintf('Unknown method: %s', $input->getArgument('method'))); + + return; } - $this->initializeBackend(); + if (!$this->isBackendLoginEnabled()) { + $output->writeln(sprintf('Backend login is not enabled.')); + + return; + } - $this->method = $method; - $this->users = $this->getUsers(); + if (!$this->setAuth0Application()) { + $output->writeln('No Application found.'); - if (empty($this->users)) { - // Skip: no users found - $this->outputLine('No users found.'); + return; + } + + if ($this->setUsers()) { + $output->writeln('No users found.'); + } + + $userCount = $this->updateUsers(); + + if ($userCount > 0) { + $output->writeln(sprintf('Removed %i users from %s', $userCount, $this->tableNames['users'])); + } else { + $output->writeln(sprintf('No users removed for table %s.', $this->tableNames['users'])); } } - /** - * @throws CommandException - */ - protected function initializeBackend() + protected function isInputValid(InputInterface $input): bool { - $configuration = new EmAuth0Configuration(); - if ($configuration->getEnableBackendLogin() === false) { - $message = 'Backend login is not enabled.'; - $this->outputLine('' . $message . ''); - throw new CommandException($message); + if (!in_array($input->getArgument('method'), $this->allowedMethods)) { + return false; } - try { - $application = ApplicationUtility::getApplication($configuration->getBackendConnection()); - $this->application = $application; - } catch (InvalidApplicationException $exception) { - $message = 'No Application found.'; - $this->outputLine('' . $message . ''); - throw new CommandException($message); - } + $this->method = $input->getArgument('method'); - $this->tableNames = [ - 'users' => 'be_users', - 'sessions' => 'be_sessions', - ]; + return true; } /** - * @param string $method "disable", "delete" or "deleteIrrevocable" - * - * @throws CommandException - * @throws \Exception + * @throws \TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException + * @throws \TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationPathDoesNotExistException */ - public function cleanUpUsersCommand(string $method = 'disable') + protected function isBackendLoginEnabled() { - $this->initialize($method); - $management = GeneralUtility::makeInstance(ManagementApi::class, $this->application); - $userCount = 0; + $configuration = new EmAuth0Configuration(); - foreach ($this->users as $user) { - $auth0User = $management->getUserById($user['auth0_user_id']); - if (isset($auth0User['statusCode']) && $auth0User['statusCode'] === 404) { - $this->handleUser($user); - $this->clearSessionData($user); - $userCount++; - } + if ($configuration->getEnableBackendLogin() === false) { + return false; } - if ($userCount > 0) { - $this->outputLine( - 'Removed %i users from %s', - [$userCount, $this->tableNames['users']] - ); - } else { - $this->outputLine( - 'No users removed for table %s.', - [$this->tableNames['users']] - ); + $this->configuration = $configuration; + + return true; + } + + protected function setAuth0Application(): bool + { + try { + $application = ApplicationUtility::getApplication($this->configuration->getBackendConnection()); + $this->application = $application; + } catch (InvalidApplicationException $exception) { + return false; } + + return true; } - protected function getUsers(): array + protected function setUsers(): bool { $queryBuilder = $this->getQueryBuilder('users'); @@ -150,12 +165,14 @@ protected function getUsers(): array $queryBuilder->getRestrictions()->removeByType(HiddenRestriction::class); } - return $queryBuilder + $this->users = $queryBuilder ->select('uid', 'auth0_user_id') ->from($this->tableNames['users']) ->where($queryBuilder->expr()->neq('auth0_user_id', $queryBuilder->createNamedParameter(''))) ->execute() ->fetchAll(); + + return !empty($this->users); } protected function handleUser(array $user) @@ -184,10 +201,7 @@ protected function handleUser(array $user) ->execute(); } - /** - * @return \TYPO3\CMS\Core\Database\Query\QueryBuilder - */ - protected function getQueryBuilder(string $type) + protected function getQueryBuilder(string $type): QueryBuilder { return GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableNames[$type]); } @@ -204,4 +218,24 @@ protected function clearSessionData(array $user) ) )->execute(); } + + /** + * @throws \Exception + */ + protected function updateUsers(): int + { + $management = GeneralUtility::makeInstance(ManagementApi::class, $this->application); + $userCount = 0; + + foreach ($this->users as $user) { + $auth0User = $management->getUserById($user['auth0_user_id']); + if (isset($auth0User['statusCode']) && $auth0User['statusCode'] === 404) { + $this->handleUser($user); + $this->clearSessionData($user); + $userCount++; + } + } + + return $userCount; + } } diff --git a/Configuration/Commands.php b/Configuration/Commands.php new file mode 100644 index 00000000..7be50246 --- /dev/null +++ b/Configuration/Commands.php @@ -0,0 +1,8 @@ + [ + 'class' => \Bitmotion\Auth0\Command\CleanUpCommand::class, + ], +]; diff --git a/ext_localconf.php b/ext_localconf.php index f0c35db6..ec4113c2 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -64,11 +64,4 @@ 'exec' => '', 'className' => \Bitmotion\Auth0\Service\AuthenticationService::class ] -); - - -// Add CommandController -if (TYPO3_MODE === 'BE') { - $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][$_EXTKEY] = - \Bitmotion\Auth0\Command\CleanUpCommandController::class; -} \ No newline at end of file +); \ No newline at end of file