Total Complexity | 194 |
Total Lines | 1107 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like UsersController 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 UsersController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
76 | class UsersController extends AUserData { |
||
77 | |||
78 | /** @var IAppManager */ |
||
79 | private $appManager; |
||
80 | /** @var IURLGenerator */ |
||
81 | protected $urlGenerator; |
||
82 | /** @var LoggerInterface */ |
||
83 | private $logger; |
||
84 | /** @var IFactory */ |
||
85 | protected $l10nFactory; |
||
86 | /** @var NewUserMailHelper */ |
||
87 | private $newUserMailHelper; |
||
88 | /** @var ISecureRandom */ |
||
89 | private $secureRandom; |
||
90 | /** @var RemoteWipe */ |
||
91 | private $remoteWipe; |
||
92 | /** @var KnownUserService */ |
||
93 | private $knownUserService; |
||
94 | /** @var IEventDispatcher */ |
||
95 | private $eventDispatcher; |
||
96 | |||
97 | public function __construct(string $appName, |
||
98 | IRequest $request, |
||
99 | IUserManager $userManager, |
||
100 | IConfig $config, |
||
101 | IAppManager $appManager, |
||
102 | IGroupManager $groupManager, |
||
103 | IUserSession $userSession, |
||
104 | IAccountManager $accountManager, |
||
105 | IURLGenerator $urlGenerator, |
||
106 | LoggerInterface $logger, |
||
107 | IFactory $l10nFactory, |
||
108 | NewUserMailHelper $newUserMailHelper, |
||
109 | ISecureRandom $secureRandom, |
||
110 | RemoteWipe $remoteWipe, |
||
111 | KnownUserService $knownUserService, |
||
112 | IEventDispatcher $eventDispatcher) { |
||
113 | parent::__construct($appName, |
||
114 | $request, |
||
115 | $userManager, |
||
116 | $config, |
||
117 | $groupManager, |
||
118 | $userSession, |
||
119 | $accountManager, |
||
120 | $l10nFactory); |
||
121 | |||
122 | $this->appManager = $appManager; |
||
123 | $this->urlGenerator = $urlGenerator; |
||
124 | $this->logger = $logger; |
||
125 | $this->l10nFactory = $l10nFactory; |
||
126 | $this->newUserMailHelper = $newUserMailHelper; |
||
127 | $this->secureRandom = $secureRandom; |
||
128 | $this->remoteWipe = $remoteWipe; |
||
129 | $this->knownUserService = $knownUserService; |
||
130 | $this->eventDispatcher = $eventDispatcher; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @NoAdminRequired |
||
135 | * |
||
136 | * returns a list of users |
||
137 | * |
||
138 | * @param string $search |
||
139 | * @param int $limit |
||
140 | * @param int $offset |
||
141 | * @return DataResponse |
||
142 | */ |
||
143 | public function getUsers(string $search = '', int $limit = null, int $offset = 0): DataResponse { |
||
144 | $user = $this->userSession->getUser(); |
||
145 | $users = []; |
||
146 | |||
147 | // Admin? Or SubAdmin? |
||
148 | $uid = $user->getUID(); |
||
149 | $subAdminManager = $this->groupManager->getSubAdmin(); |
||
|
|||
150 | if ($this->groupManager->isAdmin($uid)) { |
||
151 | $users = $this->userManager->search($search, $limit, $offset); |
||
152 | } elseif ($subAdminManager->isSubAdmin($user)) { |
||
153 | $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user); |
||
154 | foreach ($subAdminOfGroups as $key => $group) { |
||
155 | $subAdminOfGroups[$key] = $group->getGID(); |
||
156 | } |
||
157 | |||
158 | $users = []; |
||
159 | foreach ($subAdminOfGroups as $group) { |
||
160 | $users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search, $limit, $offset)); |
||
161 | } |
||
162 | } |
||
163 | |||
164 | $users = array_keys($users); |
||
165 | |||
166 | return new DataResponse([ |
||
167 | 'users' => $users |
||
168 | ]); |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @NoAdminRequired |
||
173 | * |
||
174 | * returns a list of users and their data |
||
175 | */ |
||
176 | public function getUsersDetails(string $search = '', int $limit = null, int $offset = 0): DataResponse { |
||
177 | $currentUser = $this->userSession->getUser(); |
||
178 | $users = []; |
||
179 | |||
180 | // Admin? Or SubAdmin? |
||
181 | $uid = $currentUser->getUID(); |
||
182 | $subAdminManager = $this->groupManager->getSubAdmin(); |
||
183 | if ($this->groupManager->isAdmin($uid)) { |
||
184 | $users = $this->userManager->search($search, $limit, $offset); |
||
185 | $users = array_keys($users); |
||
186 | } elseif ($subAdminManager->isSubAdmin($currentUser)) { |
||
187 | $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($currentUser); |
||
188 | foreach ($subAdminOfGroups as $key => $group) { |
||
189 | $subAdminOfGroups[$key] = $group->getGID(); |
||
190 | } |
||
191 | |||
192 | $users = []; |
||
193 | foreach ($subAdminOfGroups as $group) { |
||
194 | $users[] = array_keys($this->groupManager->displayNamesInGroup($group, $search, $limit, $offset)); |
||
195 | } |
||
196 | $users = array_merge(...$users); |
||
197 | } |
||
198 | |||
199 | $usersDetails = []; |
||
200 | foreach ($users as $userId) { |
||
201 | $userId = (string) $userId; |
||
202 | $userData = $this->getUserData($userId); |
||
203 | // Do not insert empty entry |
||
204 | if (!empty($userData)) { |
||
205 | $usersDetails[$userId] = $userData; |
||
206 | } else { |
||
207 | // Logged user does not have permissions to see this user |
||
208 | // only showing its id |
||
209 | $usersDetails[$userId] = ['id' => $userId]; |
||
210 | } |
||
211 | } |
||
212 | |||
213 | return new DataResponse([ |
||
214 | 'users' => $usersDetails |
||
215 | ]); |
||
216 | } |
||
217 | |||
218 | |||
219 | /** |
||
220 | * @NoAdminRequired |
||
221 | * @NoSubAdminRequired |
||
222 | * |
||
223 | * @param string $location |
||
224 | * @param array $search |
||
225 | * @return DataResponse |
||
226 | */ |
||
227 | public function searchByPhoneNumbers(string $location, array $search): DataResponse { |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * @throws OCSException |
||
302 | */ |
||
303 | private function createNewUserId(): string { |
||
304 | $attempts = 0; |
||
305 | do { |
||
306 | $uidCandidate = $this->secureRandom->generate(10, ISecureRandom::CHAR_HUMAN_READABLE); |
||
307 | if (!$this->userManager->userExists($uidCandidate)) { |
||
308 | return $uidCandidate; |
||
309 | } |
||
310 | $attempts++; |
||
311 | } while ($attempts < 10); |
||
312 | throw new OCSException('Could not create non-existing user id', 111); |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * @PasswordConfirmationRequired |
||
317 | * @NoAdminRequired |
||
318 | * |
||
319 | * @param string $userid |
||
320 | * @param string $password |
||
321 | * @param string $displayName |
||
322 | * @param string $email |
||
323 | * @param array $groups |
||
324 | * @param array $subadmin |
||
325 | * @param string $quota |
||
326 | * @param string $language |
||
327 | * @return DataResponse |
||
328 | * @throws OCSException |
||
329 | */ |
||
330 | public function addUser(string $userid, |
||
488 | } |
||
489 | } |
||
490 | |||
491 | /** |
||
492 | * @NoAdminRequired |
||
493 | * @NoSubAdminRequired |
||
494 | * |
||
495 | * gets user info |
||
496 | * |
||
497 | * @param string $userId |
||
498 | * @return DataResponse |
||
499 | * @throws OCSException |
||
500 | */ |
||
501 | public function getUser(string $userId): DataResponse { |
||
502 | $includeScopes = false; |
||
503 | $currentUser = $this->userSession->getUser(); |
||
504 | if ($currentUser && $currentUser->getUID() === $userId) { |
||
505 | $includeScopes = true; |
||
506 | } |
||
507 | |||
508 | $data = $this->getUserData($userId, $includeScopes); |
||
509 | // getUserData returns empty array if not enough permissions |
||
510 | if (empty($data)) { |
||
511 | throw new OCSException('', OCSController::RESPOND_NOT_FOUND); |
||
512 | } |
||
513 | return new DataResponse($data); |
||
514 | } |
||
515 | |||
516 | /** |
||
517 | * @NoAdminRequired |
||
518 | * @NoSubAdminRequired |
||
519 | * |
||
520 | * gets user info from the currently logged in user |
||
521 | * |
||
522 | * @return DataResponse |
||
523 | * @throws OCSException |
||
524 | */ |
||
525 | public function getCurrentUser(): DataResponse { |
||
537 | } |
||
538 | |||
539 | /** |
||
540 | * @NoAdminRequired |
||
541 | * @NoSubAdminRequired |
||
542 | * |
||
543 | * @return DataResponse |
||
544 | * @throws OCSException |
||
545 | */ |
||
546 | public function getEditableFields(): DataResponse { |
||
547 | $currentLoggedInUser = $this->userSession->getUser(); |
||
548 | if (!$currentLoggedInUser instanceof IUser) { |
||
549 | throw new OCSException('', OCSController::RESPOND_NOT_FOUND); |
||
550 | } |
||
551 | |||
552 | return $this->getEditableFieldsForUser($currentLoggedInUser->getUID()); |
||
553 | } |
||
554 | |||
555 | /** |
||
556 | * @NoAdminRequired |
||
557 | * @NoSubAdminRequired |
||
558 | * |
||
559 | * @param string $userId |
||
560 | * @return DataResponse |
||
561 | * @throws OCSException |
||
562 | */ |
||
563 | public function getEditableFieldsForUser(string $userId): DataResponse { |
||
601 | } |
||
602 | |||
603 | /** |
||
604 | * @NoAdminRequired |
||
605 | * @NoSubAdminRequired |
||
606 | * @PasswordConfirmationRequired |
||
607 | * |
||
608 | * edit users |
||
609 | * |
||
610 | * @param string $userId |
||
611 | * @param string $key |
||
612 | * @param string $value |
||
613 | * @return DataResponse |
||
614 | * @throws OCSException |
||
615 | */ |
||
616 | public function editUser(string $userId, string $key, string $value): DataResponse { |
||
617 | $currentLoggedInUser = $this->userSession->getUser(); |
||
618 | |||
619 | $targetUser = $this->userManager->get($userId); |
||
620 | if ($targetUser === null) { |
||
621 | throw new OCSException('', OCSController::RESPOND_NOT_FOUND); |
||
622 | } |
||
623 | |||
624 | $permittedFields = []; |
||
625 | if ($targetUser->getUID() === $currentLoggedInUser->getUID()) { |
||
626 | // Editing self (display, email) |
||
627 | if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { |
||
628 | if ($targetUser->getBackend() instanceof ISetDisplayNameBackend |
||
629 | || $targetUser->getBackend()->implementsActions(Backend::SET_DISPLAYNAME)) { |
||
630 | $permittedFields[] = 'display'; |
||
631 | $permittedFields[] = IAccountManager::PROPERTY_DISPLAYNAME; |
||
632 | } |
||
633 | $permittedFields[] = IAccountManager::PROPERTY_EMAIL; |
||
634 | } |
||
635 | |||
636 | $permittedFields[] = IAccountManager::PROPERTY_DISPLAYNAME . self::SCOPE_SUFFIX; |
||
637 | $permittedFields[] = IAccountManager::PROPERTY_EMAIL . self::SCOPE_SUFFIX; |
||
638 | |||
639 | $permittedFields[] = 'password'; |
||
640 | if ($this->config->getSystemValue('force_language', false) === false || |
||
641 | $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
||
642 | $permittedFields[] = 'language'; |
||
643 | } |
||
644 | |||
645 | if ($this->config->getSystemValue('force_locale', false) === false || |
||
646 | $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
||
647 | $permittedFields[] = 'locale'; |
||
648 | } |
||
649 | |||
650 | $permittedFields[] = IAccountManager::PROPERTY_PHONE; |
||
651 | $permittedFields[] = IAccountManager::PROPERTY_ADDRESS; |
||
652 | $permittedFields[] = IAccountManager::PROPERTY_WEBSITE; |
||
653 | $permittedFields[] = IAccountManager::PROPERTY_TWITTER; |
||
654 | $permittedFields[] = IAccountManager::PROPERTY_PHONE . self::SCOPE_SUFFIX; |
||
655 | $permittedFields[] = IAccountManager::PROPERTY_ADDRESS . self::SCOPE_SUFFIX; |
||
656 | $permittedFields[] = IAccountManager::PROPERTY_WEBSITE . self::SCOPE_SUFFIX; |
||
657 | $permittedFields[] = IAccountManager::PROPERTY_TWITTER . self::SCOPE_SUFFIX; |
||
658 | |||
659 | $permittedFields[] = IAccountManager::PROPERTY_AVATAR . self::SCOPE_SUFFIX; |
||
660 | |||
661 | // If admin they can edit their own quota |
||
662 | if ($this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
||
663 | $permittedFields[] = 'quota'; |
||
664 | } |
||
665 | } else { |
||
666 | // Check if admin / subadmin |
||
667 | $subAdminManager = $this->groupManager->getSubAdmin(); |
||
668 | if ($this->groupManager->isAdmin($currentLoggedInUser->getUID()) |
||
669 | || $subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
||
670 | // They have permissions over the user |
||
671 | if ($targetUser->getBackend() instanceof ISetDisplayNameBackend |
||
672 | || $targetUser->getBackend()->implementsActions(Backend::SET_DISPLAYNAME)) { |
||
673 | $permittedFields[] = 'display'; |
||
674 | $permittedFields[] = IAccountManager::PROPERTY_DISPLAYNAME; |
||
675 | } |
||
676 | $permittedFields[] = IAccountManager::PROPERTY_EMAIL; |
||
677 | $permittedFields[] = 'password'; |
||
678 | $permittedFields[] = 'language'; |
||
679 | $permittedFields[] = 'locale'; |
||
680 | $permittedFields[] = IAccountManager::PROPERTY_PHONE; |
||
681 | $permittedFields[] = IAccountManager::PROPERTY_ADDRESS; |
||
682 | $permittedFields[] = IAccountManager::PROPERTY_WEBSITE; |
||
683 | $permittedFields[] = IAccountManager::PROPERTY_TWITTER; |
||
684 | $permittedFields[] = 'quota'; |
||
685 | } else { |
||
686 | // No rights |
||
687 | throw new OCSException('', OCSController::RESPOND_NOT_FOUND); |
||
688 | } |
||
689 | } |
||
690 | // Check if permitted to edit this field |
||
691 | if (!in_array($key, $permittedFields)) { |
||
692 | throw new OCSException('', 103); |
||
693 | } |
||
694 | // Process the edit |
||
695 | switch ($key) { |
||
696 | case 'display': |
||
697 | case IAccountManager::PROPERTY_DISPLAYNAME: |
||
698 | $targetUser->setDisplayName($value); |
||
699 | break; |
||
700 | case 'quota': |
||
701 | $quota = $value; |
||
702 | if ($quota !== 'none' && $quota !== 'default') { |
||
703 | if (is_numeric($quota)) { |
||
704 | $quota = (float) $quota; |
||
705 | } else { |
||
706 | $quota = \OCP\Util::computerFileSize($quota); |
||
707 | } |
||
708 | if ($quota === false) { |
||
709 | throw new OCSException('Invalid quota value '.$value, 102); |
||
710 | } |
||
711 | if ($quota === -1) { |
||
712 | $quota = 'none'; |
||
713 | } else { |
||
714 | $quota = \OCP\Util::humanFileSize($quota); |
||
715 | } |
||
716 | } |
||
717 | $targetUser->setQuota($quota); |
||
718 | break; |
||
719 | case 'password': |
||
720 | try { |
||
721 | if (!$targetUser->canChangePassword()) { |
||
722 | throw new OCSException('Setting the password is not supported by the users backend', 103); |
||
723 | } |
||
724 | $targetUser->setPassword($value); |
||
725 | } catch (HintException $e) { // password policy error |
||
726 | throw new OCSException($e->getMessage(), 103); |
||
727 | } |
||
728 | break; |
||
729 | case 'language': |
||
730 | $languagesCodes = $this->l10nFactory->findAvailableLanguages(); |
||
731 | if (!in_array($value, $languagesCodes, true) && $value !== 'en') { |
||
732 | throw new OCSException('Invalid language', 102); |
||
733 | } |
||
734 | $this->config->setUserValue($targetUser->getUID(), 'core', 'lang', $value); |
||
735 | break; |
||
736 | case 'locale': |
||
737 | if (!$this->l10nFactory->localeExists($value)) { |
||
738 | throw new OCSException('Invalid locale', 102); |
||
739 | } |
||
740 | $this->config->setUserValue($targetUser->getUID(), 'core', 'locale', $value); |
||
741 | break; |
||
742 | case IAccountManager::PROPERTY_EMAIL: |
||
743 | if (filter_var($value, FILTER_VALIDATE_EMAIL) || $value === '') { |
||
744 | $targetUser->setEMailAddress($value); |
||
745 | } else { |
||
746 | throw new OCSException('', 102); |
||
747 | } |
||
748 | break; |
||
749 | case IAccountManager::PROPERTY_PHONE: |
||
750 | case IAccountManager::PROPERTY_ADDRESS: |
||
751 | case IAccountManager::PROPERTY_WEBSITE: |
||
752 | case IAccountManager::PROPERTY_TWITTER: |
||
753 | $userAccount = $this->accountManager->getAccount($targetUser); |
||
754 | $userProperty = $userAccount->getProperty($key); |
||
755 | if ($userProperty->getValue() !== $value) { |
||
756 | try { |
||
757 | $userProperty->setValue($value); |
||
758 | $this->accountManager->updateAccount($userAccount); |
||
759 | |||
760 | if ($userProperty->getName() === IAccountManager::PROPERTY_PHONE) { |
||
761 | $this->knownUserService->deleteByContactUserId($targetUser->getUID()); |
||
762 | } |
||
763 | } catch (\InvalidArgumentException $e) { |
||
764 | throw new OCSException('Invalid ' . $e->getMessage(), 102); |
||
765 | } |
||
766 | } |
||
767 | break; |
||
768 | case IAccountManager::PROPERTY_DISPLAYNAME . self::SCOPE_SUFFIX: |
||
769 | case IAccountManager::PROPERTY_EMAIL . self::SCOPE_SUFFIX: |
||
770 | case IAccountManager::PROPERTY_PHONE . self::SCOPE_SUFFIX: |
||
771 | case IAccountManager::PROPERTY_ADDRESS . self::SCOPE_SUFFIX: |
||
772 | case IAccountManager::PROPERTY_WEBSITE . self::SCOPE_SUFFIX: |
||
773 | case IAccountManager::PROPERTY_TWITTER . self::SCOPE_SUFFIX: |
||
774 | case IAccountManager::PROPERTY_AVATAR . self::SCOPE_SUFFIX: |
||
775 | $propertyName = substr($key, 0, strlen($key) - strlen(self::SCOPE_SUFFIX)); |
||
776 | $userAccount = $this->accountManager->getAccount($targetUser); |
||
777 | $userProperty = $userAccount->getProperty($propertyName); |
||
778 | if ($userProperty->getScope() !== $value) { |
||
779 | try { |
||
780 | $userProperty->setScope($value); |
||
781 | $this->accountManager->updateAccount($userAccount); |
||
782 | } catch (\InvalidArgumentException $e) { |
||
783 | throw new OCSException('Invalid ' . $e->getMessage(), 102); |
||
784 | } |
||
785 | } |
||
786 | break; |
||
787 | default: |
||
788 | throw new OCSException('', 103); |
||
789 | } |
||
790 | return new DataResponse(); |
||
791 | } |
||
792 | |||
793 | /** |
||
794 | * @PasswordConfirmationRequired |
||
795 | * @NoAdminRequired |
||
796 | * |
||
797 | * @param string $userId |
||
798 | * |
||
799 | * @return DataResponse |
||
800 | * |
||
801 | * @throws OCSException |
||
802 | */ |
||
803 | public function wipeUserDevices(string $userId): DataResponse { |
||
804 | /** @var IUser $currentLoggedInUser */ |
||
805 | $currentLoggedInUser = $this->userSession->getUser(); |
||
806 | |||
807 | $targetUser = $this->userManager->get($userId); |
||
808 | |||
809 | if ($targetUser === null) { |
||
810 | throw new OCSException('', OCSController::RESPOND_NOT_FOUND); |
||
811 | } |
||
812 | |||
813 | if ($targetUser->getUID() === $currentLoggedInUser->getUID()) { |
||
814 | throw new OCSException('', 101); |
||
815 | } |
||
816 | |||
817 | // If not permitted |
||
818 | $subAdminManager = $this->groupManager->getSubAdmin(); |
||
819 | if (!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
||
820 | throw new OCSException('', OCSController::RESPOND_NOT_FOUND); |
||
821 | } |
||
822 | |||
823 | $this->remoteWipe->markAllTokensForWipe($targetUser); |
||
824 | |||
825 | return new DataResponse(); |
||
826 | } |
||
827 | |||
828 | /** |
||
829 | * @PasswordConfirmationRequired |
||
830 | * @NoAdminRequired |
||
831 | * |
||
832 | * @param string $userId |
||
833 | * @return DataResponse |
||
834 | * @throws OCSException |
||
835 | */ |
||
836 | public function deleteUser(string $userId): DataResponse { |
||
837 | $currentLoggedInUser = $this->userSession->getUser(); |
||
838 | |||
839 | $targetUser = $this->userManager->get($userId); |
||
840 | |||
841 | if ($targetUser === null) { |
||
842 | throw new OCSException('', OCSController::RESPOND_NOT_FOUND); |
||
843 | } |
||
844 | |||
845 | if ($targetUser->getUID() === $currentLoggedInUser->getUID()) { |
||
846 | throw new OCSException('', 101); |
||
847 | } |
||
848 | |||
849 | // If not permitted |
||
850 | $subAdminManager = $this->groupManager->getSubAdmin(); |
||
851 | if (!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
||
852 | throw new OCSException('', OCSController::RESPOND_NOT_FOUND); |
||
853 | } |
||
854 | |||
855 | // Go ahead with the delete |
||
856 | if ($targetUser->delete()) { |
||
857 | return new DataResponse(); |
||
858 | } else { |
||
859 | throw new OCSException('', 101); |
||
860 | } |
||
861 | } |
||
862 | |||
863 | /** |
||
864 | * @PasswordConfirmationRequired |
||
865 | * @NoAdminRequired |
||
866 | * |
||
867 | * @param string $userId |
||
868 | * @return DataResponse |
||
869 | * @throws OCSException |
||
870 | * @throws OCSForbiddenException |
||
871 | */ |
||
872 | public function disableUser(string $userId): DataResponse { |
||
873 | return $this->setEnabled($userId, false); |
||
874 | } |
||
875 | |||
876 | /** |
||
877 | * @PasswordConfirmationRequired |
||
878 | * @NoAdminRequired |
||
879 | * |
||
880 | * @param string $userId |
||
881 | * @return DataResponse |
||
882 | * @throws OCSException |
||
883 | * @throws OCSForbiddenException |
||
884 | */ |
||
885 | public function enableUser(string $userId): DataResponse { |
||
886 | return $this->setEnabled($userId, true); |
||
887 | } |
||
888 | |||
889 | /** |
||
890 | * @param string $userId |
||
891 | * @param bool $value |
||
892 | * @return DataResponse |
||
893 | * @throws OCSException |
||
894 | */ |
||
895 | private function setEnabled(string $userId, bool $value): DataResponse { |
||
912 | } |
||
913 | |||
914 | /** |
||
915 | * @NoAdminRequired |
||
916 | * @NoSubAdminRequired |
||
917 | * |
||
918 | * @param string $userId |
||
919 | * @return DataResponse |
||
920 | * @throws OCSException |
||
921 | */ |
||
922 | public function getUsersGroups(string $userId): DataResponse { |
||
923 | $loggedInUser = $this->userSession->getUser(); |
||
924 | |||
925 | $targetUser = $this->userManager->get($userId); |
||
926 | if ($targetUser === null) { |
||
927 | throw new OCSException('', OCSController::RESPOND_NOT_FOUND); |
||
928 | } |
||
929 | |||
930 | if ($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) { |
||
931 | // Self lookup or admin lookup |
||
932 | return new DataResponse([ |
||
933 | 'groups' => $this->groupManager->getUserGroupIds($targetUser) |
||
934 | ]); |
||
935 | } else { |
||
936 | $subAdminManager = $this->groupManager->getSubAdmin(); |
||
937 | |||
938 | // Looking up someone else |
||
939 | if ($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) { |
||
940 | // Return the group that the method caller is subadmin of for the user in question |
||
941 | /** @var IGroup[] $getSubAdminsGroups */ |
||
942 | $getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); |
||
943 | foreach ($getSubAdminsGroups as $key => $group) { |
||
944 | $getSubAdminsGroups[$key] = $group->getGID(); |
||
945 | } |
||
946 | $groups = array_intersect( |
||
947 | $getSubAdminsGroups, |
||
948 | $this->groupManager->getUserGroupIds($targetUser) |
||
949 | ); |
||
950 | return new DataResponse(['groups' => $groups]); |
||
951 | } else { |
||
952 | // Not permitted |
||
953 | throw new OCSException('', OCSController::RESPOND_NOT_FOUND); |
||
954 | } |
||
955 | } |
||
956 | } |
||
957 | |||
958 | /** |
||
959 | * @PasswordConfirmationRequired |
||
960 | * @NoAdminRequired |
||
961 | * |
||
962 | * @param string $userId |
||
963 | * @param string $groupid |
||
964 | * @return DataResponse |
||
965 | * @throws OCSException |
||
966 | */ |
||
967 | public function addToGroup(string $userId, string $groupid = ''): DataResponse { |
||
968 | if ($groupid === '') { |
||
969 | throw new OCSException('', 101); |
||
970 | } |
||
971 | |||
972 | $group = $this->groupManager->get($groupid); |
||
973 | $targetUser = $this->userManager->get($userId); |
||
974 | if ($group === null) { |
||
975 | throw new OCSException('', 102); |
||
976 | } |
||
977 | if ($targetUser === null) { |
||
978 | throw new OCSException('', 103); |
||
979 | } |
||
980 | |||
981 | // If they're not an admin, check they are a subadmin of the group in question |
||
982 | $loggedInUser = $this->userSession->getUser(); |
||
983 | $subAdminManager = $this->groupManager->getSubAdmin(); |
||
984 | if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) { |
||
985 | throw new OCSException('', 104); |
||
986 | } |
||
987 | |||
988 | // Add user to group |
||
989 | $group->addUser($targetUser); |
||
990 | return new DataResponse(); |
||
991 | } |
||
992 | |||
993 | /** |
||
994 | * @PasswordConfirmationRequired |
||
995 | * @NoAdminRequired |
||
996 | * |
||
997 | * @param string $userId |
||
998 | * @param string $groupid |
||
999 | * @return DataResponse |
||
1000 | * @throws OCSException |
||
1001 | */ |
||
1002 | public function removeFromGroup(string $userId, string $groupid): DataResponse { |
||
1003 | $loggedInUser = $this->userSession->getUser(); |
||
1004 | |||
1005 | if ($groupid === null || trim($groupid) === '') { |
||
1006 | throw new OCSException('', 101); |
||
1007 | } |
||
1008 | |||
1009 | $group = $this->groupManager->get($groupid); |
||
1010 | if ($group === null) { |
||
1011 | throw new OCSException('', 102); |
||
1012 | } |
||
1013 | |||
1014 | $targetUser = $this->userManager->get($userId); |
||
1015 | if ($targetUser === null) { |
||
1016 | throw new OCSException('', 103); |
||
1017 | } |
||
1018 | |||
1019 | // If they're not an admin, check they are a subadmin of the group in question |
||
1020 | $subAdminManager = $this->groupManager->getSubAdmin(); |
||
1021 | if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) { |
||
1022 | throw new OCSException('', 104); |
||
1023 | } |
||
1024 | |||
1025 | // Check they aren't removing themselves from 'admin' or their 'subadmin; group |
||
1026 | if ($targetUser->getUID() === $loggedInUser->getUID()) { |
||
1027 | if ($this->groupManager->isAdmin($loggedInUser->getUID())) { |
||
1028 | if ($group->getGID() === 'admin') { |
||
1029 | throw new OCSException('Cannot remove yourself from the admin group', 105); |
||
1030 | } |
||
1031 | } else { |
||
1032 | // Not an admin, so the user must be a subadmin of this group, but that is not allowed. |
||
1033 | throw new OCSException('Cannot remove yourself from this group as you are a SubAdmin', 105); |
||
1034 | } |
||
1035 | } elseif (!$this->groupManager->isAdmin($loggedInUser->getUID())) { |
||
1036 | /** @var IGroup[] $subAdminGroups */ |
||
1037 | $subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); |
||
1038 | $subAdminGroups = array_map(function (IGroup $subAdminGroup) { |
||
1039 | return $subAdminGroup->getGID(); |
||
1040 | }, $subAdminGroups); |
||
1041 | $userGroups = $this->groupManager->getUserGroupIds($targetUser); |
||
1042 | $userSubAdminGroups = array_intersect($subAdminGroups, $userGroups); |
||
1043 | |||
1044 | if (count($userSubAdminGroups) <= 1) { |
||
1045 | // Subadmin must not be able to remove a user from all their subadmin groups. |
||
1046 | throw new OCSException('Not viable to remove user from the last group you are SubAdmin of', 105); |
||
1047 | } |
||
1048 | } |
||
1049 | |||
1050 | // Remove user from group |
||
1051 | $group->removeUser($targetUser); |
||
1052 | return new DataResponse(); |
||
1053 | } |
||
1054 | |||
1055 | /** |
||
1056 | * Creates a subadmin |
||
1057 | * |
||
1058 | * @PasswordConfirmationRequired |
||
1059 | * |
||
1060 | * @param string $userId |
||
1061 | * @param string $groupid |
||
1062 | * @return DataResponse |
||
1063 | * @throws OCSException |
||
1064 | */ |
||
1065 | public function addSubAdmin(string $userId, string $groupid): DataResponse { |
||
1066 | $group = $this->groupManager->get($groupid); |
||
1067 | $user = $this->userManager->get($userId); |
||
1068 | |||
1069 | // Check if the user exists |
||
1070 | if ($user === null) { |
||
1071 | throw new OCSException('User does not exist', 101); |
||
1072 | } |
||
1073 | // Check if group exists |
||
1074 | if ($group === null) { |
||
1075 | throw new OCSException('Group does not exist', 102); |
||
1076 | } |
||
1077 | // Check if trying to make subadmin of admin group |
||
1078 | if ($group->getGID() === 'admin') { |
||
1079 | throw new OCSException('Cannot create subadmins for admin group', 103); |
||
1080 | } |
||
1081 | |||
1082 | $subAdminManager = $this->groupManager->getSubAdmin(); |
||
1083 | |||
1084 | // We cannot be subadmin twice |
||
1085 | if ($subAdminManager->isSubAdminOfGroup($user, $group)) { |
||
1086 | return new DataResponse(); |
||
1087 | } |
||
1088 | // Go |
||
1089 | $subAdminManager->createSubAdmin($user, $group); |
||
1090 | return new DataResponse(); |
||
1091 | } |
||
1092 | |||
1093 | /** |
||
1094 | * Removes a subadmin from a group |
||
1095 | * |
||
1096 | * @PasswordConfirmationRequired |
||
1097 | * |
||
1098 | * @param string $userId |
||
1099 | * @param string $groupid |
||
1100 | * @return DataResponse |
||
1101 | * @throws OCSException |
||
1102 | */ |
||
1103 | public function removeSubAdmin(string $userId, string $groupid): DataResponse { |
||
1104 | $group = $this->groupManager->get($groupid); |
||
1105 | $user = $this->userManager->get($userId); |
||
1106 | $subAdminManager = $this->groupManager->getSubAdmin(); |
||
1107 | |||
1108 | // Check if the user exists |
||
1109 | if ($user === null) { |
||
1110 | throw new OCSException('User does not exist', 101); |
||
1111 | } |
||
1112 | // Check if the group exists |
||
1113 | if ($group === null) { |
||
1114 | throw new OCSException('Group does not exist', 101); |
||
1115 | } |
||
1116 | // Check if they are a subadmin of this said group |
||
1117 | if (!$subAdminManager->isSubAdminOfGroup($user, $group)) { |
||
1118 | throw new OCSException('User is not a subadmin of this group', 102); |
||
1119 | } |
||
1120 | |||
1121 | // Go |
||
1122 | $subAdminManager->deleteSubAdmin($user, $group); |
||
1123 | return new DataResponse(); |
||
1124 | } |
||
1125 | |||
1126 | /** |
||
1127 | * Get the groups a user is a subadmin of |
||
1128 | * |
||
1129 | * @param string $userId |
||
1130 | * @return DataResponse |
||
1131 | * @throws OCSException |
||
1132 | */ |
||
1133 | public function getUserSubAdminGroups(string $userId): DataResponse { |
||
1136 | } |
||
1137 | |||
1138 | /** |
||
1139 | * @NoAdminRequired |
||
1140 | * @PasswordConfirmationRequired |
||
1141 | * |
||
1142 | * resend welcome message |
||
1143 | * |
||
1144 | * @param string $userId |
||
1145 | * @return DataResponse |
||
1146 | * @throws OCSException |
||
1147 | */ |
||
1148 | public function resendWelcomeMessage(string $userId): DataResponse { |
||
1183 | } |
||
1184 | } |
||
1185 |