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('Application\Entity\UserEntity') |
41
|
|
|
->findOneBy(array( |
42
|
|
|
'username' => $username, |
43
|
|
|
)) |
44
|
|
|
; |
45
|
|
|
|
46
|
|
|
$userByEmail = $this->app['orm.em'] |
47
|
|
|
->getRepository('Application\Entity\UserEntity') |
48
|
|
|
->findOneBy(array( |
49
|
|
|
'email' => $username, |
50
|
|
|
)) |
51
|
|
|
; |
52
|
|
|
|
53
|
|
|
if ($userByUsername) { |
54
|
|
|
$user = $userByUsername; |
55
|
|
|
} elseif ($userByEmail) { |
56
|
|
|
$user = $userByEmail; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ( |
60
|
|
|
!$user && |
61
|
|
|
$showExceptionIfNotExistent |
62
|
|
|
) { |
63
|
|
|
throw new UsernameNotFoundException( |
64
|
|
|
sprintf( |
65
|
|
|
'Username or Email "%s" does not exist.', |
66
|
|
|
$username |
67
|
|
|
) |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $user; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $accessToken |
76
|
|
|
* @param bolean $throwExceptionIfNotExistent |
77
|
|
|
* |
78
|
|
|
* @return UserEntity |
79
|
|
|
* |
80
|
|
|
* @throws UsernameNotFoundException If user was not found |
81
|
|
|
*/ |
82
|
|
|
public function loadUserByAccessToken($accessToken, $throwExceptionIfNotExistent = true) |
83
|
|
|
{ |
84
|
|
|
$user = $this->app['orm.em'] |
85
|
|
|
->getRepository('Application\Entity\UserEntity') |
86
|
|
|
->findOneBy(array( |
87
|
|
|
'accessToken' => $accessToken, |
88
|
|
|
)) |
89
|
|
|
; |
90
|
|
|
|
91
|
|
|
if ( |
92
|
|
|
!$user && |
93
|
|
|
$throwExceptionIfNotExistent |
94
|
|
|
) { |
95
|
|
|
throw new UsernameNotFoundException( |
96
|
|
|
'A user with this access token was not found.' |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $user; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function refreshUser(UserInterface $user) |
104
|
|
|
{ |
105
|
|
|
if (!$user instanceof UserEntity) { |
106
|
|
|
throw new UnsupportedUserException( |
107
|
|
|
sprintf( |
108
|
|
|
'Instances of "%s" are not supported.', |
109
|
|
|
get_class($user) |
110
|
|
|
) |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this->loadUserByUsername($user->getUsername()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function supportsClass($class) |
118
|
|
|
{ |
119
|
|
|
return $class === 'Application\Entity\UserEntity'; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|