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 Graviton\SecurityBundle\Entities\SecurityContract; |
10
|
|
|
use GravitonDyn\ContractBundle\Document\Contract; |
11
|
|
|
use \Graviton\RestBundle\Model\ModelInterface; |
12
|
|
|
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; |
13
|
|
|
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
14
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
15
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class AirlockAuthenticationKeyUserProvider |
19
|
|
|
* |
20
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
21
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
22
|
|
|
* @link http://swisscom.ch |
23
|
|
|
*/ |
24
|
|
|
class AirlockAuthenticationKeyConsultantProvider implements UserProviderInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var \Graviton\RestBundle\Model\ModelInterface |
28
|
|
|
*/ |
29
|
|
|
private $documentModel; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param \Graviton\RestBundle\Model\ModelInterface $contract contract to use as documentModel |
33
|
|
|
*/ |
34
|
|
|
public function __construct(ModelInterface $contract) |
35
|
|
|
{ |
36
|
|
|
$this->documentModel = $contract; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Loads the user for the given username. |
41
|
|
|
* |
42
|
|
|
* This method must throw UsernameNotFoundException if the user is not |
43
|
|
|
* found. |
44
|
|
|
* |
45
|
|
|
* @param string $username the consultants username |
46
|
|
|
* |
47
|
|
|
* @return \Symfony\Component\Security\Core\User\UserInterface |
48
|
|
|
* |
49
|
|
|
* @see \Symfony\Component\Security\Core\Exception\UsernameNotFoundException |
50
|
|
|
* |
51
|
|
|
* @throws \Symfony\Component\Security\Core\Exception\UsernameNotFoundException if the user is not found |
52
|
|
|
*/ |
53
|
|
|
public function loadUserByUsername($username) |
54
|
|
|
{ |
55
|
|
|
$user = $this->documentModel->getRepository()->findOneBy(array('username' => $username)); |
56
|
|
|
|
57
|
|
|
if($user) { |
58
|
|
|
return new SecurityConsultant($user, array('ROLE_GRAVITON_USER')); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
throw new UsernameNotFoundException(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Finds a contract based on the provided ApiKey. |
66
|
|
|
* |
67
|
|
|
* @param string $apiKey key from airlock |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public function getUsernameForApiKey($apiKey) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$contractId = ''; |
74
|
|
|
/** @var \GravitonDyn\ContractBundle\Document\Contract $contract */ |
75
|
|
|
$contract = $this->documentModel->getRepository()->findOneBy(array('number' => $apiKey)); |
76
|
|
|
if ($contract instanceof Contract) { |
|
|
|
|
77
|
|
|
$contractId = $contract->getId(); |
78
|
|
|
} |
79
|
|
|
return $contractId; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Refreshes the user for the account interface. |
84
|
|
|
* |
85
|
|
|
* It is up to the implementation to decide if the user data should be |
86
|
|
|
* totally reloaded (e.g. from the database), or if the UserInterface |
87
|
|
|
* object can just be merged into some internal array of users / identity |
88
|
|
|
* map. |
89
|
|
|
* |
90
|
|
|
* @param \Symfony\Component\Security\Core\User\UserInterface $user user to refresh |
91
|
|
|
* |
92
|
|
|
* @return \Symfony\Component\Security\Core\User\UserInterface |
93
|
|
|
* |
94
|
|
|
* @throws \Symfony\Component\Security\Core\Exception\UnsupportedUserException if the account is not supported |
95
|
|
|
*/ |
96
|
|
|
public function refreshUser(UserInterface $user) |
97
|
|
|
{ |
98
|
|
|
// this is used for storing authentication in the session |
99
|
|
|
// but in this example, the token is sent in each request, |
100
|
|
|
// so authentication can be stateless. Throwing this exception |
101
|
|
|
// is proper to make things stateless |
102
|
|
|
throw new UnsupportedUserException(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Whether this provider supports the given user class. |
107
|
|
|
* |
108
|
|
|
* @param string $class class to check for support |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
public function supportsClass($class) |
113
|
|
|
{ |
114
|
|
|
return $class instanceof \Symfony\Component\Security\Core\User\UserInterface; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
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: