1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* airlock authkey based consultant user provider |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\SecurityBundle\Authentication\Provider; |
7
|
|
|
|
8
|
|
|
use \Graviton\RestBundle\Model\ModelInterface; |
9
|
|
|
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; |
10
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
11
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class AirlockAuthenticationKeyConsultantProvider |
15
|
|
|
* |
16
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
17
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
18
|
|
|
* @link http://swisscom.ch |
19
|
|
|
*/ |
20
|
|
|
class AuthenticationProvider implements UserProviderInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var ModelInterface |
24
|
|
|
*/ |
25
|
|
|
private $documentModel; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var String |
29
|
|
|
*/ |
30
|
|
|
private $queryField; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param ModelInterface $model The documentModel |
34
|
|
|
* @param string $queryField Field to search by |
35
|
|
|
*/ |
36
|
2 |
|
public function __construct(ModelInterface $model, $queryField) |
37
|
|
|
{ |
38
|
2 |
|
$this->documentModel = $model; |
39
|
2 |
|
$this->queryField = $queryField; |
40
|
2 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Loads the user for the given username. |
44
|
|
|
* |
45
|
|
|
* This method must throw UsernameNotFoundException if the user is not |
46
|
|
|
* found. |
47
|
|
|
* |
48
|
|
|
* @param string $username the consultants username |
49
|
|
|
* |
50
|
|
|
* @return \Symfony\Component\Security\Core\User\UserInterface |
51
|
|
|
* |
52
|
|
|
* @see \Symfony\Component\Security\Core\Exception\UsernameNotFoundException |
53
|
|
|
* |
54
|
|
|
* @throws \Symfony\Component\Security\Core\Exception\UsernameNotFoundException if the user is not found |
55
|
|
|
*/ |
56
|
|
|
public function loadUserByUsername($username) |
57
|
|
|
{ |
58
|
|
|
if (!$this->queryField || !$username) { |
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$user = $this->documentModel->getRepository()->findOneBy( |
63
|
|
|
[ |
64
|
|
|
$this->queryField => new \MongoRegex('/^'.preg_quote($username).'$/i') |
65
|
|
|
] |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
return $user ? $user : false; |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Refreshes the user for the account interface. |
73
|
|
|
* |
74
|
|
|
* It is up to the implementation to decide if the user data should be |
75
|
|
|
* totally reloaded (e.g. from the database), or if the UserInterface |
76
|
|
|
* object can just be merged into some internal array of users / identity |
77
|
|
|
* map. |
78
|
|
|
* |
79
|
|
|
* @param \Symfony\Component\Security\Core\User\UserInterface $user user to refresh |
80
|
|
|
* |
81
|
|
|
* @return \Symfony\Component\Security\Core\User\UserInterface |
82
|
|
|
* |
83
|
|
|
* @throws \Symfony\Component\Security\Core\Exception\UnsupportedUserException if the account is not supported |
84
|
|
|
*/ |
85
|
|
|
public function refreshUser(UserInterface $user) |
86
|
|
|
{ |
87
|
|
|
// this is used for storing authentication in the session |
88
|
|
|
// but in this example, the token is sent in each request, |
89
|
|
|
// so authentication can be stateless. Throwing this exception |
90
|
|
|
// is proper to make things stateless |
91
|
|
|
throw new UnsupportedUserException(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Whether this provider supports the given user class. |
96
|
|
|
* |
97
|
|
|
* @param string $class class to check for support |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public function supportsClass($class) |
102
|
|
|
{ |
103
|
|
|
return $class instanceof \Symfony\Component\Security\Core\User\UserInterface; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|