|
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); |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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
|
|
|
|
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.