Completed
Push — master ( 2b9271...11d3cd )
by
unknown
13:17
created

UserChecker::checkPreAuth()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 13
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\MVC\Symfony\Security;
10
11
use eZ\Publish\API\Repository\UserService;
12
use Symfony\Component\Security\Core\Exception\CredentialsExpiredException;
13
use Symfony\Component\Security\Core\Exception\DisabledException;
14
use Symfony\Component\Security\Core\User\UserCheckerInterface;
15
use Symfony\Component\Security\Core\User\UserInterface;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, eZ\Publish\Core\MVC\Symfony\Security\UserInterface.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
16
use eZ\Publish\Core\MVC\Symfony\Security\UserInterface as EzUserInterface;
17
18
final class UserChecker implements UserCheckerInterface
19
{
20
    /** @var \eZ\Publish\API\Repository\UserService */
21
    private $userService;
22
23
    public function __construct(UserService $userService)
24
    {
25
        $this->userService = $userService;
26
    }
27
28 View Code Duplication
    public function checkPreAuth(UserInterface $user): void
29
    {
30
        if (!$user instanceof EzUserInterface) {
31
            return;
32
        }
33
34
        if (!$user->getAPIUser()->enabled) {
35
            $exception = new DisabledException('User account is locked.');
36
            $exception->setUser($user);
37
38
            throw $exception;
39
        }
40
    }
41
42 View Code Duplication
    public function checkPostAuth(UserInterface $user): void
43
    {
44
        if (!$user instanceof EzUserInterface) {
45
            return;
46
        }
47
48
        if ($this->userService->getPasswordInfo($user->getAPIUser())->isPasswordExpired()) {
49
            $exception = new CredentialsExpiredException('User account has expired.');
50
            $exception->setUser($user);
51
52
            throw $exception;
53
        }
54
    }
55
}
56