|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Security\User; |
|
4
|
|
|
|
|
5
|
|
|
use App\Service\AlmaApi; |
|
6
|
|
|
use App\Service\AlmaUserData; |
|
7
|
|
|
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; |
|
8
|
|
|
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
|
9
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
10
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
|
11
|
|
|
use Psr\Log\LoggerInterface; |
|
12
|
|
|
|
|
13
|
|
|
class AlmaUserProvider implements UserProviderInterface |
|
14
|
|
|
{ |
|
15
|
|
|
private $api; |
|
16
|
|
|
private $userData; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(AlmaApi $api, AlmaUserData $userData, LoggerInterface $logger) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->api = $api; |
|
21
|
|
|
$this->userData = $userData; |
|
22
|
|
|
$this->logger = $logger; |
|
|
|
|
|
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function loadUserByUsername($username) |
|
26
|
|
|
{ |
|
27
|
|
|
try { |
|
28
|
|
|
$response = $this->api->findUserById($username); |
|
29
|
|
|
if ($this->userData->isValidUser($response)) { |
|
30
|
|
|
list($firstName, $lastName) = $this->userData->getFirstLastName($response); |
|
31
|
|
|
|
|
32
|
|
|
return new AlmaUser($username, array('ROLE_USER'), $firstName, $lastName); |
|
33
|
|
|
} |
|
34
|
|
|
} catch (\GuzzleHttp\Exception\GuzzleException $e) { |
|
35
|
|
|
$this->logger->error($e->getCode() . $e->getMessage()); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username)); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function refreshUser(UserInterface $user) |
|
42
|
|
|
{ |
|
43
|
|
|
if (!$user instanceof AlmaUser) { |
|
44
|
|
|
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user))); |
|
45
|
|
|
} |
|
46
|
|
|
return $user; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function supportsClass($class) |
|
50
|
|
|
{ |
|
51
|
|
|
return AlmaUser::class === $class; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|