Completed
Push — master ( 45cee5...7109cb )
by Maximilian
03:01
created

validateUserOnAuthentication()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 23
rs 8.5906
cc 6
eloc 15
nc 4
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sententiaregum project.
5
 *
6
 * (c) Maximilian Bosch <[email protected]>
7
 * (c) Ben Bieler <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace AppBundle\EventListener;
16
17
use AppBundle\Model\User\Provider\BlockedAccountReadInterface;
18
use AppBundle\Model\User\User;
19
use Ma27\ApiKeyAuthenticationBundle\Event\OnAuthenticationEvent;
20
use Ma27\ApiKeyAuthenticationBundle\Exception\CredentialException;
21
22
/**
23
 * Hook which observes the authentication and stops the authentication process if the user is
24
 * not approved or locked or blocked due to suspicious activity.
25
 *
26
 * @author Maximilian Bosch <[email protected]>
27
 */
28
class IncompleteUserCheckListener
29
{
30
    /**
31
     * @var BlockedAccountReadInterface
32
     */
33
    private $temporaryBlockedAccountProvider;
34
35
    /**
36
     * Constructor.
37
     *
38
     * @param BlockedAccountReadInterface $blockedAccountProvider
39
     */
40
    public function __construct(BlockedAccountReadInterface $blockedAccountProvider)
41
    {
42
        $this->temporaryBlockedAccountProvider = $blockedAccountProvider;
43
    }
44
45
    /**
46
     * Validates the user during the authentication process.
47
     *
48
     * @param OnAuthenticationEvent $event
49
     *
50
     * @throws CredentialException If the user is locked
51
     */
52
    public function validateUserOnAuthentication(OnAuthenticationEvent $event)
53
    {
54
        /** @var User $user */
55
        $user = $event->getUser();
56
57
        $isLocked      = $user->isLocked();
58
        $isNonApproved = $user->getActivationStatus() !== User::STATE_APPROVED;
59
60
        if ($isLocked || $isNonApproved || $this->temporaryBlockedAccountProvider->isAccountTemporaryBlocked($user->getId())) {
61
            switch (true) {
62
                case $isNonApproved:
63
                    $message = 'BACKEND_AUTH_NON_APPROVED';
64
                    break;
65
                case $isLocked:
66
                    $message = 'BACKEND_AUTH_LOCKED';
67
                    break;
68
                default:
69
                    $message = 'BACKEND_AUTH_BLOCKED';
70
            }
71
72
            throw new CredentialException($message);
73
        }
74
    }
75
}
76