1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Application\Provider; |
4
|
|
|
|
5
|
|
|
use Application\Entity\UserEntity; |
6
|
|
|
use Silex\Application; |
7
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
8
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
9
|
|
|
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
10
|
|
|
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Borut Balažek <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class UsersProvider implements UserProviderInterface |
16
|
|
|
{ |
17
|
|
|
private $app; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param Application $app |
21
|
|
|
*/ |
22
|
|
|
public function __construct(Application $app) |
23
|
|
|
{ |
24
|
|
|
$this->app = $app; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $username |
29
|
|
|
* @param bolean $showExceptionIfNotExistent |
30
|
|
|
* |
31
|
|
|
* @return UserEntity |
32
|
|
|
* |
33
|
|
|
* @throws UsernameNotFoundException If user was not found |
34
|
|
|
*/ |
35
|
|
|
public function loadUserByUsername($username, $showExceptionIfNotExistent = true) |
36
|
|
|
{ |
37
|
|
|
$user = null; |
38
|
|
|
|
39
|
|
|
$userByUsername = $this->app['orm.em'] |
40
|
|
|
->getRepository( |
41
|
|
|
'Application\Entity\UserEntity' |
42
|
|
|
) |
43
|
|
|
->findOneBy(array( |
44
|
|
|
'username' => $username, |
45
|
|
|
)) |
46
|
|
|
; |
47
|
|
|
|
48
|
|
|
$userByEmail = $this->app['orm.em'] |
49
|
|
|
->getRepository( |
50
|
|
|
'Application\Entity\UserEntity' |
51
|
|
|
) |
52
|
|
|
->findOneBy(array( |
53
|
|
|
'email' => $username, |
54
|
|
|
)) |
55
|
|
|
; |
56
|
|
|
|
57
|
|
|
if ($userByUsername) { |
58
|
|
|
$user = $userByUsername; |
59
|
|
|
} elseif ($userByEmail) { |
60
|
|
|
$user = $userByEmail; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (!$user && $showExceptionIfNotExistent) { |
64
|
|
|
throw new UsernameNotFoundException( |
65
|
|
|
sprintf( |
66
|
|
|
'Username or Email "%s" does not exist.', |
67
|
|
|
$username |
68
|
|
|
) |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $user; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function refreshUser(UserInterface $user) |
76
|
|
|
{ |
77
|
|
|
if (!$user instanceof UserEntity) { |
78
|
|
|
throw new UnsupportedUserException( |
79
|
|
|
sprintf( |
80
|
|
|
'Instances of "%s" are not supported.', |
81
|
|
|
get_class($user) |
82
|
|
|
) |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->loadUserByUsername($user->getUsername()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function supportsClass($class) |
90
|
|
|
{ |
91
|
|
|
return $class === 'Application\Entity\UserEntity'; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|