Skip to content

Commit

Permalink
[TASK] Migrate CommandController to Symfony Command
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Wessels committed Dec 19, 2018
1 parent c4294e7 commit 7e2f25d
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,7 +41,10 @@ class CleanUpCommandController extends CommandController
/**
* @var array
*/
protected $tableNames = [];
protected $tableNames = [
'users' => 'be_users',
'sessions' => 'be_sessions',
];

/**
* @var array
Expand All @@ -48,114 +54,125 @@ class CleanUpCommandController extends CommandController
/**
* @var Application
*/
protected $application = null;
protected $application;

/**
* @var string
*/
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(
'<error>' . $message . '</error>',
[$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('<error>Unknown method: %s</error>', $input->getArgument('method')));

return;
}

$this->initializeBackend();
if (!$this->isBackendLoginEnabled()) {
$output->writeln(sprintf('<error>Backend login is not enabled.</error>'));

return;
}

$this->method = $method;
$this->users = $this->getUsers();
if (!$this->setAuth0Application()) {
$output->writeln('<error>No Application found.</error>');

if (empty($this->users)) {
// Skip: no users found
$this->outputLine('<info>No users found.</info>');
return;
}

if ($this->setUsers()) {
$output->writeln('<info>No users found.</info>');
}

$userCount = $this->updateUsers();

if ($userCount > 0) {
$output->writeln(sprintf('<info>Removed %i users from %s</info>', $userCount, $this->tableNames['users']));
} else {
$output->writeln(sprintf('<info>No users removed for table %s.</info>', $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('<error>' . $message . '</error>');
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('<error>' . $message . '</error>');
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(
'<info>Removed %i users from %s</info>',
[$userCount, $this->tableNames['users']]
);
} else {
$this->outputLine(
'<info>No users removed for table %s.</info>',
[$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');

if ($this->method === 'delete') {
$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)
Expand Down Expand Up @@ -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]);
}
Expand All @@ -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;
}
}
8 changes: 8 additions & 0 deletions Configuration/Commands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);

return [
'auth0:cleanupusers' => [
'class' => \Bitmotion\Auth0\Command\CleanUpCommand::class,
],
];
9 changes: 1 addition & 8 deletions ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
);

0 comments on commit 7e2f25d

Please sign in to comment.