Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 482: Add a command to change the admin user status
- Loading branch information
1 parent
a212846
commit ba4fc7c
Showing
3 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/N98/Magento/Command/Admin/User/ChangeStatusCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |