Completed
Pull Request — develop (#813)
by Robbie
12:23 queued 07:38
created

UnlockCommand::execute()   C

Complexity

Conditions 9
Paths 13

Size

Total Lines 49
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 49
rs 5.7446
cc 9
eloc 29
nc 13
nop 2
1
<?php
2
3
namespace N98\Magento\Command\Admin\User;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
9
class UnlockCommand extends AbstractAdminUserCommand
10
{
11
    /**
12
     * Setup
13
     *
14
     * @return void
15
     */
16
    protected function configure()
17
    {
18
        $this
19
            ->setName('admin:user:unlock')
20
            ->addArgument(
21
                'username',
22
                \Symfony\Component\Console\Input\InputArgument::OPTIONAL,
23
                'Admin Username to Unlock'
24
            )
25
            ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Dry run mode')
26
            ->setDescription('Release lock on admin user for one or all users');
27
    }
28
29
    /**
30
     * @return bool
31
     */
32
    public function isEnabled()
33
    {
34
        return $this->getApplication()->isMagentoEnterprise();
35
    }
36
37
    /**
38
     * @param InputInterface  $input
39
     * @param OutputInterface $output
40
     *
41
     * @return void
42
     */
43
    protected function execute(InputInterface $input, OutputInterface $output)
44
    {
45
        $this->detectMagento($output, true);
46
        if ($this->initMagento()) {
47
            if ($dryrun = $input->getOption('dry-run')) {
48
                $output->writeln('<info>Dry run mode enabled.</info>');
49
            }
50
51
            // Unlock a single admin account
52
            if ($username = $input->getArgument('username')) {
53
                $user = \Mage::getModel('admin/user')->loadByUsername($username);
54
                if (!$user || !$user->getId()) {
55
                    $output->writeln('<error>Couldn\'t find admin ' . $username . '</error>');
56
                    return;
57
                }
58
                \Mage::getResourceModel('enterprise_pci/admin_user')->unlock($user->getId());
59
                $output->writeln('<info><comment>' . $username . '</comment> unlocked</info>');
60
                return;
61
            }
62
63
            // Unlock all admin accounts
64
            $userIds = \Mage::getModel('admin/user')->getCollection()->getAllIds();
65
66
            if (empty($userIds)) {
67
                $output->writeln('<error>No admin users found.</error>');
68
                return;
69
            }
70
71
            /** @var $dialog \Symfony\Component\Console\Helper\DialogHelper */
72
            $dialog = $this->getHelperSet()->get('dialog');
73
            $shouldUnlockAll = $dialog->askConfirmation(
74
                $output,
75
                sprintf(
76
                    '<question>Really unlock all %d admin users?</question> <comment>[n]</comment>: ',
77
                    count($userIds)
78
                ),
79
                false
80
            );
81
82
            if ($shouldUnlockAll) {
83
                if (!$dryrun) {
84
                    \Mage::getResourceModel('enterprise_pci/admin_user')->unlock($userIds);
85
                }
86
                $output->writeln(
87
                    sprintf('<info><comment>All %d admin users</comment> unlocked</info>', count($userIds))
88
                );
89
            }
90
        }
91
    }
92
}
93