@@ -40,163 +40,163 @@ |
||
| 40 | 40 | |
| 41 | 41 | class GroupsController extends OCSController { |
| 42 | 42 | |
| 43 | - /** @var IGroupManager */ |
|
| 44 | - private $groupManager; |
|
| 45 | - |
|
| 46 | - /** @var IUserSession */ |
|
| 47 | - private $userSession; |
|
| 48 | - |
|
| 49 | - /** @var ILogger */ |
|
| 50 | - private $logger; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @param string $appName |
|
| 54 | - * @param IRequest $request |
|
| 55 | - * @param IGroupManager $groupManager |
|
| 56 | - * @param IUserSession $userSession |
|
| 57 | - * @param ILogger $logger |
|
| 58 | - */ |
|
| 59 | - public function __construct( |
|
| 60 | - string $appName, |
|
| 61 | - IRequest $request, |
|
| 62 | - IGroupManager $groupManager, |
|
| 63 | - IUserSession $userSession, |
|
| 64 | - ILogger $logger) { |
|
| 65 | - parent::__construct($appName, $request); |
|
| 66 | - |
|
| 67 | - $this->groupManager = $groupManager; |
|
| 68 | - $this->userSession = $userSession; |
|
| 69 | - $this->logger = $logger; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * returns a list of groups |
|
| 74 | - * |
|
| 75 | - * @NoAdminRequired |
|
| 76 | - * |
|
| 77 | - * @param string $search |
|
| 78 | - * @param int $limit |
|
| 79 | - * @param int $offset |
|
| 80 | - * @return DataResponse |
|
| 81 | - */ |
|
| 82 | - public function getGroups(string $search = '', $limit = null, $offset = null): DataResponse { |
|
| 83 | - if ($limit !== null) { |
|
| 84 | - $limit = (int)$limit; |
|
| 85 | - } |
|
| 86 | - if ($offset !== null) { |
|
| 87 | - $offset = (int)$offset; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - $groups = $this->groupManager->search($search, $limit, $offset); |
|
| 91 | - $groups = array_map(function($group) { |
|
| 92 | - /** @var IGroup $group */ |
|
| 93 | - return ['id' => $group->getGID(), 'displayname' => $group->getDisplayName()]; |
|
| 94 | - }, $groups); |
|
| 95 | - |
|
| 96 | - return new DataResponse(['groups' => $groups]); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * returns an array of users in the group specified |
|
| 101 | - * |
|
| 102 | - * @NoAdminRequired |
|
| 103 | - * |
|
| 104 | - * @param string $groupId |
|
| 105 | - * @return DataResponse |
|
| 106 | - * @throws OCSException |
|
| 107 | - */ |
|
| 108 | - public function getGroup(string $groupId): DataResponse { |
|
| 109 | - $user = $this->userSession->getUser(); |
|
| 110 | - |
|
| 111 | - // Check the group exists |
|
| 112 | - if(!$this->groupManager->groupExists($groupId)) { |
|
| 113 | - throw new OCSException('The requested group could not be found', \OCP\API::RESPOND_NOT_FOUND); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - $isSubadminOfGroup = false; |
|
| 117 | - $group = $this->groupManager->get($groupId); |
|
| 118 | - if ($group !== null) { |
|
| 119 | - $isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminofGroup($user, $group); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - // Check subadmin has access to this group |
|
| 123 | - if($this->groupManager->isAdmin($user->getUID()) |
|
| 124 | - || $isSubadminOfGroup) { |
|
| 125 | - $users = $this->groupManager->get($groupId)->getUsers(); |
|
| 126 | - $users = array_map(function($user) { |
|
| 127 | - /** @var IUser $user */ |
|
| 128 | - return $user->getUID(); |
|
| 129 | - }, $users); |
|
| 130 | - $users = array_values($users); |
|
| 131 | - return new DataResponse(['users' => $users]); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - throw new OCSException('User does not have access to specified group', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * creates a new group |
|
| 139 | - * |
|
| 140 | - * @PasswordConfirmationRequired |
|
| 141 | - * |
|
| 142 | - * @param string $groupid |
|
| 143 | - * @return DataResponse |
|
| 144 | - * @throws OCSException |
|
| 145 | - */ |
|
| 146 | - public function addGroup(string $groupid): DataResponse { |
|
| 147 | - // Validate name |
|
| 148 | - if(empty($groupid)) { |
|
| 149 | - $this->logger->error('Group name not supplied', ['app' => 'provisioning_api']); |
|
| 150 | - throw new OCSException('Invalid group name', 101); |
|
| 151 | - } |
|
| 152 | - // Check if it exists |
|
| 153 | - if($this->groupManager->groupExists($groupid)){ |
|
| 154 | - throw new OCSException('', 102); |
|
| 155 | - } |
|
| 156 | - $this->groupManager->createGroup($groupid); |
|
| 157 | - return new DataResponse(); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * @PasswordConfirmationRequired |
|
| 162 | - * |
|
| 163 | - * @param string $groupId |
|
| 164 | - * @return DataResponse |
|
| 165 | - * @throws OCSException |
|
| 166 | - */ |
|
| 167 | - public function deleteGroup(string $groupId): DataResponse { |
|
| 168 | - // Check it exists |
|
| 169 | - if(!$this->groupManager->groupExists($groupId)){ |
|
| 170 | - throw new OCSException('', 101); |
|
| 171 | - } else if($groupId === 'admin' || !$this->groupManager->get($groupId)->delete()){ |
|
| 172 | - // Cannot delete admin group |
|
| 173 | - throw new OCSException('', 102); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - return new DataResponse(); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * @param string $groupId |
|
| 181 | - * @return DataResponse |
|
| 182 | - * @throws OCSException |
|
| 183 | - */ |
|
| 184 | - public function getSubAdminsOfGroup(string $groupId): DataResponse { |
|
| 185 | - // Check group exists |
|
| 186 | - $targetGroup = $this->groupManager->get($groupId); |
|
| 187 | - if($targetGroup === null) { |
|
| 188 | - throw new OCSException('Group does not exist', 101); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** @var IUser[] $subadmins */ |
|
| 192 | - $subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup); |
|
| 193 | - // New class returns IUser[] so convert back |
|
| 194 | - $uids = []; |
|
| 195 | - foreach ($subadmins as $user) { |
|
| 196 | - $uids[] = $user->getUID(); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - return new DataResponse($uids); |
|
| 200 | - } |
|
| 43 | + /** @var IGroupManager */ |
|
| 44 | + private $groupManager; |
|
| 45 | + |
|
| 46 | + /** @var IUserSession */ |
|
| 47 | + private $userSession; |
|
| 48 | + |
|
| 49 | + /** @var ILogger */ |
|
| 50 | + private $logger; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @param string $appName |
|
| 54 | + * @param IRequest $request |
|
| 55 | + * @param IGroupManager $groupManager |
|
| 56 | + * @param IUserSession $userSession |
|
| 57 | + * @param ILogger $logger |
|
| 58 | + */ |
|
| 59 | + public function __construct( |
|
| 60 | + string $appName, |
|
| 61 | + IRequest $request, |
|
| 62 | + IGroupManager $groupManager, |
|
| 63 | + IUserSession $userSession, |
|
| 64 | + ILogger $logger) { |
|
| 65 | + parent::__construct($appName, $request); |
|
| 66 | + |
|
| 67 | + $this->groupManager = $groupManager; |
|
| 68 | + $this->userSession = $userSession; |
|
| 69 | + $this->logger = $logger; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * returns a list of groups |
|
| 74 | + * |
|
| 75 | + * @NoAdminRequired |
|
| 76 | + * |
|
| 77 | + * @param string $search |
|
| 78 | + * @param int $limit |
|
| 79 | + * @param int $offset |
|
| 80 | + * @return DataResponse |
|
| 81 | + */ |
|
| 82 | + public function getGroups(string $search = '', $limit = null, $offset = null): DataResponse { |
|
| 83 | + if ($limit !== null) { |
|
| 84 | + $limit = (int)$limit; |
|
| 85 | + } |
|
| 86 | + if ($offset !== null) { |
|
| 87 | + $offset = (int)$offset; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + $groups = $this->groupManager->search($search, $limit, $offset); |
|
| 91 | + $groups = array_map(function($group) { |
|
| 92 | + /** @var IGroup $group */ |
|
| 93 | + return ['id' => $group->getGID(), 'displayname' => $group->getDisplayName()]; |
|
| 94 | + }, $groups); |
|
| 95 | + |
|
| 96 | + return new DataResponse(['groups' => $groups]); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * returns an array of users in the group specified |
|
| 101 | + * |
|
| 102 | + * @NoAdminRequired |
|
| 103 | + * |
|
| 104 | + * @param string $groupId |
|
| 105 | + * @return DataResponse |
|
| 106 | + * @throws OCSException |
|
| 107 | + */ |
|
| 108 | + public function getGroup(string $groupId): DataResponse { |
|
| 109 | + $user = $this->userSession->getUser(); |
|
| 110 | + |
|
| 111 | + // Check the group exists |
|
| 112 | + if(!$this->groupManager->groupExists($groupId)) { |
|
| 113 | + throw new OCSException('The requested group could not be found', \OCP\API::RESPOND_NOT_FOUND); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + $isSubadminOfGroup = false; |
|
| 117 | + $group = $this->groupManager->get($groupId); |
|
| 118 | + if ($group !== null) { |
|
| 119 | + $isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminofGroup($user, $group); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + // Check subadmin has access to this group |
|
| 123 | + if($this->groupManager->isAdmin($user->getUID()) |
|
| 124 | + || $isSubadminOfGroup) { |
|
| 125 | + $users = $this->groupManager->get($groupId)->getUsers(); |
|
| 126 | + $users = array_map(function($user) { |
|
| 127 | + /** @var IUser $user */ |
|
| 128 | + return $user->getUID(); |
|
| 129 | + }, $users); |
|
| 130 | + $users = array_values($users); |
|
| 131 | + return new DataResponse(['users' => $users]); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + throw new OCSException('User does not have access to specified group', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * creates a new group |
|
| 139 | + * |
|
| 140 | + * @PasswordConfirmationRequired |
|
| 141 | + * |
|
| 142 | + * @param string $groupid |
|
| 143 | + * @return DataResponse |
|
| 144 | + * @throws OCSException |
|
| 145 | + */ |
|
| 146 | + public function addGroup(string $groupid): DataResponse { |
|
| 147 | + // Validate name |
|
| 148 | + if(empty($groupid)) { |
|
| 149 | + $this->logger->error('Group name not supplied', ['app' => 'provisioning_api']); |
|
| 150 | + throw new OCSException('Invalid group name', 101); |
|
| 151 | + } |
|
| 152 | + // Check if it exists |
|
| 153 | + if($this->groupManager->groupExists($groupid)){ |
|
| 154 | + throw new OCSException('', 102); |
|
| 155 | + } |
|
| 156 | + $this->groupManager->createGroup($groupid); |
|
| 157 | + return new DataResponse(); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * @PasswordConfirmationRequired |
|
| 162 | + * |
|
| 163 | + * @param string $groupId |
|
| 164 | + * @return DataResponse |
|
| 165 | + * @throws OCSException |
|
| 166 | + */ |
|
| 167 | + public function deleteGroup(string $groupId): DataResponse { |
|
| 168 | + // Check it exists |
|
| 169 | + if(!$this->groupManager->groupExists($groupId)){ |
|
| 170 | + throw new OCSException('', 101); |
|
| 171 | + } else if($groupId === 'admin' || !$this->groupManager->get($groupId)->delete()){ |
|
| 172 | + // Cannot delete admin group |
|
| 173 | + throw new OCSException('', 102); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + return new DataResponse(); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * @param string $groupId |
|
| 181 | + * @return DataResponse |
|
| 182 | + * @throws OCSException |
|
| 183 | + */ |
|
| 184 | + public function getSubAdminsOfGroup(string $groupId): DataResponse { |
|
| 185 | + // Check group exists |
|
| 186 | + $targetGroup = $this->groupManager->get($groupId); |
|
| 187 | + if($targetGroup === null) { |
|
| 188 | + throw new OCSException('Group does not exist', 101); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** @var IUser[] $subadmins */ |
|
| 192 | + $subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup); |
|
| 193 | + // New class returns IUser[] so convert back |
|
| 194 | + $uids = []; |
|
| 195 | + foreach ($subadmins as $user) { |
|
| 196 | + $uids[] = $user->getUID(); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + return new DataResponse($uids); |
|
| 200 | + } |
|
| 201 | 201 | |
| 202 | 202 | } |