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