1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the CRUDlexUser package. |
5
|
|
|
* |
6
|
|
|
* (c) Philip Lehmann-Böhm <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CRUDlex; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
15
|
|
|
use CRUDlex\Entity; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The UserInterface implementation for the UserProvider. |
19
|
|
|
*/ |
20
|
|
|
class User implements UserInterface |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Holds the actual user data. |
25
|
|
|
*/ |
26
|
|
|
private $userData; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The CRUDEntity fieldname of the username. |
30
|
|
|
*/ |
31
|
|
|
protected $usernameField; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The fieldname of the password (hash). |
35
|
|
|
*/ |
36
|
|
|
protected $passwordField; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The fieldname of the password hash salt. |
40
|
|
|
*/ |
41
|
|
|
protected $saltField; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Hold password hash salt. |
45
|
|
|
*/ |
46
|
|
|
private $roles; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Constructor. |
50
|
|
|
* |
51
|
|
|
* @param string $usernameField |
52
|
|
|
* the username |
53
|
|
|
* |
54
|
|
|
* @param string $passwordField |
55
|
|
|
* the password (hash) |
56
|
|
|
* |
57
|
|
|
* @param string $saltField |
58
|
|
|
* the password hash salt |
59
|
|
|
* |
60
|
|
|
* @param Entity $userEntity |
61
|
|
|
* the actual user data |
62
|
|
|
* |
63
|
|
|
* @param array $roles |
64
|
|
|
* the roles |
65
|
|
|
*/ |
66
|
|
|
public function __construct($usernameField, $passwordField, $saltField, Entity $userEntity, array $roles) |
67
|
|
|
{ |
68
|
|
|
$this->usernameField = $usernameField; |
69
|
|
|
$this->passwordField = $passwordField; |
70
|
|
|
$this->saltField = $saltField; |
71
|
|
|
// We have to copy it over as symfony/security wants something serializable. |
72
|
|
|
$this->userData = []; |
73
|
|
|
foreach ($userEntity->getDefinition()->getFieldNames() as $field) { |
74
|
|
|
$this->userData[$field] = $userEntity->get($field); |
75
|
|
|
} |
76
|
|
|
$this->roles = $roles; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Gets the roles. |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
* the roles |
84
|
|
|
*/ |
85
|
|
|
public function getRoles() |
86
|
|
|
{ |
87
|
|
|
return $this->roles; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Gets the password (hash). |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
* the password |
95
|
|
|
*/ |
96
|
|
|
public function getPassword() |
97
|
|
|
{ |
98
|
|
|
return $this->userData[$this->passwordField]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Gets the password hash salt. |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
* the salt |
106
|
|
|
*/ |
107
|
|
|
public function getSalt() |
108
|
|
|
{ |
109
|
|
|
return $this->userData[$this->saltField]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Gets the username. |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
* the username |
117
|
|
|
*/ |
118
|
|
|
public function getUsername() |
119
|
|
|
{ |
120
|
|
|
return $this->userData[$this->usernameField]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Should erase some crucial data if needed. But nothing to do here in this |
125
|
|
|
* implementation. |
126
|
|
|
*/ |
127
|
|
|
public function eraseCredentials() |
128
|
|
|
{ |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Gets the user data. |
133
|
|
|
* |
134
|
|
|
* @return array |
135
|
|
|
* the user data |
136
|
|
|
*/ |
137
|
|
|
public function getUserData() |
138
|
|
|
{ |
139
|
|
|
return $this->userData; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
|