1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\User\Console;
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 Magento\User\Model\ResourceModel\User as AdminUser;
/**
* Command for unlocking an account.
*/
class UnlockAdminAccountCommand extends Command
{
const ARGUMENT_ADMIN_USERNAME = 'username';
const ARGUMENT_ADMIN_USERNAME_DESCRIPTION = 'The admin username to unlock';
const COMMAND_ADMIN_ACCOUNT_UNLOCK = 'admin:user:unlock';
const COMMAND_DESCRIPTION = 'Unlock Admin Account';
const USER_ID = 'user_id';
/**
* @var AdminUser
*/
private $adminUser;
/**
* {@inheritdoc}
*
* @param AdminUser $userResource
*/
public function __construct(
AdminUser $adminUser,
$name = null
) {
$this->adminUser = $adminUser;
parent::__construct($name);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$adminUserName = $input->getArgument(self::ARGUMENT_ADMIN_USERNAME);
$userData = $this->adminUser->loadByUsername($adminUserName);
$outputMessage = sprintf('Couldn\'t find the user account "%s"', $adminUserName);
if ($userData) {
if (isset($userData[self::USER_ID]) && $this->adminUser->unlock($userData[self::USER_ID])) {
$outputMessage = sprintf('The user account "%s" has been unlocked', $adminUserName);
} else {
$outputMessage = sprintf(
'The user account "%s" was not locked or could not be unlocked',
$adminUserName
);
}
}
$output->writeln('<info>' . $outputMessage . '</info>');
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName(self::COMMAND_ADMIN_ACCOUNT_UNLOCK);
$this->setDescription(self::COMMAND_DESCRIPTION);
$this->addArgument(
self::ARGUMENT_ADMIN_USERNAME,
InputArgument::REQUIRED,
self::ARGUMENT_ADMIN_USERNAME_DESCRIPTION
);
$this->setHelp(
<<<HELP
This command unlocks an admin account by its username.
To unlock:
<comment>%command.full_name% username</comment>
HELP
);
parent::configure();
}
}