UserStatusCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Biurad opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 *
17
 */
18
19
namespace Biurad\Security\Commands;
20
21
use Biurad\Security\Interfaces\UserStatusInterface;
22
use Biurad\Security\Interfaces\CredentialsHolderInterface;
23
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use Symfony\Component\Console\Input\InputArgument;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Output\ConsoleOutputInterface;
27
use Symfony\Component\Console\Output\OutputInterface;
28
use Symfony\Component\Console\Question\Question;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Question\Question was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
use Symfony\Component\Console\Style\SymfonyStyle;
30
use Symfony\Component\Security\Core\User\UserProviderInterface;
31
use Symfony\Component\Console\Exception\InvalidArgumentException;
32
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
33
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
34
35
/**
36
 * Get a user's status.
37
 *
38
 * @author Divine Niiquaye Ibok <[email protected]>
39
 */
40
class UserStatusCommand extends Command
41
{
42
    protected static $defaultName = 'security:user-status';
43
    private UserProviderInterface $provider;
44
45
    public function __construct(UserProviderInterface $provider)
46
    {
47
        $this->provider = $provider;
48
        parent::__construct();
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    protected function configure(): void
55
    {
56
        $this
57
            ->setDescription('Check a registered user\'s status')
58
            ->addArgument('user', InputArgument::OPTIONAL, 'The User\'s identity that can be access in the website')
59
            ->setHelp(
60
                <<<EOF
61
62
The <info>%command.name%</info> command shows a registered user's status according to your
63
security configuration. This command is mainly used to view user's account status.
64
65
Suppose that you want to check a user's status or for performing an action on the website:
66
67
<info>php %command.full_name% [username or email]</info>
68
69
EOF
70
            )
71
        ;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     *
77
     * @throws \Throwable
78
     */
79
    protected function execute(InputInterface $input, OutputInterface $output)
80
    {
81
        $errorIo = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
82
        $input->isInteractive() ? $errorIo->title('Biurad User Utility') : $errorIo->newLine();
83
84
        if (!$identity = $input->getArgument('user')) {
85
            if (!$input->isInteractive()) {
86
                $errorIo->error('The user\'s username or email should be provided and must not be empty.');
87
88
                return self::FAILURE;
89
            }
90
91
            $identityQuestion = $this->createIdentityQuestion();
92
            $identity = $errorIo->askQuestion($identityQuestion);
93
        }
94
95
        try {
96
            $user = $this->provider->loadUserByIdentifier($identity);
97
        } catch (UserNotFoundException $e) {
98
            $errorIo->note($e->getMessage());
99
100
            return self::FAILURE;
101
        }
102
103
        $rows = [
104
            ['Username', $user->getUserIdentifier()],
105
        ];
106
107
        if ($user instanceof CredentialsHolderInterface) {
108
            $rows[] = ['Fullname', $user->getFullName() ?? 'No record'];
109
            $rows[] = ['Email', $user->getEmail() ?? 'No record'];
110
            $rows[] = ['Password', $user->getPassword() ?? 'No record'];
111
            $rows[] = ['Status', $user->isEnabled() ? 'enabled' : 'disabled'];
112
        } elseif ($user instanceof PasswordAuthenticatedUserInterface) {
113
            $rows[] = ['Password', $user->getPassword() ?? 'No record'];
114
        }
115
116
        if ($user instanceof UserStatusInterface) {
117
            $rows[] = ['Created at', $user->getCreatedAt()->format('Y-m-d H:i:s')];
118
            $rows[] = ['Updated at', $user->getUpdatedAt() ? $user->getUpdatedAt()->format('Y-m-d H:i:s') : 'No record'];
119
            $rows[] = ['Last login', $user->getLastLogin() ? $user->getLastLogin()->format('Y-m-d H:i:s') : 'No record'];
120
            $rows[] = ['Location', $user->getLocation() ?? 'No record'];
121
            $rows[] = ['Locked', $user->isLocked() ? 'Yes' : 'No'];
122
        }
123
124
        $errorIo->table(['User', 'Credentials'], $rows);
125
126
        return self::SUCCESS;
127
    }
128
129
    /**
130
     * Create the user question to ask the user for the username or email to be encoded.
131
     */
132
    private function createIdentityQuestion(): Question
133
    {
134
        $identityQuestion = new Question('Type in a username or email to proceed');
135
136
        return $identityQuestion->setValidator(function ($value) {
137
            if ('' === \trim($value)) {
138
                throw new InvalidArgumentException('The user\'s identity must not be empty.');
139
            }
140
141
            return $value;
142
        })->setMaxAttempts(20);
143
    }
144
}
145