Total Complexity | 44 |
Total Lines | 398 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like BackendUser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BackendUser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class BackendUser extends AbstractEntity |
||
30 | { |
||
31 | /** |
||
32 | * @var string |
||
33 | * @Extbase\Validate("NotEmpty") |
||
34 | */ |
||
35 | protected $userName = ''; |
||
36 | |||
37 | /** |
||
38 | * @var ObjectStorage<BackendUserGroup> |
||
39 | */ |
||
40 | protected $backendUserGroups; |
||
41 | |||
42 | /** |
||
43 | * Comma separated list of uids in multi-select |
||
44 | * Might retrieve the labels from TCA/DataMapper |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $allowedLanguages = ''; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $dbMountPoints = ''; |
||
54 | |||
55 | /** |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $description = ''; |
||
59 | |||
60 | /** |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $fileMountPoints = ''; |
||
64 | |||
65 | /** |
||
66 | * @var bool |
||
67 | */ |
||
68 | protected $isAdministrator = false; |
||
69 | |||
70 | /** |
||
71 | * @var bool |
||
72 | */ |
||
73 | protected $isDisabled = false; |
||
74 | |||
75 | /** |
||
76 | * @var \DateTime|null |
||
77 | */ |
||
78 | protected $startDateAndTime; |
||
79 | |||
80 | /** |
||
81 | * @var \DateTime|null |
||
82 | */ |
||
83 | protected $endDateAndTime; |
||
84 | |||
85 | /** |
||
86 | * @var string |
||
87 | */ |
||
88 | protected $email = ''; |
||
89 | |||
90 | /** |
||
91 | * @var string |
||
92 | */ |
||
93 | protected $realName = ''; |
||
94 | |||
95 | /** |
||
96 | * @var \DateTime|null |
||
97 | */ |
||
98 | protected $lastLoginDateAndTime; |
||
99 | |||
100 | /** |
||
101 | * @param string $allowedLanguages |
||
102 | */ |
||
103 | public function setAllowedLanguages($allowedLanguages) |
||
104 | { |
||
105 | $this->allowedLanguages = $allowedLanguages; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @return string |
||
110 | */ |
||
111 | public function getAllowedLanguages() |
||
112 | { |
||
113 | return $this->allowedLanguages; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param string $dbMountPoints |
||
118 | */ |
||
119 | public function setDbMountPoints($dbMountPoints) |
||
120 | { |
||
121 | $this->dbMountPoints = $dbMountPoints; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @return string |
||
126 | */ |
||
127 | public function getDbMountPoints() |
||
128 | { |
||
129 | return $this->dbMountPoints; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param string $fileMountPoints |
||
134 | */ |
||
135 | public function setFileMountPoints($fileMountPoints) |
||
136 | { |
||
137 | $this->fileMountPoints = $fileMountPoints; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @return string |
||
142 | */ |
||
143 | public function getFileMountPoints() |
||
144 | { |
||
145 | return $this->fileMountPoints; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Check if user is active, not disabled |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | public function isActive() |
||
154 | { |
||
155 | if ($this->getIsDisabled()) { |
||
156 | return false; |
||
157 | } |
||
158 | $now = new \DateTime('now'); |
||
159 | return !$this->getStartDateAndTime() && !$this->getEndDateAndTime() || $this->getStartDateAndTime() <= $now && (!$this->getEndDateAndTime() || $this->getEndDateAndTime() > $now); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @param ObjectStorage<BackendUserGroup> $backendUserGroups |
||
164 | */ |
||
165 | public function setBackendUserGroups($backendUserGroups) |
||
166 | { |
||
167 | $this->backendUserGroups = $backendUserGroups; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @return ObjectStorage<BackendUserGroup> |
||
172 | */ |
||
173 | public function getBackendUserGroups() |
||
174 | { |
||
175 | return $this->backendUserGroups; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Check if user is currently logged in |
||
180 | * |
||
181 | * @return bool |
||
182 | */ |
||
183 | public function isCurrentlyLoggedIn() |
||
184 | { |
||
185 | return $this->getUid() === (int)$this->getBackendUser()->user['uid']; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Check if the user is allowed to trigger a password reset |
||
190 | * |
||
191 | * Requirements: |
||
192 | * 1. The user for which the password reset should be triggered is not the currently logged in user |
||
193 | * 2. Password reset is enabled for the user (Email+Password are set) |
||
194 | * 3. The currently logged in user is allowed to reset passwords in the backend (Enabled in user TSconfig) |
||
195 | * |
||
196 | * @return bool |
||
197 | */ |
||
198 | public function isPasswordResetEnabled(): bool |
||
199 | { |
||
200 | return !$this->isCurrentlyLoggedIn() |
||
201 | && GeneralUtility::makeInstance(PasswordReset::class)->isEnabledForUser((int)$this->getUid()) |
||
202 | && ($this->getBackendUser()->getTSConfig()['options.']['passwordReset'] ?? true); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Gets the user name. |
||
207 | * |
||
208 | * @return string the user name, will not be empty |
||
209 | */ |
||
210 | public function getUserName() |
||
211 | { |
||
212 | return $this->userName; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Sets the user name. |
||
217 | * |
||
218 | * @param string $userName the user name to set, must not be empty |
||
219 | */ |
||
220 | public function setUserName($userName) |
||
221 | { |
||
222 | $this->userName = $userName; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @return string |
||
227 | */ |
||
228 | public function getDescription() |
||
229 | { |
||
230 | return $this->description; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @param string $description |
||
235 | */ |
||
236 | public function setDescription($description) |
||
237 | { |
||
238 | $this->description = $description; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Checks whether this user is an administrator. |
||
243 | * |
||
244 | * @return bool whether this user is an administrator |
||
245 | */ |
||
246 | public function getIsAdministrator() |
||
247 | { |
||
248 | return $this->isAdministrator; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Sets whether this user should be an administrator. |
||
253 | * |
||
254 | * @param bool $isAdministrator whether this user should be an administrator |
||
255 | */ |
||
256 | public function setIsAdministrator($isAdministrator) |
||
257 | { |
||
258 | $this->isAdministrator = $isAdministrator; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Checks whether this user is disabled. |
||
263 | * |
||
264 | * @return bool whether this user is disabled |
||
265 | */ |
||
266 | public function getIsDisabled() |
||
267 | { |
||
268 | return $this->isDisabled; |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Sets whether this user is disabled. |
||
273 | * |
||
274 | * @param bool $isDisabled whether this user is disabled |
||
275 | */ |
||
276 | public function setIsDisabled($isDisabled) |
||
277 | { |
||
278 | $this->isDisabled = $isDisabled; |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Returns the point in time from which this user is enabled. |
||
283 | * |
||
284 | * @return \DateTime|null the start date and time |
||
285 | */ |
||
286 | public function getStartDateAndTime() |
||
287 | { |
||
288 | return $this->startDateAndTime; |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * Sets the point in time from which this user is enabled. |
||
293 | * |
||
294 | * @param \DateTime|null $dateAndTime the start date and time |
||
295 | */ |
||
296 | public function setStartDateAndTime(\DateTime $dateAndTime = null) |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Returns the point in time before which this user is enabled. |
||
303 | * |
||
304 | * @return \DateTime|null the end date and time |
||
305 | */ |
||
306 | public function getEndDateAndTime() |
||
307 | { |
||
308 | return $this->endDateAndTime; |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * Sets the point in time before which this user is enabled. |
||
313 | * |
||
314 | * @param \DateTime|null $dateAndTime the end date and time |
||
315 | */ |
||
316 | public function setEndDateAndTime(\DateTime $dateAndTime = null) |
||
317 | { |
||
318 | $this->endDateAndTime = $dateAndTime; |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Gets the e-mail address of this user. |
||
323 | * |
||
324 | * @return string the e-mail address, might be empty |
||
325 | */ |
||
326 | public function getEmail() |
||
327 | { |
||
328 | return $this->email; |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Sets the e-mail address of this user. |
||
333 | * |
||
334 | * @param string $email the e-mail address, may be empty |
||
335 | */ |
||
336 | public function setEmail($email) |
||
337 | { |
||
338 | $this->email = $email; |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * Returns this user's real name. |
||
343 | * |
||
344 | * @return string the real name. might be empty |
||
345 | */ |
||
346 | public function getRealName() |
||
347 | { |
||
348 | return $this->realName; |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * Sets this user's real name. |
||
353 | * |
||
354 | * @param string $name the user's real name, may be empty. |
||
355 | */ |
||
356 | public function setRealName($name) |
||
357 | { |
||
358 | $this->realName = $name; |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Checks whether this user is currently activated. |
||
363 | * |
||
364 | * This function takes the "disabled" flag, the start date/time and the end date/time into account. |
||
365 | * |
||
366 | * @return bool whether this user is currently activated |
||
367 | */ |
||
368 | public function isActivated() |
||
369 | { |
||
370 | return !$this->getIsDisabled() && $this->isActivatedViaStartDateAndTime() && $this->isActivatedViaEndDateAndTime(); |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Checks whether this user is activated as far as the start date and time is concerned. |
||
375 | * |
||
376 | * @return bool whether this user is activated as far as the start date and time is concerned |
||
377 | */ |
||
378 | protected function isActivatedViaStartDateAndTime() |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Checks whether this user is activated as far as the end date and time is concerned. |
||
389 | * |
||
390 | * @return bool whether this user is activated as far as the end date and time is concerned |
||
391 | */ |
||
392 | protected function isActivatedViaEndDateAndTime() |
||
393 | { |
||
394 | if ($this->getEndDateAndTime() === null) { |
||
395 | return true; |
||
396 | } |
||
397 | $now = new \DateTime('now'); |
||
398 | return $now <= $this->getEndDateAndTime(); |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * Gets this user's last login date and time. |
||
403 | * |
||
404 | * @return \DateTime|null this user's last login date and time, will be NULL if this user has never logged in before |
||
405 | */ |
||
406 | public function getLastLoginDateAndTime() |
||
407 | { |
||
408 | return $this->lastLoginDateAndTime; |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * Sets this user's last login date and time. |
||
413 | * |
||
414 | * @param \DateTime|null $dateAndTime this user's last login date and time |
||
415 | */ |
||
416 | public function setLastLoginDateAndTime(\DateTime $dateAndTime = null) |
||
419 | } |
||
420 | |||
421 | /** |
||
422 | * Gets the currently logged in backend user |
||
423 | */ |
||
424 | public function getBackendUser(): BackendUserAuthentication |
||
425 | { |
||
426 | return $GLOBALS['BE_USER']; |
||
427 | } |
||
428 | } |
||
429 |