Completed
Pull Request — develop (#810)
by Robbie
04:45
created

LockCommand::execute()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 27
rs 8.439
cc 6
eloc 16
nc 7
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
use Symfony\Component\Console\Input\InputArgument;
9
10
class LockCommand extends AbstractAdminUserCommand
11
{
12
    /**
13
     * The number of days to lock for (by default)
14
     * @var int
15
     */
16
    const LIFETIME_DEFAULT = 31;
17
18
    /**
19
     * Setup
20
     *
21
     * @return void
22
     */
23
    protected function configure()
24
    {
25
        $this
26
            ->setName('admin:user:lock')
27
            ->addArgument('username', InputArgument::REQUIRED, 'Admin username to lock')
28
            ->addArgument('lifetime', InputArgument::OPTIONAL, 'Optional - lock lifetime in days (default one month)')
29
            ->setDescription(
30
                <<<HELP
31
Enforce a lock on an admin user account. Specify the username and an optional lifetime parameter in seconds.
32
HELP
33
            );
34
    }
35
36
    /**
37
     * @return bool
38
     */
39
    public function isEnabled()
40
    {
41
        return $this->getApplication()->isMagentoEnterprise();
42
    }
43
44
    /**
45
     * @param InputInterface  $input
46
     * @param OutputInterface $output
47
     *
48
     * @return void
49
     */
50
    protected function execute(InputInterface $input, OutputInterface $output)
51
    {
52
        $this->detectMagento($output, true);
53
        if (!$this->initMagento()) {
54
            return;
55
        }
56
57
        $username = $input->getArgument('username');
58
        $lifetime = $input->getArgument('lifetime') ?: $this->daysToSeconds(self::LIFETIME_DEFAULT);
59
60
        $user = \Mage::getModel('admin/user')->loadByUsername($username);
61
        if (!$user || !$user->getId()) {
62
            $output->writeln("<error>Couldn't find admin username '{$username}'</error>");
63
            return;
64
        }
65
66
        \Mage::getResourceModel('enterprise_pci/admin_user')->lock($user->getId(), 0, $lifetime);
67
68
        $lifetimeMessage = '';
69
        if ($input->getArgument('lifetime')) {
70
            $lifetimeMessage = sprintf(' for %d days.', $input->getArgument('lifetime'));
71
        }
72
73
        $output->writeln(
74
            sprintf('<info><comment>%s</comment> locked%s</info>', $username, $lifetimeMessage)
75
        );
76
    }
77
78
    /**
79
     * Convert a number of days to seconds for the lock lifetime parameter
80
     *
81
     * @param  int $days
82
     * @return int       Seconds
83
     */
84
    public function daysToSeconds($days)
85
    {
86
        return $days * 24 * 60 * 60;
87
    }
88
}
89