@@ -44,239 +44,239 @@ |
||
| 44 | 44 | |
| 45 | 45 | class GroupsController extends AUserData { |
| 46 | 46 | |
| 47 | - /** @var ILogger */ |
|
| 48 | - private $logger; |
|
| 47 | + /** @var ILogger */ |
|
| 48 | + private $logger; |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * @param string $appName |
|
| 52 | - * @param IRequest $request |
|
| 53 | - * @param IUserManager $userManager |
|
| 54 | - * @param IConfig $config |
|
| 55 | - * @param IGroupManager $groupManager |
|
| 56 | - * @param IUserSession $userSession |
|
| 57 | - * @param AccountManager $accountManager |
|
| 58 | - * @param ILogger $logger |
|
| 59 | - * @param UsersController $userController |
|
| 60 | - */ |
|
| 61 | - public function __construct(string $appName, |
|
| 62 | - IRequest $request, |
|
| 63 | - IUserManager $userManager, |
|
| 64 | - IConfig $config, |
|
| 65 | - IGroupManager $groupManager, |
|
| 66 | - IUserSession $userSession, |
|
| 67 | - AccountManager $accountManager, |
|
| 68 | - ILogger $logger) { |
|
| 69 | - parent::__construct($appName, |
|
| 70 | - $request, |
|
| 71 | - $userManager, |
|
| 72 | - $config, |
|
| 73 | - $groupManager, |
|
| 74 | - $userSession, |
|
| 75 | - $accountManager); |
|
| 50 | + /** |
|
| 51 | + * @param string $appName |
|
| 52 | + * @param IRequest $request |
|
| 53 | + * @param IUserManager $userManager |
|
| 54 | + * @param IConfig $config |
|
| 55 | + * @param IGroupManager $groupManager |
|
| 56 | + * @param IUserSession $userSession |
|
| 57 | + * @param AccountManager $accountManager |
|
| 58 | + * @param ILogger $logger |
|
| 59 | + * @param UsersController $userController |
|
| 60 | + */ |
|
| 61 | + public function __construct(string $appName, |
|
| 62 | + IRequest $request, |
|
| 63 | + IUserManager $userManager, |
|
| 64 | + IConfig $config, |
|
| 65 | + IGroupManager $groupManager, |
|
| 66 | + IUserSession $userSession, |
|
| 67 | + AccountManager $accountManager, |
|
| 68 | + ILogger $logger) { |
|
| 69 | + parent::__construct($appName, |
|
| 70 | + $request, |
|
| 71 | + $userManager, |
|
| 72 | + $config, |
|
| 73 | + $groupManager, |
|
| 74 | + $userSession, |
|
| 75 | + $accountManager); |
|
| 76 | 76 | |
| 77 | - $this->logger = $logger; |
|
| 78 | - } |
|
| 77 | + $this->logger = $logger; |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * returns a list of groups |
|
| 82 | - * |
|
| 83 | - * @NoAdminRequired |
|
| 84 | - * |
|
| 85 | - * @param string $search |
|
| 86 | - * @param int $limit |
|
| 87 | - * @param int $offset |
|
| 88 | - * @return DataResponse |
|
| 89 | - */ |
|
| 90 | - public function getGroups(string $search = '', int $limit = null, int $offset = 0): DataResponse { |
|
| 91 | - $groups = $this->groupManager->search($search, $limit, $offset); |
|
| 92 | - $groups = array_map(function($group) { |
|
| 93 | - /** @var IGroup $group */ |
|
| 94 | - return $group->getGID(); |
|
| 95 | - }, $groups); |
|
| 80 | + /** |
|
| 81 | + * returns a list of groups |
|
| 82 | + * |
|
| 83 | + * @NoAdminRequired |
|
| 84 | + * |
|
| 85 | + * @param string $search |
|
| 86 | + * @param int $limit |
|
| 87 | + * @param int $offset |
|
| 88 | + * @return DataResponse |
|
| 89 | + */ |
|
| 90 | + public function getGroups(string $search = '', int $limit = null, int $offset = 0): DataResponse { |
|
| 91 | + $groups = $this->groupManager->search($search, $limit, $offset); |
|
| 92 | + $groups = array_map(function($group) { |
|
| 93 | + /** @var IGroup $group */ |
|
| 94 | + return $group->getGID(); |
|
| 95 | + }, $groups); |
|
| 96 | 96 | |
| 97 | - return new DataResponse(['groups' => $groups]); |
|
| 98 | - } |
|
| 97 | + return new DataResponse(['groups' => $groups]); |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * returns a list of groups details with ids and displaynames |
|
| 102 | - * |
|
| 103 | - * @NoAdminRequired |
|
| 104 | - * |
|
| 105 | - * @param string $search |
|
| 106 | - * @param int $limit |
|
| 107 | - * @param int $offset |
|
| 108 | - * @return DataResponse |
|
| 109 | - */ |
|
| 110 | - public function getGroupsDetails(string $search = '', int $limit = null, int $offset = 0): DataResponse { |
|
| 111 | - $groups = $this->groupManager->search($search, $limit, $offset); |
|
| 112 | - $groups = array_map(function($group) { |
|
| 113 | - /** @var IGroup $group */ |
|
| 114 | - return ['id' => $group->getGID(), 'displayname' => $group->getDisplayName()]; |
|
| 115 | - }, $groups); |
|
| 100 | + /** |
|
| 101 | + * returns a list of groups details with ids and displaynames |
|
| 102 | + * |
|
| 103 | + * @NoAdminRequired |
|
| 104 | + * |
|
| 105 | + * @param string $search |
|
| 106 | + * @param int $limit |
|
| 107 | + * @param int $offset |
|
| 108 | + * @return DataResponse |
|
| 109 | + */ |
|
| 110 | + public function getGroupsDetails(string $search = '', int $limit = null, int $offset = 0): DataResponse { |
|
| 111 | + $groups = $this->groupManager->search($search, $limit, $offset); |
|
| 112 | + $groups = array_map(function($group) { |
|
| 113 | + /** @var IGroup $group */ |
|
| 114 | + return ['id' => $group->getGID(), 'displayname' => $group->getDisplayName()]; |
|
| 115 | + }, $groups); |
|
| 116 | 116 | |
| 117 | - return new DataResponse(['groups' => $groups]); |
|
| 118 | - } |
|
| 117 | + return new DataResponse(['groups' => $groups]); |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * @NoAdminRequired |
|
| 122 | - * |
|
| 123 | - * @param string $groupId |
|
| 124 | - * @return DataResponse |
|
| 125 | - * @throws OCSException |
|
| 126 | - * |
|
| 127 | - * @deprecated 14 Use getGroupUsers |
|
| 128 | - */ |
|
| 129 | - public function getGroup(string $groupId): DataResponse { |
|
| 130 | - return $this->getGroupUsers($groupId); |
|
| 131 | - } |
|
| 120 | + /** |
|
| 121 | + * @NoAdminRequired |
|
| 122 | + * |
|
| 123 | + * @param string $groupId |
|
| 124 | + * @return DataResponse |
|
| 125 | + * @throws OCSException |
|
| 126 | + * |
|
| 127 | + * @deprecated 14 Use getGroupUsers |
|
| 128 | + */ |
|
| 129 | + public function getGroup(string $groupId): DataResponse { |
|
| 130 | + return $this->getGroupUsers($groupId); |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - /** |
|
| 134 | - * returns an array of users in the specified group |
|
| 135 | - * |
|
| 136 | - * @NoAdminRequired |
|
| 137 | - * |
|
| 138 | - * @param string $groupId |
|
| 139 | - * @return DataResponse |
|
| 140 | - * @throws OCSException |
|
| 141 | - */ |
|
| 142 | - public function getGroupUsers(string $groupId): DataResponse { |
|
| 143 | - $user = $this->userSession->getUser(); |
|
| 144 | - $isSubadminOfGroup = false; |
|
| 133 | + /** |
|
| 134 | + * returns an array of users in the specified group |
|
| 135 | + * |
|
| 136 | + * @NoAdminRequired |
|
| 137 | + * |
|
| 138 | + * @param string $groupId |
|
| 139 | + * @return DataResponse |
|
| 140 | + * @throws OCSException |
|
| 141 | + */ |
|
| 142 | + public function getGroupUsers(string $groupId): DataResponse { |
|
| 143 | + $user = $this->userSession->getUser(); |
|
| 144 | + $isSubadminOfGroup = false; |
|
| 145 | 145 | |
| 146 | - // Check the group exists |
|
| 147 | - $group = $this->groupManager->get($groupId); |
|
| 148 | - if ($group !== null) { |
|
| 149 | - $isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group); |
|
| 150 | - } else { |
|
| 151 | - throw new OCSNotFoundException('The requested group could not be found'); |
|
| 152 | - } |
|
| 146 | + // Check the group exists |
|
| 147 | + $group = $this->groupManager->get($groupId); |
|
| 148 | + if ($group !== null) { |
|
| 149 | + $isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group); |
|
| 150 | + } else { |
|
| 151 | + throw new OCSNotFoundException('The requested group could not be found'); |
|
| 152 | + } |
|
| 153 | 153 | |
| 154 | - // Check subadmin has access to this group |
|
| 155 | - if($this->groupManager->isAdmin($user->getUID()) |
|
| 156 | - || $isSubadminOfGroup) { |
|
| 157 | - $users = $this->groupManager->get($groupId)->getUsers(); |
|
| 158 | - $users = array_map(function($user) { |
|
| 159 | - /** @var IUser $user */ |
|
| 160 | - return $user->getUID(); |
|
| 161 | - }, $users); |
|
| 162 | - $users = array_values($users); |
|
| 163 | - return new DataResponse(['users' => $users]); |
|
| 164 | - } |
|
| 154 | + // Check subadmin has access to this group |
|
| 155 | + if($this->groupManager->isAdmin($user->getUID()) |
|
| 156 | + || $isSubadminOfGroup) { |
|
| 157 | + $users = $this->groupManager->get($groupId)->getUsers(); |
|
| 158 | + $users = array_map(function($user) { |
|
| 159 | + /** @var IUser $user */ |
|
| 160 | + return $user->getUID(); |
|
| 161 | + }, $users); |
|
| 162 | + $users = array_values($users); |
|
| 163 | + return new DataResponse(['users' => $users]); |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - throw new OCSForbiddenException(); |
|
| 167 | - } |
|
| 166 | + throw new OCSForbiddenException(); |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - /** |
|
| 170 | - * returns an array of users details in the specified group |
|
| 171 | - * |
|
| 172 | - * @NoAdminRequired |
|
| 173 | - * |
|
| 174 | - * @param string $groupId |
|
| 175 | - * @param int $limit |
|
| 176 | - * @param int $offset |
|
| 177 | - * @return DataResponse |
|
| 178 | - * @throws OCSException |
|
| 179 | - */ |
|
| 180 | - public function getGroupUsersDetails(string $groupId, int $limit = null, int $offset = 0): DataResponse { |
|
| 181 | - $user = $this->userSession->getUser(); |
|
| 182 | - $isSubadminOfGroup = false; |
|
| 169 | + /** |
|
| 170 | + * returns an array of users details in the specified group |
|
| 171 | + * |
|
| 172 | + * @NoAdminRequired |
|
| 173 | + * |
|
| 174 | + * @param string $groupId |
|
| 175 | + * @param int $limit |
|
| 176 | + * @param int $offset |
|
| 177 | + * @return DataResponse |
|
| 178 | + * @throws OCSException |
|
| 179 | + */ |
|
| 180 | + public function getGroupUsersDetails(string $groupId, int $limit = null, int $offset = 0): DataResponse { |
|
| 181 | + $user = $this->userSession->getUser(); |
|
| 182 | + $isSubadminOfGroup = false; |
|
| 183 | 183 | |
| 184 | - // Check the group exists |
|
| 185 | - $group = $this->groupManager->get($groupId); |
|
| 186 | - if ($group !== null) { |
|
| 187 | - $isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group); |
|
| 188 | - } else { |
|
| 189 | - throw new OCSException('The requested group could not be found', \OCP\API::RESPOND_NOT_FOUND); |
|
| 190 | - } |
|
| 184 | + // Check the group exists |
|
| 185 | + $group = $this->groupManager->get($groupId); |
|
| 186 | + if ($group !== null) { |
|
| 187 | + $isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group); |
|
| 188 | + } else { |
|
| 189 | + throw new OCSException('The requested group could not be found', \OCP\API::RESPOND_NOT_FOUND); |
|
| 190 | + } |
|
| 191 | 191 | |
| 192 | - // Check subadmin has access to this group |
|
| 193 | - if($this->groupManager->isAdmin($user->getUID()) |
|
| 194 | - || $isSubadminOfGroup) { |
|
| 195 | - $users = $this->groupManager->get($groupId)->getUsers(); |
|
| 196 | - // Extract required number |
|
| 197 | - $users = array_slice($users, $offset, $limit); |
|
| 198 | - $users = array_keys($users); |
|
| 199 | - $usersDetails = []; |
|
| 200 | - foreach ($users as $userId) { |
|
| 201 | - $userData = $this->getUserData($userId); |
|
| 202 | - // Do not insert empty entry |
|
| 203 | - if(!empty($userData)) { |
|
| 204 | - $usersDetails[$userId] = $userData; |
|
| 205 | - } |
|
| 206 | - } |
|
| 207 | - return new DataResponse(['users' => $usersDetails]); |
|
| 208 | - } |
|
| 192 | + // Check subadmin has access to this group |
|
| 193 | + if($this->groupManager->isAdmin($user->getUID()) |
|
| 194 | + || $isSubadminOfGroup) { |
|
| 195 | + $users = $this->groupManager->get($groupId)->getUsers(); |
|
| 196 | + // Extract required number |
|
| 197 | + $users = array_slice($users, $offset, $limit); |
|
| 198 | + $users = array_keys($users); |
|
| 199 | + $usersDetails = []; |
|
| 200 | + foreach ($users as $userId) { |
|
| 201 | + $userData = $this->getUserData($userId); |
|
| 202 | + // Do not insert empty entry |
|
| 203 | + if(!empty($userData)) { |
|
| 204 | + $usersDetails[$userId] = $userData; |
|
| 205 | + } |
|
| 206 | + } |
|
| 207 | + return new DataResponse(['users' => $usersDetails]); |
|
| 208 | + } |
|
| 209 | 209 | |
| 210 | - throw new OCSException('User does not have access to specified group', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 211 | - } |
|
| 210 | + throw new OCSException('User does not have access to specified group', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 211 | + } |
|
| 212 | 212 | |
| 213 | - /** |
|
| 214 | - * creates a new group |
|
| 215 | - * |
|
| 216 | - * @PasswordConfirmationRequired |
|
| 217 | - * |
|
| 218 | - * @param string $groupid |
|
| 219 | - * @return DataResponse |
|
| 220 | - * @throws OCSException |
|
| 221 | - */ |
|
| 222 | - public function addGroup(string $groupid): DataResponse { |
|
| 223 | - // Validate name |
|
| 224 | - if(empty($groupid)) { |
|
| 225 | - $this->logger->error('Group name not supplied', ['app' => 'provisioning_api']); |
|
| 226 | - throw new OCSException('Invalid group name', 101); |
|
| 227 | - } |
|
| 228 | - // Check if trying to create an admin group |
|
| 229 | - if (trim(strtolower($groupid)) === 'admin') { |
|
| 230 | - throw new OCSException('', 102); |
|
| 231 | - } |
|
| 232 | - // Check if it exists |
|
| 233 | - if($this->groupManager->groupExists($groupid)){ |
|
| 234 | - throw new OCSException('', 102); |
|
| 235 | - } |
|
| 236 | - $this->groupManager->createGroup($groupid); |
|
| 237 | - return new DataResponse(); |
|
| 238 | - } |
|
| 213 | + /** |
|
| 214 | + * creates a new group |
|
| 215 | + * |
|
| 216 | + * @PasswordConfirmationRequired |
|
| 217 | + * |
|
| 218 | + * @param string $groupid |
|
| 219 | + * @return DataResponse |
|
| 220 | + * @throws OCSException |
|
| 221 | + */ |
|
| 222 | + public function addGroup(string $groupid): DataResponse { |
|
| 223 | + // Validate name |
|
| 224 | + if(empty($groupid)) { |
|
| 225 | + $this->logger->error('Group name not supplied', ['app' => 'provisioning_api']); |
|
| 226 | + throw new OCSException('Invalid group name', 101); |
|
| 227 | + } |
|
| 228 | + // Check if trying to create an admin group |
|
| 229 | + if (trim(strtolower($groupid)) === 'admin') { |
|
| 230 | + throw new OCSException('', 102); |
|
| 231 | + } |
|
| 232 | + // Check if it exists |
|
| 233 | + if($this->groupManager->groupExists($groupid)){ |
|
| 234 | + throw new OCSException('', 102); |
|
| 235 | + } |
|
| 236 | + $this->groupManager->createGroup($groupid); |
|
| 237 | + return new DataResponse(); |
|
| 238 | + } |
|
| 239 | 239 | |
| 240 | - /** |
|
| 241 | - * @PasswordConfirmationRequired |
|
| 242 | - * |
|
| 243 | - * @param string $groupId |
|
| 244 | - * @return DataResponse |
|
| 245 | - * @throws OCSException |
|
| 246 | - */ |
|
| 247 | - public function deleteGroup(string $groupId): DataResponse { |
|
| 248 | - // Check it exists |
|
| 249 | - if(!$this->groupManager->groupExists($groupId)){ |
|
| 250 | - throw new OCSException('', 101); |
|
| 251 | - } else if($groupId === 'admin' || !$this->groupManager->get($groupId)->delete()){ |
|
| 252 | - // Cannot delete admin group |
|
| 253 | - throw new OCSException('', 102); |
|
| 254 | - } |
|
| 240 | + /** |
|
| 241 | + * @PasswordConfirmationRequired |
|
| 242 | + * |
|
| 243 | + * @param string $groupId |
|
| 244 | + * @return DataResponse |
|
| 245 | + * @throws OCSException |
|
| 246 | + */ |
|
| 247 | + public function deleteGroup(string $groupId): DataResponse { |
|
| 248 | + // Check it exists |
|
| 249 | + if(!$this->groupManager->groupExists($groupId)){ |
|
| 250 | + throw new OCSException('', 101); |
|
| 251 | + } else if($groupId === 'admin' || !$this->groupManager->get($groupId)->delete()){ |
|
| 252 | + // Cannot delete admin group |
|
| 253 | + throw new OCSException('', 102); |
|
| 254 | + } |
|
| 255 | 255 | |
| 256 | - return new DataResponse(); |
|
| 257 | - } |
|
| 256 | + return new DataResponse(); |
|
| 257 | + } |
|
| 258 | 258 | |
| 259 | - /** |
|
| 260 | - * @param string $groupId |
|
| 261 | - * @return DataResponse |
|
| 262 | - * @throws OCSException |
|
| 263 | - */ |
|
| 264 | - public function getSubAdminsOfGroup(string $groupId): DataResponse { |
|
| 265 | - // Check group exists |
|
| 266 | - $targetGroup = $this->groupManager->get($groupId); |
|
| 267 | - if($targetGroup === null) { |
|
| 268 | - throw new OCSException('Group does not exist', 101); |
|
| 269 | - } |
|
| 259 | + /** |
|
| 260 | + * @param string $groupId |
|
| 261 | + * @return DataResponse |
|
| 262 | + * @throws OCSException |
|
| 263 | + */ |
|
| 264 | + public function getSubAdminsOfGroup(string $groupId): DataResponse { |
|
| 265 | + // Check group exists |
|
| 266 | + $targetGroup = $this->groupManager->get($groupId); |
|
| 267 | + if($targetGroup === null) { |
|
| 268 | + throw new OCSException('Group does not exist', 101); |
|
| 269 | + } |
|
| 270 | 270 | |
| 271 | - /** @var IUser[] $subadmins */ |
|
| 272 | - $subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup); |
|
| 273 | - // New class returns IUser[] so convert back |
|
| 274 | - $uids = []; |
|
| 275 | - foreach ($subadmins as $user) { |
|
| 276 | - $uids[] = $user->getUID(); |
|
| 277 | - } |
|
| 271 | + /** @var IUser[] $subadmins */ |
|
| 272 | + $subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup); |
|
| 273 | + // New class returns IUser[] so convert back |
|
| 274 | + $uids = []; |
|
| 275 | + foreach ($subadmins as $user) { |
|
| 276 | + $uids[] = $user->getUID(); |
|
| 277 | + } |
|
| 278 | 278 | |
| 279 | - return new DataResponse($uids); |
|
| 280 | - } |
|
| 279 | + return new DataResponse($uids); |
|
| 280 | + } |
|
| 281 | 281 | |
| 282 | 282 | } |
@@ -146,16 +146,16 @@ discard block |
||
| 146 | 146 | // Check the group exists |
| 147 | 147 | $group = $this->groupManager->get($groupId); |
| 148 | 148 | if ($group !== null) { |
| 149 | - $isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group); |
|
| 149 | + $isSubadminOfGroup = $this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group); |
|
| 150 | 150 | } else { |
| 151 | 151 | throw new OCSNotFoundException('The requested group could not be found'); |
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | // Check subadmin has access to this group |
| 155 | - if($this->groupManager->isAdmin($user->getUID()) |
|
| 155 | + if ($this->groupManager->isAdmin($user->getUID()) |
|
| 156 | 156 | || $isSubadminOfGroup) { |
| 157 | 157 | $users = $this->groupManager->get($groupId)->getUsers(); |
| 158 | - $users = array_map(function($user) { |
|
| 158 | + $users = array_map(function($user) { |
|
| 159 | 159 | /** @var IUser $user */ |
| 160 | 160 | return $user->getUID(); |
| 161 | 161 | }, $users); |
@@ -184,13 +184,13 @@ discard block |
||
| 184 | 184 | // Check the group exists |
| 185 | 185 | $group = $this->groupManager->get($groupId); |
| 186 | 186 | if ($group !== null) { |
| 187 | - $isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group); |
|
| 187 | + $isSubadminOfGroup = $this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group); |
|
| 188 | 188 | } else { |
| 189 | 189 | throw new OCSException('The requested group could not be found', \OCP\API::RESPOND_NOT_FOUND); |
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | // Check subadmin has access to this group |
| 193 | - if($this->groupManager->isAdmin($user->getUID()) |
|
| 193 | + if ($this->groupManager->isAdmin($user->getUID()) |
|
| 194 | 194 | || $isSubadminOfGroup) { |
| 195 | 195 | $users = $this->groupManager->get($groupId)->getUsers(); |
| 196 | 196 | // Extract required number |
@@ -200,7 +200,7 @@ discard block |
||
| 200 | 200 | foreach ($users as $userId) { |
| 201 | 201 | $userData = $this->getUserData($userId); |
| 202 | 202 | // Do not insert empty entry |
| 203 | - if(!empty($userData)) { |
|
| 203 | + if (!empty($userData)) { |
|
| 204 | 204 | $usersDetails[$userId] = $userData; |
| 205 | 205 | } |
| 206 | 206 | } |
@@ -221,7 +221,7 @@ discard block |
||
| 221 | 221 | */ |
| 222 | 222 | public function addGroup(string $groupid): DataResponse { |
| 223 | 223 | // Validate name |
| 224 | - if(empty($groupid)) { |
|
| 224 | + if (empty($groupid)) { |
|
| 225 | 225 | $this->logger->error('Group name not supplied', ['app' => 'provisioning_api']); |
| 226 | 226 | throw new OCSException('Invalid group name', 101); |
| 227 | 227 | } |
@@ -230,7 +230,7 @@ discard block |
||
| 230 | 230 | throw new OCSException('', 102); |
| 231 | 231 | } |
| 232 | 232 | // Check if it exists |
| 233 | - if($this->groupManager->groupExists($groupid)){ |
|
| 233 | + if ($this->groupManager->groupExists($groupid)) { |
|
| 234 | 234 | throw new OCSException('', 102); |
| 235 | 235 | } |
| 236 | 236 | $this->groupManager->createGroup($groupid); |
@@ -246,9 +246,9 @@ discard block |
||
| 246 | 246 | */ |
| 247 | 247 | public function deleteGroup(string $groupId): DataResponse { |
| 248 | 248 | // Check it exists |
| 249 | - if(!$this->groupManager->groupExists($groupId)){ |
|
| 249 | + if (!$this->groupManager->groupExists($groupId)) { |
|
| 250 | 250 | throw new OCSException('', 101); |
| 251 | - } else if($groupId === 'admin' || !$this->groupManager->get($groupId)->delete()){ |
|
| 251 | + } else if ($groupId === 'admin' || !$this->groupManager->get($groupId)->delete()) { |
|
| 252 | 252 | // Cannot delete admin group |
| 253 | 253 | throw new OCSException('', 102); |
| 254 | 254 | } |
@@ -264,7 +264,7 @@ discard block |
||
| 264 | 264 | public function getSubAdminsOfGroup(string $groupId): DataResponse { |
| 265 | 265 | // Check group exists |
| 266 | 266 | $targetGroup = $this->groupManager->get($groupId); |
| 267 | - if($targetGroup === null) { |
|
| 267 | + if ($targetGroup === null) { |
|
| 268 | 268 | throw new OCSException('Group does not exist', 101); |
| 269 | 269 | } |
| 270 | 270 | |
@@ -61,350 +61,350 @@ |
||
| 61 | 61 | * @package OC\Group |
| 62 | 62 | */ |
| 63 | 63 | class Manager extends PublicEmitter implements IGroupManager { |
| 64 | - /** |
|
| 65 | - * @var GroupInterface[] $backends |
|
| 66 | - */ |
|
| 67 | - private $backends = array(); |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @var \OC\User\Manager $userManager |
|
| 71 | - */ |
|
| 72 | - private $userManager; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @var \OC\Group\Group[] |
|
| 76 | - */ |
|
| 77 | - private $cachedGroups = array(); |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @var \OC\Group\Group[] |
|
| 81 | - */ |
|
| 82 | - private $cachedUserGroups = array(); |
|
| 83 | - |
|
| 84 | - /** @var \OC\SubAdmin */ |
|
| 85 | - private $subAdmin = null; |
|
| 86 | - |
|
| 87 | - /** @var ILogger */ |
|
| 88 | - private $logger; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @param \OC\User\Manager $userManager |
|
| 92 | - * @param ILogger $logger |
|
| 93 | - */ |
|
| 94 | - public function __construct(\OC\User\Manager $userManager, ILogger $logger) { |
|
| 95 | - $this->userManager = $userManager; |
|
| 96 | - $this->logger = $logger; |
|
| 97 | - $cachedGroups = & $this->cachedGroups; |
|
| 98 | - $cachedUserGroups = & $this->cachedUserGroups; |
|
| 99 | - $this->listen('\OC\Group', 'postDelete', function ($group) use (&$cachedGroups, &$cachedUserGroups) { |
|
| 100 | - /** |
|
| 101 | - * @var \OC\Group\Group $group |
|
| 102 | - */ |
|
| 103 | - unset($cachedGroups[$group->getGID()]); |
|
| 104 | - $cachedUserGroups = array(); |
|
| 105 | - }); |
|
| 106 | - $this->listen('\OC\Group', 'postAddUser', function ($group) use (&$cachedUserGroups) { |
|
| 107 | - /** |
|
| 108 | - * @var \OC\Group\Group $group |
|
| 109 | - */ |
|
| 110 | - $cachedUserGroups = array(); |
|
| 111 | - }); |
|
| 112 | - $this->listen('\OC\Group', 'postRemoveUser', function ($group) use (&$cachedUserGroups) { |
|
| 113 | - /** |
|
| 114 | - * @var \OC\Group\Group $group |
|
| 115 | - */ |
|
| 116 | - $cachedUserGroups = array(); |
|
| 117 | - }); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Checks whether a given backend is used |
|
| 122 | - * |
|
| 123 | - * @param string $backendClass Full classname including complete namespace |
|
| 124 | - * @return bool |
|
| 125 | - */ |
|
| 126 | - public function isBackendUsed($backendClass) { |
|
| 127 | - $backendClass = strtolower(ltrim($backendClass, '\\')); |
|
| 128 | - |
|
| 129 | - foreach ($this->backends as $backend) { |
|
| 130 | - if (strtolower(get_class($backend)) === $backendClass) { |
|
| 131 | - return true; |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - return false; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * @param \OCP\GroupInterface $backend |
|
| 140 | - */ |
|
| 141 | - public function addBackend($backend) { |
|
| 142 | - $this->backends[] = $backend; |
|
| 143 | - $this->clearCaches(); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - public function clearBackends() { |
|
| 147 | - $this->backends = array(); |
|
| 148 | - $this->clearCaches(); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Get the active backends |
|
| 153 | - * @return \OCP\GroupInterface[] |
|
| 154 | - */ |
|
| 155 | - public function getBackends() { |
|
| 156 | - return $this->backends; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - |
|
| 160 | - protected function clearCaches() { |
|
| 161 | - $this->cachedGroups = array(); |
|
| 162 | - $this->cachedUserGroups = array(); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * @param string $gid |
|
| 167 | - * @return \OC\Group\Group |
|
| 168 | - */ |
|
| 169 | - public function get($gid) { |
|
| 170 | - if (isset($this->cachedGroups[$gid])) { |
|
| 171 | - return $this->cachedGroups[$gid]; |
|
| 172 | - } |
|
| 173 | - return $this->getGroupObject($gid); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * @param string $gid |
|
| 178 | - * @param string $displayName |
|
| 179 | - * @return \OCP\IGroup |
|
| 180 | - */ |
|
| 181 | - protected function getGroupObject($gid, $displayName = null) { |
|
| 182 | - $backends = array(); |
|
| 183 | - foreach ($this->backends as $backend) { |
|
| 184 | - if ($backend->implementsActions(\OC\Group\Backend::GROUP_DETAILS)) { |
|
| 185 | - $groupData = $backend->getGroupDetails($gid); |
|
| 186 | - if (is_array($groupData)) { |
|
| 187 | - // take the display name from the first backend that has a non-null one |
|
| 188 | - if (is_null($displayName) && isset($groupData['displayName'])) { |
|
| 189 | - $displayName = $groupData['displayName']; |
|
| 190 | - } |
|
| 191 | - $backends[] = $backend; |
|
| 192 | - } |
|
| 193 | - } else if ($backend->groupExists($gid)) { |
|
| 194 | - $backends[] = $backend; |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - if (count($backends) === 0) { |
|
| 198 | - return null; |
|
| 199 | - } |
|
| 200 | - $this->cachedGroups[$gid] = new Group($gid, $backends, $this->userManager, $this, $displayName); |
|
| 201 | - return $this->cachedGroups[$gid]; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * @param string $gid |
|
| 206 | - * @return bool |
|
| 207 | - */ |
|
| 208 | - public function groupExists($gid) { |
|
| 209 | - return $this->get($gid) instanceof IGroup; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * @param string $gid |
|
| 214 | - * @return \OC\Group\Group |
|
| 215 | - */ |
|
| 216 | - public function createGroup($gid) { |
|
| 217 | - if ($gid === '' || $gid === null) { |
|
| 218 | - return false; |
|
| 219 | - } else if (trim(strtolower($gid)) === 'admin' && $this->get('admin')) { |
|
| 220 | - return $this->get('admin'); |
|
| 221 | - } else if ($group = $this->get($gid)) { |
|
| 222 | - return $group; |
|
| 223 | - } else { |
|
| 224 | - $this->emit('\OC\Group', 'preCreate', array($gid)); |
|
| 225 | - foreach ($this->backends as $backend) { |
|
| 226 | - if ($backend->implementsActions(\OC\Group\Backend::CREATE_GROUP)) { |
|
| 227 | - $backend->createGroup($gid); |
|
| 228 | - $group = $this->getGroupObject($gid); |
|
| 229 | - $this->emit('\OC\Group', 'postCreate', array($group)); |
|
| 230 | - return $group; |
|
| 231 | - } |
|
| 232 | - } |
|
| 233 | - return null; |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * @param string $search |
|
| 239 | - * @param int $limit |
|
| 240 | - * @param int $offset |
|
| 241 | - * @return \OC\Group\Group[] |
|
| 242 | - */ |
|
| 243 | - public function search($search, $limit = null, $offset = null) { |
|
| 244 | - $groups = array(); |
|
| 245 | - foreach ($this->backends as $backend) { |
|
| 246 | - $groupIds = $backend->getGroups($search, $limit, $offset); |
|
| 247 | - foreach ($groupIds as $groupId) { |
|
| 248 | - $aGroup = $this->get($groupId); |
|
| 249 | - if ($aGroup instanceof IGroup) { |
|
| 250 | - $groups[$groupId] = $aGroup; |
|
| 251 | - } else { |
|
| 252 | - $this->logger->debug('Group "' . $groupId . '" was returned by search but not found through direct access', ['app' => 'core']); |
|
| 253 | - } |
|
| 254 | - } |
|
| 255 | - if (!is_null($limit) and $limit <= 0) { |
|
| 256 | - return array_values($groups); |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - return array_values($groups); |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * @param IUser|null $user |
|
| 264 | - * @return \OC\Group\Group[] |
|
| 265 | - */ |
|
| 266 | - public function getUserGroups(IUser $user= null) { |
|
| 267 | - if (!$user instanceof IUser) { |
|
| 268 | - return []; |
|
| 269 | - } |
|
| 270 | - return $this->getUserIdGroups($user->getUID()); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * @param string $uid the user id |
|
| 275 | - * @return \OC\Group\Group[] |
|
| 276 | - */ |
|
| 277 | - public function getUserIdGroups($uid) { |
|
| 278 | - if (isset($this->cachedUserGroups[$uid])) { |
|
| 279 | - return $this->cachedUserGroups[$uid]; |
|
| 280 | - } |
|
| 281 | - $groups = array(); |
|
| 282 | - foreach ($this->backends as $backend) { |
|
| 283 | - $groupIds = $backend->getUserGroups($uid); |
|
| 284 | - if (is_array($groupIds)) { |
|
| 285 | - foreach ($groupIds as $groupId) { |
|
| 286 | - $aGroup = $this->get($groupId); |
|
| 287 | - if ($aGroup instanceof IGroup) { |
|
| 288 | - $groups[$groupId] = $aGroup; |
|
| 289 | - } else { |
|
| 290 | - $this->logger->debug('User "' . $uid . '" belongs to deleted group: "' . $groupId . '"', ['app' => 'core']); |
|
| 291 | - } |
|
| 292 | - } |
|
| 293 | - } |
|
| 294 | - } |
|
| 295 | - $this->cachedUserGroups[$uid] = $groups; |
|
| 296 | - return $this->cachedUserGroups[$uid]; |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * Checks if a userId is in the admin group |
|
| 301 | - * @param string $userId |
|
| 302 | - * @return bool if admin |
|
| 303 | - */ |
|
| 304 | - public function isAdmin($userId) { |
|
| 305 | - foreach ($this->backends as $backend) { |
|
| 306 | - if ($backend->implementsActions(\OC\Group\Backend::IS_ADMIN) && $backend->isAdmin($userId)) { |
|
| 307 | - return true; |
|
| 308 | - } |
|
| 309 | - } |
|
| 310 | - return $this->isInGroup($userId, 'admin'); |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Checks if a userId is in a group |
|
| 315 | - * @param string $userId |
|
| 316 | - * @param string $group |
|
| 317 | - * @return bool if in group |
|
| 318 | - */ |
|
| 319 | - public function isInGroup($userId, $group) { |
|
| 320 | - return array_key_exists($group, $this->getUserIdGroups($userId)); |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * get a list of group ids for a user |
|
| 325 | - * @param IUser $user |
|
| 326 | - * @return array with group ids |
|
| 327 | - */ |
|
| 328 | - public function getUserGroupIds(IUser $user) { |
|
| 329 | - return array_map(function($value) { |
|
| 330 | - return (string) $value; |
|
| 331 | - }, array_keys($this->getUserGroups($user))); |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * get an array of groupid and displayName for a user |
|
| 336 | - * @param IUser $user |
|
| 337 | - * @return array ['displayName' => displayname] |
|
| 338 | - */ |
|
| 339 | - public function getUserGroupNames(IUser $user) { |
|
| 340 | - return array_map(function($group) { |
|
| 341 | - return array('displayName' => $group->getDisplayName()); |
|
| 342 | - }, $this->getUserGroups($user)); |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * get a list of all display names in a group |
|
| 347 | - * @param string $gid |
|
| 348 | - * @param string $search |
|
| 349 | - * @param int $limit |
|
| 350 | - * @param int $offset |
|
| 351 | - * @return array an array of display names (value) and user ids (key) |
|
| 352 | - */ |
|
| 353 | - public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
| 354 | - $group = $this->get($gid); |
|
| 355 | - if(is_null($group)) { |
|
| 356 | - return array(); |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - $search = trim($search); |
|
| 360 | - $groupUsers = array(); |
|
| 361 | - |
|
| 362 | - if(!empty($search)) { |
|
| 363 | - // only user backends have the capability to do a complex search for users |
|
| 364 | - $searchOffset = 0; |
|
| 365 | - $searchLimit = $limit * 100; |
|
| 366 | - if($limit === -1) { |
|
| 367 | - $searchLimit = 500; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - do { |
|
| 371 | - $filteredUsers = $this->userManager->searchDisplayName($search, $searchLimit, $searchOffset); |
|
| 372 | - foreach($filteredUsers as $filteredUser) { |
|
| 373 | - if($group->inGroup($filteredUser)) { |
|
| 374 | - $groupUsers[]= $filteredUser; |
|
| 375 | - } |
|
| 376 | - } |
|
| 377 | - $searchOffset += $searchLimit; |
|
| 378 | - } while(count($groupUsers) < $searchLimit+$offset && count($filteredUsers) >= $searchLimit); |
|
| 379 | - |
|
| 380 | - if($limit === -1) { |
|
| 381 | - $groupUsers = array_slice($groupUsers, $offset); |
|
| 382 | - } else { |
|
| 383 | - $groupUsers = array_slice($groupUsers, $offset, $limit); |
|
| 384 | - } |
|
| 385 | - } else { |
|
| 386 | - $groupUsers = $group->searchUsers('', $limit, $offset); |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - $matchingUsers = array(); |
|
| 390 | - foreach($groupUsers as $groupUser) { |
|
| 391 | - $matchingUsers[$groupUser->getUID()] = $groupUser->getDisplayName(); |
|
| 392 | - } |
|
| 393 | - return $matchingUsers; |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * @return \OC\SubAdmin |
|
| 398 | - */ |
|
| 399 | - public function getSubAdmin() { |
|
| 400 | - if (!$this->subAdmin) { |
|
| 401 | - $this->subAdmin = new \OC\SubAdmin( |
|
| 402 | - $this->userManager, |
|
| 403 | - $this, |
|
| 404 | - \OC::$server->getDatabaseConnection() |
|
| 405 | - ); |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - return $this->subAdmin; |
|
| 409 | - } |
|
| 64 | + /** |
|
| 65 | + * @var GroupInterface[] $backends |
|
| 66 | + */ |
|
| 67 | + private $backends = array(); |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @var \OC\User\Manager $userManager |
|
| 71 | + */ |
|
| 72 | + private $userManager; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @var \OC\Group\Group[] |
|
| 76 | + */ |
|
| 77 | + private $cachedGroups = array(); |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @var \OC\Group\Group[] |
|
| 81 | + */ |
|
| 82 | + private $cachedUserGroups = array(); |
|
| 83 | + |
|
| 84 | + /** @var \OC\SubAdmin */ |
|
| 85 | + private $subAdmin = null; |
|
| 86 | + |
|
| 87 | + /** @var ILogger */ |
|
| 88 | + private $logger; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @param \OC\User\Manager $userManager |
|
| 92 | + * @param ILogger $logger |
|
| 93 | + */ |
|
| 94 | + public function __construct(\OC\User\Manager $userManager, ILogger $logger) { |
|
| 95 | + $this->userManager = $userManager; |
|
| 96 | + $this->logger = $logger; |
|
| 97 | + $cachedGroups = & $this->cachedGroups; |
|
| 98 | + $cachedUserGroups = & $this->cachedUserGroups; |
|
| 99 | + $this->listen('\OC\Group', 'postDelete', function ($group) use (&$cachedGroups, &$cachedUserGroups) { |
|
| 100 | + /** |
|
| 101 | + * @var \OC\Group\Group $group |
|
| 102 | + */ |
|
| 103 | + unset($cachedGroups[$group->getGID()]); |
|
| 104 | + $cachedUserGroups = array(); |
|
| 105 | + }); |
|
| 106 | + $this->listen('\OC\Group', 'postAddUser', function ($group) use (&$cachedUserGroups) { |
|
| 107 | + /** |
|
| 108 | + * @var \OC\Group\Group $group |
|
| 109 | + */ |
|
| 110 | + $cachedUserGroups = array(); |
|
| 111 | + }); |
|
| 112 | + $this->listen('\OC\Group', 'postRemoveUser', function ($group) use (&$cachedUserGroups) { |
|
| 113 | + /** |
|
| 114 | + * @var \OC\Group\Group $group |
|
| 115 | + */ |
|
| 116 | + $cachedUserGroups = array(); |
|
| 117 | + }); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Checks whether a given backend is used |
|
| 122 | + * |
|
| 123 | + * @param string $backendClass Full classname including complete namespace |
|
| 124 | + * @return bool |
|
| 125 | + */ |
|
| 126 | + public function isBackendUsed($backendClass) { |
|
| 127 | + $backendClass = strtolower(ltrim($backendClass, '\\')); |
|
| 128 | + |
|
| 129 | + foreach ($this->backends as $backend) { |
|
| 130 | + if (strtolower(get_class($backend)) === $backendClass) { |
|
| 131 | + return true; |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + return false; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * @param \OCP\GroupInterface $backend |
|
| 140 | + */ |
|
| 141 | + public function addBackend($backend) { |
|
| 142 | + $this->backends[] = $backend; |
|
| 143 | + $this->clearCaches(); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + public function clearBackends() { |
|
| 147 | + $this->backends = array(); |
|
| 148 | + $this->clearCaches(); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Get the active backends |
|
| 153 | + * @return \OCP\GroupInterface[] |
|
| 154 | + */ |
|
| 155 | + public function getBackends() { |
|
| 156 | + return $this->backends; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + |
|
| 160 | + protected function clearCaches() { |
|
| 161 | + $this->cachedGroups = array(); |
|
| 162 | + $this->cachedUserGroups = array(); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * @param string $gid |
|
| 167 | + * @return \OC\Group\Group |
|
| 168 | + */ |
|
| 169 | + public function get($gid) { |
|
| 170 | + if (isset($this->cachedGroups[$gid])) { |
|
| 171 | + return $this->cachedGroups[$gid]; |
|
| 172 | + } |
|
| 173 | + return $this->getGroupObject($gid); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * @param string $gid |
|
| 178 | + * @param string $displayName |
|
| 179 | + * @return \OCP\IGroup |
|
| 180 | + */ |
|
| 181 | + protected function getGroupObject($gid, $displayName = null) { |
|
| 182 | + $backends = array(); |
|
| 183 | + foreach ($this->backends as $backend) { |
|
| 184 | + if ($backend->implementsActions(\OC\Group\Backend::GROUP_DETAILS)) { |
|
| 185 | + $groupData = $backend->getGroupDetails($gid); |
|
| 186 | + if (is_array($groupData)) { |
|
| 187 | + // take the display name from the first backend that has a non-null one |
|
| 188 | + if (is_null($displayName) && isset($groupData['displayName'])) { |
|
| 189 | + $displayName = $groupData['displayName']; |
|
| 190 | + } |
|
| 191 | + $backends[] = $backend; |
|
| 192 | + } |
|
| 193 | + } else if ($backend->groupExists($gid)) { |
|
| 194 | + $backends[] = $backend; |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + if (count($backends) === 0) { |
|
| 198 | + return null; |
|
| 199 | + } |
|
| 200 | + $this->cachedGroups[$gid] = new Group($gid, $backends, $this->userManager, $this, $displayName); |
|
| 201 | + return $this->cachedGroups[$gid]; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * @param string $gid |
|
| 206 | + * @return bool |
|
| 207 | + */ |
|
| 208 | + public function groupExists($gid) { |
|
| 209 | + return $this->get($gid) instanceof IGroup; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * @param string $gid |
|
| 214 | + * @return \OC\Group\Group |
|
| 215 | + */ |
|
| 216 | + public function createGroup($gid) { |
|
| 217 | + if ($gid === '' || $gid === null) { |
|
| 218 | + return false; |
|
| 219 | + } else if (trim(strtolower($gid)) === 'admin' && $this->get('admin')) { |
|
| 220 | + return $this->get('admin'); |
|
| 221 | + } else if ($group = $this->get($gid)) { |
|
| 222 | + return $group; |
|
| 223 | + } else { |
|
| 224 | + $this->emit('\OC\Group', 'preCreate', array($gid)); |
|
| 225 | + foreach ($this->backends as $backend) { |
|
| 226 | + if ($backend->implementsActions(\OC\Group\Backend::CREATE_GROUP)) { |
|
| 227 | + $backend->createGroup($gid); |
|
| 228 | + $group = $this->getGroupObject($gid); |
|
| 229 | + $this->emit('\OC\Group', 'postCreate', array($group)); |
|
| 230 | + return $group; |
|
| 231 | + } |
|
| 232 | + } |
|
| 233 | + return null; |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * @param string $search |
|
| 239 | + * @param int $limit |
|
| 240 | + * @param int $offset |
|
| 241 | + * @return \OC\Group\Group[] |
|
| 242 | + */ |
|
| 243 | + public function search($search, $limit = null, $offset = null) { |
|
| 244 | + $groups = array(); |
|
| 245 | + foreach ($this->backends as $backend) { |
|
| 246 | + $groupIds = $backend->getGroups($search, $limit, $offset); |
|
| 247 | + foreach ($groupIds as $groupId) { |
|
| 248 | + $aGroup = $this->get($groupId); |
|
| 249 | + if ($aGroup instanceof IGroup) { |
|
| 250 | + $groups[$groupId] = $aGroup; |
|
| 251 | + } else { |
|
| 252 | + $this->logger->debug('Group "' . $groupId . '" was returned by search but not found through direct access', ['app' => 'core']); |
|
| 253 | + } |
|
| 254 | + } |
|
| 255 | + if (!is_null($limit) and $limit <= 0) { |
|
| 256 | + return array_values($groups); |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + return array_values($groups); |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * @param IUser|null $user |
|
| 264 | + * @return \OC\Group\Group[] |
|
| 265 | + */ |
|
| 266 | + public function getUserGroups(IUser $user= null) { |
|
| 267 | + if (!$user instanceof IUser) { |
|
| 268 | + return []; |
|
| 269 | + } |
|
| 270 | + return $this->getUserIdGroups($user->getUID()); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * @param string $uid the user id |
|
| 275 | + * @return \OC\Group\Group[] |
|
| 276 | + */ |
|
| 277 | + public function getUserIdGroups($uid) { |
|
| 278 | + if (isset($this->cachedUserGroups[$uid])) { |
|
| 279 | + return $this->cachedUserGroups[$uid]; |
|
| 280 | + } |
|
| 281 | + $groups = array(); |
|
| 282 | + foreach ($this->backends as $backend) { |
|
| 283 | + $groupIds = $backend->getUserGroups($uid); |
|
| 284 | + if (is_array($groupIds)) { |
|
| 285 | + foreach ($groupIds as $groupId) { |
|
| 286 | + $aGroup = $this->get($groupId); |
|
| 287 | + if ($aGroup instanceof IGroup) { |
|
| 288 | + $groups[$groupId] = $aGroup; |
|
| 289 | + } else { |
|
| 290 | + $this->logger->debug('User "' . $uid . '" belongs to deleted group: "' . $groupId . '"', ['app' => 'core']); |
|
| 291 | + } |
|
| 292 | + } |
|
| 293 | + } |
|
| 294 | + } |
|
| 295 | + $this->cachedUserGroups[$uid] = $groups; |
|
| 296 | + return $this->cachedUserGroups[$uid]; |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * Checks if a userId is in the admin group |
|
| 301 | + * @param string $userId |
|
| 302 | + * @return bool if admin |
|
| 303 | + */ |
|
| 304 | + public function isAdmin($userId) { |
|
| 305 | + foreach ($this->backends as $backend) { |
|
| 306 | + if ($backend->implementsActions(\OC\Group\Backend::IS_ADMIN) && $backend->isAdmin($userId)) { |
|
| 307 | + return true; |
|
| 308 | + } |
|
| 309 | + } |
|
| 310 | + return $this->isInGroup($userId, 'admin'); |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Checks if a userId is in a group |
|
| 315 | + * @param string $userId |
|
| 316 | + * @param string $group |
|
| 317 | + * @return bool if in group |
|
| 318 | + */ |
|
| 319 | + public function isInGroup($userId, $group) { |
|
| 320 | + return array_key_exists($group, $this->getUserIdGroups($userId)); |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * get a list of group ids for a user |
|
| 325 | + * @param IUser $user |
|
| 326 | + * @return array with group ids |
|
| 327 | + */ |
|
| 328 | + public function getUserGroupIds(IUser $user) { |
|
| 329 | + return array_map(function($value) { |
|
| 330 | + return (string) $value; |
|
| 331 | + }, array_keys($this->getUserGroups($user))); |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * get an array of groupid and displayName for a user |
|
| 336 | + * @param IUser $user |
|
| 337 | + * @return array ['displayName' => displayname] |
|
| 338 | + */ |
|
| 339 | + public function getUserGroupNames(IUser $user) { |
|
| 340 | + return array_map(function($group) { |
|
| 341 | + return array('displayName' => $group->getDisplayName()); |
|
| 342 | + }, $this->getUserGroups($user)); |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * get a list of all display names in a group |
|
| 347 | + * @param string $gid |
|
| 348 | + * @param string $search |
|
| 349 | + * @param int $limit |
|
| 350 | + * @param int $offset |
|
| 351 | + * @return array an array of display names (value) and user ids (key) |
|
| 352 | + */ |
|
| 353 | + public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
| 354 | + $group = $this->get($gid); |
|
| 355 | + if(is_null($group)) { |
|
| 356 | + return array(); |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + $search = trim($search); |
|
| 360 | + $groupUsers = array(); |
|
| 361 | + |
|
| 362 | + if(!empty($search)) { |
|
| 363 | + // only user backends have the capability to do a complex search for users |
|
| 364 | + $searchOffset = 0; |
|
| 365 | + $searchLimit = $limit * 100; |
|
| 366 | + if($limit === -1) { |
|
| 367 | + $searchLimit = 500; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + do { |
|
| 371 | + $filteredUsers = $this->userManager->searchDisplayName($search, $searchLimit, $searchOffset); |
|
| 372 | + foreach($filteredUsers as $filteredUser) { |
|
| 373 | + if($group->inGroup($filteredUser)) { |
|
| 374 | + $groupUsers[]= $filteredUser; |
|
| 375 | + } |
|
| 376 | + } |
|
| 377 | + $searchOffset += $searchLimit; |
|
| 378 | + } while(count($groupUsers) < $searchLimit+$offset && count($filteredUsers) >= $searchLimit); |
|
| 379 | + |
|
| 380 | + if($limit === -1) { |
|
| 381 | + $groupUsers = array_slice($groupUsers, $offset); |
|
| 382 | + } else { |
|
| 383 | + $groupUsers = array_slice($groupUsers, $offset, $limit); |
|
| 384 | + } |
|
| 385 | + } else { |
|
| 386 | + $groupUsers = $group->searchUsers('', $limit, $offset); |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + $matchingUsers = array(); |
|
| 390 | + foreach($groupUsers as $groupUser) { |
|
| 391 | + $matchingUsers[$groupUser->getUID()] = $groupUser->getDisplayName(); |
|
| 392 | + } |
|
| 393 | + return $matchingUsers; |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + /** |
|
| 397 | + * @return \OC\SubAdmin |
|
| 398 | + */ |
|
| 399 | + public function getSubAdmin() { |
|
| 400 | + if (!$this->subAdmin) { |
|
| 401 | + $this->subAdmin = new \OC\SubAdmin( |
|
| 402 | + $this->userManager, |
|
| 403 | + $this, |
|
| 404 | + \OC::$server->getDatabaseConnection() |
|
| 405 | + ); |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + return $this->subAdmin; |
|
| 409 | + } |
|
| 410 | 410 | } |