@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | public function __construct(IConfig $config) { |
| 79 | 79 | $this->config = $config; |
| 80 | 80 | $cachedUsers = &$this->cachedUsers; |
| 81 | - $this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) { |
|
| 81 | + $this->listen('\OC\User', 'postDelete', function($user) use (&$cachedUsers) { |
|
| 82 | 82 | /** @var \OC\User\User $user */ |
| 83 | 83 | unset($cachedUsers[$user->getUID()]); |
| 84 | 84 | }); |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | $result = $this->checkPasswordNoLogging($loginName, $password); |
| 185 | 185 | |
| 186 | 186 | if ($result === false) { |
| 187 | - \OC::$server->getLogger()->warning('Login failed: \''. $loginName .'\' (Remote IP: \''. \OC::$server->getRequest()->getRemoteAddress(). '\')', ['app' => 'core']); |
|
| 187 | + \OC::$server->getLogger()->warning('Login failed: \''.$loginName.'\' (Remote IP: \''.\OC::$server->getRequest()->getRemoteAddress().'\')', ['app' => 'core']); |
|
| 188 | 188 | } |
| 189 | 189 | |
| 190 | 190 | return $result; |
@@ -233,7 +233,7 @@ discard block |
||
| 233 | 233 | } |
| 234 | 234 | } |
| 235 | 235 | |
| 236 | - uasort($users, function ($a, $b) { |
|
| 236 | + uasort($users, function($a, $b) { |
|
| 237 | 237 | /** |
| 238 | 238 | * @var \OC\User\User $a |
| 239 | 239 | * @var \OC\User\User $b |
@@ -262,7 +262,7 @@ discard block |
||
| 262 | 262 | } |
| 263 | 263 | } |
| 264 | 264 | |
| 265 | - usort($users, function ($a, $b) { |
|
| 265 | + usort($users, function($a, $b) { |
|
| 266 | 266 | /** |
| 267 | 267 | * @var \OC\User\User $a |
| 268 | 268 | * @var \OC\User\User $b |
@@ -341,7 +341,7 @@ discard block |
||
| 341 | 341 | |
| 342 | 342 | $this->emit('\OC\User', 'preCreateUser', [$uid, $password]); |
| 343 | 343 | $state = $backend->createUser($uid, $password); |
| 344 | - if($state === false) { |
|
| 344 | + if ($state === false) { |
|
| 345 | 345 | throw new \InvalidArgumentException($l->t('Could not create user')); |
| 346 | 346 | } |
| 347 | 347 | $user = $this->getUserObject($uid, $backend); |
@@ -367,13 +367,13 @@ discard block |
||
| 367 | 367 | foreach ($this->backends as $backend) { |
| 368 | 368 | if ($backend->implementsActions(Backend::COUNT_USERS)) { |
| 369 | 369 | $backendUsers = $backend->countUsers(); |
| 370 | - if($backendUsers !== false) { |
|
| 371 | - if($backend instanceof IUserBackend) { |
|
| 370 | + if ($backendUsers !== false) { |
|
| 371 | + if ($backend instanceof IUserBackend) { |
|
| 372 | 372 | $name = $backend->getBackendName(); |
| 373 | 373 | } else { |
| 374 | 374 | $name = get_class($backend); |
| 375 | 375 | } |
| 376 | - if(isset($userCountStatistics[$name])) { |
|
| 376 | + if (isset($userCountStatistics[$name])) { |
|
| 377 | 377 | $userCountStatistics[$name] += $backendUsers; |
| 378 | 378 | } else { |
| 379 | 379 | $userCountStatistics[$name] = $backendUsers; |
@@ -435,7 +435,7 @@ discard block |
||
| 435 | 435 | |
| 436 | 436 | $query = $queryBuilder->execute(); |
| 437 | 437 | |
| 438 | - $result = (int)$query->fetchColumn(); |
|
| 438 | + $result = (int) $query->fetchColumn(); |
|
| 439 | 439 | $query->closeCursor(); |
| 440 | 440 | |
| 441 | 441 | return $result; |
@@ -459,7 +459,7 @@ discard block |
||
| 459 | 459 | |
| 460 | 460 | $query = $queryBuilder->execute(); |
| 461 | 461 | |
| 462 | - $result = (int)$query->fetchColumn(); |
|
| 462 | + $result = (int) $query->fetchColumn(); |
|
| 463 | 463 | $query->closeCursor(); |
| 464 | 464 | |
| 465 | 465 | return $result; |
@@ -481,7 +481,7 @@ discard block |
||
| 481 | 481 | |
| 482 | 482 | $query = $queryBuilder->execute(); |
| 483 | 483 | |
| 484 | - $result = (int)$query->fetchColumn(); |
|
| 484 | + $result = (int) $query->fetchColumn(); |
|
| 485 | 485 | $query->closeCursor(); |
| 486 | 486 | |
| 487 | 487 | return $result; |
@@ -57,512 +57,512 @@ |
||
| 57 | 57 | * @package OC\User |
| 58 | 58 | */ |
| 59 | 59 | class Manager extends PublicEmitter implements IUserManager { |
| 60 | - /** |
|
| 61 | - * @var \OCP\UserInterface[] $backends |
|
| 62 | - */ |
|
| 63 | - private $backends = array(); |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @var \OC\User\User[] $cachedUsers |
|
| 67 | - */ |
|
| 68 | - private $cachedUsers = array(); |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @var \OCP\IConfig $config |
|
| 72 | - */ |
|
| 73 | - private $config; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @param \OCP\IConfig $config |
|
| 77 | - */ |
|
| 78 | - public function __construct(IConfig $config) { |
|
| 79 | - $this->config = $config; |
|
| 80 | - $cachedUsers = &$this->cachedUsers; |
|
| 81 | - $this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) { |
|
| 82 | - /** @var \OC\User\User $user */ |
|
| 83 | - unset($cachedUsers[$user->getUID()]); |
|
| 84 | - }); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Get the active backends |
|
| 89 | - * @return \OCP\UserInterface[] |
|
| 90 | - */ |
|
| 91 | - public function getBackends() { |
|
| 92 | - return $this->backends; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * register a user backend |
|
| 97 | - * |
|
| 98 | - * @param \OCP\UserInterface $backend |
|
| 99 | - */ |
|
| 100 | - public function registerBackend($backend) { |
|
| 101 | - $this->backends[] = $backend; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * remove a user backend |
|
| 106 | - * |
|
| 107 | - * @param \OCP\UserInterface $backend |
|
| 108 | - */ |
|
| 109 | - public function removeBackend($backend) { |
|
| 110 | - $this->cachedUsers = array(); |
|
| 111 | - if (($i = array_search($backend, $this->backends)) !== false) { |
|
| 112 | - unset($this->backends[$i]); |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * remove all user backends |
|
| 118 | - */ |
|
| 119 | - public function clearBackends() { |
|
| 120 | - $this->cachedUsers = array(); |
|
| 121 | - $this->backends = array(); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * get a user by user id |
|
| 126 | - * |
|
| 127 | - * @param string $uid |
|
| 128 | - * @return \OC\User\User|null Either the user or null if the specified user does not exist |
|
| 129 | - */ |
|
| 130 | - public function get($uid) { |
|
| 131 | - if (is_null($uid) || $uid === '' || $uid === false) { |
|
| 132 | - return null; |
|
| 133 | - } |
|
| 134 | - if (isset($this->cachedUsers[$uid])) { //check the cache first to prevent having to loop over the backends |
|
| 135 | - return $this->cachedUsers[$uid]; |
|
| 136 | - } |
|
| 137 | - foreach ($this->backends as $backend) { |
|
| 138 | - if ($backend->userExists($uid)) { |
|
| 139 | - return $this->getUserObject($uid, $backend); |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - return null; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * get or construct the user object |
|
| 147 | - * |
|
| 148 | - * @param string $uid |
|
| 149 | - * @param \OCP\UserInterface $backend |
|
| 150 | - * @param bool $cacheUser If false the newly created user object will not be cached |
|
| 151 | - * @return \OC\User\User |
|
| 152 | - */ |
|
| 153 | - protected function getUserObject($uid, $backend, $cacheUser = true) { |
|
| 154 | - if (isset($this->cachedUsers[$uid])) { |
|
| 155 | - return $this->cachedUsers[$uid]; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - $user = new User($uid, $backend, $this, $this->config); |
|
| 159 | - if ($cacheUser) { |
|
| 160 | - $this->cachedUsers[$uid] = $user; |
|
| 161 | - } |
|
| 162 | - return $user; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * check if a user exists |
|
| 167 | - * |
|
| 168 | - * @param string $uid |
|
| 169 | - * @return bool |
|
| 170 | - */ |
|
| 171 | - public function userExists($uid) { |
|
| 172 | - $user = $this->get($uid); |
|
| 173 | - return ($user !== null); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Check if the password is valid for the user |
|
| 178 | - * |
|
| 179 | - * @param string $loginName |
|
| 180 | - * @param string $password |
|
| 181 | - * @return mixed the User object on success, false otherwise |
|
| 182 | - */ |
|
| 183 | - public function checkPassword($loginName, $password) { |
|
| 184 | - $result = $this->checkPasswordNoLogging($loginName, $password); |
|
| 185 | - |
|
| 186 | - if ($result === false) { |
|
| 187 | - \OC::$server->getLogger()->warning('Login failed: \''. $loginName .'\' (Remote IP: \''. \OC::$server->getRequest()->getRemoteAddress(). '\')', ['app' => 'core']); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - return $result; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Check if the password is valid for the user |
|
| 195 | - * |
|
| 196 | - * @internal |
|
| 197 | - * @param string $loginName |
|
| 198 | - * @param string $password |
|
| 199 | - * @return mixed the User object on success, false otherwise |
|
| 200 | - */ |
|
| 201 | - public function checkPasswordNoLogging($loginName, $password) { |
|
| 202 | - $loginName = str_replace("\0", '', $loginName); |
|
| 203 | - $password = str_replace("\0", '', $password); |
|
| 204 | - |
|
| 205 | - foreach ($this->backends as $backend) { |
|
| 206 | - if ($backend->implementsActions(Backend::CHECK_PASSWORD)) { |
|
| 207 | - $uid = $backend->checkPassword($loginName, $password); |
|
| 208 | - if ($uid !== false) { |
|
| 209 | - return $this->getUserObject($uid, $backend); |
|
| 210 | - } |
|
| 211 | - } |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - return false; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * search by user id |
|
| 219 | - * |
|
| 220 | - * @param string $pattern |
|
| 221 | - * @param int $limit |
|
| 222 | - * @param int $offset |
|
| 223 | - * @return \OC\User\User[] |
|
| 224 | - */ |
|
| 225 | - public function search($pattern, $limit = null, $offset = null) { |
|
| 226 | - $users = array(); |
|
| 227 | - foreach ($this->backends as $backend) { |
|
| 228 | - $backendUsers = $backend->getUsers($pattern, $limit, $offset); |
|
| 229 | - if (is_array($backendUsers)) { |
|
| 230 | - foreach ($backendUsers as $uid) { |
|
| 231 | - $users[$uid] = $this->getUserObject($uid, $backend); |
|
| 232 | - } |
|
| 233 | - } |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - uasort($users, function ($a, $b) { |
|
| 237 | - /** |
|
| 238 | - * @var \OC\User\User $a |
|
| 239 | - * @var \OC\User\User $b |
|
| 240 | - */ |
|
| 241 | - return strcmp($a->getUID(), $b->getUID()); |
|
| 242 | - }); |
|
| 243 | - return $users; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * search by displayName |
|
| 248 | - * |
|
| 249 | - * @param string $pattern |
|
| 250 | - * @param int $limit |
|
| 251 | - * @param int $offset |
|
| 252 | - * @return \OC\User\User[] |
|
| 253 | - */ |
|
| 254 | - public function searchDisplayName($pattern, $limit = null, $offset = null) { |
|
| 255 | - $users = array(); |
|
| 256 | - foreach ($this->backends as $backend) { |
|
| 257 | - $backendUsers = $backend->getDisplayNames($pattern, $limit, $offset); |
|
| 258 | - if (is_array($backendUsers)) { |
|
| 259 | - foreach ($backendUsers as $uid => $displayName) { |
|
| 260 | - $users[] = $this->getUserObject($uid, $backend); |
|
| 261 | - } |
|
| 262 | - } |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - usort($users, function ($a, $b) { |
|
| 266 | - /** |
|
| 267 | - * @var \OC\User\User $a |
|
| 268 | - * @var \OC\User\User $b |
|
| 269 | - */ |
|
| 270 | - return strcmp(strtolower($a->getDisplayName()), strtolower($b->getDisplayName())); |
|
| 271 | - }); |
|
| 272 | - return $users; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * @param string $uid |
|
| 277 | - * @param string $password |
|
| 278 | - * @throws \InvalidArgumentException |
|
| 279 | - * @return bool|IUser the created user or false |
|
| 280 | - */ |
|
| 281 | - public function createUser($uid, $password) { |
|
| 282 | - $localBackends = []; |
|
| 283 | - foreach ($this->backends as $backend) { |
|
| 284 | - if ($backend instanceof Database) { |
|
| 285 | - // First check if there is another user backend |
|
| 286 | - $localBackends[] = $backend; |
|
| 287 | - continue; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - if ($backend->implementsActions(Backend::CREATE_USER)) { |
|
| 291 | - return $this->createUserFromBackend($uid, $password, $backend); |
|
| 292 | - } |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - foreach ($localBackends as $backend) { |
|
| 296 | - if ($backend->implementsActions(Backend::CREATE_USER)) { |
|
| 297 | - return $this->createUserFromBackend($uid, $password, $backend); |
|
| 298 | - } |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - return false; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * @param string $uid |
|
| 306 | - * @param string $password |
|
| 307 | - * @param UserInterface $backend |
|
| 308 | - * @return IUser|null |
|
| 309 | - * @throws \InvalidArgumentException |
|
| 310 | - */ |
|
| 311 | - public function createUserFromBackend($uid, $password, UserInterface $backend) { |
|
| 312 | - $l = \OC::$server->getL10N('lib'); |
|
| 313 | - |
|
| 314 | - // Check the name for bad characters |
|
| 315 | - // Allowed are: "a-z", "A-Z", "0-9" and "_.@-'" |
|
| 316 | - if (preg_match('/[^a-zA-Z0-9 _\.@\-\']/', $uid)) { |
|
| 317 | - throw new \InvalidArgumentException($l->t('Only the following characters are allowed in a username:' |
|
| 318 | - . ' "a-z", "A-Z", "0-9", and "_.@-\'"')); |
|
| 319 | - } |
|
| 320 | - // No empty username |
|
| 321 | - if (trim($uid) === '') { |
|
| 322 | - throw new \InvalidArgumentException($l->t('A valid username must be provided')); |
|
| 323 | - } |
|
| 324 | - // No whitespace at the beginning or at the end |
|
| 325 | - if (trim($uid) !== $uid) { |
|
| 326 | - throw new \InvalidArgumentException($l->t('Username contains whitespace at the beginning or at the end')); |
|
| 327 | - } |
|
| 328 | - // Username only consists of 1 or 2 dots (directory traversal) |
|
| 329 | - if ($uid === '.' || $uid === '..') { |
|
| 330 | - throw new \InvalidArgumentException($l->t('Username must not consist of dots only')); |
|
| 331 | - } |
|
| 332 | - // No empty password |
|
| 333 | - if (trim($password) === '') { |
|
| 334 | - throw new \InvalidArgumentException($l->t('A valid password must be provided')); |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - // Check if user already exists |
|
| 338 | - if ($this->userExists($uid)) { |
|
| 339 | - throw new \InvalidArgumentException($l->t('The username is already being used')); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - $this->emit('\OC\User', 'preCreateUser', [$uid, $password]); |
|
| 343 | - $state = $backend->createUser($uid, $password); |
|
| 344 | - if($state === false) { |
|
| 345 | - throw new \InvalidArgumentException($l->t('Could not create user')); |
|
| 346 | - } |
|
| 347 | - $user = $this->getUserObject($uid, $backend); |
|
| 348 | - if ($user instanceof IUser) { |
|
| 349 | - $this->emit('\OC\User', 'postCreateUser', [$user, $password]); |
|
| 350 | - } |
|
| 351 | - return $user; |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - /** |
|
| 355 | - * returns how many users per backend exist (if supported by backend) |
|
| 356 | - * |
|
| 357 | - * @param boolean $hasLoggedIn when true only users that have a lastLogin |
|
| 358 | - * entry in the preferences table will be affected |
|
| 359 | - * @return array|int an array of backend class as key and count number as value |
|
| 360 | - * if $hasLoggedIn is true only an int is returned |
|
| 361 | - */ |
|
| 362 | - public function countUsers($hasLoggedIn = false) { |
|
| 363 | - if ($hasLoggedIn) { |
|
| 364 | - return $this->countSeenUsers(); |
|
| 365 | - } |
|
| 366 | - $userCountStatistics = []; |
|
| 367 | - foreach ($this->backends as $backend) { |
|
| 368 | - if ($backend->implementsActions(Backend::COUNT_USERS)) { |
|
| 369 | - $backendUsers = $backend->countUsers(); |
|
| 370 | - if($backendUsers !== false) { |
|
| 371 | - if($backend instanceof IUserBackend) { |
|
| 372 | - $name = $backend->getBackendName(); |
|
| 373 | - } else { |
|
| 374 | - $name = get_class($backend); |
|
| 375 | - } |
|
| 376 | - if(isset($userCountStatistics[$name])) { |
|
| 377 | - $userCountStatistics[$name] += $backendUsers; |
|
| 378 | - } else { |
|
| 379 | - $userCountStatistics[$name] = $backendUsers; |
|
| 380 | - } |
|
| 381 | - } |
|
| 382 | - } |
|
| 383 | - } |
|
| 384 | - return $userCountStatistics; |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - /** |
|
| 388 | - * The callback is executed for each user on each backend. |
|
| 389 | - * If the callback returns false no further users will be retrieved. |
|
| 390 | - * |
|
| 391 | - * @param \Closure $callback |
|
| 392 | - * @param string $search |
|
| 393 | - * @param boolean $onlySeen when true only users that have a lastLogin entry |
|
| 394 | - * in the preferences table will be affected |
|
| 395 | - * @since 9.0.0 |
|
| 396 | - */ |
|
| 397 | - public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = false) { |
|
| 398 | - if ($onlySeen) { |
|
| 399 | - $this->callForSeenUsers($callback); |
|
| 400 | - } else { |
|
| 401 | - foreach ($this->getBackends() as $backend) { |
|
| 402 | - $limit = 500; |
|
| 403 | - $offset = 0; |
|
| 404 | - do { |
|
| 405 | - $users = $backend->getUsers($search, $limit, $offset); |
|
| 406 | - foreach ($users as $uid) { |
|
| 407 | - if (!$backend->userExists($uid)) { |
|
| 408 | - continue; |
|
| 409 | - } |
|
| 410 | - $user = $this->getUserObject($uid, $backend, false); |
|
| 411 | - $return = $callback($user); |
|
| 412 | - if ($return === false) { |
|
| 413 | - break; |
|
| 414 | - } |
|
| 415 | - } |
|
| 416 | - $offset += $limit; |
|
| 417 | - } while (count($users) >= $limit); |
|
| 418 | - } |
|
| 419 | - } |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - /** |
|
| 423 | - * returns how many users are disabled |
|
| 424 | - * |
|
| 425 | - * @return int |
|
| 426 | - * @since 12.0.0 |
|
| 427 | - */ |
|
| 428 | - public function countDisabledUsers() { |
|
| 429 | - $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 430 | - $queryBuilder->select($queryBuilder->createFunction('COUNT(*)')) |
|
| 431 | - ->from('preferences') |
|
| 432 | - ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core'))) |
|
| 433 | - ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled'))) |
|
| 434 | - ->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR)); |
|
| 435 | - |
|
| 436 | - $query = $queryBuilder->execute(); |
|
| 437 | - |
|
| 438 | - $result = (int)$query->fetchColumn(); |
|
| 439 | - $query->closeCursor(); |
|
| 440 | - |
|
| 441 | - return $result; |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - /** |
|
| 445 | - * returns how many users are disabled in the requested groups |
|
| 446 | - * |
|
| 447 | - * @param array $groups groupids to search |
|
| 448 | - * @return int |
|
| 449 | - * @since 14.0.0 |
|
| 450 | - */ |
|
| 451 | - public function countDisabledUsersOfGroups(array $groups) { |
|
| 452 | - $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 453 | - $queryBuilder->select($queryBuilder->createFunction('COUNT(Distinct uid)')) |
|
| 454 | - ->from('preferences', 'p') |
|
| 455 | - ->innerJoin('p', 'group_user', 'g', 'p.userid = g.uid') |
|
| 456 | - ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core'))) |
|
| 457 | - ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled'))) |
|
| 458 | - ->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR)) |
|
| 459 | - ->andWhere($queryBuilder->expr()->in('gid', $queryBuilder->createNamedParameter(implode(',', $groups)))); |
|
| 460 | - |
|
| 461 | - $query = $queryBuilder->execute(); |
|
| 462 | - |
|
| 463 | - $result = (int)$query->fetchColumn(); |
|
| 464 | - $query->closeCursor(); |
|
| 465 | - |
|
| 466 | - return $result; |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - /** |
|
| 470 | - * returns how many users have logged in once |
|
| 471 | - * |
|
| 472 | - * @return int |
|
| 473 | - * @since 11.0.0 |
|
| 474 | - */ |
|
| 475 | - public function countSeenUsers() { |
|
| 476 | - $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 477 | - $queryBuilder->select($queryBuilder->createFunction('COUNT(*)')) |
|
| 478 | - ->from('preferences') |
|
| 479 | - ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('login'))) |
|
| 480 | - ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('lastLogin'))) |
|
| 481 | - ->andWhere($queryBuilder->expr()->isNotNull('configvalue')); |
|
| 482 | - |
|
| 483 | - $query = $queryBuilder->execute(); |
|
| 484 | - |
|
| 485 | - $result = (int)$query->fetchColumn(); |
|
| 486 | - $query->closeCursor(); |
|
| 487 | - |
|
| 488 | - return $result; |
|
| 489 | - } |
|
| 490 | - |
|
| 491 | - /** |
|
| 492 | - * @param \Closure $callback |
|
| 493 | - * @since 11.0.0 |
|
| 494 | - */ |
|
| 495 | - public function callForSeenUsers(\Closure $callback) { |
|
| 496 | - $limit = 1000; |
|
| 497 | - $offset = 0; |
|
| 498 | - do { |
|
| 499 | - $userIds = $this->getSeenUserIds($limit, $offset); |
|
| 500 | - $offset += $limit; |
|
| 501 | - foreach ($userIds as $userId) { |
|
| 502 | - foreach ($this->backends as $backend) { |
|
| 503 | - if ($backend->userExists($userId)) { |
|
| 504 | - $user = $this->getUserObject($userId, $backend, false); |
|
| 505 | - $return = $callback($user); |
|
| 506 | - if ($return === false) { |
|
| 507 | - return; |
|
| 508 | - } |
|
| 509 | - break; |
|
| 510 | - } |
|
| 511 | - } |
|
| 512 | - } |
|
| 513 | - } while (count($userIds) >= $limit); |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - /** |
|
| 517 | - * Getting all userIds that have a listLogin value requires checking the |
|
| 518 | - * value in php because on oracle you cannot use a clob in a where clause, |
|
| 519 | - * preventing us from doing a not null or length(value) > 0 check. |
|
| 520 | - * |
|
| 521 | - * @param int $limit |
|
| 522 | - * @param int $offset |
|
| 523 | - * @return string[] with user ids |
|
| 524 | - */ |
|
| 525 | - private function getSeenUserIds($limit = null, $offset = null) { |
|
| 526 | - $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 527 | - $queryBuilder->select(['userid']) |
|
| 528 | - ->from('preferences') |
|
| 529 | - ->where($queryBuilder->expr()->eq( |
|
| 530 | - 'appid', $queryBuilder->createNamedParameter('login')) |
|
| 531 | - ) |
|
| 532 | - ->andWhere($queryBuilder->expr()->eq( |
|
| 533 | - 'configkey', $queryBuilder->createNamedParameter('lastLogin')) |
|
| 534 | - ) |
|
| 535 | - ->andWhere($queryBuilder->expr()->isNotNull('configvalue') |
|
| 536 | - ); |
|
| 537 | - |
|
| 538 | - if ($limit !== null) { |
|
| 539 | - $queryBuilder->setMaxResults($limit); |
|
| 540 | - } |
|
| 541 | - if ($offset !== null) { |
|
| 542 | - $queryBuilder->setFirstResult($offset); |
|
| 543 | - } |
|
| 544 | - $query = $queryBuilder->execute(); |
|
| 545 | - $result = []; |
|
| 546 | - |
|
| 547 | - while ($row = $query->fetch()) { |
|
| 548 | - $result[] = $row['userid']; |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - $query->closeCursor(); |
|
| 552 | - |
|
| 553 | - return $result; |
|
| 554 | - } |
|
| 555 | - |
|
| 556 | - /** |
|
| 557 | - * @param string $email |
|
| 558 | - * @return IUser[] |
|
| 559 | - * @since 9.1.0 |
|
| 560 | - */ |
|
| 561 | - public function getByEmail($email) { |
|
| 562 | - $userIds = $this->config->getUsersForUserValue('settings', 'email', $email); |
|
| 563 | - |
|
| 564 | - return array_map(function($uid) { |
|
| 565 | - return $this->get($uid); |
|
| 566 | - }, $userIds); |
|
| 567 | - } |
|
| 60 | + /** |
|
| 61 | + * @var \OCP\UserInterface[] $backends |
|
| 62 | + */ |
|
| 63 | + private $backends = array(); |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @var \OC\User\User[] $cachedUsers |
|
| 67 | + */ |
|
| 68 | + private $cachedUsers = array(); |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @var \OCP\IConfig $config |
|
| 72 | + */ |
|
| 73 | + private $config; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @param \OCP\IConfig $config |
|
| 77 | + */ |
|
| 78 | + public function __construct(IConfig $config) { |
|
| 79 | + $this->config = $config; |
|
| 80 | + $cachedUsers = &$this->cachedUsers; |
|
| 81 | + $this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) { |
|
| 82 | + /** @var \OC\User\User $user */ |
|
| 83 | + unset($cachedUsers[$user->getUID()]); |
|
| 84 | + }); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Get the active backends |
|
| 89 | + * @return \OCP\UserInterface[] |
|
| 90 | + */ |
|
| 91 | + public function getBackends() { |
|
| 92 | + return $this->backends; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * register a user backend |
|
| 97 | + * |
|
| 98 | + * @param \OCP\UserInterface $backend |
|
| 99 | + */ |
|
| 100 | + public function registerBackend($backend) { |
|
| 101 | + $this->backends[] = $backend; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * remove a user backend |
|
| 106 | + * |
|
| 107 | + * @param \OCP\UserInterface $backend |
|
| 108 | + */ |
|
| 109 | + public function removeBackend($backend) { |
|
| 110 | + $this->cachedUsers = array(); |
|
| 111 | + if (($i = array_search($backend, $this->backends)) !== false) { |
|
| 112 | + unset($this->backends[$i]); |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * remove all user backends |
|
| 118 | + */ |
|
| 119 | + public function clearBackends() { |
|
| 120 | + $this->cachedUsers = array(); |
|
| 121 | + $this->backends = array(); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * get a user by user id |
|
| 126 | + * |
|
| 127 | + * @param string $uid |
|
| 128 | + * @return \OC\User\User|null Either the user or null if the specified user does not exist |
|
| 129 | + */ |
|
| 130 | + public function get($uid) { |
|
| 131 | + if (is_null($uid) || $uid === '' || $uid === false) { |
|
| 132 | + return null; |
|
| 133 | + } |
|
| 134 | + if (isset($this->cachedUsers[$uid])) { //check the cache first to prevent having to loop over the backends |
|
| 135 | + return $this->cachedUsers[$uid]; |
|
| 136 | + } |
|
| 137 | + foreach ($this->backends as $backend) { |
|
| 138 | + if ($backend->userExists($uid)) { |
|
| 139 | + return $this->getUserObject($uid, $backend); |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + return null; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * get or construct the user object |
|
| 147 | + * |
|
| 148 | + * @param string $uid |
|
| 149 | + * @param \OCP\UserInterface $backend |
|
| 150 | + * @param bool $cacheUser If false the newly created user object will not be cached |
|
| 151 | + * @return \OC\User\User |
|
| 152 | + */ |
|
| 153 | + protected function getUserObject($uid, $backend, $cacheUser = true) { |
|
| 154 | + if (isset($this->cachedUsers[$uid])) { |
|
| 155 | + return $this->cachedUsers[$uid]; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + $user = new User($uid, $backend, $this, $this->config); |
|
| 159 | + if ($cacheUser) { |
|
| 160 | + $this->cachedUsers[$uid] = $user; |
|
| 161 | + } |
|
| 162 | + return $user; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * check if a user exists |
|
| 167 | + * |
|
| 168 | + * @param string $uid |
|
| 169 | + * @return bool |
|
| 170 | + */ |
|
| 171 | + public function userExists($uid) { |
|
| 172 | + $user = $this->get($uid); |
|
| 173 | + return ($user !== null); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Check if the password is valid for the user |
|
| 178 | + * |
|
| 179 | + * @param string $loginName |
|
| 180 | + * @param string $password |
|
| 181 | + * @return mixed the User object on success, false otherwise |
|
| 182 | + */ |
|
| 183 | + public function checkPassword($loginName, $password) { |
|
| 184 | + $result = $this->checkPasswordNoLogging($loginName, $password); |
|
| 185 | + |
|
| 186 | + if ($result === false) { |
|
| 187 | + \OC::$server->getLogger()->warning('Login failed: \''. $loginName .'\' (Remote IP: \''. \OC::$server->getRequest()->getRemoteAddress(). '\')', ['app' => 'core']); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + return $result; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Check if the password is valid for the user |
|
| 195 | + * |
|
| 196 | + * @internal |
|
| 197 | + * @param string $loginName |
|
| 198 | + * @param string $password |
|
| 199 | + * @return mixed the User object on success, false otherwise |
|
| 200 | + */ |
|
| 201 | + public function checkPasswordNoLogging($loginName, $password) { |
|
| 202 | + $loginName = str_replace("\0", '', $loginName); |
|
| 203 | + $password = str_replace("\0", '', $password); |
|
| 204 | + |
|
| 205 | + foreach ($this->backends as $backend) { |
|
| 206 | + if ($backend->implementsActions(Backend::CHECK_PASSWORD)) { |
|
| 207 | + $uid = $backend->checkPassword($loginName, $password); |
|
| 208 | + if ($uid !== false) { |
|
| 209 | + return $this->getUserObject($uid, $backend); |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + return false; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * search by user id |
|
| 219 | + * |
|
| 220 | + * @param string $pattern |
|
| 221 | + * @param int $limit |
|
| 222 | + * @param int $offset |
|
| 223 | + * @return \OC\User\User[] |
|
| 224 | + */ |
|
| 225 | + public function search($pattern, $limit = null, $offset = null) { |
|
| 226 | + $users = array(); |
|
| 227 | + foreach ($this->backends as $backend) { |
|
| 228 | + $backendUsers = $backend->getUsers($pattern, $limit, $offset); |
|
| 229 | + if (is_array($backendUsers)) { |
|
| 230 | + foreach ($backendUsers as $uid) { |
|
| 231 | + $users[$uid] = $this->getUserObject($uid, $backend); |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + uasort($users, function ($a, $b) { |
|
| 237 | + /** |
|
| 238 | + * @var \OC\User\User $a |
|
| 239 | + * @var \OC\User\User $b |
|
| 240 | + */ |
|
| 241 | + return strcmp($a->getUID(), $b->getUID()); |
|
| 242 | + }); |
|
| 243 | + return $users; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * search by displayName |
|
| 248 | + * |
|
| 249 | + * @param string $pattern |
|
| 250 | + * @param int $limit |
|
| 251 | + * @param int $offset |
|
| 252 | + * @return \OC\User\User[] |
|
| 253 | + */ |
|
| 254 | + public function searchDisplayName($pattern, $limit = null, $offset = null) { |
|
| 255 | + $users = array(); |
|
| 256 | + foreach ($this->backends as $backend) { |
|
| 257 | + $backendUsers = $backend->getDisplayNames($pattern, $limit, $offset); |
|
| 258 | + if (is_array($backendUsers)) { |
|
| 259 | + foreach ($backendUsers as $uid => $displayName) { |
|
| 260 | + $users[] = $this->getUserObject($uid, $backend); |
|
| 261 | + } |
|
| 262 | + } |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + usort($users, function ($a, $b) { |
|
| 266 | + /** |
|
| 267 | + * @var \OC\User\User $a |
|
| 268 | + * @var \OC\User\User $b |
|
| 269 | + */ |
|
| 270 | + return strcmp(strtolower($a->getDisplayName()), strtolower($b->getDisplayName())); |
|
| 271 | + }); |
|
| 272 | + return $users; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * @param string $uid |
|
| 277 | + * @param string $password |
|
| 278 | + * @throws \InvalidArgumentException |
|
| 279 | + * @return bool|IUser the created user or false |
|
| 280 | + */ |
|
| 281 | + public function createUser($uid, $password) { |
|
| 282 | + $localBackends = []; |
|
| 283 | + foreach ($this->backends as $backend) { |
|
| 284 | + if ($backend instanceof Database) { |
|
| 285 | + // First check if there is another user backend |
|
| 286 | + $localBackends[] = $backend; |
|
| 287 | + continue; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + if ($backend->implementsActions(Backend::CREATE_USER)) { |
|
| 291 | + return $this->createUserFromBackend($uid, $password, $backend); |
|
| 292 | + } |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + foreach ($localBackends as $backend) { |
|
| 296 | + if ($backend->implementsActions(Backend::CREATE_USER)) { |
|
| 297 | + return $this->createUserFromBackend($uid, $password, $backend); |
|
| 298 | + } |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + return false; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * @param string $uid |
|
| 306 | + * @param string $password |
|
| 307 | + * @param UserInterface $backend |
|
| 308 | + * @return IUser|null |
|
| 309 | + * @throws \InvalidArgumentException |
|
| 310 | + */ |
|
| 311 | + public function createUserFromBackend($uid, $password, UserInterface $backend) { |
|
| 312 | + $l = \OC::$server->getL10N('lib'); |
|
| 313 | + |
|
| 314 | + // Check the name for bad characters |
|
| 315 | + // Allowed are: "a-z", "A-Z", "0-9" and "_.@-'" |
|
| 316 | + if (preg_match('/[^a-zA-Z0-9 _\.@\-\']/', $uid)) { |
|
| 317 | + throw new \InvalidArgumentException($l->t('Only the following characters are allowed in a username:' |
|
| 318 | + . ' "a-z", "A-Z", "0-9", and "_.@-\'"')); |
|
| 319 | + } |
|
| 320 | + // No empty username |
|
| 321 | + if (trim($uid) === '') { |
|
| 322 | + throw new \InvalidArgumentException($l->t('A valid username must be provided')); |
|
| 323 | + } |
|
| 324 | + // No whitespace at the beginning or at the end |
|
| 325 | + if (trim($uid) !== $uid) { |
|
| 326 | + throw new \InvalidArgumentException($l->t('Username contains whitespace at the beginning or at the end')); |
|
| 327 | + } |
|
| 328 | + // Username only consists of 1 or 2 dots (directory traversal) |
|
| 329 | + if ($uid === '.' || $uid === '..') { |
|
| 330 | + throw new \InvalidArgumentException($l->t('Username must not consist of dots only')); |
|
| 331 | + } |
|
| 332 | + // No empty password |
|
| 333 | + if (trim($password) === '') { |
|
| 334 | + throw new \InvalidArgumentException($l->t('A valid password must be provided')); |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + // Check if user already exists |
|
| 338 | + if ($this->userExists($uid)) { |
|
| 339 | + throw new \InvalidArgumentException($l->t('The username is already being used')); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + $this->emit('\OC\User', 'preCreateUser', [$uid, $password]); |
|
| 343 | + $state = $backend->createUser($uid, $password); |
|
| 344 | + if($state === false) { |
|
| 345 | + throw new \InvalidArgumentException($l->t('Could not create user')); |
|
| 346 | + } |
|
| 347 | + $user = $this->getUserObject($uid, $backend); |
|
| 348 | + if ($user instanceof IUser) { |
|
| 349 | + $this->emit('\OC\User', 'postCreateUser', [$user, $password]); |
|
| 350 | + } |
|
| 351 | + return $user; |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + /** |
|
| 355 | + * returns how many users per backend exist (if supported by backend) |
|
| 356 | + * |
|
| 357 | + * @param boolean $hasLoggedIn when true only users that have a lastLogin |
|
| 358 | + * entry in the preferences table will be affected |
|
| 359 | + * @return array|int an array of backend class as key and count number as value |
|
| 360 | + * if $hasLoggedIn is true only an int is returned |
|
| 361 | + */ |
|
| 362 | + public function countUsers($hasLoggedIn = false) { |
|
| 363 | + if ($hasLoggedIn) { |
|
| 364 | + return $this->countSeenUsers(); |
|
| 365 | + } |
|
| 366 | + $userCountStatistics = []; |
|
| 367 | + foreach ($this->backends as $backend) { |
|
| 368 | + if ($backend->implementsActions(Backend::COUNT_USERS)) { |
|
| 369 | + $backendUsers = $backend->countUsers(); |
|
| 370 | + if($backendUsers !== false) { |
|
| 371 | + if($backend instanceof IUserBackend) { |
|
| 372 | + $name = $backend->getBackendName(); |
|
| 373 | + } else { |
|
| 374 | + $name = get_class($backend); |
|
| 375 | + } |
|
| 376 | + if(isset($userCountStatistics[$name])) { |
|
| 377 | + $userCountStatistics[$name] += $backendUsers; |
|
| 378 | + } else { |
|
| 379 | + $userCountStatistics[$name] = $backendUsers; |
|
| 380 | + } |
|
| 381 | + } |
|
| 382 | + } |
|
| 383 | + } |
|
| 384 | + return $userCountStatistics; |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + /** |
|
| 388 | + * The callback is executed for each user on each backend. |
|
| 389 | + * If the callback returns false no further users will be retrieved. |
|
| 390 | + * |
|
| 391 | + * @param \Closure $callback |
|
| 392 | + * @param string $search |
|
| 393 | + * @param boolean $onlySeen when true only users that have a lastLogin entry |
|
| 394 | + * in the preferences table will be affected |
|
| 395 | + * @since 9.0.0 |
|
| 396 | + */ |
|
| 397 | + public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = false) { |
|
| 398 | + if ($onlySeen) { |
|
| 399 | + $this->callForSeenUsers($callback); |
|
| 400 | + } else { |
|
| 401 | + foreach ($this->getBackends() as $backend) { |
|
| 402 | + $limit = 500; |
|
| 403 | + $offset = 0; |
|
| 404 | + do { |
|
| 405 | + $users = $backend->getUsers($search, $limit, $offset); |
|
| 406 | + foreach ($users as $uid) { |
|
| 407 | + if (!$backend->userExists($uid)) { |
|
| 408 | + continue; |
|
| 409 | + } |
|
| 410 | + $user = $this->getUserObject($uid, $backend, false); |
|
| 411 | + $return = $callback($user); |
|
| 412 | + if ($return === false) { |
|
| 413 | + break; |
|
| 414 | + } |
|
| 415 | + } |
|
| 416 | + $offset += $limit; |
|
| 417 | + } while (count($users) >= $limit); |
|
| 418 | + } |
|
| 419 | + } |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + /** |
|
| 423 | + * returns how many users are disabled |
|
| 424 | + * |
|
| 425 | + * @return int |
|
| 426 | + * @since 12.0.0 |
|
| 427 | + */ |
|
| 428 | + public function countDisabledUsers() { |
|
| 429 | + $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 430 | + $queryBuilder->select($queryBuilder->createFunction('COUNT(*)')) |
|
| 431 | + ->from('preferences') |
|
| 432 | + ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core'))) |
|
| 433 | + ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled'))) |
|
| 434 | + ->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR)); |
|
| 435 | + |
|
| 436 | + $query = $queryBuilder->execute(); |
|
| 437 | + |
|
| 438 | + $result = (int)$query->fetchColumn(); |
|
| 439 | + $query->closeCursor(); |
|
| 440 | + |
|
| 441 | + return $result; |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + /** |
|
| 445 | + * returns how many users are disabled in the requested groups |
|
| 446 | + * |
|
| 447 | + * @param array $groups groupids to search |
|
| 448 | + * @return int |
|
| 449 | + * @since 14.0.0 |
|
| 450 | + */ |
|
| 451 | + public function countDisabledUsersOfGroups(array $groups) { |
|
| 452 | + $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 453 | + $queryBuilder->select($queryBuilder->createFunction('COUNT(Distinct uid)')) |
|
| 454 | + ->from('preferences', 'p') |
|
| 455 | + ->innerJoin('p', 'group_user', 'g', 'p.userid = g.uid') |
|
| 456 | + ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core'))) |
|
| 457 | + ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled'))) |
|
| 458 | + ->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR)) |
|
| 459 | + ->andWhere($queryBuilder->expr()->in('gid', $queryBuilder->createNamedParameter(implode(',', $groups)))); |
|
| 460 | + |
|
| 461 | + $query = $queryBuilder->execute(); |
|
| 462 | + |
|
| 463 | + $result = (int)$query->fetchColumn(); |
|
| 464 | + $query->closeCursor(); |
|
| 465 | + |
|
| 466 | + return $result; |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + /** |
|
| 470 | + * returns how many users have logged in once |
|
| 471 | + * |
|
| 472 | + * @return int |
|
| 473 | + * @since 11.0.0 |
|
| 474 | + */ |
|
| 475 | + public function countSeenUsers() { |
|
| 476 | + $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 477 | + $queryBuilder->select($queryBuilder->createFunction('COUNT(*)')) |
|
| 478 | + ->from('preferences') |
|
| 479 | + ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('login'))) |
|
| 480 | + ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('lastLogin'))) |
|
| 481 | + ->andWhere($queryBuilder->expr()->isNotNull('configvalue')); |
|
| 482 | + |
|
| 483 | + $query = $queryBuilder->execute(); |
|
| 484 | + |
|
| 485 | + $result = (int)$query->fetchColumn(); |
|
| 486 | + $query->closeCursor(); |
|
| 487 | + |
|
| 488 | + return $result; |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + /** |
|
| 492 | + * @param \Closure $callback |
|
| 493 | + * @since 11.0.0 |
|
| 494 | + */ |
|
| 495 | + public function callForSeenUsers(\Closure $callback) { |
|
| 496 | + $limit = 1000; |
|
| 497 | + $offset = 0; |
|
| 498 | + do { |
|
| 499 | + $userIds = $this->getSeenUserIds($limit, $offset); |
|
| 500 | + $offset += $limit; |
|
| 501 | + foreach ($userIds as $userId) { |
|
| 502 | + foreach ($this->backends as $backend) { |
|
| 503 | + if ($backend->userExists($userId)) { |
|
| 504 | + $user = $this->getUserObject($userId, $backend, false); |
|
| 505 | + $return = $callback($user); |
|
| 506 | + if ($return === false) { |
|
| 507 | + return; |
|
| 508 | + } |
|
| 509 | + break; |
|
| 510 | + } |
|
| 511 | + } |
|
| 512 | + } |
|
| 513 | + } while (count($userIds) >= $limit); |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + /** |
|
| 517 | + * Getting all userIds that have a listLogin value requires checking the |
|
| 518 | + * value in php because on oracle you cannot use a clob in a where clause, |
|
| 519 | + * preventing us from doing a not null or length(value) > 0 check. |
|
| 520 | + * |
|
| 521 | + * @param int $limit |
|
| 522 | + * @param int $offset |
|
| 523 | + * @return string[] with user ids |
|
| 524 | + */ |
|
| 525 | + private function getSeenUserIds($limit = null, $offset = null) { |
|
| 526 | + $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 527 | + $queryBuilder->select(['userid']) |
|
| 528 | + ->from('preferences') |
|
| 529 | + ->where($queryBuilder->expr()->eq( |
|
| 530 | + 'appid', $queryBuilder->createNamedParameter('login')) |
|
| 531 | + ) |
|
| 532 | + ->andWhere($queryBuilder->expr()->eq( |
|
| 533 | + 'configkey', $queryBuilder->createNamedParameter('lastLogin')) |
|
| 534 | + ) |
|
| 535 | + ->andWhere($queryBuilder->expr()->isNotNull('configvalue') |
|
| 536 | + ); |
|
| 537 | + |
|
| 538 | + if ($limit !== null) { |
|
| 539 | + $queryBuilder->setMaxResults($limit); |
|
| 540 | + } |
|
| 541 | + if ($offset !== null) { |
|
| 542 | + $queryBuilder->setFirstResult($offset); |
|
| 543 | + } |
|
| 544 | + $query = $queryBuilder->execute(); |
|
| 545 | + $result = []; |
|
| 546 | + |
|
| 547 | + while ($row = $query->fetch()) { |
|
| 548 | + $result[] = $row['userid']; |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + $query->closeCursor(); |
|
| 552 | + |
|
| 553 | + return $result; |
|
| 554 | + } |
|
| 555 | + |
|
| 556 | + /** |
|
| 557 | + * @param string $email |
|
| 558 | + * @return IUser[] |
|
| 559 | + * @since 9.1.0 |
|
| 560 | + */ |
|
| 561 | + public function getByEmail($email) { |
|
| 562 | + $userIds = $this->config->getUsersForUserValue('settings', 'email', $email); |
|
| 563 | + |
|
| 564 | + return array_map(function($uid) { |
|
| 565 | + return $this->get($uid); |
|
| 566 | + }, $userIds); |
|
| 567 | + } |
|
| 568 | 568 | } |
@@ -32,178 +32,178 @@ |
||
| 32 | 32 | use OCP\IUserManager; |
| 33 | 33 | |
| 34 | 34 | class MetaData { |
| 35 | - const SORT_NONE = 0; |
|
| 36 | - const SORT_USERCOUNT = 1; // May have performance issues on LDAP backends |
|
| 37 | - const SORT_GROUPNAME = 2; |
|
| 38 | - |
|
| 39 | - /** @var string */ |
|
| 40 | - protected $user; |
|
| 41 | - /** @var bool */ |
|
| 42 | - protected $isAdmin; |
|
| 43 | - /** @var array */ |
|
| 44 | - protected $metaData = array(); |
|
| 45 | - /** @var IGroupManager */ |
|
| 46 | - protected $groupManager; |
|
| 47 | - /** @var IUserManager */ |
|
| 48 | - protected $userManager; |
|
| 49 | - /** @var bool */ |
|
| 50 | - protected $sorting = false; |
|
| 51 | - /** @var IUserSession */ |
|
| 52 | - protected $userSession; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @param string $user the uid of the current user |
|
| 56 | - * @param bool $isAdmin whether the current users is an admin |
|
| 57 | - * @param IGroupManager $groupManager |
|
| 58 | - * @param IUserManager $userManager |
|
| 59 | - * @param IUserSession $userSession |
|
| 60 | - */ |
|
| 61 | - public function __construct( |
|
| 62 | - $user, |
|
| 63 | - $isAdmin, |
|
| 64 | - IGroupManager $groupManager, |
|
| 65 | - IUserManager $userManager, |
|
| 66 | - IUserSession $userSession |
|
| 67 | - ) { |
|
| 68 | - $this->user = $user; |
|
| 69 | - $this->isAdmin = (bool)$isAdmin; |
|
| 70 | - $this->groupManager = $groupManager; |
|
| 71 | - $this->userManager = $userManager; |
|
| 72 | - $this->userSession = $userSession; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * returns an array with meta data about all available groups |
|
| 77 | - * the array is structured as follows: |
|
| 78 | - * [0] array containing meta data about admin groups |
|
| 79 | - * [1] array containing meta data about unprivileged groups |
|
| 80 | - * @param string $groupSearch only effective when instance was created with |
|
| 81 | - * isAdmin being true |
|
| 82 | - * @param string $userSearch the pattern users are search for |
|
| 83 | - * @return array |
|
| 84 | - */ |
|
| 85 | - public function get($groupSearch = '', $userSearch = '') { |
|
| 86 | - $key = $groupSearch . '::' . $userSearch; |
|
| 87 | - if(isset($this->metaData[$key])) { |
|
| 88 | - return $this->metaData[$key]; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - $adminGroups = array(); |
|
| 92 | - $groups = array(); |
|
| 93 | - $sortGroupsIndex = 0; |
|
| 94 | - $sortGroupsKeys = array(); |
|
| 95 | - $sortAdminGroupsIndex = 0; |
|
| 96 | - $sortAdminGroupsKeys = array(); |
|
| 97 | - |
|
| 98 | - foreach($this->getGroups($groupSearch) as $group) { |
|
| 99 | - $groupMetaData = $this->generateGroupMetaData($group, $userSearch); |
|
| 100 | - if (strtolower($group->getGID()) !== 'admin') { |
|
| 101 | - $this->addEntry( |
|
| 102 | - $groups, |
|
| 103 | - $sortGroupsKeys, |
|
| 104 | - $sortGroupsIndex, |
|
| 105 | - $groupMetaData); |
|
| 106 | - } else { |
|
| 107 | - //admin group is hard coded to 'admin' for now. In future, |
|
| 108 | - //backends may define admin groups too. Then the if statement |
|
| 109 | - //has to be adjusted accordingly. |
|
| 110 | - $this->addEntry( |
|
| 111 | - $adminGroups, |
|
| 112 | - $sortAdminGroupsKeys, |
|
| 113 | - $sortAdminGroupsIndex, |
|
| 114 | - $groupMetaData); |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - //whether sorting is necessary is will be checked in sort() |
|
| 119 | - $this->sort($groups, $sortGroupsKeys); |
|
| 120 | - $this->sort($adminGroups, $sortAdminGroupsKeys); |
|
| 121 | - |
|
| 122 | - $this->metaData[$key] = array($adminGroups, $groups); |
|
| 123 | - return $this->metaData[$key]; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * sets the sort mode, see SORT_* constants for supported modes |
|
| 128 | - * |
|
| 129 | - * @param int $sortMode |
|
| 130 | - */ |
|
| 131 | - public function setSorting($sortMode) { |
|
| 132 | - switch ($sortMode) { |
|
| 133 | - case self::SORT_USERCOUNT: |
|
| 134 | - case self::SORT_GROUPNAME: |
|
| 135 | - $this->sorting = $sortMode; |
|
| 136 | - break; |
|
| 137 | - |
|
| 138 | - default: |
|
| 139 | - $this->sorting = self::SORT_NONE; |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * adds an group entry to the resulting array |
|
| 145 | - * @param array $entries the resulting array, by reference |
|
| 146 | - * @param array $sortKeys the sort key array, by reference |
|
| 147 | - * @param int $sortIndex the sort key index, by reference |
|
| 148 | - * @param array $data the group's meta data as returned by generateGroupMetaData() |
|
| 149 | - */ |
|
| 150 | - private function addEntry(&$entries, &$sortKeys, &$sortIndex, $data) { |
|
| 151 | - $entries[] = $data; |
|
| 152 | - if ($this->sorting === self::SORT_USERCOUNT) { |
|
| 153 | - $sortKeys[$sortIndex] = $data['usercount']; |
|
| 154 | - $sortIndex++; |
|
| 155 | - } else if ($this->sorting === self::SORT_GROUPNAME) { |
|
| 156 | - $sortKeys[$sortIndex] = $data['name']; |
|
| 157 | - $sortIndex++; |
|
| 158 | - } |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * creates an array containing the group meta data |
|
| 163 | - * @param \OCP\IGroup $group |
|
| 164 | - * @param string $userSearch |
|
| 165 | - * @return array with the keys 'id', 'name' and 'usercount' |
|
| 166 | - */ |
|
| 167 | - private function generateGroupMetaData(\OCP\IGroup $group, $userSearch) { |
|
| 168 | - return array( |
|
| 169 | - 'id' => $group->getGID(), |
|
| 170 | - 'name' => $group->getDisplayName(), |
|
| 171 | - 'usercount' => $this->sorting === self::SORT_USERCOUNT ? $group->count($userSearch) : 0, |
|
| 172 | - 'disabled' => $this->userManager->countDisabledUsersOfGroups([$group->getGID()]) |
|
| 173 | - ); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * sorts the result array, if applicable |
|
| 178 | - * @param array $entries the result array, by reference |
|
| 179 | - * @param array $sortKeys the array containing the sort keys |
|
| 180 | - * @param return null |
|
| 181 | - */ |
|
| 182 | - private function sort(&$entries, $sortKeys) { |
|
| 183 | - if ($this->sorting === self::SORT_USERCOUNT) { |
|
| 184 | - array_multisort($sortKeys, SORT_DESC, $entries); |
|
| 185 | - } else if ($this->sorting === self::SORT_GROUPNAME) { |
|
| 186 | - array_multisort($sortKeys, SORT_ASC, $entries); |
|
| 187 | - } |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * returns the available groups |
|
| 192 | - * @param string $search a search string |
|
| 193 | - * @return \OCP\IGroup[] |
|
| 194 | - */ |
|
| 195 | - protected function getGroups($search = '') { |
|
| 196 | - if($this->isAdmin) { |
|
| 197 | - return $this->groupManager->search($search); |
|
| 198 | - } else { |
|
| 199 | - $userObject = $this->userSession->getUser(); |
|
| 200 | - if($userObject !== null) { |
|
| 201 | - $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject); |
|
| 202 | - } else { |
|
| 203 | - $groups = []; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - return $groups; |
|
| 207 | - } |
|
| 208 | - } |
|
| 35 | + const SORT_NONE = 0; |
|
| 36 | + const SORT_USERCOUNT = 1; // May have performance issues on LDAP backends |
|
| 37 | + const SORT_GROUPNAME = 2; |
|
| 38 | + |
|
| 39 | + /** @var string */ |
|
| 40 | + protected $user; |
|
| 41 | + /** @var bool */ |
|
| 42 | + protected $isAdmin; |
|
| 43 | + /** @var array */ |
|
| 44 | + protected $metaData = array(); |
|
| 45 | + /** @var IGroupManager */ |
|
| 46 | + protected $groupManager; |
|
| 47 | + /** @var IUserManager */ |
|
| 48 | + protected $userManager; |
|
| 49 | + /** @var bool */ |
|
| 50 | + protected $sorting = false; |
|
| 51 | + /** @var IUserSession */ |
|
| 52 | + protected $userSession; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @param string $user the uid of the current user |
|
| 56 | + * @param bool $isAdmin whether the current users is an admin |
|
| 57 | + * @param IGroupManager $groupManager |
|
| 58 | + * @param IUserManager $userManager |
|
| 59 | + * @param IUserSession $userSession |
|
| 60 | + */ |
|
| 61 | + public function __construct( |
|
| 62 | + $user, |
|
| 63 | + $isAdmin, |
|
| 64 | + IGroupManager $groupManager, |
|
| 65 | + IUserManager $userManager, |
|
| 66 | + IUserSession $userSession |
|
| 67 | + ) { |
|
| 68 | + $this->user = $user; |
|
| 69 | + $this->isAdmin = (bool)$isAdmin; |
|
| 70 | + $this->groupManager = $groupManager; |
|
| 71 | + $this->userManager = $userManager; |
|
| 72 | + $this->userSession = $userSession; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * returns an array with meta data about all available groups |
|
| 77 | + * the array is structured as follows: |
|
| 78 | + * [0] array containing meta data about admin groups |
|
| 79 | + * [1] array containing meta data about unprivileged groups |
|
| 80 | + * @param string $groupSearch only effective when instance was created with |
|
| 81 | + * isAdmin being true |
|
| 82 | + * @param string $userSearch the pattern users are search for |
|
| 83 | + * @return array |
|
| 84 | + */ |
|
| 85 | + public function get($groupSearch = '', $userSearch = '') { |
|
| 86 | + $key = $groupSearch . '::' . $userSearch; |
|
| 87 | + if(isset($this->metaData[$key])) { |
|
| 88 | + return $this->metaData[$key]; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + $adminGroups = array(); |
|
| 92 | + $groups = array(); |
|
| 93 | + $sortGroupsIndex = 0; |
|
| 94 | + $sortGroupsKeys = array(); |
|
| 95 | + $sortAdminGroupsIndex = 0; |
|
| 96 | + $sortAdminGroupsKeys = array(); |
|
| 97 | + |
|
| 98 | + foreach($this->getGroups($groupSearch) as $group) { |
|
| 99 | + $groupMetaData = $this->generateGroupMetaData($group, $userSearch); |
|
| 100 | + if (strtolower($group->getGID()) !== 'admin') { |
|
| 101 | + $this->addEntry( |
|
| 102 | + $groups, |
|
| 103 | + $sortGroupsKeys, |
|
| 104 | + $sortGroupsIndex, |
|
| 105 | + $groupMetaData); |
|
| 106 | + } else { |
|
| 107 | + //admin group is hard coded to 'admin' for now. In future, |
|
| 108 | + //backends may define admin groups too. Then the if statement |
|
| 109 | + //has to be adjusted accordingly. |
|
| 110 | + $this->addEntry( |
|
| 111 | + $adminGroups, |
|
| 112 | + $sortAdminGroupsKeys, |
|
| 113 | + $sortAdminGroupsIndex, |
|
| 114 | + $groupMetaData); |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + //whether sorting is necessary is will be checked in sort() |
|
| 119 | + $this->sort($groups, $sortGroupsKeys); |
|
| 120 | + $this->sort($adminGroups, $sortAdminGroupsKeys); |
|
| 121 | + |
|
| 122 | + $this->metaData[$key] = array($adminGroups, $groups); |
|
| 123 | + return $this->metaData[$key]; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * sets the sort mode, see SORT_* constants for supported modes |
|
| 128 | + * |
|
| 129 | + * @param int $sortMode |
|
| 130 | + */ |
|
| 131 | + public function setSorting($sortMode) { |
|
| 132 | + switch ($sortMode) { |
|
| 133 | + case self::SORT_USERCOUNT: |
|
| 134 | + case self::SORT_GROUPNAME: |
|
| 135 | + $this->sorting = $sortMode; |
|
| 136 | + break; |
|
| 137 | + |
|
| 138 | + default: |
|
| 139 | + $this->sorting = self::SORT_NONE; |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * adds an group entry to the resulting array |
|
| 145 | + * @param array $entries the resulting array, by reference |
|
| 146 | + * @param array $sortKeys the sort key array, by reference |
|
| 147 | + * @param int $sortIndex the sort key index, by reference |
|
| 148 | + * @param array $data the group's meta data as returned by generateGroupMetaData() |
|
| 149 | + */ |
|
| 150 | + private function addEntry(&$entries, &$sortKeys, &$sortIndex, $data) { |
|
| 151 | + $entries[] = $data; |
|
| 152 | + if ($this->sorting === self::SORT_USERCOUNT) { |
|
| 153 | + $sortKeys[$sortIndex] = $data['usercount']; |
|
| 154 | + $sortIndex++; |
|
| 155 | + } else if ($this->sorting === self::SORT_GROUPNAME) { |
|
| 156 | + $sortKeys[$sortIndex] = $data['name']; |
|
| 157 | + $sortIndex++; |
|
| 158 | + } |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * creates an array containing the group meta data |
|
| 163 | + * @param \OCP\IGroup $group |
|
| 164 | + * @param string $userSearch |
|
| 165 | + * @return array with the keys 'id', 'name' and 'usercount' |
|
| 166 | + */ |
|
| 167 | + private function generateGroupMetaData(\OCP\IGroup $group, $userSearch) { |
|
| 168 | + return array( |
|
| 169 | + 'id' => $group->getGID(), |
|
| 170 | + 'name' => $group->getDisplayName(), |
|
| 171 | + 'usercount' => $this->sorting === self::SORT_USERCOUNT ? $group->count($userSearch) : 0, |
|
| 172 | + 'disabled' => $this->userManager->countDisabledUsersOfGroups([$group->getGID()]) |
|
| 173 | + ); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * sorts the result array, if applicable |
|
| 178 | + * @param array $entries the result array, by reference |
|
| 179 | + * @param array $sortKeys the array containing the sort keys |
|
| 180 | + * @param return null |
|
| 181 | + */ |
|
| 182 | + private function sort(&$entries, $sortKeys) { |
|
| 183 | + if ($this->sorting === self::SORT_USERCOUNT) { |
|
| 184 | + array_multisort($sortKeys, SORT_DESC, $entries); |
|
| 185 | + } else if ($this->sorting === self::SORT_GROUPNAME) { |
|
| 186 | + array_multisort($sortKeys, SORT_ASC, $entries); |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * returns the available groups |
|
| 192 | + * @param string $search a search string |
|
| 193 | + * @return \OCP\IGroup[] |
|
| 194 | + */ |
|
| 195 | + protected function getGroups($search = '') { |
|
| 196 | + if($this->isAdmin) { |
|
| 197 | + return $this->groupManager->search($search); |
|
| 198 | + } else { |
|
| 199 | + $userObject = $this->userSession->getUser(); |
|
| 200 | + if($userObject !== null) { |
|
| 201 | + $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject); |
|
| 202 | + } else { |
|
| 203 | + $groups = []; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + return $groups; |
|
| 207 | + } |
|
| 208 | + } |
|
| 209 | 209 | } |
@@ -66,7 +66,7 @@ discard block |
||
| 66 | 66 | IUserSession $userSession |
| 67 | 67 | ) { |
| 68 | 68 | $this->user = $user; |
| 69 | - $this->isAdmin = (bool)$isAdmin; |
|
| 69 | + $this->isAdmin = (bool) $isAdmin; |
|
| 70 | 70 | $this->groupManager = $groupManager; |
| 71 | 71 | $this->userManager = $userManager; |
| 72 | 72 | $this->userSession = $userSession; |
@@ -83,8 +83,8 @@ discard block |
||
| 83 | 83 | * @return array |
| 84 | 84 | */ |
| 85 | 85 | public function get($groupSearch = '', $userSearch = '') { |
| 86 | - $key = $groupSearch . '::' . $userSearch; |
|
| 87 | - if(isset($this->metaData[$key])) { |
|
| 86 | + $key = $groupSearch.'::'.$userSearch; |
|
| 87 | + if (isset($this->metaData[$key])) { |
|
| 88 | 88 | return $this->metaData[$key]; |
| 89 | 89 | } |
| 90 | 90 | |
@@ -95,7 +95,7 @@ discard block |
||
| 95 | 95 | $sortAdminGroupsIndex = 0; |
| 96 | 96 | $sortAdminGroupsKeys = array(); |
| 97 | 97 | |
| 98 | - foreach($this->getGroups($groupSearch) as $group) { |
|
| 98 | + foreach ($this->getGroups($groupSearch) as $group) { |
|
| 99 | 99 | $groupMetaData = $this->generateGroupMetaData($group, $userSearch); |
| 100 | 100 | if (strtolower($group->getGID()) !== 'admin') { |
| 101 | 101 | $this->addEntry( |
@@ -193,11 +193,11 @@ discard block |
||
| 193 | 193 | * @return \OCP\IGroup[] |
| 194 | 194 | */ |
| 195 | 195 | protected function getGroups($search = '') { |
| 196 | - if($this->isAdmin) { |
|
| 196 | + if ($this->isAdmin) { |
|
| 197 | 197 | return $this->groupManager->search($search); |
| 198 | 198 | } else { |
| 199 | 199 | $userObject = $this->userSession->getUser(); |
| 200 | - if($userObject !== null) { |
|
| 200 | + if ($userObject !== null) { |
|
| 201 | 201 | $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject); |
| 202 | 202 | } else { |
| 203 | 203 | $groups = []; |
@@ -48,18 +48,18 @@ discard block |
||
| 48 | 48 | $config = \OC::$server->getConfig(); |
| 49 | 49 | |
| 50 | 50 | if ($config->getSystemValue('sort_groups_by_name', false)) { |
| 51 | - $sortGroupsBy = \OC\Group\MetaData::SORT_GROUPNAME; |
|
| 51 | + $sortGroupsBy = \OC\Group\MetaData::SORT_GROUPNAME; |
|
| 52 | 52 | } else { |
| 53 | - $isLDAPUsed = false; |
|
| 54 | - if ($appManager->isEnabledForUser('user_ldap')) { |
|
| 55 | - $isLDAPUsed = |
|
| 56 | - $groupManager->isBackendUsed('\OCA\User_LDAP\Group_LDAP') |
|
| 57 | - || $groupManager->isBackendUsed('\OCA\User_LDAP\Group_Proxy'); |
|
| 58 | - if ($isLDAPUsed) { |
|
| 59 | - // LDAP user count can be slow, so we sort by group name here |
|
| 60 | - $sortGroupsBy = \OC\Group\MetaData::SORT_GROUPNAME; |
|
| 61 | - } |
|
| 62 | - } |
|
| 53 | + $isLDAPUsed = false; |
|
| 54 | + if ($appManager->isEnabledForUser('user_ldap')) { |
|
| 55 | + $isLDAPUsed = |
|
| 56 | + $groupManager->isBackendUsed('\OCA\User_LDAP\Group_LDAP') |
|
| 57 | + || $groupManager->isBackendUsed('\OCA\User_LDAP\Group_Proxy'); |
|
| 58 | + if ($isLDAPUsed) { |
|
| 59 | + // LDAP user count can be slow, so we sort by group name here |
|
| 60 | + $sortGroupsBy = \OC\Group\MetaData::SORT_GROUPNAME; |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | $uid = \OC_User::getUser(); |
@@ -68,68 +68,68 @@ discard block |
||
| 68 | 68 | $isDisabled = true; |
| 69 | 69 | $user = $userManager->get($uid); |
| 70 | 70 | if ($user) { |
| 71 | - $isDisabled = !$user->isEnabled(); |
|
| 71 | + $isDisabled = !$user->isEnabled(); |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | $groupsInfo = new \OC\Group\MetaData( |
| 75 | - $uid, |
|
| 76 | - $isAdmin, |
|
| 77 | - $groupManager, |
|
| 78 | - $userManager, |
|
| 79 | - \OC::$server->getUserSession() |
|
| 75 | + $uid, |
|
| 76 | + $isAdmin, |
|
| 77 | + $groupManager, |
|
| 78 | + $userManager, |
|
| 79 | + \OC::$server->getUserSession() |
|
| 80 | 80 | ); |
| 81 | 81 | |
| 82 | 82 | $groupsInfo->setSorting($sortGroupsBy); |
| 83 | 83 | list($adminGroup, $groups) = $groupsInfo->get(); |
| 84 | 84 | |
| 85 | 85 | $recoveryAdminEnabled = $appManager->isEnabledForUser('encryption') && |
| 86 | - $config->getAppValue( 'encryption', 'recoveryAdminEnabled', '0'); |
|
| 86 | + $config->getAppValue( 'encryption', 'recoveryAdminEnabled', '0'); |
|
| 87 | 87 | |
| 88 | 88 | if ($isAdmin) { |
| 89 | - $subAdmins = \OC::$server->getGroupManager()->getSubAdmin()->getAllSubAdmins(); |
|
| 90 | - // New class returns IUser[] so convert back |
|
| 91 | - $result = []; |
|
| 92 | - foreach ($subAdmins as $subAdmin) { |
|
| 93 | - $result[] = [ |
|
| 94 | - 'gid' => $subAdmin['group']->getGID(), |
|
| 95 | - 'uid' => $subAdmin['user']->getUID(), |
|
| 96 | - ]; |
|
| 97 | - } |
|
| 98 | - $subAdmins = $result; |
|
| 89 | + $subAdmins = \OC::$server->getGroupManager()->getSubAdmin()->getAllSubAdmins(); |
|
| 90 | + // New class returns IUser[] so convert back |
|
| 91 | + $result = []; |
|
| 92 | + foreach ($subAdmins as $subAdmin) { |
|
| 93 | + $result[] = [ |
|
| 94 | + 'gid' => $subAdmin['group']->getGID(), |
|
| 95 | + 'uid' => $subAdmin['user']->getUID(), |
|
| 96 | + ]; |
|
| 97 | + } |
|
| 98 | + $subAdmins = $result; |
|
| 99 | 99 | } else { |
| 100 | - /* Retrieve group IDs from $groups array, so we can pass that information into OC_Group::displayNamesInGroups() */ |
|
| 101 | - $gids = array(); |
|
| 102 | - foreach($groups as $group) { |
|
| 103 | - if (isset($group['id'])) { |
|
| 104 | - $gids[] = $group['id']; |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - $subAdmins = false; |
|
| 100 | + /* Retrieve group IDs from $groups array, so we can pass that information into OC_Group::displayNamesInGroups() */ |
|
| 101 | + $gids = array(); |
|
| 102 | + foreach($groups as $group) { |
|
| 103 | + if (isset($group['id'])) { |
|
| 104 | + $gids[] = $group['id']; |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + $subAdmins = false; |
|
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | // remove disabled from total count |
| 111 | 111 | foreach($groups as $key => $group) { |
| 112 | - $groups[$key]['usercount'] -= $group['disabled']; |
|
| 112 | + $groups[$key]['usercount'] -= $group['disabled']; |
|
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | $disabledUsers = $isLDAPUsed ? 0 : $isAdmin ? $userManager->countDisabledUsers() : $userManager->countDisabledUsersOfGroups(); |
| 116 | 116 | $disabledUsersGroup = [ |
| 117 | - 'id' => '_disabledUsers', |
|
| 118 | - 'name' => '_disabledUsers', |
|
| 119 | - 'usercount' => $disabledUsers |
|
| 117 | + 'id' => '_disabledUsers', |
|
| 118 | + 'name' => '_disabledUsers', |
|
| 119 | + 'usercount' => $disabledUsers |
|
| 120 | 120 | ]; |
| 121 | 121 | |
| 122 | 122 | // load preset quotas |
| 123 | 123 | $quotaPreset=$config->getAppValue('files', 'quota_preset', '1 GB, 5 GB, 10 GB'); |
| 124 | 124 | $quotaPreset=explode(',', $quotaPreset); |
| 125 | 125 | foreach($quotaPreset as &$preset) { |
| 126 | - $preset=trim($preset); |
|
| 126 | + $preset=trim($preset); |
|
| 127 | 127 | } |
| 128 | 128 | $quotaPreset=array_diff($quotaPreset, array('default', 'none')); |
| 129 | 129 | |
| 130 | 130 | $defaultQuota=$config->getAppValue('files', 'default_quota', 'none'); |
| 131 | 131 | $defaultQuotaIsUserDefined=array_search($defaultQuota, $quotaPreset)===false |
| 132 | - && array_search($defaultQuota, array('none', 'default'))===false; |
|
| 132 | + && array_search($defaultQuota, array('none', 'default'))===false; |
|
| 133 | 133 | |
| 134 | 134 | \OC::$server->getEventDispatcher()->dispatch('OC\Settings\Users::loadAdditionalScripts'); |
| 135 | 135 | |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | list($adminGroup, $groups) = $groupsInfo->get(); |
| 84 | 84 | |
| 85 | 85 | $recoveryAdminEnabled = $appManager->isEnabledForUser('encryption') && |
| 86 | - $config->getAppValue( 'encryption', 'recoveryAdminEnabled', '0'); |
|
| 86 | + $config->getAppValue('encryption', 'recoveryAdminEnabled', '0'); |
|
| 87 | 87 | |
| 88 | 88 | if ($isAdmin) { |
| 89 | 89 | $subAdmins = \OC::$server->getGroupManager()->getSubAdmin()->getAllSubAdmins(); |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | } else { |
| 100 | 100 | /* Retrieve group IDs from $groups array, so we can pass that information into OC_Group::displayNamesInGroups() */ |
| 101 | 101 | $gids = array(); |
| 102 | - foreach($groups as $group) { |
|
| 102 | + foreach ($groups as $group) { |
|
| 103 | 103 | if (isset($group['id'])) { |
| 104 | 104 | $gids[] = $group['id']; |
| 105 | 105 | } |
@@ -108,7 +108,7 @@ discard block |
||
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | // remove disabled from total count |
| 111 | -foreach($groups as $key => $group) { |
|
| 111 | +foreach ($groups as $key => $group) { |
|
| 112 | 112 | $groups[$key]['usercount'] -= $group['disabled']; |
| 113 | 113 | } |
| 114 | 114 | |
@@ -120,16 +120,16 @@ discard block |
||
| 120 | 120 | ]; |
| 121 | 121 | |
| 122 | 122 | // load preset quotas |
| 123 | -$quotaPreset=$config->getAppValue('files', 'quota_preset', '1 GB, 5 GB, 10 GB'); |
|
| 124 | -$quotaPreset=explode(',', $quotaPreset); |
|
| 125 | -foreach($quotaPreset as &$preset) { |
|
| 126 | - $preset=trim($preset); |
|
| 123 | +$quotaPreset = $config->getAppValue('files', 'quota_preset', '1 GB, 5 GB, 10 GB'); |
|
| 124 | +$quotaPreset = explode(',', $quotaPreset); |
|
| 125 | +foreach ($quotaPreset as &$preset) { |
|
| 126 | + $preset = trim($preset); |
|
| 127 | 127 | } |
| 128 | -$quotaPreset=array_diff($quotaPreset, array('default', 'none')); |
|
| 128 | +$quotaPreset = array_diff($quotaPreset, array('default', 'none')); |
|
| 129 | 129 | |
| 130 | -$defaultQuota=$config->getAppValue('files', 'default_quota', 'none'); |
|
| 131 | -$defaultQuotaIsUserDefined=array_search($defaultQuota, $quotaPreset)===false |
|
| 132 | - && array_search($defaultQuota, array('none', 'default'))===false; |
|
| 130 | +$defaultQuota = $config->getAppValue('files', 'default_quota', 'none'); |
|
| 131 | +$defaultQuotaIsUserDefined = array_search($defaultQuota, $quotaPreset) === false |
|
| 132 | + && array_search($defaultQuota, array('none', 'default')) === false; |
|
| 133 | 133 | |
| 134 | 134 | \OC::$server->getEventDispatcher()->dispatch('OC\Settings\Users::loadAdditionalScripts'); |
| 135 | 135 | |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | $tmpl->assign('sortGroups', $sortGroupsBy); |
| 139 | 139 | $tmpl->assign('adminGroup', $adminGroup); |
| 140 | 140 | $tmpl->assign('disabledUsersGroup', $disabledUsersGroup); |
| 141 | -$tmpl->assign('isAdmin', (int)$isAdmin); |
|
| 141 | +$tmpl->assign('isAdmin', (int) $isAdmin); |
|
| 142 | 142 | $tmpl->assign('subadmins', $subAdmins); |
| 143 | 143 | $tmpl->assign('numofgroups', count($groups) + count($adminGroup)); |
| 144 | 144 | $tmpl->assign('quota_preset', $quotaPreset); |
@@ -48,154 +48,154 @@ |
||
| 48 | 48 | * @since 8.0.0 |
| 49 | 49 | */ |
| 50 | 50 | interface IUserManager { |
| 51 | - /** |
|
| 52 | - * register a user backend |
|
| 53 | - * |
|
| 54 | - * @param \OCP\UserInterface $backend |
|
| 55 | - * @since 8.0.0 |
|
| 56 | - */ |
|
| 57 | - public function registerBackend($backend); |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Get the active backends |
|
| 61 | - * @return \OCP\UserInterface[] |
|
| 62 | - * @since 8.0.0 |
|
| 63 | - */ |
|
| 64 | - public function getBackends(); |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * remove a user backend |
|
| 68 | - * |
|
| 69 | - * @param \OCP\UserInterface $backend |
|
| 70 | - * @since 8.0.0 |
|
| 71 | - */ |
|
| 72 | - public function removeBackend($backend); |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * remove all user backends |
|
| 76 | - * @since 8.0.0 |
|
| 77 | - */ |
|
| 78 | - public function clearBackends() ; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * get a user by user id |
|
| 82 | - * |
|
| 83 | - * @param string $uid |
|
| 84 | - * @return \OCP\IUser|null Either the user or null if the specified user does not exist |
|
| 85 | - * @since 8.0.0 |
|
| 86 | - */ |
|
| 87 | - public function get($uid); |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * check if a user exists |
|
| 91 | - * |
|
| 92 | - * @param string $uid |
|
| 93 | - * @return bool |
|
| 94 | - * @since 8.0.0 |
|
| 95 | - */ |
|
| 96 | - public function userExists($uid); |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Check if the password is valid for the user |
|
| 100 | - * |
|
| 101 | - * @param string $loginName |
|
| 102 | - * @param string $password |
|
| 103 | - * @return mixed the User object on success, false otherwise |
|
| 104 | - * @since 8.0.0 |
|
| 105 | - */ |
|
| 106 | - public function checkPassword($loginName, $password); |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * search by user id |
|
| 110 | - * |
|
| 111 | - * @param string $pattern |
|
| 112 | - * @param int $limit |
|
| 113 | - * @param int $offset |
|
| 114 | - * @return \OCP\IUser[] |
|
| 115 | - * @since 8.0.0 |
|
| 116 | - */ |
|
| 117 | - public function search($pattern, $limit = null, $offset = null); |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * search by displayName |
|
| 121 | - * |
|
| 122 | - * @param string $pattern |
|
| 123 | - * @param int $limit |
|
| 124 | - * @param int $offset |
|
| 125 | - * @return \OCP\IUser[] |
|
| 126 | - * @since 8.0.0 |
|
| 127 | - */ |
|
| 128 | - public function searchDisplayName($pattern, $limit = null, $offset = null); |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * @param string $uid |
|
| 132 | - * @param string $password |
|
| 133 | - * @throws \InvalidArgumentException |
|
| 134 | - * @return bool|\OCP\IUser the created user of false |
|
| 135 | - * @since 8.0.0 |
|
| 136 | - */ |
|
| 137 | - public function createUser($uid, $password); |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * @param string $uid |
|
| 141 | - * @param string $password |
|
| 142 | - * @param UserInterface $backend |
|
| 143 | - * @return IUser|null |
|
| 144 | - * @throws \InvalidArgumentException |
|
| 145 | - * @since 12.0.0 |
|
| 146 | - */ |
|
| 147 | - public function createUserFromBackend($uid, $password, UserInterface $backend); |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * returns how many users per backend exist (if supported by backend) |
|
| 151 | - * |
|
| 152 | - * @return array an array of backend class as key and count number as value |
|
| 153 | - * @since 8.0.0 |
|
| 154 | - */ |
|
| 155 | - public function countUsers(); |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * @param \Closure $callback |
|
| 159 | - * @param string $search |
|
| 160 | - * @since 9.0.0 |
|
| 161 | - */ |
|
| 162 | - public function callForAllUsers(\Closure $callback, $search = ''); |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * returns how many users are disabled |
|
| 166 | - * |
|
| 167 | - * @return int |
|
| 168 | - * @since 12.0.0 |
|
| 169 | - */ |
|
| 170 | - public function countDisabledUsers(); |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * returns how many users are disabled in the requested groups |
|
| 174 | - * |
|
| 175 | - * @param array $groups groupids to search |
|
| 176 | - * @return int |
|
| 177 | - * @since 14.0.0 |
|
| 178 | - */ |
|
| 179 | - public function countDisabledUsersOfGroups(array $groups); |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * returns how many users have logged in once |
|
| 183 | - * |
|
| 184 | - * @return int |
|
| 185 | - * @since 11.0.0 |
|
| 186 | - */ |
|
| 187 | - public function countSeenUsers(); |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * @param \Closure $callback |
|
| 191 | - * @since 11.0.0 |
|
| 192 | - */ |
|
| 193 | - public function callForSeenUsers(\Closure $callback); |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * @param string $email |
|
| 197 | - * @return IUser[] |
|
| 198 | - * @since 9.1.0 |
|
| 199 | - */ |
|
| 200 | - public function getByEmail($email); |
|
| 51 | + /** |
|
| 52 | + * register a user backend |
|
| 53 | + * |
|
| 54 | + * @param \OCP\UserInterface $backend |
|
| 55 | + * @since 8.0.0 |
|
| 56 | + */ |
|
| 57 | + public function registerBackend($backend); |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Get the active backends |
|
| 61 | + * @return \OCP\UserInterface[] |
|
| 62 | + * @since 8.0.0 |
|
| 63 | + */ |
|
| 64 | + public function getBackends(); |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * remove a user backend |
|
| 68 | + * |
|
| 69 | + * @param \OCP\UserInterface $backend |
|
| 70 | + * @since 8.0.0 |
|
| 71 | + */ |
|
| 72 | + public function removeBackend($backend); |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * remove all user backends |
|
| 76 | + * @since 8.0.0 |
|
| 77 | + */ |
|
| 78 | + public function clearBackends() ; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * get a user by user id |
|
| 82 | + * |
|
| 83 | + * @param string $uid |
|
| 84 | + * @return \OCP\IUser|null Either the user or null if the specified user does not exist |
|
| 85 | + * @since 8.0.0 |
|
| 86 | + */ |
|
| 87 | + public function get($uid); |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * check if a user exists |
|
| 91 | + * |
|
| 92 | + * @param string $uid |
|
| 93 | + * @return bool |
|
| 94 | + * @since 8.0.0 |
|
| 95 | + */ |
|
| 96 | + public function userExists($uid); |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Check if the password is valid for the user |
|
| 100 | + * |
|
| 101 | + * @param string $loginName |
|
| 102 | + * @param string $password |
|
| 103 | + * @return mixed the User object on success, false otherwise |
|
| 104 | + * @since 8.0.0 |
|
| 105 | + */ |
|
| 106 | + public function checkPassword($loginName, $password); |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * search by user id |
|
| 110 | + * |
|
| 111 | + * @param string $pattern |
|
| 112 | + * @param int $limit |
|
| 113 | + * @param int $offset |
|
| 114 | + * @return \OCP\IUser[] |
|
| 115 | + * @since 8.0.0 |
|
| 116 | + */ |
|
| 117 | + public function search($pattern, $limit = null, $offset = null); |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * search by displayName |
|
| 121 | + * |
|
| 122 | + * @param string $pattern |
|
| 123 | + * @param int $limit |
|
| 124 | + * @param int $offset |
|
| 125 | + * @return \OCP\IUser[] |
|
| 126 | + * @since 8.0.0 |
|
| 127 | + */ |
|
| 128 | + public function searchDisplayName($pattern, $limit = null, $offset = null); |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * @param string $uid |
|
| 132 | + * @param string $password |
|
| 133 | + * @throws \InvalidArgumentException |
|
| 134 | + * @return bool|\OCP\IUser the created user of false |
|
| 135 | + * @since 8.0.0 |
|
| 136 | + */ |
|
| 137 | + public function createUser($uid, $password); |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * @param string $uid |
|
| 141 | + * @param string $password |
|
| 142 | + * @param UserInterface $backend |
|
| 143 | + * @return IUser|null |
|
| 144 | + * @throws \InvalidArgumentException |
|
| 145 | + * @since 12.0.0 |
|
| 146 | + */ |
|
| 147 | + public function createUserFromBackend($uid, $password, UserInterface $backend); |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * returns how many users per backend exist (if supported by backend) |
|
| 151 | + * |
|
| 152 | + * @return array an array of backend class as key and count number as value |
|
| 153 | + * @since 8.0.0 |
|
| 154 | + */ |
|
| 155 | + public function countUsers(); |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * @param \Closure $callback |
|
| 159 | + * @param string $search |
|
| 160 | + * @since 9.0.0 |
|
| 161 | + */ |
|
| 162 | + public function callForAllUsers(\Closure $callback, $search = ''); |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * returns how many users are disabled |
|
| 166 | + * |
|
| 167 | + * @return int |
|
| 168 | + * @since 12.0.0 |
|
| 169 | + */ |
|
| 170 | + public function countDisabledUsers(); |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * returns how many users are disabled in the requested groups |
|
| 174 | + * |
|
| 175 | + * @param array $groups groupids to search |
|
| 176 | + * @return int |
|
| 177 | + * @since 14.0.0 |
|
| 178 | + */ |
|
| 179 | + public function countDisabledUsersOfGroups(array $groups); |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * returns how many users have logged in once |
|
| 183 | + * |
|
| 184 | + * @return int |
|
| 185 | + * @since 11.0.0 |
|
| 186 | + */ |
|
| 187 | + public function countSeenUsers(); |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * @param \Closure $callback |
|
| 191 | + * @since 11.0.0 |
|
| 192 | + */ |
|
| 193 | + public function callForSeenUsers(\Closure $callback); |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * @param string $email |
|
| 197 | + * @return IUser[] |
|
| 198 | + * @since 9.1.0 |
|
| 199 | + */ |
|
| 200 | + public function getByEmail($email); |
|
| 201 | 201 | } |