Completed
Push — master ( 4d471d...7714f4 )
by
unknown
318:27 queued 306:34
created

BaseProvider::refreshUser()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 5
nop 1
dl 0
loc 22
rs 9.568
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\User;
10
11
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
12
use eZ\Publish\API\Repository\PermissionResolver;
13
use eZ\Publish\API\Repository\UserService;
14
use eZ\Publish\Core\MVC\Symfony\Security\User;
15
use eZ\Publish\Core\MVC\Symfony\Security\UserInterface;
16
use eZ\Publish\Core\MVC\Symfony\Security\ReferenceUserInterface;
17
use eZ\Publish\API\Repository\Values\User\User as APIUser;
18
use eZ\Publish\Core\Repository\Values\User\UserReference;
19
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
20
use Symfony\Component\Security\Core\User\UserInterface as CoreUserInterface;
21
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
22
23
abstract class BaseProvider implements APIUserProviderInterface
24
{
25
    /** @var \eZ\Publish\API\Repository\PermissionResolver */
26
    protected $permissionResolver;
27
28
    /** @var \eZ\Publish\API\Repository\UserService */
29
    protected $userService;
30
31
    public function __construct(
32
        UserService $userService,
33
        PermissionResolver $permissionResolver
34
    ) {
35
        $this->permissionResolver = $permissionResolver;
36
        $this->userService = $userService;
37
    }
38
39
    public function refreshUser(CoreUserInterface $user)
40
    {
41
        if (!$user instanceof UserInterface) {
42
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
43
        }
44
45
        try {
46
            $refreshedAPIUser = $this->userService->loadUser(
47
                $user instanceof ReferenceUserInterface
48
                    ? $user->getAPIUserReference()->getUserId()
49
                    : $user->getAPIUser()->id
50
            );
51
            $user->setAPIUser($refreshedAPIUser);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\MVC\Symf...Interface::setAPIUser() has been deprecated with message: Will be replaced by {@link ReferenceUserInterface::getAPIUser()}, adding LogicException to signature.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
52
            $this->permissionResolver->setCurrentUserReference(
53
                new UserReference($refreshedAPIUser->getUserId())
54
            );
55
56
            return $user;
57
        } catch (NotFoundException $e) {
58
            throw new UsernameNotFoundException($e->getMessage(), 0, $e);
59
        }
60
    }
61
62
    /**
63
     * Whether this provider supports the given user class.
64
     *
65
     * @param string $class
66
     *
67
     * @return bool
68
     */
69
    public function supportsClass($class)
70
    {
71
        $supportedClass = 'eZ\\Publish\\Core\\MVC\\Symfony\\Security\\User';
72
73
        return $class === $supportedClass || is_subclass_of($class, $supportedClass);
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $supportedClass can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
74
    }
75
76
    /**
77
     * Loads a regular user object, usable by Symfony Security component, from a user object returned by Public API.
78
     *
79
     * @param \eZ\Publish\API\Repository\Values\User\User $apiUser
80
     *
81
     * @return \eZ\Publish\Core\MVC\Symfony\Security\User
82
     */
83
    public function loadUserByAPIUser(APIUser $apiUser)
84
    {
85
        return $this->createSecurityUser($apiUser);
86
    }
87
88
    /**
89
     * Creates user object, usable by Symfony Security component, from a user object returned by Public API.
90
     *
91
     * @param \eZ\Publish\API\Repository\Values\User\User $apiUser
92
     *
93
     * @return \eZ\Publish\Core\MVC\Symfony\Security\User
94
     */
95
    protected function createSecurityUser(APIUser $apiUser): User
96
    {
97
        return new User($apiUser, ['ROLE_USER']);
98
    }
99
}
100