Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
78.33% |
94 / 120 |
|
70.83% |
17 / 24 |
CRAP | |
0.00% |
0 / 1 |
Useraccount | |
78.33% |
94 / 120 |
|
70.83% |
17 / 24 |
107.97 | |
0.00% |
0 / 1 |
getDefaults | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
1 | |||
hasProperties | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
getDepartmentList | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
addDepartment | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getDepartment | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
hasDepartment | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasScope | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getRightsLevel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setRights | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
hasRights | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
6 | |||
testRights | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
isOveraged | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
isSuperUser | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDepartmentById | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
testDepartmentById | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
setPassword | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
6 | |||
withDepartmentList | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
withCleanedUpFormData | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
8.06 | |||
setVerifiedHash | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
withVerifiedHash | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
isPasswordNeedingRehash | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getHash | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
createFromOpenidData | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
getOidcProviderFromName | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace BO\Zmsentities; |
4 | |
5 | use BO\Zmsentities\Helper\Property; |
6 | |
7 | /** |
8 | * @SuppressWarnings(Complexity) |
9 | * @SuppressWarnings(PublicMethod) |
10 | * |
11 | */ |
12 | class Useraccount extends Schema\Entity |
13 | { |
14 | public const PRIMARY = 'id'; |
15 | |
16 | public static $schema = "useraccount.json"; |
17 | |
18 | public function getDefaults() |
19 | { |
20 | return [ |
21 | 'rights' => [ |
22 | "availability" => false, |
23 | "basic" => true, |
24 | "cluster" => false, |
25 | "department" => false, |
26 | "organisation" => false, |
27 | "scope" => false, |
28 | "sms" => false, |
29 | "superuser" => false, |
30 | "ticketprinter" => false, |
31 | "useraccount" => false, |
32 | ], |
33 | 'departments' => new Collection\DepartmentList(), |
34 | ]; |
35 | } |
36 | |
37 | public function hasProperties() |
38 | { |
39 | foreach (func_get_args() as $property) { |
40 | if (!$this->toProperty()->$property->get()) { |
41 | throw new Exception\UserAccountMissingProperties("Missing property " . htmlspecialchars($property)); |
42 | return false; |
43 | } |
44 | } |
45 | return true; |
46 | } |
47 | |
48 | public function getDepartmentList() |
49 | { |
50 | if (!$this->departments instanceof Collection\DepartmentList) { |
51 | $this->departments = new Collection\DepartmentList($this->departments); |
52 | foreach ($this->departments as $key => $department) { |
53 | $this->departments[$key] = new Department($department); |
54 | } |
55 | } |
56 | return $this->departments; |
57 | } |
58 | |
59 | public function addDepartment($department) |
60 | { |
61 | $this->departments[] = $department; |
62 | return $this; |
63 | } |
64 | |
65 | public function getDepartment($departmentId) |
66 | { |
67 | if (count($this->departments)) { |
68 | foreach ($this->getDepartmentList() as $department) { |
69 | if ($department['id'] == $departmentId) { |
70 | return $department; |
71 | } |
72 | } |
73 | } |
74 | return new Department(['name' => 'Not existing']); |
75 | } |
76 | |
77 | public function hasDepartment($departmentId) |
78 | { |
79 | return $this->getDepartment($departmentId)->hasId(); |
80 | } |
81 | |
82 | public function hasScope($scopeId) |
83 | { |
84 | return $this->getDepartmentList()->getUniqueScopeList()->hasEntity($scopeId); |
85 | } |
86 | |
87 | /** |
88 | * @todo Remove this function, keep no contraint on old DB schema in zmsentities |
89 | */ |
90 | public function getRightsLevel() |
91 | { |
92 | return Helper\RightsLevelManager::getLevel($this->rights); |
93 | } |
94 | |
95 | public function setRights() |
96 | { |
97 | $givenRights = func_get_args(); |
98 | foreach ($givenRights as $right) { |
99 | if (Property::__keyExists($right, $this->rights)) { |
100 | $this->rights[$right] = true; |
101 | } |
102 | } |
103 | return $this; |
104 | } |
105 | |
106 | public function hasRights(array $requiredRights) |
107 | { |
108 | if ($this->isSuperUser()) { |
109 | return true; |
110 | } |
111 | foreach ($requiredRights as $required) { |
112 | if ($required instanceof Useraccount\RightsInterface) { |
113 | if (!$required->validateUseraccount($this)) { |
114 | return false; |
115 | } |
116 | } elseif (! $this->toProperty()->rights->$required->get()) { |
117 | return false; |
118 | } |
119 | } |
120 | return true; |
121 | } |
122 | |
123 | public function testRights(array $requiredRights) |
124 | { |
125 | if ($this->hasId()) { |
126 | if (!$this->hasRights($requiredRights)) { |
127 | throw new Exception\UserAccountMissingRights( |
128 | "Missing rights " . htmlspecialchars(implode(',', $requiredRights)) |
129 | ); |
130 | } |
131 | } else { |
132 | throw new Exception\UserAccountMissingLogin(); |
133 | } |
134 | return $this; |
135 | } |
136 | |
137 | public function isOveraged(\DateTimeInterface $dateTime) |
138 | { |
139 | if (Property::__keyExists('lastLogin', $this)) { |
140 | $lastLogin = (new \DateTimeImmutable())->setTimestamp($this['lastLogin'])->modify('23:59:59'); |
141 | return ($lastLogin < $dateTime); |
142 | } |
143 | return false; |
144 | } |
145 | |
146 | public function isSuperUser() |
147 | { |
148 | return $this->toProperty()->rights->superuser->get(); |
149 | } |
150 | |
151 | public function getDepartmentById($departmentId) |
152 | { |
153 | foreach ($this->departments as $department) { |
154 | if ($departmentId == $department['id']) { |
155 | return new Department($department); |
156 | } |
157 | } |
158 | return new Department(); |
159 | } |
160 | |
161 | public function testDepartmentById($departmentId) |
162 | { |
163 | $department = $this->getDepartmentById($departmentId); |
164 | if (!$department->hasId()) { |
165 | throw new Exception\UserAccountMissingDepartment( |
166 | "Missing department " . htmlspecialchars($departmentId) |
167 | ); |
168 | } |
169 | return $department; |
170 | } |
171 | |
172 | public function setPassword($input) |
173 | { |
174 | if (isset($input['password']) && '' != $input['password']) { |
175 | $this->password = $input['password']; |
176 | } |
177 | if (isset($input['changePassword']) && 0 < count(array_filter($input['changePassword']))) { |
178 | if (! isset($input['password'])) { |
179 | $this->password = $input['changePassword'][0]; |
180 | } |
181 | $this->changePassword = $input['changePassword']; |
182 | } |
183 | return $this; |
184 | } |
185 | |
186 | public function withDepartmentList() |
187 | { |
188 | $departmentList = new Collection\DepartmentList(); |
189 | $entity = clone $this; |
190 | foreach ($this->departments as $department) { |
191 | if (! is_array($department) && ! $department instanceof Department) { |
192 | $department = new Department(array('id' => $department)); |
193 | } |
194 | $departmentList->addEntity($department); |
195 | } |
196 | $entity->departments = $departmentList; |
197 | return $entity; |
198 | } |
199 | |
200 | public function withCleanedUpFormData($keepPassword = false) |
201 | { |
202 | unset($this['save']); |
203 | if (isset($this['password']) && '' == $this['password'] && false === $keepPassword) { |
204 | unset($this['password']); |
205 | } |
206 | if ( |
207 | isset($this['changePassword']) && |
208 | 0 == count(array_filter($this['changePassword'])) && |
209 | false === $keepPassword |
210 | ) { |
211 | unset($this['changePassword']); |
212 | } |
213 | if (isset($this['oidcProvider'])) { |
214 | unset($this['oidcProvider']); |
215 | } |
216 | |
217 | return $this; |
218 | } |
219 | |
220 | /** |
221 | * verify hashed password and create new if needs rehash |
222 | * |
223 | * @return array $useraccount |
224 | */ |
225 | public function setVerifiedHash($password) |
226 | { |
227 | // Do you have old, turbo-legacy, non-crypt hashes? |
228 | if (strpos($this->password, '$') !== 0) { |
229 | //error_log(__METHOD__ . "::legacy_hash\n"); |
230 | $result = $this->password === md5($password); |
231 | } else { |
232 | //error_log(__METHOD__ . "::password_verify\n"); |
233 | $result = password_verify($password, $this->password); |
234 | } |
235 | |
236 | // on passed validation check if the hash needs updating. |
237 | if ($result && $this->isPasswordNeedingRehash()) { |
238 | $this->password = $this->getHash($password); |
239 | //error_log(__METHOD__ . "::rehash\n"); |
240 | } |
241 | |
242 | return $this; |
243 | } |
244 | |
245 | public function withVerifiedHash($password) |
246 | { |
247 | $useraccount = clone $this; |
248 | if ($useraccount->isPasswordNeedingRehash()) { |
249 | $useraccount->setVerifiedHash($password); |
250 | } |
251 | return $useraccount; |
252 | } |
253 | |
254 | public function isPasswordNeedingRehash() |
255 | { |
256 | return password_needs_rehash($this->password, PASSWORD_DEFAULT); |
257 | } |
258 | |
259 | /** |
260 | * set salted hash by string |
261 | * |
262 | * @return string $hash |
263 | */ |
264 | public function getHash($string) |
265 | { |
266 | $hash = password_hash($string, PASSWORD_DEFAULT); |
267 | return $hash; |
268 | } |
269 | |
270 | /** |
271 | * create useraccount from open id input data with random password |
272 | * |
273 | * @return string $entity |
274 | */ |
275 | public function createFromOpenidData($data) |
276 | { |
277 | $entity = new self(); |
278 | $entity->id = $data['username']; |
279 | $entity->email = $data['email']; |
280 | $department = new Department(['id' => 0]); |
281 | $entity->addDepartment($department); |
282 | $password = substr(str_shuffle($entity->id . $entity->email), 0, 8); |
283 | $entity->password = $this->getHash($password); |
284 | return $entity; |
285 | } |
286 | |
287 | /** |
288 | * get oidc provider from $entity id if it exists |
289 | * |
290 | * @return string $entity |
291 | */ |
292 | public function getOidcProviderFromName() |
293 | { |
294 | $providerName = ''; |
295 | if (($pos = strpos($this->id, "@")) !== false) { |
296 | $providerName = substr($this->id, $pos + 1); |
297 | } |
298 | return ('' !== $providerName) ? $providerName : null; |
299 | } |
300 | } |