Skip to content

Commit

Permalink
Issue 482: Add a command to change the admin user status
Browse files Browse the repository at this point in the history
  • Loading branch information
melvinversluijs authored and cmuench committed Oct 31, 2020
1 parent a212846 commit ba4fc7c
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 1 deletion.
14 changes: 13 additions & 1 deletion README.md
Expand Up @@ -968,4 +968,16 @@ Toggles the status for a CMS block based on the given Block identifier.
```sh
n98-magerun2.phar cms:block:toggle [blockId]
```
```
### Change Admin user status
Changes the admin user based on the options, the command will toggle
the status if no options are supplied.
```sh
n98-magerun2.phar admin:user:change-status [user] [--activate] [--deactivate]
```
_Note: It is possible for a user to exist with a username that matches
the email of a different user. In this case the first matched user will be changed._
1 change: 1 addition & 0 deletions config.yaml
Expand Up @@ -52,6 +52,7 @@ commands:
- N98\Magento\Command\Admin\User\ListCommand
- N98\Magento\Command\Admin\User\DeleteUserCommand
- N98\Magento\Command\Admin\User\ChangePasswordCommand
- N98\Magento\Command\Admin\User\ChangeStatusCommand
- N98\Magento\Command\Admin\Token\CreateCommand
- N98\Magento\Command\Cache\CleanCommand
- N98\Magento\Command\Cache\DisableCommand
Expand Down
109 changes: 109 additions & 0 deletions src/N98/Magento/Command/Admin/User/ChangeStatusCommand.php
@@ -0,0 +1,109 @@
<?php

declare(strict_types=1);

namespace N98\Magento\Command\Admin\User;

use Magento\User\Model\User;
use Magento\User\Model\ResourceModel\User as UserResourceModel;
use Magento\User\Model\ResourceModel\User\CollectionFactory;
use N98\Magento\Command\AbstractMagentoCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

use function is_string;
use function sprintf;

/**
* Class ToggleBlockCommand
* @package N98\Magento\Command\Admin\User
*/
class ChangeStatusCommand extends AbstractMagentoCommand
{
protected const USER_ARGUMENT = 'user';
protected const ACTIVATE_OPTION = 'activate';
protected const DEACTIVATE_OPTION = 'deactivate';

protected $userResourceModel;
protected $userCollectionFactory;

public function inject(UserResourceModel $userResourceModel, CollectionFactory $userCollectionFactory): void
{
$this->userResourceModel = $userResourceModel;
$this->userCollectionFactory = $userCollectionFactory;
}

protected function configure(): void
{
$this
->setName('admin:user:change-status')
->addArgument(self::USER_ARGUMENT, InputArgument::REQUIRED, 'Username or email for the admin user')
->addOption(self::ACTIVATE_OPTION, null, InputOption::VALUE_NONE, 'Activate the user')
->addOption(self::DEACTIVATE_OPTION, null, InputOption::VALUE_NONE, 'Deactivate the user')
->setDescription(
'Set the status of an admin user, if no status is given the status will be toggled.
Note: the first found user is edited (since it is possible to use someone else\'s email as your
username, although you should try to avoid this scenario).'
);
}

protected function execute(InputInterface $input, OutputInterface $output): void
{
$this->detectMagento($output, true);
if (!$this->initMagento()) {
return;
}

$username = $input->getArgument(self::USER_ARGUMENT);
if (!is_string($username)) {
$output->writeln('Please provide an username or email for the admin user. Use --help for more information.');
return;
}

$user = $this->getUser($username);
if ($user === null) {
$output->writeln(sprintf('Could not find a user associated to <info>%s</info>.', $username));
return;
}

$newStatus = $this->getNewStatusForUser($user, $input);
$user->setIsActive($newStatus);
$this->userResourceModel->save($user);
$output->writeln(sprintf('User has been <info>%s</info>.', $newStatus ? 'activated' : 'deactivated'));
}

protected function getUser(string $username): ?User
{
$collection = $this->userCollectionFactory->create();
// Get the user where either the username or the email matches.
$collection->addFieldToFilter(
[
'username',
'email',
],
[
$username,
$username,
]
);
$collection->getItems();
$user = $collection->getFirstItem();
return $user->getUserId() !== null ? $user : null;
}

protected function getNewStatusForUser(User $user, InputInterface $input): bool
{
if ($input->getOption(self::ACTIVATE_OPTION) === true) {
return true;
}

if($input->getOption(self::DEACTIVATE_OPTION) === true) {
return false;
}

// If no option is supplied, toggle the status.
return !$user->getIsActive();
}
}

0 comments on commit ba4fc7c

Please sign in to comment.