Total Complexity | 63 |
Total Lines | 610 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like PageUserManagement 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 PageUserManagement, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class PageUserManagement extends InternalPageBase |
||
34 | { |
||
35 | const OAUTH_STATE_NONE = 'none'; |
||
36 | const OAUTH_STATE_PARTIAL = 'partial'; |
||
37 | const OAUTH_STATE_FULL = 'full'; |
||
38 | |||
39 | // FIXME: domains |
||
40 | /** @var string */ |
||
41 | private $adminMailingList = '[email protected]'; |
||
42 | |||
43 | private function getOAuthStatusMap(PdoDatabase $database): array |
||
44 | { |
||
45 | $oauthStatusQuery = $database->prepare( |
||
46 | <<<SQL |
||
47 | SELECT u.id, |
||
48 | CASE |
||
49 | WHEN SUM(IF(ot.type = :access, 1, 0)) OVER (PARTITION BY ot.user) > 0 THEN :full |
||
50 | WHEN SUM(IF(ot.type = :request, 1, 0)) OVER (PARTITION BY ot.user) > 0 THEN :partial |
||
51 | ELSE :none |
||
52 | END AS status |
||
53 | FROM user u |
||
54 | LEFT JOIN oauthtoken ot ON u.id = ot.user; |
||
55 | SQL |
||
56 | ); |
||
57 | |||
58 | $oauthStatusQuery->execute([ |
||
59 | ':access' => OAuthUserHelper::TOKEN_ACCESS, |
||
60 | ':request' => OAuthUserHelper::TOKEN_REQUEST, |
||
61 | ':full' => self::OAUTH_STATE_FULL, |
||
62 | ':partial' => self::OAUTH_STATE_PARTIAL, |
||
63 | ':none' => self::OAUTH_STATE_NONE, |
||
64 | ]); |
||
65 | $oauthStatusRawData = $oauthStatusQuery->fetchAll(PDO::FETCH_ASSOC); |
||
66 | $oauthStatusQuery->closeCursor(); |
||
67 | $oauthStatusMap = []; |
||
68 | |||
69 | foreach ($oauthStatusRawData as $row) { |
||
70 | $oauthStatusMap[(int)$row['id']] = $row['status']; |
||
71 | } |
||
72 | |||
73 | return $oauthStatusMap; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Main function for this page, when no specific actions are called. |
||
78 | */ |
||
79 | protected function main() |
||
80 | { |
||
81 | $this->setHtmlTitle('User Management'); |
||
82 | |||
83 | $database = $this->getDatabase(); |
||
84 | $currentUser = User::getCurrent($database); |
||
85 | |||
86 | $userSearchRequest = WebRequest::getString('usersearch'); |
||
87 | if ($userSearchRequest !== null) { |
||
88 | $searchedUser = User::getByUsername($userSearchRequest, $database); |
||
89 | if ($searchedUser !== false) { |
||
90 | $this->redirect('statistics/users', 'detail', ['user' => $searchedUser->getId()]); |
||
91 | return; |
||
92 | } |
||
93 | } |
||
94 | |||
95 | $this->assign('oauthStatusMap', $this->getOAuthStatusMap($database)); |
||
96 | |||
97 | if (WebRequest::getBoolean("showAll")) { |
||
98 | $this->assign("showAll", true); |
||
99 | |||
100 | $deactivatedUsers = UserSearchHelper::get($database)->byStatus(User::STATUS_DEACTIVATED)->fetch(); |
||
101 | $this->assign('deactivatedUsers', $deactivatedUsers); |
||
102 | |||
103 | UserSearchHelper::get($database)->getRoleMap($roleMap); |
||
104 | } |
||
105 | else { |
||
106 | $this->assign("showAll", false); |
||
107 | $this->assign('deactivatedUsers', array()); |
||
108 | |||
109 | UserSearchHelper::get($database)->statusIn(array('New', 'Active'))->getRoleMap($roleMap); |
||
110 | } |
||
111 | |||
112 | $newUsers = UserSearchHelper::get($database)->byStatus(User::STATUS_NEW)->fetch(); |
||
113 | $normalUsers = UserSearchHelper::get($database)->byStatus(User::STATUS_ACTIVE)->byRole('user')->fetch(); |
||
114 | $adminUsers = UserSearchHelper::get($database)->byStatus(User::STATUS_ACTIVE)->byRole('admin')->fetch(); |
||
115 | $checkUsers = UserSearchHelper::get($database)->byStatus(User::STATUS_ACTIVE)->byRole('checkuser')->fetch(); |
||
116 | $stewards = UserSearchHelper::get($database)->byStatus(User::STATUS_ACTIVE)->byRole('steward')->fetch(); |
||
117 | $toolRoots = UserSearchHelper::get($database)->byStatus(User::STATUS_ACTIVE)->byRole('toolRoot')->fetch(); |
||
118 | $this->assign('newUsers', $newUsers); |
||
119 | $this->assign('normalUsers', $normalUsers); |
||
120 | $this->assign('adminUsers', $adminUsers); |
||
121 | $this->assign('checkUsers', $checkUsers); |
||
122 | $this->assign('stewards', $stewards); |
||
123 | $this->assign('toolRoots', $toolRoots); |
||
124 | |||
125 | $this->assign('roles', $roleMap); |
||
126 | |||
127 | $this->addJs("/api.php?action=users&all=true&targetVariable=typeaheaddata"); |
||
128 | |||
129 | $this->assign('canApprove', $this->barrierTest('approve', $currentUser)); |
||
130 | $this->assign('canDeactivate', $this->barrierTest('deactivate', $currentUser)); |
||
131 | $this->assign('canRename', $this->barrierTest('rename', $currentUser)); |
||
132 | $this->assign('canEditUser', $this->barrierTest('editUser', $currentUser)); |
||
133 | $this->assign('canEditRoles', $this->barrierTest('editRoles', $currentUser)); |
||
134 | |||
135 | // FIXME: domains! |
||
136 | /** @var Domain $domain */ |
||
137 | $domain = Domain::getById(1, $this->getDatabase()); |
||
138 | $this->assign('mediawikiScriptPath', $domain->getWikiArticlePath()); |
||
139 | |||
140 | $this->setTemplate("usermanagement/main.tpl"); |
||
141 | } |
||
142 | |||
143 | #region Access control |
||
144 | |||
145 | /** |
||
146 | * Action target for editing the roles assigned to a user |
||
147 | * |
||
148 | * @throws ApplicationLogicException |
||
149 | * @throws SmartyException |
||
150 | * @throws OptimisticLockFailedException |
||
151 | * @throws Exception |
||
152 | */ |
||
153 | protected function editRoles(): void |
||
154 | { |
||
155 | $this->setHtmlTitle('User Management'); |
||
156 | $database = $this->getDatabase(); |
||
157 | $domain = Domain::getCurrent($database); |
||
158 | $userId = WebRequest::getInt('user'); |
||
159 | |||
160 | /** @var User|false $user */ |
||
161 | $user = User::getById($userId, $database); |
||
162 | |||
163 | if ($user === false || $user->isCommunityUser()) { |
||
164 | throw new ApplicationLogicException('Sorry, the user you are trying to edit could not be found.'); |
||
165 | } |
||
166 | |||
167 | $roleData = $this->getRoleData(UserRole::getForUser($user->getId(), $database, $domain->getId())); |
||
168 | |||
169 | // Dual-mode action |
||
170 | if (WebRequest::wasPosted()) { |
||
171 | $this->validateCSRFToken(); |
||
172 | |||
173 | $reason = WebRequest::postString('reason'); |
||
174 | if ($reason === false || trim($reason) === '') { |
||
175 | throw new ApplicationLogicException('No reason specified for roles change'); |
||
176 | } |
||
177 | |||
178 | /** @var UserRole[] $delete */ |
||
179 | $delete = array(); |
||
180 | /** @var string[] $add */ |
||
181 | $add = array(); |
||
182 | |||
183 | /** @var UserRole[] $globalDelete */ |
||
184 | $globalDelete = array(); |
||
185 | /** @var string[] $globalAdd */ |
||
186 | $globalAdd = array(); |
||
187 | |||
188 | foreach ($roleData as $name => $r) { |
||
189 | if ($r['allowEdit'] !== 1) { |
||
190 | // not allowed, to touch this, so ignore it |
||
191 | continue; |
||
192 | } |
||
193 | |||
194 | $newValue = WebRequest::postBoolean('role-' . $name) ? 1 : 0; |
||
195 | if ($newValue !== $r['active']) { |
||
196 | if ($newValue === 0) { |
||
197 | if ($r['globalOnly']) { |
||
198 | $globalDelete[] = $r['object']; |
||
199 | } |
||
200 | else { |
||
201 | $delete[] = $r['object']; |
||
202 | } |
||
203 | } |
||
204 | |||
205 | if ($newValue === 1) { |
||
206 | if ($r['globalOnly']) { |
||
207 | $globalAdd[] = $name; |
||
208 | } |
||
209 | else { |
||
210 | $add[] = $name; |
||
211 | } |
||
212 | } |
||
213 | } |
||
214 | } |
||
215 | |||
216 | // Check there's something to do |
||
217 | if ((count($add) + count($delete) + count($globalAdd) + count($globalDelete)) === 0) { |
||
218 | $this->redirect('statistics/users', 'detail', array('user' => $user->getId())); |
||
219 | SessionAlert::warning('No changes made to roles.'); |
||
220 | |||
221 | return; |
||
222 | } |
||
223 | |||
224 | $removed = array(); |
||
225 | $globalRemoved = array(); |
||
226 | |||
227 | foreach ($delete as $d) { |
||
228 | $removed[] = $d->getRole(); |
||
229 | $d->delete(); |
||
230 | } |
||
231 | |||
232 | foreach ($globalDelete as $d) { |
||
233 | $globalRemoved[] = $d->getRole(); |
||
234 | $d->delete(); |
||
235 | } |
||
236 | |||
237 | foreach ($add as $x) { |
||
238 | $a = new UserRole(); |
||
239 | $a->setUser($user->getId()); |
||
240 | $a->setRole($x); |
||
241 | $a->setDomain($domain->getId()); |
||
242 | $a->setDatabase($database); |
||
243 | $a->save(); |
||
244 | } |
||
245 | |||
246 | foreach ($globalAdd as $x) { |
||
247 | $a = new UserRole(); |
||
248 | $a->setUser($user->getId()); |
||
249 | $a->setRole($x); |
||
250 | $a->setDomain(null); |
||
251 | $a->setDatabase($database); |
||
252 | $a->save(); |
||
253 | } |
||
254 | |||
255 | if ((count($add) + count($delete)) > 0) { |
||
256 | Logger::userRolesEdited($database, $user, $reason, $add, $removed, $domain->getId()); |
||
257 | } |
||
258 | |||
259 | if ((count($globalAdd) + count($globalDelete)) > 0) { |
||
260 | Logger::userGlobalRolesEdited($database, $user, $reason, $globalAdd, $globalRemoved); |
||
261 | } |
||
262 | |||
263 | // dummy save for optimistic locking. If this fails, the entire txn will roll back. |
||
264 | $user->setUpdateVersion(WebRequest::postInt('updateversion')); |
||
265 | $user->save(); |
||
266 | |||
267 | $this->getNotificationHelper()->userRolesEdited($user, $reason); |
||
268 | SessionAlert::quick('Roles changed for user ' . htmlentities($user->getUsername(), ENT_COMPAT, 'UTF-8')); |
||
269 | |||
270 | $this->redirect('statistics/users', 'detail', array('user' => $user->getId())); |
||
271 | } |
||
272 | else { |
||
273 | $this->assignCSRFToken(); |
||
274 | $this->setTemplate('usermanagement/roleedit.tpl'); |
||
275 | $this->assign('user', $user); |
||
276 | $this->assign('roleData', $roleData); |
||
277 | } |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Action target for deactivating users |
||
282 | * |
||
283 | * @throws ApplicationLogicException |
||
284 | */ |
||
285 | protected function deactivate() |
||
286 | { |
||
287 | $this->setHtmlTitle('User Management'); |
||
288 | |||
289 | $database = $this->getDatabase(); |
||
290 | |||
291 | $userId = WebRequest::getInt('user'); |
||
292 | |||
293 | /** @var User $user */ |
||
294 | $user = User::getById($userId, $database); |
||
295 | |||
296 | if ($user === false || $user->isCommunityUser()) { |
||
297 | throw new ApplicationLogicException('Sorry, the user you are trying to deactivate could not be found.'); |
||
298 | } |
||
299 | |||
300 | if ($user->isDeactivated()) { |
||
301 | throw new ApplicationLogicException('Sorry, the user you are trying to deactivate is already deactivated.'); |
||
302 | } |
||
303 | |||
304 | // Dual-mode action |
||
305 | if (WebRequest::wasPosted()) { |
||
306 | $this->validateCSRFToken(); |
||
307 | $reason = WebRequest::postString('reason'); |
||
308 | |||
309 | if ($reason === null || trim($reason) === '') { |
||
310 | throw new ApplicationLogicException('No reason provided'); |
||
311 | } |
||
312 | |||
313 | $user->setStatus(User::STATUS_DEACTIVATED); |
||
314 | $user->setUpdateVersion(WebRequest::postInt('updateversion')); |
||
315 | $user->save(); |
||
316 | Logger::deactivatedUser($database, $user, $reason); |
||
317 | |||
318 | $this->getNotificationHelper()->userDeactivated($user); |
||
319 | SessionAlert::quick('Deactivated user ' . htmlentities($user->getUsername(), ENT_COMPAT, 'UTF-8')); |
||
320 | |||
321 | // send email |
||
322 | $this->sendStatusChangeEmail( |
||
323 | 'Your WP:ACC account has been deactivated', |
||
324 | 'usermanagement/emails/deactivated.tpl', |
||
325 | $reason, |
||
326 | $user, |
||
327 | User::getCurrent($database)->getUsername() |
||
328 | ); |
||
329 | |||
330 | $this->redirect('userManagement'); |
||
331 | |||
332 | return; |
||
333 | } |
||
334 | else { |
||
335 | $this->assignCSRFToken(); |
||
336 | $this->setTemplate('usermanagement/changelevel-reason.tpl'); |
||
337 | $this->assign('user', $user); |
||
338 | $this->assign('status', User::STATUS_DEACTIVATED); |
||
339 | $this->assign("showReason", true); |
||
340 | |||
341 | if (WebRequest::getString('preload')) { |
||
342 | $this->assign('preload', WebRequest::getString('preload')); |
||
343 | } |
||
344 | } |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Entry point for the approve action |
||
349 | * |
||
350 | * @throws ApplicationLogicException |
||
351 | */ |
||
352 | protected function approve() |
||
353 | { |
||
354 | $this->setHtmlTitle('User Management'); |
||
355 | |||
356 | $database = $this->getDatabase(); |
||
357 | |||
358 | $userId = WebRequest::getInt('user'); |
||
359 | $user = User::getById($userId, $database); |
||
360 | |||
361 | if ($user === false || $user->isCommunityUser()) { |
||
362 | throw new ApplicationLogicException('Sorry, the user you are trying to approve could not be found.'); |
||
363 | } |
||
364 | |||
365 | if ($user->isActive()) { |
||
366 | throw new ApplicationLogicException('Sorry, the user you are trying to approve is already an active user.'); |
||
367 | } |
||
368 | |||
369 | // Dual-mode action |
||
370 | if (WebRequest::wasPosted()) { |
||
371 | $this->validateCSRFToken(); |
||
372 | $user->setStatus(User::STATUS_ACTIVE); |
||
373 | $user->setUpdateVersion(WebRequest::postInt('updateversion')); |
||
374 | $user->save(); |
||
375 | Logger::approvedUser($database, $user); |
||
376 | |||
377 | $this->getNotificationHelper()->userApproved($user); |
||
378 | SessionAlert::quick('Approved user ' . htmlentities($user->getUsername(), ENT_COMPAT, 'UTF-8')); |
||
379 | |||
380 | // send email |
||
381 | $this->sendStatusChangeEmail( |
||
382 | 'Your WP:ACC account has been approved', |
||
383 | 'usermanagement/emails/approved.tpl', |
||
384 | null, |
||
385 | $user, |
||
386 | User::getCurrent($database)->getUsername() |
||
387 | ); |
||
388 | |||
389 | $this->redirect("userManagement"); |
||
390 | |||
391 | return; |
||
392 | } |
||
393 | else { |
||
394 | $this->assignCSRFToken(); |
||
395 | $this->setTemplate("usermanagement/changelevel-reason.tpl"); |
||
396 | $this->assign("user", $user); |
||
397 | $this->assign("status", "Active"); |
||
398 | $this->assign("showReason", false); |
||
399 | } |
||
400 | } |
||
401 | |||
402 | #endregion |
||
403 | |||
404 | #region Renaming / Editing |
||
405 | |||
406 | /** |
||
407 | * Entry point for the rename action |
||
408 | * |
||
409 | * @throws ApplicationLogicException |
||
410 | */ |
||
411 | protected function rename() |
||
481 | } |
||
482 | } |
||
483 | |||
484 | /** |
||
485 | * Entry point for the edit action |
||
486 | * |
||
487 | * @throws ApplicationLogicException |
||
488 | */ |
||
489 | protected function editUser() |
||
490 | { |
||
491 | $this->setHtmlTitle('User Management'); |
||
492 | |||
493 | $database = $this->getDatabase(); |
||
494 | |||
495 | $userId = WebRequest::getInt('user'); |
||
496 | $user = User::getById($userId, $database); |
||
497 | $oauth = new OAuthUserHelper($user, $database, $this->getOAuthProtocolHelper(), $this->getSiteConfiguration()); |
||
498 | |||
499 | if ($user === false || $user->isCommunityUser()) { |
||
500 | throw new ApplicationLogicException('Sorry, the user you are trying to edit could not be found.'); |
||
501 | } |
||
502 | |||
503 | // FIXME: domains |
||
504 | $prefs = new PreferenceManager($database, $user->getId(), 1); |
||
505 | |||
506 | // Dual-mode action |
||
507 | if (WebRequest::wasPosted()) { |
||
508 | $this->validateCSRFToken(); |
||
509 | $newEmail = WebRequest::postEmail('user_email'); |
||
510 | $newOnWikiName = WebRequest::postString('user_onwikiname'); |
||
511 | |||
512 | if ($newEmail === null) { |
||
513 | throw new ApplicationLogicException('Invalid email address'); |
||
514 | } |
||
515 | |||
516 | if ($this->validateUnusedEmail($newEmail, $userId)) { |
||
517 | throw new ApplicationLogicException('The specified email address is already in use.'); |
||
518 | } |
||
519 | |||
520 | if (!($oauth->isFullyLinked() || $oauth->isPartiallyLinked())) { |
||
521 | if (trim($newOnWikiName) == "") { |
||
522 | throw new ApplicationLogicException('New on-wiki username cannot be blank'); |
||
523 | } |
||
524 | |||
525 | $user->setOnWikiName($newOnWikiName); |
||
526 | } |
||
527 | |||
528 | $user->setEmail($newEmail); |
||
529 | |||
530 | $prefs->setLocalPreference(PreferenceManager::PREF_CREATION_MODE, WebRequest::postInt('creationmode')); |
||
531 | |||
532 | $prefs->setLocalPreference(PreferenceManager::ADMIN_PREF_PREVENT_REACTIVATION, WebRequest::postBoolean('preventReactivation')); |
||
533 | |||
534 | $user->setUpdateVersion(WebRequest::postInt('updateversion')); |
||
535 | |||
536 | $user->save(); |
||
537 | |||
538 | Logger::userPreferencesChange($database, $user); |
||
539 | $this->getNotificationHelper()->userPrefChange($user); |
||
540 | SessionAlert::quick('Changes to user\'s preferences have been saved'); |
||
541 | |||
542 | $this->redirect("userManagement"); |
||
543 | |||
544 | return; |
||
545 | } |
||
546 | else { |
||
547 | $this->assignCSRFToken(); |
||
548 | $oauth = new OAuthUserHelper($user, $database, $this->getOAuthProtocolHelper(), |
||
549 | $this->getSiteConfiguration()); |
||
550 | $this->setTemplate('usermanagement/edituser.tpl'); |
||
551 | $this->assign('user', $user); |
||
552 | $this->assign('oauth', $oauth); |
||
553 | |||
554 | $this->assign('preferredCreationMode', (int)$prefs->getPreference(PreferenceManager::PREF_CREATION_MODE)); |
||
555 | $this->assign('emailSignature', $prefs->getPreference(PreferenceManager::PREF_EMAIL_SIGNATURE)); |
||
556 | |||
557 | $this->assign('preventReactivation', $prefs->getPreference(PreferenceManager::ADMIN_PREF_PREVENT_REACTIVATION) ?? false); |
||
558 | |||
559 | $this->assign('canManualCreate', |
||
560 | $this->barrierTest(PreferenceManager::CREATION_MANUAL, $user, 'RequestCreation')); |
||
561 | $this->assign('canOauthCreate', |
||
562 | $this->barrierTest(PreferenceManager::CREATION_OAUTH, $user, 'RequestCreation')); |
||
563 | $this->assign('canBotCreate', |
||
564 | $this->barrierTest(PreferenceManager::CREATION_BOT, $user, 'RequestCreation')); |
||
565 | } |
||
566 | } |
||
567 | |||
568 | #endregion |
||
569 | |||
570 | private function validateUnusedEmail(string $email, int $userId) : bool { |
||
578 | } |
||
579 | |||
580 | /** |
||
581 | * Sends a status change email to the user. |
||
582 | * |
||
583 | * @param string $subject The subject of the email |
||
584 | * @param string $template The smarty template to use |
||
585 | * @param string|null $reason The reason for performing the status change |
||
586 | * @param User $user The user affected |
||
587 | * @param string $toolAdminUsername The tool admin's username who is making the edit |
||
588 | */ |
||
589 | private function sendStatusChangeEmail($subject, $template, $reason, $user, $toolAdminUsername) |
||
590 | { |
||
591 | $this->assign('targetUsername', $user->getUsername()); |
||
592 | $this->assign('toolAdmin', $toolAdminUsername); |
||
593 | $this->assign('actionReason', $reason); |
||
594 | $this->assign('mailingList', $this->adminMailingList); |
||
595 | |||
596 | // FIXME: domains! |
||
597 | /** @var Domain $domain */ |
||
598 | $domain = Domain::getById(1, $this->getDatabase()); |
||
599 | $this->getEmailHelper()->sendMail( |
||
600 | $this->adminMailingList, |
||
601 | $user->getEmail(), |
||
602 | $subject, |
||
603 | $this->fetchTemplate($template) |
||
604 | ); |
||
605 | } |
||
606 | |||
607 | /** |
||
608 | * @param UserRole[] $activeRoles |
||
609 | * |
||
610 | * @return array |
||
611 | */ |
||
612 | private function getRoleData($activeRoles) |
||
643 | } |
||
644 | } |
||
645 |