1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the vseth-semesterly-reports project. |
5
|
|
|
* |
6
|
|
|
* (c) Florian Moser <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace App\Security; |
13
|
|
|
|
14
|
|
|
use App\Entity\Organisation; |
15
|
|
|
use App\Model\User; |
16
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
17
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
18
|
|
|
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; |
19
|
|
|
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
20
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
21
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
22
|
|
|
|
23
|
|
|
class UserProvider implements UserProviderInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var ManagerRegistry |
27
|
|
|
*/ |
28
|
|
|
private $registry; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $adminPassword; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* PasswordContainerProvider constructor. |
37
|
|
|
*/ |
38
|
|
|
public function __construct(ManagerRegistry $registry, ParameterBagInterface $parameterBag) |
39
|
|
|
{ |
40
|
|
|
$this->registry = $registry; |
41
|
|
|
$this->adminPassword = $parameterBag->get('ADMIN_PASSWORD'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Refreshes the user for the account interface. |
46
|
|
|
* |
47
|
|
|
* It is up to the implementation to decide if the user data should be |
48
|
|
|
* totally reloaded (e.g. from the database), or if the UserInterface |
49
|
|
|
* object can just be merged into some internal array of users / identity |
50
|
|
|
* map. |
51
|
|
|
* |
52
|
|
|
* @throws UnsupportedUserException if the account is not supported |
53
|
|
|
* |
54
|
|
|
* @return UserInterface |
55
|
|
|
*/ |
56
|
|
|
public function refreshUser(UserInterface $user) |
57
|
|
|
{ |
58
|
|
|
if (!$user instanceof User) { |
59
|
|
|
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user))); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this->loadUserByUsername($user->getUsername()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Loads the user for the given username. |
67
|
|
|
* |
68
|
|
|
* This method must throw UsernameNotFoundException if the user is not |
69
|
|
|
* found. |
70
|
|
|
* |
71
|
|
|
* @param string $username The username |
72
|
|
|
* |
73
|
|
|
* @throws UsernameNotFoundException if the user is not found |
74
|
|
|
* |
75
|
|
|
* @return UserInterface |
76
|
|
|
*/ |
77
|
|
|
public function loadUserByUsername($username) |
78
|
|
|
{ |
79
|
|
|
if ($username === '[email protected]') { |
80
|
|
|
return new User($this->adminPassword, $username, ['ROLE_ADMIN']); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** @var Organisation|null $organisation */ |
84
|
|
|
$organisation = $this->registry->getRepository(Organisation::class)->findOneBy(['email' => $username]); |
85
|
|
|
if (null !== $organisation) { |
86
|
|
|
return new User($organisation->getAuthenticationCode(), $organisation->getEmail(), ['ROLE_ORGANISATION']); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
throw new UsernameNotFoundException(sprintf('Username "%s" does not exist in UserProvider.', $username)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Whether this provider supports the given user class. |
94
|
|
|
* |
95
|
|
|
* @param string $class |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
public function supportsClass($class) |
100
|
|
|
{ |
101
|
|
|
return User::class === $class; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|