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