GenericUserChecker   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 46
ccs 23
cts 23
cp 1
rs 10
c 1
b 0
f 0
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A checkPostAuth() 0 8 3
B checkPreAuth() 0 26 7
1
<?php
2
3
//----------------------------------------------------------------------
4
//
5
//  Copyright (C) 2018-2020 Artem Rodygin
6
//
7
//  You should have received a copy of the MIT License along with
8
//  this file. If not, see <http://opensource.org/licenses/MIT>.
9
//
10
//----------------------------------------------------------------------
11
12
namespace LazySec\Checker;
13
14
use LazySec\Entity\DisableAccountTrait;
15
use LazySec\Entity\ExpireAccountTrait;
16
use LazySec\Entity\ExpirePasswordTrait;
17
use LazySec\Entity\LockAccountTrait;
18
use Symfony\Component\Security\Core\Exception\AccountExpiredException;
19
use Symfony\Component\Security\Core\Exception\CredentialsExpiredException;
20
use Symfony\Component\Security\Core\Exception\DisabledException;
21
use Symfony\Component\Security\Core\Exception\LockedException;
22
use Symfony\Component\Security\Core\User\UserCheckerInterface;
23
use Symfony\Component\Security\Core\User\UserInterface;
24
25
/**
26
 * Generic user checker which autodetects included LazySec traits.
27
 */
28
class GenericUserChecker implements UserCheckerInterface
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33 4
    public function checkPreAuth(UserInterface $user)
34
    {
35 4
        if (in_array(LockAccountTrait::class, class_uses($user), true)) {
36
            /** @var LockAccountTrait|UserInterface $user */
37 4
            if (!$user->isAccountNonLocked()) {
38 1
                $exception = new LockedException('User account is locked.');
39 1
                $exception->setUser($user);
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type LazySec\Entity\LockAccountTrait; however, parameter $user of Symfony\Component\Securi...tusException::setUser() does only seem to accept Symfony\Component\Security\Core\User\UserInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
                $exception->setUser(/** @scrutinizer ignore-type */ $user);
Loading history...
40 1
                throw $exception;
41
            }
42
        }
43
44 3
        if (in_array(DisableAccountTrait::class, class_uses($user), true)) {
45
            /** @var DisableAccountTrait|UserInterface $user */
46 3
            if (!$user->isEnabled()) {
47 1
                $exception = new DisabledException('User account is disabled.');
48 1
                $exception->setUser($user);
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type LazySec\Entity\DisableAccountTrait; however, parameter $user of Symfony\Component\Securi...tusException::setUser() does only seem to accept Symfony\Component\Security\Core\User\UserInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
                $exception->setUser(/** @scrutinizer ignore-type */ $user);
Loading history...
49 1
                throw $exception;
50
            }
51
        }
52
53 2
        if (in_array(ExpireAccountTrait::class, class_uses($user), true)) {
54
            /** @var ExpireAccountTrait|UserInterface $user */
55 2
            if (!$user->isAccountNonExpired()) {
56 1
                $exception = new AccountExpiredException('User account has expired.');
57 1
                $exception->setUser($user);
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type LazySec\Entity\ExpireAccountTrait; however, parameter $user of Symfony\Component\Securi...tusException::setUser() does only seem to accept Symfony\Component\Security\Core\User\UserInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
                $exception->setUser(/** @scrutinizer ignore-type */ $user);
Loading history...
58 1
                throw $exception;
59
            }
60
        }
61 1
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 2
    public function checkPostAuth(UserInterface $user)
67
    {
68 2
        if (in_array(ExpirePasswordTrait::class, class_uses($user), true)) {
69
            /** @var ExpirePasswordTrait|UserInterface $user */
70 2
            if (!$user->isCredentialsNonExpired()) {
71 1
                $exception = new CredentialsExpiredException('User credentials have expired.');
72 1
                $exception->setUser($user);
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type LazySec\Entity\ExpirePasswordTrait; however, parameter $user of Symfony\Component\Securi...tusException::setUser() does only seem to accept Symfony\Component\Security\Core\User\UserInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
                $exception->setUser(/** @scrutinizer ignore-type */ $user);
Loading history...
73 1
                throw $exception;
74
            }
75
        }
76 1
    }
77
}
78