1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkMultiUserBundle\Security; |
4
|
|
|
|
5
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\User; |
6
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\BaseUserRepositoryCollection; |
7
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
8
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
9
|
|
|
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; |
10
|
|
|
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
11
|
|
|
|
12
|
|
View Code Duplication |
class ObjectUserEmailProvider implements UserProviderInterface |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
/** @var BaseUserRepositoryCollection */ |
15
|
|
|
private $userRepositoryCollection; |
16
|
|
|
|
17
|
|
|
public function __construct(BaseUserRepositoryCollection $userRepositoryCollection) |
18
|
|
|
{ |
19
|
|
|
$this->userRepositoryCollection = $userRepositoryCollection; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $emailAddress |
24
|
|
|
* |
25
|
|
|
* @return User |
26
|
|
|
* |
27
|
|
|
* @throws UsernameNotFoundException |
28
|
|
|
*/ |
29
|
|
|
public function loadUserByUsername($emailAddress): User |
30
|
|
|
{ |
31
|
|
|
foreach ($this->userRepositoryCollection->all() as $repository) { |
32
|
|
|
$user = $repository->findByEmailAddress($emailAddress); |
33
|
|
|
|
34
|
|
|
if ($user instanceof User) { |
35
|
|
|
return $user; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// Since we are using the email as username we keep this exception since it is a part of symfony |
40
|
|
|
throw new UsernameNotFoundException( |
41
|
|
|
sprintf('Email "%s" does not exist.', $emailAddress) |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param UserInterface $user |
47
|
|
|
* |
48
|
|
|
* @return User |
49
|
|
|
* |
50
|
|
|
* @throws UnsupportedUserException |
51
|
|
|
*/ |
52
|
|
|
public function refreshUser(UserInterface $user): User |
53
|
|
|
{ |
54
|
|
|
if (!$this->supportsClass(get_class($user))) { |
55
|
|
|
throw new UnsupportedUserException( |
56
|
|
|
sprintf('Instances of "%s" are not supported.', get_class($user)) |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->loadUserByUsername($user->getEmail()); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function supportsClass($class): bool |
64
|
|
|
{ |
65
|
|
|
return $this->userRepositoryCollection->supportsClass($class); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.