@@ -61,348 +61,348 @@ |
||
| 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) && !empty($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 ($group = $this->get($gid)) { |
|
| 220 | - return $group; |
|
| 221 | - } else { |
|
| 222 | - $this->emit('\OC\Group', 'preCreate', array($gid)); |
|
| 223 | - foreach ($this->backends as $backend) { |
|
| 224 | - if ($backend->implementsActions(\OC\Group\Backend::CREATE_GROUP)) { |
|
| 225 | - $backend->createGroup($gid); |
|
| 226 | - $group = $this->getGroupObject($gid); |
|
| 227 | - $this->emit('\OC\Group', 'postCreate', array($group)); |
|
| 228 | - return $group; |
|
| 229 | - } |
|
| 230 | - } |
|
| 231 | - return null; |
|
| 232 | - } |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * @param string $search |
|
| 237 | - * @param int $limit |
|
| 238 | - * @param int $offset |
|
| 239 | - * @return \OC\Group\Group[] |
|
| 240 | - */ |
|
| 241 | - public function search($search, $limit = null, $offset = null) { |
|
| 242 | - $groups = array(); |
|
| 243 | - foreach ($this->backends as $backend) { |
|
| 244 | - $groupIds = $backend->getGroups($search, $limit, $offset); |
|
| 245 | - foreach ($groupIds as $groupId) { |
|
| 246 | - $aGroup = $this->get($groupId); |
|
| 247 | - if ($aGroup instanceof IGroup) { |
|
| 248 | - $groups[$groupId] = $aGroup; |
|
| 249 | - } else { |
|
| 250 | - $this->logger->debug('Group "' . $groupId . '" was returned by search but not found through direct access', ['app' => 'core']); |
|
| 251 | - } |
|
| 252 | - } |
|
| 253 | - if (!is_null($limit) and $limit <= 0) { |
|
| 254 | - return array_values($groups); |
|
| 255 | - } |
|
| 256 | - } |
|
| 257 | - return array_values($groups); |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * @param IUser|null $user |
|
| 262 | - * @return \OC\Group\Group[] |
|
| 263 | - */ |
|
| 264 | - public function getUserGroups(IUser $user= null) { |
|
| 265 | - if (!$user instanceof IUser) { |
|
| 266 | - return []; |
|
| 267 | - } |
|
| 268 | - return $this->getUserIdGroups($user->getUID()); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * @param string $uid the user id |
|
| 273 | - * @return \OC\Group\Group[] |
|
| 274 | - */ |
|
| 275 | - public function getUserIdGroups($uid) { |
|
| 276 | - if (isset($this->cachedUserGroups[$uid])) { |
|
| 277 | - return $this->cachedUserGroups[$uid]; |
|
| 278 | - } |
|
| 279 | - $groups = array(); |
|
| 280 | - foreach ($this->backends as $backend) { |
|
| 281 | - $groupIds = $backend->getUserGroups($uid); |
|
| 282 | - if (is_array($groupIds)) { |
|
| 283 | - foreach ($groupIds as $groupId) { |
|
| 284 | - $aGroup = $this->get($groupId); |
|
| 285 | - if ($aGroup instanceof IGroup) { |
|
| 286 | - $groups[$groupId] = $aGroup; |
|
| 287 | - } else { |
|
| 288 | - $this->logger->debug('User "' . $uid . '" belongs to deleted group: "' . $groupId . '"', ['app' => 'core']); |
|
| 289 | - } |
|
| 290 | - } |
|
| 291 | - } |
|
| 292 | - } |
|
| 293 | - $this->cachedUserGroups[$uid] = $groups; |
|
| 294 | - return $this->cachedUserGroups[$uid]; |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * Checks if a userId is in the admin group |
|
| 299 | - * @param string $userId |
|
| 300 | - * @return bool if admin |
|
| 301 | - */ |
|
| 302 | - public function isAdmin($userId) { |
|
| 303 | - foreach ($this->backends as $backend) { |
|
| 304 | - if ($backend->implementsActions(\OC\Group\Backend::IS_ADMIN) && $backend->isAdmin($userId)) { |
|
| 305 | - return true; |
|
| 306 | - } |
|
| 307 | - } |
|
| 308 | - return $this->isInGroup($userId, 'admin'); |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * Checks if a userId is in a group |
|
| 313 | - * @param string $userId |
|
| 314 | - * @param string $group |
|
| 315 | - * @return bool if in group |
|
| 316 | - */ |
|
| 317 | - public function isInGroup($userId, $group) { |
|
| 318 | - return array_key_exists($group, $this->getUserIdGroups($userId)); |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * get a list of group ids for a user |
|
| 323 | - * @param IUser $user |
|
| 324 | - * @return array with group ids |
|
| 325 | - */ |
|
| 326 | - public function getUserGroupIds(IUser $user) { |
|
| 327 | - return array_map(function($value) { |
|
| 328 | - return (string) $value; |
|
| 329 | - }, array_keys($this->getUserGroups($user))); |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * get an array of groupid and displayName for a user |
|
| 334 | - * @param IUser $user |
|
| 335 | - * @return array ['displayName' => displayname] |
|
| 336 | - */ |
|
| 337 | - public function getUserGroupNames(IUser $user) { |
|
| 338 | - return array_map(function($group) { |
|
| 339 | - return array('displayName' => $group->getDisplayName()); |
|
| 340 | - }, $this->getUserGroups($user)); |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * get a list of all display names in a group |
|
| 345 | - * @param string $gid |
|
| 346 | - * @param string $search |
|
| 347 | - * @param int $limit |
|
| 348 | - * @param int $offset |
|
| 349 | - * @return array an array of display names (value) and user ids (key) |
|
| 350 | - */ |
|
| 351 | - public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
| 352 | - $group = $this->get($gid); |
|
| 353 | - if(is_null($group)) { |
|
| 354 | - return array(); |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - $search = trim($search); |
|
| 358 | - $groupUsers = array(); |
|
| 359 | - |
|
| 360 | - if(!empty($search)) { |
|
| 361 | - // only user backends have the capability to do a complex search for users |
|
| 362 | - $searchOffset = 0; |
|
| 363 | - $searchLimit = $limit * 100; |
|
| 364 | - if($limit === -1) { |
|
| 365 | - $searchLimit = 500; |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - do { |
|
| 369 | - $filteredUsers = $this->userManager->searchDisplayName($search, $searchLimit, $searchOffset); |
|
| 370 | - foreach($filteredUsers as $filteredUser) { |
|
| 371 | - if($group->inGroup($filteredUser)) { |
|
| 372 | - $groupUsers[]= $filteredUser; |
|
| 373 | - } |
|
| 374 | - } |
|
| 375 | - $searchOffset += $searchLimit; |
|
| 376 | - } while(count($groupUsers) < $searchLimit+$offset && count($filteredUsers) >= $searchLimit); |
|
| 377 | - |
|
| 378 | - if($limit === -1) { |
|
| 379 | - $groupUsers = array_slice($groupUsers, $offset); |
|
| 380 | - } else { |
|
| 381 | - $groupUsers = array_slice($groupUsers, $offset, $limit); |
|
| 382 | - } |
|
| 383 | - } else { |
|
| 384 | - $groupUsers = $group->searchUsers('', $limit, $offset); |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - $matchingUsers = array(); |
|
| 388 | - foreach($groupUsers as $groupUser) { |
|
| 389 | - $matchingUsers[$groupUser->getUID()] = $groupUser->getDisplayName(); |
|
| 390 | - } |
|
| 391 | - return $matchingUsers; |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - /** |
|
| 395 | - * @return \OC\SubAdmin |
|
| 396 | - */ |
|
| 397 | - public function getSubAdmin() { |
|
| 398 | - if (!$this->subAdmin) { |
|
| 399 | - $this->subAdmin = new \OC\SubAdmin( |
|
| 400 | - $this->userManager, |
|
| 401 | - $this, |
|
| 402 | - \OC::$server->getDatabaseConnection() |
|
| 403 | - ); |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - return $this->subAdmin; |
|
| 407 | - } |
|
| 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) && !empty($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 ($group = $this->get($gid)) { |
|
| 220 | + return $group; |
|
| 221 | + } else { |
|
| 222 | + $this->emit('\OC\Group', 'preCreate', array($gid)); |
|
| 223 | + foreach ($this->backends as $backend) { |
|
| 224 | + if ($backend->implementsActions(\OC\Group\Backend::CREATE_GROUP)) { |
|
| 225 | + $backend->createGroup($gid); |
|
| 226 | + $group = $this->getGroupObject($gid); |
|
| 227 | + $this->emit('\OC\Group', 'postCreate', array($group)); |
|
| 228 | + return $group; |
|
| 229 | + } |
|
| 230 | + } |
|
| 231 | + return null; |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * @param string $search |
|
| 237 | + * @param int $limit |
|
| 238 | + * @param int $offset |
|
| 239 | + * @return \OC\Group\Group[] |
|
| 240 | + */ |
|
| 241 | + public function search($search, $limit = null, $offset = null) { |
|
| 242 | + $groups = array(); |
|
| 243 | + foreach ($this->backends as $backend) { |
|
| 244 | + $groupIds = $backend->getGroups($search, $limit, $offset); |
|
| 245 | + foreach ($groupIds as $groupId) { |
|
| 246 | + $aGroup = $this->get($groupId); |
|
| 247 | + if ($aGroup instanceof IGroup) { |
|
| 248 | + $groups[$groupId] = $aGroup; |
|
| 249 | + } else { |
|
| 250 | + $this->logger->debug('Group "' . $groupId . '" was returned by search but not found through direct access', ['app' => 'core']); |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | + if (!is_null($limit) and $limit <= 0) { |
|
| 254 | + return array_values($groups); |
|
| 255 | + } |
|
| 256 | + } |
|
| 257 | + return array_values($groups); |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * @param IUser|null $user |
|
| 262 | + * @return \OC\Group\Group[] |
|
| 263 | + */ |
|
| 264 | + public function getUserGroups(IUser $user= null) { |
|
| 265 | + if (!$user instanceof IUser) { |
|
| 266 | + return []; |
|
| 267 | + } |
|
| 268 | + return $this->getUserIdGroups($user->getUID()); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * @param string $uid the user id |
|
| 273 | + * @return \OC\Group\Group[] |
|
| 274 | + */ |
|
| 275 | + public function getUserIdGroups($uid) { |
|
| 276 | + if (isset($this->cachedUserGroups[$uid])) { |
|
| 277 | + return $this->cachedUserGroups[$uid]; |
|
| 278 | + } |
|
| 279 | + $groups = array(); |
|
| 280 | + foreach ($this->backends as $backend) { |
|
| 281 | + $groupIds = $backend->getUserGroups($uid); |
|
| 282 | + if (is_array($groupIds)) { |
|
| 283 | + foreach ($groupIds as $groupId) { |
|
| 284 | + $aGroup = $this->get($groupId); |
|
| 285 | + if ($aGroup instanceof IGroup) { |
|
| 286 | + $groups[$groupId] = $aGroup; |
|
| 287 | + } else { |
|
| 288 | + $this->logger->debug('User "' . $uid . '" belongs to deleted group: "' . $groupId . '"', ['app' => 'core']); |
|
| 289 | + } |
|
| 290 | + } |
|
| 291 | + } |
|
| 292 | + } |
|
| 293 | + $this->cachedUserGroups[$uid] = $groups; |
|
| 294 | + return $this->cachedUserGroups[$uid]; |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * Checks if a userId is in the admin group |
|
| 299 | + * @param string $userId |
|
| 300 | + * @return bool if admin |
|
| 301 | + */ |
|
| 302 | + public function isAdmin($userId) { |
|
| 303 | + foreach ($this->backends as $backend) { |
|
| 304 | + if ($backend->implementsActions(\OC\Group\Backend::IS_ADMIN) && $backend->isAdmin($userId)) { |
|
| 305 | + return true; |
|
| 306 | + } |
|
| 307 | + } |
|
| 308 | + return $this->isInGroup($userId, 'admin'); |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * Checks if a userId is in a group |
|
| 313 | + * @param string $userId |
|
| 314 | + * @param string $group |
|
| 315 | + * @return bool if in group |
|
| 316 | + */ |
|
| 317 | + public function isInGroup($userId, $group) { |
|
| 318 | + return array_key_exists($group, $this->getUserIdGroups($userId)); |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * get a list of group ids for a user |
|
| 323 | + * @param IUser $user |
|
| 324 | + * @return array with group ids |
|
| 325 | + */ |
|
| 326 | + public function getUserGroupIds(IUser $user) { |
|
| 327 | + return array_map(function($value) { |
|
| 328 | + return (string) $value; |
|
| 329 | + }, array_keys($this->getUserGroups($user))); |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * get an array of groupid and displayName for a user |
|
| 334 | + * @param IUser $user |
|
| 335 | + * @return array ['displayName' => displayname] |
|
| 336 | + */ |
|
| 337 | + public function getUserGroupNames(IUser $user) { |
|
| 338 | + return array_map(function($group) { |
|
| 339 | + return array('displayName' => $group->getDisplayName()); |
|
| 340 | + }, $this->getUserGroups($user)); |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * get a list of all display names in a group |
|
| 345 | + * @param string $gid |
|
| 346 | + * @param string $search |
|
| 347 | + * @param int $limit |
|
| 348 | + * @param int $offset |
|
| 349 | + * @return array an array of display names (value) and user ids (key) |
|
| 350 | + */ |
|
| 351 | + public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
| 352 | + $group = $this->get($gid); |
|
| 353 | + if(is_null($group)) { |
|
| 354 | + return array(); |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + $search = trim($search); |
|
| 358 | + $groupUsers = array(); |
|
| 359 | + |
|
| 360 | + if(!empty($search)) { |
|
| 361 | + // only user backends have the capability to do a complex search for users |
|
| 362 | + $searchOffset = 0; |
|
| 363 | + $searchLimit = $limit * 100; |
|
| 364 | + if($limit === -1) { |
|
| 365 | + $searchLimit = 500; |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + do { |
|
| 369 | + $filteredUsers = $this->userManager->searchDisplayName($search, $searchLimit, $searchOffset); |
|
| 370 | + foreach($filteredUsers as $filteredUser) { |
|
| 371 | + if($group->inGroup($filteredUser)) { |
|
| 372 | + $groupUsers[]= $filteredUser; |
|
| 373 | + } |
|
| 374 | + } |
|
| 375 | + $searchOffset += $searchLimit; |
|
| 376 | + } while(count($groupUsers) < $searchLimit+$offset && count($filteredUsers) >= $searchLimit); |
|
| 377 | + |
|
| 378 | + if($limit === -1) { |
|
| 379 | + $groupUsers = array_slice($groupUsers, $offset); |
|
| 380 | + } else { |
|
| 381 | + $groupUsers = array_slice($groupUsers, $offset, $limit); |
|
| 382 | + } |
|
| 383 | + } else { |
|
| 384 | + $groupUsers = $group->searchUsers('', $limit, $offset); |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + $matchingUsers = array(); |
|
| 388 | + foreach($groupUsers as $groupUser) { |
|
| 389 | + $matchingUsers[$groupUser->getUID()] = $groupUser->getDisplayName(); |
|
| 390 | + } |
|
| 391 | + return $matchingUsers; |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + /** |
|
| 395 | + * @return \OC\SubAdmin |
|
| 396 | + */ |
|
| 397 | + public function getSubAdmin() { |
|
| 398 | + if (!$this->subAdmin) { |
|
| 399 | + $this->subAdmin = new \OC\SubAdmin( |
|
| 400 | + $this->userManager, |
|
| 401 | + $this, |
|
| 402 | + \OC::$server->getDatabaseConnection() |
|
| 403 | + ); |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + return $this->subAdmin; |
|
| 407 | + } |
|
| 408 | 408 | } |