@@ -24,224 +24,224 @@ |
||
| 24 | 24 | * @since 8.0.0 |
| 25 | 25 | */ |
| 26 | 26 | interface IUserManager { |
| 27 | - /** |
|
| 28 | - * @since 26.0.0 |
|
| 29 | - */ |
|
| 30 | - public const MAX_PASSWORD_LENGTH = 469; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * register a user backend |
|
| 34 | - * |
|
| 35 | - * @since 8.0.0 |
|
| 36 | - * @return void |
|
| 37 | - */ |
|
| 38 | - public function registerBackend(UserInterface $backend); |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Get the active backends |
|
| 42 | - * @return UserInterface[] |
|
| 43 | - * @since 8.0.0 |
|
| 44 | - */ |
|
| 45 | - public function getBackends(); |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * remove a user backend |
|
| 49 | - * |
|
| 50 | - * @since 8.0.0 |
|
| 51 | - * @return void |
|
| 52 | - */ |
|
| 53 | - public function removeBackend(UserInterface $backend); |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * remove all user backends |
|
| 57 | - * @since 8.0.0 |
|
| 58 | - * @return void |
|
| 59 | - */ |
|
| 60 | - public function clearBackends(); |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * get a user by user id |
|
| 64 | - * |
|
| 65 | - * @param string $uid |
|
| 66 | - * @return \OCP\IUser|null Either the user or null if the specified user does not exist |
|
| 67 | - * @since 8.0.0 |
|
| 68 | - */ |
|
| 69 | - public function get($uid); |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Get the display name of a user |
|
| 73 | - * |
|
| 74 | - * @param string $uid |
|
| 75 | - * @return string|null |
|
| 76 | - * @since 25.0.0 |
|
| 77 | - */ |
|
| 78 | - public function getDisplayName(string $uid): ?string; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * check if a user exists |
|
| 82 | - * |
|
| 83 | - * @param string $uid |
|
| 84 | - * @return bool |
|
| 85 | - * @since 8.0.0 |
|
| 86 | - */ |
|
| 87 | - public function userExists($uid); |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Check if the password is valid for the user |
|
| 91 | - * |
|
| 92 | - * @param string $loginName |
|
| 93 | - * @param string $password |
|
| 94 | - * @return IUser|false the User object on success, false otherwise |
|
| 95 | - * @since 8.0.0 |
|
| 96 | - */ |
|
| 97 | - public function checkPassword($loginName, $password); |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * search by user id |
|
| 101 | - * |
|
| 102 | - * @param string $pattern |
|
| 103 | - * @param int $limit |
|
| 104 | - * @param int $offset |
|
| 105 | - * @return \OCP\IUser[] |
|
| 106 | - * @since 8.0.0 |
|
| 107 | - */ |
|
| 108 | - public function search($pattern, $limit = null, $offset = null); |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * search by displayName |
|
| 112 | - * |
|
| 113 | - * @param string $pattern |
|
| 114 | - * @param int $limit |
|
| 115 | - * @param int $offset |
|
| 116 | - * @return \OCP\IUser[] |
|
| 117 | - * @since 8.0.0 |
|
| 118 | - */ |
|
| 119 | - public function searchDisplayName($pattern, $limit = null, $offset = null); |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * @return IUser[] |
|
| 123 | - * @since 28.0.0 |
|
| 124 | - * @since 30.0.0 $search parameter added |
|
| 125 | - */ |
|
| 126 | - public function getDisabledUsers(?int $limit = null, int $offset = 0, string $search = ''): array; |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Search known users (from phonebook sync) by displayName |
|
| 130 | - * |
|
| 131 | - * @param string $searcher |
|
| 132 | - * @param string $pattern |
|
| 133 | - * @param int|null $limit |
|
| 134 | - * @param int|null $offset |
|
| 135 | - * @return IUser[] |
|
| 136 | - * @since 21.0.1 |
|
| 137 | - */ |
|
| 138 | - public function searchKnownUsersByDisplayName(string $searcher, string $pattern, ?int $limit = null, ?int $offset = null): array; |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @param string $uid |
|
| 142 | - * @param string $password |
|
| 143 | - * @throws \InvalidArgumentException |
|
| 144 | - * @return false|\OCP\IUser the created user or false |
|
| 145 | - * @since 8.0.0 |
|
| 146 | - */ |
|
| 147 | - public function createUser($uid, $password); |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * @param string $uid |
|
| 151 | - * @param string $password |
|
| 152 | - * @param UserInterface $backend |
|
| 153 | - * @return IUser|null |
|
| 154 | - * @throws \InvalidArgumentException |
|
| 155 | - * @since 12.0.0 |
|
| 156 | - */ |
|
| 157 | - public function createUserFromBackend($uid, $password, UserInterface $backend); |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * Get how many users per backend exist (if supported by backend) |
|
| 161 | - * |
|
| 162 | - * @return array<string, int> an array of backend class name as key and count number as value |
|
| 163 | - * @since 8.0.0 |
|
| 164 | - */ |
|
| 165 | - public function countUsers(); |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Get how many users exists in total, whithin limit |
|
| 169 | - * |
|
| 170 | - * @param int $limit Limit the count to avoid resource waste. 0 to disable |
|
| 171 | - * @param bool $onlyMappedUsers Count mapped users instead of all users for compatible backends |
|
| 172 | - * |
|
| 173 | - * @since 31.0.0 |
|
| 174 | - */ |
|
| 175 | - public function countUsersTotal(int $limit = 0, bool $onlyMappedUsers = false): int|false; |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * @param \Closure $callback |
|
| 179 | - * @psalm-param \Closure(\OCP\IUser):void $callback |
|
| 180 | - * @param string $search |
|
| 181 | - * @since 9.0.0 |
|
| 182 | - */ |
|
| 183 | - public function callForAllUsers(\Closure $callback, $search = ''); |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * returns how many users have logged in once |
|
| 187 | - * |
|
| 188 | - * @return int |
|
| 189 | - * @since 11.0.0 |
|
| 190 | - */ |
|
| 191 | - public function countDisabledUsers(); |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * returns how many users have logged in once |
|
| 195 | - * |
|
| 196 | - * @return int |
|
| 197 | - * @since 11.0.0 |
|
| 198 | - */ |
|
| 199 | - public function countSeenUsers(); |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * @param \Closure $callback |
|
| 203 | - * @psalm-param \Closure(\OCP\IUser):?bool $callback |
|
| 204 | - * @since 11.0.0 |
|
| 205 | - */ |
|
| 206 | - public function callForSeenUsers(\Closure $callback); |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * returns all users having the provided email set as system email address |
|
| 210 | - * |
|
| 211 | - * @param string $email |
|
| 212 | - * @return IUser[] |
|
| 213 | - * @since 9.1.0 |
|
| 214 | - */ |
|
| 215 | - public function getByEmail($email); |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * @param string $uid The user ID to validate |
|
| 219 | - * @param bool $checkDataDirectory Whether it should be checked if files for the ID exist inside the data directory |
|
| 220 | - * @throws \InvalidArgumentException Message is an already translated string with a reason why the ID is not valid |
|
| 221 | - * @since 26.0.0 |
|
| 222 | - */ |
|
| 223 | - public function validateUserId(string $uid, bool $checkDataDirectory = false): void; |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Gets the list of users sorted by lastLogin, from most recent to least recent |
|
| 227 | - * |
|
| 228 | - * @param int|null $limit how many records to fetch |
|
| 229 | - * @param int $offset from which offset to fetch |
|
| 230 | - * @param string $search search users based on search params |
|
| 231 | - * @return list<string> list of user IDs |
|
| 232 | - * @since 30.0.0 |
|
| 233 | - */ |
|
| 234 | - public function getLastLoggedInUsers(?int $limit = null, int $offset = 0, string $search = ''): array; |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * Gets the list of users. |
|
| 238 | - * An iterator is returned allowing the caller to stop the iteration at any time. |
|
| 239 | - * The offset argument allows the caller to continue the iteration at a specific offset. |
|
| 240 | - * |
|
| 241 | - * @param int $offset from which offset to fetch |
|
| 242 | - * @param int|null $limit maximum number of records to fetch |
|
| 243 | - * @return \Iterator<IUser> list of IUser object |
|
| 244 | - * @since 32.0.0 |
|
| 245 | - */ |
|
| 246 | - public function getSeenUsers(int $offset = 0, ?int $limit = null): \Iterator; |
|
| 27 | + /** |
|
| 28 | + * @since 26.0.0 |
|
| 29 | + */ |
|
| 30 | + public const MAX_PASSWORD_LENGTH = 469; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * register a user backend |
|
| 34 | + * |
|
| 35 | + * @since 8.0.0 |
|
| 36 | + * @return void |
|
| 37 | + */ |
|
| 38 | + public function registerBackend(UserInterface $backend); |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Get the active backends |
|
| 42 | + * @return UserInterface[] |
|
| 43 | + * @since 8.0.0 |
|
| 44 | + */ |
|
| 45 | + public function getBackends(); |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * remove a user backend |
|
| 49 | + * |
|
| 50 | + * @since 8.0.0 |
|
| 51 | + * @return void |
|
| 52 | + */ |
|
| 53 | + public function removeBackend(UserInterface $backend); |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * remove all user backends |
|
| 57 | + * @since 8.0.0 |
|
| 58 | + * @return void |
|
| 59 | + */ |
|
| 60 | + public function clearBackends(); |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * get a user by user id |
|
| 64 | + * |
|
| 65 | + * @param string $uid |
|
| 66 | + * @return \OCP\IUser|null Either the user or null if the specified user does not exist |
|
| 67 | + * @since 8.0.0 |
|
| 68 | + */ |
|
| 69 | + public function get($uid); |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Get the display name of a user |
|
| 73 | + * |
|
| 74 | + * @param string $uid |
|
| 75 | + * @return string|null |
|
| 76 | + * @since 25.0.0 |
|
| 77 | + */ |
|
| 78 | + public function getDisplayName(string $uid): ?string; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * check if a user exists |
|
| 82 | + * |
|
| 83 | + * @param string $uid |
|
| 84 | + * @return bool |
|
| 85 | + * @since 8.0.0 |
|
| 86 | + */ |
|
| 87 | + public function userExists($uid); |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Check if the password is valid for the user |
|
| 91 | + * |
|
| 92 | + * @param string $loginName |
|
| 93 | + * @param string $password |
|
| 94 | + * @return IUser|false the User object on success, false otherwise |
|
| 95 | + * @since 8.0.0 |
|
| 96 | + */ |
|
| 97 | + public function checkPassword($loginName, $password); |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * search by user id |
|
| 101 | + * |
|
| 102 | + * @param string $pattern |
|
| 103 | + * @param int $limit |
|
| 104 | + * @param int $offset |
|
| 105 | + * @return \OCP\IUser[] |
|
| 106 | + * @since 8.0.0 |
|
| 107 | + */ |
|
| 108 | + public function search($pattern, $limit = null, $offset = null); |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * search by displayName |
|
| 112 | + * |
|
| 113 | + * @param string $pattern |
|
| 114 | + * @param int $limit |
|
| 115 | + * @param int $offset |
|
| 116 | + * @return \OCP\IUser[] |
|
| 117 | + * @since 8.0.0 |
|
| 118 | + */ |
|
| 119 | + public function searchDisplayName($pattern, $limit = null, $offset = null); |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * @return IUser[] |
|
| 123 | + * @since 28.0.0 |
|
| 124 | + * @since 30.0.0 $search parameter added |
|
| 125 | + */ |
|
| 126 | + public function getDisabledUsers(?int $limit = null, int $offset = 0, string $search = ''): array; |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Search known users (from phonebook sync) by displayName |
|
| 130 | + * |
|
| 131 | + * @param string $searcher |
|
| 132 | + * @param string $pattern |
|
| 133 | + * @param int|null $limit |
|
| 134 | + * @param int|null $offset |
|
| 135 | + * @return IUser[] |
|
| 136 | + * @since 21.0.1 |
|
| 137 | + */ |
|
| 138 | + public function searchKnownUsersByDisplayName(string $searcher, string $pattern, ?int $limit = null, ?int $offset = null): array; |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @param string $uid |
|
| 142 | + * @param string $password |
|
| 143 | + * @throws \InvalidArgumentException |
|
| 144 | + * @return false|\OCP\IUser the created user or false |
|
| 145 | + * @since 8.0.0 |
|
| 146 | + */ |
|
| 147 | + public function createUser($uid, $password); |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * @param string $uid |
|
| 151 | + * @param string $password |
|
| 152 | + * @param UserInterface $backend |
|
| 153 | + * @return IUser|null |
|
| 154 | + * @throws \InvalidArgumentException |
|
| 155 | + * @since 12.0.0 |
|
| 156 | + */ |
|
| 157 | + public function createUserFromBackend($uid, $password, UserInterface $backend); |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * Get how many users per backend exist (if supported by backend) |
|
| 161 | + * |
|
| 162 | + * @return array<string, int> an array of backend class name as key and count number as value |
|
| 163 | + * @since 8.0.0 |
|
| 164 | + */ |
|
| 165 | + public function countUsers(); |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Get how many users exists in total, whithin limit |
|
| 169 | + * |
|
| 170 | + * @param int $limit Limit the count to avoid resource waste. 0 to disable |
|
| 171 | + * @param bool $onlyMappedUsers Count mapped users instead of all users for compatible backends |
|
| 172 | + * |
|
| 173 | + * @since 31.0.0 |
|
| 174 | + */ |
|
| 175 | + public function countUsersTotal(int $limit = 0, bool $onlyMappedUsers = false): int|false; |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * @param \Closure $callback |
|
| 179 | + * @psalm-param \Closure(\OCP\IUser):void $callback |
|
| 180 | + * @param string $search |
|
| 181 | + * @since 9.0.0 |
|
| 182 | + */ |
|
| 183 | + public function callForAllUsers(\Closure $callback, $search = ''); |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * returns how many users have logged in once |
|
| 187 | + * |
|
| 188 | + * @return int |
|
| 189 | + * @since 11.0.0 |
|
| 190 | + */ |
|
| 191 | + public function countDisabledUsers(); |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * returns how many users have logged in once |
|
| 195 | + * |
|
| 196 | + * @return int |
|
| 197 | + * @since 11.0.0 |
|
| 198 | + */ |
|
| 199 | + public function countSeenUsers(); |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * @param \Closure $callback |
|
| 203 | + * @psalm-param \Closure(\OCP\IUser):?bool $callback |
|
| 204 | + * @since 11.0.0 |
|
| 205 | + */ |
|
| 206 | + public function callForSeenUsers(\Closure $callback); |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * returns all users having the provided email set as system email address |
|
| 210 | + * |
|
| 211 | + * @param string $email |
|
| 212 | + * @return IUser[] |
|
| 213 | + * @since 9.1.0 |
|
| 214 | + */ |
|
| 215 | + public function getByEmail($email); |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * @param string $uid The user ID to validate |
|
| 219 | + * @param bool $checkDataDirectory Whether it should be checked if files for the ID exist inside the data directory |
|
| 220 | + * @throws \InvalidArgumentException Message is an already translated string with a reason why the ID is not valid |
|
| 221 | + * @since 26.0.0 |
|
| 222 | + */ |
|
| 223 | + public function validateUserId(string $uid, bool $checkDataDirectory = false): void; |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Gets the list of users sorted by lastLogin, from most recent to least recent |
|
| 227 | + * |
|
| 228 | + * @param int|null $limit how many records to fetch |
|
| 229 | + * @param int $offset from which offset to fetch |
|
| 230 | + * @param string $search search users based on search params |
|
| 231 | + * @return list<string> list of user IDs |
|
| 232 | + * @since 30.0.0 |
|
| 233 | + */ |
|
| 234 | + public function getLastLoggedInUsers(?int $limit = null, int $offset = 0, string $search = ''): array; |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * Gets the list of users. |
|
| 238 | + * An iterator is returned allowing the caller to stop the iteration at any time. |
|
| 239 | + * The offset argument allows the caller to continue the iteration at a specific offset. |
|
| 240 | + * |
|
| 241 | + * @param int $offset from which offset to fetch |
|
| 242 | + * @param int|null $limit maximum number of records to fetch |
|
| 243 | + * @return \Iterator<IUser> list of IUser object |
|
| 244 | + * @since 32.0.0 |
|
| 245 | + */ |
|
| 246 | + public function getSeenUsers(int $offset = 0, ?int $limit = null): \Iterator; |
|
| 247 | 247 | } |
@@ -53,789 +53,789 @@ |
||
| 53 | 53 | * @package OC\User |
| 54 | 54 | */ |
| 55 | 55 | class Manager extends PublicEmitter implements IUserManager { |
| 56 | - /** |
|
| 57 | - * @var UserInterface[] $backends |
|
| 58 | - */ |
|
| 59 | - private array $backends = []; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @var array<string,\OC\User\User> $cachedUsers |
|
| 63 | - */ |
|
| 64 | - private array $cachedUsers = []; |
|
| 65 | - |
|
| 66 | - private ICache $cache; |
|
| 67 | - |
|
| 68 | - private DisplayNameCache $displayNameCache; |
|
| 69 | - |
|
| 70 | - public function __construct( |
|
| 71 | - private IConfig $config, |
|
| 72 | - ICacheFactory $cacheFactory, |
|
| 73 | - private IEventDispatcher $eventDispatcher, |
|
| 74 | - private LoggerInterface $logger, |
|
| 75 | - ) { |
|
| 76 | - $this->cache = new WithLocalCache($cacheFactory->createDistributed('user_backend_map')); |
|
| 77 | - $this->listen('\OC\User', 'postDelete', function (IUser $user): void { |
|
| 78 | - unset($this->cachedUsers[$user->getUID()]); |
|
| 79 | - }); |
|
| 80 | - $this->displayNameCache = new DisplayNameCache($cacheFactory, $this); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Get the active backends |
|
| 85 | - * @return UserInterface[] |
|
| 86 | - */ |
|
| 87 | - public function getBackends(): array { |
|
| 88 | - return $this->backends; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - public function registerBackend(UserInterface $backend): void { |
|
| 92 | - $this->backends[] = $backend; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - public function removeBackend(UserInterface $backend): void { |
|
| 96 | - $this->cachedUsers = []; |
|
| 97 | - if (($i = array_search($backend, $this->backends)) !== false) { |
|
| 98 | - unset($this->backends[$i]); |
|
| 99 | - } |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - public function clearBackends(): void { |
|
| 103 | - $this->cachedUsers = []; |
|
| 104 | - $this->backends = []; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * get a user by user id |
|
| 109 | - * |
|
| 110 | - * @param string $uid |
|
| 111 | - * @return \OC\User\User|null Either the user or null if the specified user does not exist |
|
| 112 | - */ |
|
| 113 | - public function get($uid) { |
|
| 114 | - if (is_null($uid) || $uid === '' || $uid === false) { |
|
| 115 | - return null; |
|
| 116 | - } |
|
| 117 | - if (isset($this->cachedUsers[$uid])) { //check the cache first to prevent having to loop over the backends |
|
| 118 | - return $this->cachedUsers[$uid]; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - if (strlen($uid) > IUser::MAX_USERID_LENGTH) { |
|
| 122 | - return null; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - $cachedBackend = $this->cache->get(sha1($uid)); |
|
| 126 | - if ($cachedBackend !== null && isset($this->backends[$cachedBackend])) { |
|
| 127 | - // Cache has the info of the user backend already, so ask that one directly |
|
| 128 | - $backend = $this->backends[$cachedBackend]; |
|
| 129 | - if ($backend->userExists($uid)) { |
|
| 130 | - return $this->getUserObject($uid, $backend); |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - foreach ($this->backends as $i => $backend) { |
|
| 135 | - if ($i === $cachedBackend) { |
|
| 136 | - // Tried that one already |
|
| 137 | - continue; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - if ($backend->userExists($uid)) { |
|
| 141 | - // Hash $uid to ensure that only valid characters are used for the cache key |
|
| 142 | - $this->cache->set(sha1($uid), $i, 300); |
|
| 143 | - return $this->getUserObject($uid, $backend); |
|
| 144 | - } |
|
| 145 | - } |
|
| 146 | - return null; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - public function getDisplayName(string $uid): ?string { |
|
| 150 | - return $this->displayNameCache->getDisplayName($uid); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * get or construct the user object |
|
| 155 | - * |
|
| 156 | - * @param string $uid |
|
| 157 | - * @param \OCP\UserInterface $backend |
|
| 158 | - * @param bool $cacheUser If false the newly created user object will not be cached |
|
| 159 | - * @return \OC\User\User |
|
| 160 | - */ |
|
| 161 | - public function getUserObject($uid, $backend, $cacheUser = true) { |
|
| 162 | - if ($backend instanceof IGetRealUIDBackend) { |
|
| 163 | - $uid = $backend->getRealUID($uid); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - if (isset($this->cachedUsers[$uid])) { |
|
| 167 | - return $this->cachedUsers[$uid]; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - $user = new User($uid, $backend, $this->eventDispatcher, $this, $this->config); |
|
| 171 | - if ($cacheUser) { |
|
| 172 | - $this->cachedUsers[$uid] = $user; |
|
| 173 | - } |
|
| 174 | - return $user; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * check if a user exists |
|
| 179 | - * |
|
| 180 | - * @param string $uid |
|
| 181 | - * @return bool |
|
| 182 | - */ |
|
| 183 | - public function userExists($uid) { |
|
| 184 | - if (strlen($uid) > IUser::MAX_USERID_LENGTH) { |
|
| 185 | - return false; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - $user = $this->get($uid); |
|
| 189 | - return ($user !== null); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Check if the password is valid for the user |
|
| 194 | - * |
|
| 195 | - * @param string $loginName |
|
| 196 | - * @param string $password |
|
| 197 | - * @return IUser|false the User object on success, false otherwise |
|
| 198 | - */ |
|
| 199 | - public function checkPassword($loginName, $password) { |
|
| 200 | - $result = $this->checkPasswordNoLogging($loginName, $password); |
|
| 201 | - |
|
| 202 | - if ($result === false) { |
|
| 203 | - $this->logger->warning('Login failed: \'' . $loginName . '\' (Remote IP: \'' . \OC::$server->getRequest()->getRemoteAddress() . '\')', ['app' => 'core']); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - return $result; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Check if the password is valid for the user |
|
| 211 | - * |
|
| 212 | - * @internal |
|
| 213 | - * @param string $loginName |
|
| 214 | - * @param string $password |
|
| 215 | - * @return IUser|false the User object on success, false otherwise |
|
| 216 | - */ |
|
| 217 | - public function checkPasswordNoLogging($loginName, $password) { |
|
| 218 | - $loginName = str_replace("\0", '', $loginName); |
|
| 219 | - $password = str_replace("\0", '', $password); |
|
| 220 | - |
|
| 221 | - $cachedBackend = $this->cache->get($loginName); |
|
| 222 | - if ($cachedBackend !== null && isset($this->backends[$cachedBackend])) { |
|
| 223 | - $backends = [$this->backends[$cachedBackend]]; |
|
| 224 | - } else { |
|
| 225 | - $backends = $this->backends; |
|
| 226 | - } |
|
| 227 | - foreach ($backends as $backend) { |
|
| 228 | - if ($backend instanceof ICheckPasswordBackend || $backend->implementsActions(Backend::CHECK_PASSWORD)) { |
|
| 229 | - /** @var ICheckPasswordBackend $backend */ |
|
| 230 | - $uid = $backend->checkPassword($loginName, $password); |
|
| 231 | - if ($uid !== false) { |
|
| 232 | - return $this->getUserObject($uid, $backend); |
|
| 233 | - } |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - // since http basic auth doesn't provide a standard way of handling non ascii password we allow password to be urlencoded |
|
| 238 | - // we only do this decoding after using the plain password fails to maintain compatibility with any password that happens |
|
| 239 | - // to contain urlencoded patterns by "accident". |
|
| 240 | - $password = urldecode($password); |
|
| 241 | - |
|
| 242 | - foreach ($backends as $backend) { |
|
| 243 | - if ($backend instanceof ICheckPasswordBackend || $backend->implementsActions(Backend::CHECK_PASSWORD)) { |
|
| 244 | - /** @var ICheckPasswordBackend|UserInterface $backend */ |
|
| 245 | - $uid = $backend->checkPassword($loginName, $password); |
|
| 246 | - if ($uid !== false) { |
|
| 247 | - return $this->getUserObject($uid, $backend); |
|
| 248 | - } |
|
| 249 | - } |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - return false; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * Search by user id |
|
| 257 | - * |
|
| 258 | - * @param string $pattern |
|
| 259 | - * @param int $limit |
|
| 260 | - * @param int $offset |
|
| 261 | - * @return IUser[] |
|
| 262 | - * @deprecated 27.0.0, use searchDisplayName instead |
|
| 263 | - */ |
|
| 264 | - public function search($pattern, $limit = null, $offset = null) { |
|
| 265 | - $users = []; |
|
| 266 | - foreach ($this->backends as $backend) { |
|
| 267 | - $backendUsers = $backend->getUsers($pattern, $limit, $offset); |
|
| 268 | - if (is_array($backendUsers)) { |
|
| 269 | - foreach ($backendUsers as $uid) { |
|
| 270 | - $users[$uid] = new LazyUser($uid, $this, null, $backend); |
|
| 271 | - } |
|
| 272 | - } |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - uasort($users, function (IUser $a, IUser $b) { |
|
| 276 | - return strcasecmp($a->getUID(), $b->getUID()); |
|
| 277 | - }); |
|
| 278 | - return $users; |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - /** |
|
| 282 | - * Search by displayName |
|
| 283 | - * |
|
| 284 | - * @param string $pattern |
|
| 285 | - * @param int $limit |
|
| 286 | - * @param int $offset |
|
| 287 | - * @return IUser[] |
|
| 288 | - */ |
|
| 289 | - public function searchDisplayName($pattern, $limit = null, $offset = null) { |
|
| 290 | - $users = []; |
|
| 291 | - foreach ($this->backends as $backend) { |
|
| 292 | - $backendUsers = $backend->getDisplayNames($pattern, $limit, $offset); |
|
| 293 | - if (is_array($backendUsers)) { |
|
| 294 | - foreach ($backendUsers as $uid => $displayName) { |
|
| 295 | - $users[] = new LazyUser($uid, $this, $displayName, $backend); |
|
| 296 | - } |
|
| 297 | - } |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - usort($users, function (IUser $a, IUser $b) { |
|
| 301 | - return strcasecmp($a->getDisplayName(), $b->getDisplayName()); |
|
| 302 | - }); |
|
| 303 | - return $users; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * @return IUser[] |
|
| 308 | - */ |
|
| 309 | - public function getDisabledUsers(?int $limit = null, int $offset = 0, string $search = ''): array { |
|
| 310 | - $users = $this->config->getUsersForUserValue('core', 'enabled', 'false'); |
|
| 311 | - $users = array_combine( |
|
| 312 | - $users, |
|
| 313 | - array_map( |
|
| 314 | - fn (string $uid): IUser => new LazyUser($uid, $this), |
|
| 315 | - $users |
|
| 316 | - ) |
|
| 317 | - ); |
|
| 318 | - if ($search !== '') { |
|
| 319 | - $users = array_filter( |
|
| 320 | - $users, |
|
| 321 | - function (IUser $user) use ($search): bool { |
|
| 322 | - try { |
|
| 323 | - return mb_stripos($user->getUID(), $search) !== false |
|
| 324 | - || mb_stripos($user->getDisplayName(), $search) !== false |
|
| 325 | - || mb_stripos($user->getEMailAddress() ?? '', $search) !== false; |
|
| 326 | - } catch (NoUserException $ex) { |
|
| 327 | - $this->logger->error('Error while filtering disabled users', ['exception' => $ex, 'userUID' => $user->getUID()]); |
|
| 328 | - return false; |
|
| 329 | - } |
|
| 330 | - }); |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - $tempLimit = ($limit === null ? null : $limit + $offset); |
|
| 334 | - foreach ($this->backends as $backend) { |
|
| 335 | - if (($tempLimit !== null) && (count($users) >= $tempLimit)) { |
|
| 336 | - break; |
|
| 337 | - } |
|
| 338 | - if ($backend instanceof IProvideEnabledStateBackend) { |
|
| 339 | - $backendUsers = $backend->getDisabledUserList(($tempLimit === null ? null : $tempLimit - count($users)), 0, $search); |
|
| 340 | - foreach ($backendUsers as $uid) { |
|
| 341 | - $users[$uid] = new LazyUser($uid, $this, null, $backend); |
|
| 342 | - } |
|
| 343 | - } |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - return array_slice($users, $offset, $limit); |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - /** |
|
| 350 | - * Search known users (from phonebook sync) by displayName |
|
| 351 | - * |
|
| 352 | - * @param string $searcher |
|
| 353 | - * @param string $pattern |
|
| 354 | - * @param int|null $limit |
|
| 355 | - * @param int|null $offset |
|
| 356 | - * @return IUser[] |
|
| 357 | - */ |
|
| 358 | - public function searchKnownUsersByDisplayName(string $searcher, string $pattern, ?int $limit = null, ?int $offset = null): array { |
|
| 359 | - $users = []; |
|
| 360 | - foreach ($this->backends as $backend) { |
|
| 361 | - if ($backend instanceof ISearchKnownUsersBackend) { |
|
| 362 | - $backendUsers = $backend->searchKnownUsersByDisplayName($searcher, $pattern, $limit, $offset); |
|
| 363 | - } else { |
|
| 364 | - // Better than nothing, but filtering after pagination can remove lots of results. |
|
| 365 | - $backendUsers = $backend->getDisplayNames($pattern, $limit, $offset); |
|
| 366 | - } |
|
| 367 | - if (is_array($backendUsers)) { |
|
| 368 | - foreach ($backendUsers as $uid => $displayName) { |
|
| 369 | - $users[] = $this->getUserObject($uid, $backend); |
|
| 370 | - } |
|
| 371 | - } |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - usort($users, function ($a, $b) { |
|
| 375 | - /** |
|
| 376 | - * @var IUser $a |
|
| 377 | - * @var IUser $b |
|
| 378 | - */ |
|
| 379 | - return strcasecmp($a->getDisplayName(), $b->getDisplayName()); |
|
| 380 | - }); |
|
| 381 | - return $users; |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - /** |
|
| 385 | - * @param string $uid |
|
| 386 | - * @param string $password |
|
| 387 | - * @return false|IUser the created user or false |
|
| 388 | - * @throws \InvalidArgumentException |
|
| 389 | - * @throws HintException |
|
| 390 | - */ |
|
| 391 | - public function createUser($uid, $password) { |
|
| 392 | - // DI injection is not used here as IRegistry needs the user manager itself for user count and thus it would create a cyclic dependency |
|
| 393 | - /** @var IAssertion $assertion */ |
|
| 394 | - $assertion = \OC::$server->get(IAssertion::class); |
|
| 395 | - $assertion->createUserIsLegit(); |
|
| 396 | - |
|
| 397 | - $localBackends = []; |
|
| 398 | - foreach ($this->backends as $backend) { |
|
| 399 | - if ($backend instanceof Database) { |
|
| 400 | - // First check if there is another user backend |
|
| 401 | - $localBackends[] = $backend; |
|
| 402 | - continue; |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - if ($backend->implementsActions(Backend::CREATE_USER)) { |
|
| 406 | - return $this->createUserFromBackend($uid, $password, $backend); |
|
| 407 | - } |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - foreach ($localBackends as $backend) { |
|
| 411 | - if ($backend->implementsActions(Backend::CREATE_USER)) { |
|
| 412 | - return $this->createUserFromBackend($uid, $password, $backend); |
|
| 413 | - } |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - return false; |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - /** |
|
| 420 | - * @param string $uid |
|
| 421 | - * @param string $password |
|
| 422 | - * @param UserInterface $backend |
|
| 423 | - * @return IUser|false |
|
| 424 | - * @throws \InvalidArgumentException |
|
| 425 | - */ |
|
| 426 | - public function createUserFromBackend($uid, $password, UserInterface $backend) { |
|
| 427 | - $l = \OCP\Util::getL10N('lib'); |
|
| 428 | - |
|
| 429 | - $this->validateUserId($uid, true); |
|
| 430 | - |
|
| 431 | - // No empty password |
|
| 432 | - if (trim($password) === '') { |
|
| 433 | - throw new \InvalidArgumentException($l->t('A valid password must be provided')); |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - // Check if user already exists |
|
| 437 | - if ($this->userExists($uid)) { |
|
| 438 | - throw new \InvalidArgumentException($l->t('The Login is already being used')); |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - /** @deprecated 21.0.0 use BeforeUserCreatedEvent event with the IEventDispatcher instead */ |
|
| 442 | - $this->emit('\OC\User', 'preCreateUser', [$uid, $password]); |
|
| 443 | - $this->eventDispatcher->dispatchTyped(new BeforeUserCreatedEvent($uid, $password)); |
|
| 444 | - $state = $backend->createUser($uid, $password); |
|
| 445 | - if ($state === false) { |
|
| 446 | - throw new \InvalidArgumentException($l->t('Could not create account')); |
|
| 447 | - } |
|
| 448 | - $user = $this->getUserObject($uid, $backend); |
|
| 449 | - if ($user instanceof IUser) { |
|
| 450 | - /** @deprecated 21.0.0 use UserCreatedEvent event with the IEventDispatcher instead */ |
|
| 451 | - $this->emit('\OC\User', 'postCreateUser', [$user, $password]); |
|
| 452 | - $this->eventDispatcher->dispatchTyped(new UserCreatedEvent($user, $password)); |
|
| 453 | - return $user; |
|
| 454 | - } |
|
| 455 | - return false; |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - /** |
|
| 459 | - * returns how many users per backend exist (if supported by backend) |
|
| 460 | - * |
|
| 461 | - * @param boolean $hasLoggedIn when true only users that have a lastLogin |
|
| 462 | - * entry in the preferences table will be affected |
|
| 463 | - * @return array<string, int> an array of backend class as key and count number as value |
|
| 464 | - */ |
|
| 465 | - public function countUsers() { |
|
| 466 | - $userCountStatistics = []; |
|
| 467 | - foreach ($this->backends as $backend) { |
|
| 468 | - if ($backend instanceof ICountUsersBackend || $backend->implementsActions(Backend::COUNT_USERS)) { |
|
| 469 | - /** @var ICountUsersBackend|IUserBackend $backend */ |
|
| 470 | - $backendUsers = $backend->countUsers(); |
|
| 471 | - if ($backendUsers !== false) { |
|
| 472 | - if ($backend instanceof IUserBackend) { |
|
| 473 | - $name = $backend->getBackendName(); |
|
| 474 | - } else { |
|
| 475 | - $name = get_class($backend); |
|
| 476 | - } |
|
| 477 | - if (isset($userCountStatistics[$name])) { |
|
| 478 | - $userCountStatistics[$name] += $backendUsers; |
|
| 479 | - } else { |
|
| 480 | - $userCountStatistics[$name] = $backendUsers; |
|
| 481 | - } |
|
| 482 | - } |
|
| 483 | - } |
|
| 484 | - } |
|
| 485 | - return $userCountStatistics; |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - public function countUsersTotal(int $limit = 0, bool $onlyMappedUsers = false): int|false { |
|
| 489 | - $userCount = false; |
|
| 490 | - |
|
| 491 | - foreach ($this->backends as $backend) { |
|
| 492 | - if ($onlyMappedUsers && $backend instanceof ICountMappedUsersBackend) { |
|
| 493 | - $backendUsers = $backend->countMappedUsers(); |
|
| 494 | - } elseif ($backend instanceof ILimitAwareCountUsersBackend) { |
|
| 495 | - $backendUsers = $backend->countUsers($limit); |
|
| 496 | - } elseif ($backend instanceof ICountUsersBackend || $backend->implementsActions(Backend::COUNT_USERS)) { |
|
| 497 | - /** @var ICountUsersBackend $backend */ |
|
| 498 | - $backendUsers = $backend->countUsers(); |
|
| 499 | - } else { |
|
| 500 | - $this->logger->debug('Skip backend for user count: ' . get_class($backend)); |
|
| 501 | - continue; |
|
| 502 | - } |
|
| 503 | - if ($backendUsers !== false) { |
|
| 504 | - $userCount = (int)$userCount + $backendUsers; |
|
| 505 | - if ($limit > 0) { |
|
| 506 | - if ($userCount >= $limit) { |
|
| 507 | - break; |
|
| 508 | - } |
|
| 509 | - $limit -= $userCount; |
|
| 510 | - } |
|
| 511 | - } else { |
|
| 512 | - $this->logger->warning('Can not determine user count for ' . get_class($backend)); |
|
| 513 | - } |
|
| 514 | - } |
|
| 515 | - return $userCount; |
|
| 516 | - } |
|
| 517 | - |
|
| 518 | - /** |
|
| 519 | - * returns how many users per backend exist in the requested groups (if supported by backend) |
|
| 520 | - * |
|
| 521 | - * @param IGroup[] $groups an array of groups to search in |
|
| 522 | - * @param int $limit limit to stop counting |
|
| 523 | - * @return array{int,int} total number of users, and number of disabled users in the given groups, below $limit. If limit is reached, -1 is returned for number of disabled users |
|
| 524 | - */ |
|
| 525 | - public function countUsersAndDisabledUsersOfGroups(array $groups, int $limit): array { |
|
| 526 | - $users = []; |
|
| 527 | - $disabled = []; |
|
| 528 | - foreach ($groups as $group) { |
|
| 529 | - foreach ($group->getUsers() as $user) { |
|
| 530 | - $users[$user->getUID()] = 1; |
|
| 531 | - if (!$user->isEnabled()) { |
|
| 532 | - $disabled[$user->getUID()] = 1; |
|
| 533 | - } |
|
| 534 | - if (count($users) >= $limit) { |
|
| 535 | - return [count($users),-1]; |
|
| 536 | - } |
|
| 537 | - } |
|
| 538 | - } |
|
| 539 | - return [count($users),count($disabled)]; |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - /** |
|
| 543 | - * The callback is executed for each user on each backend. |
|
| 544 | - * If the callback returns false no further users will be retrieved. |
|
| 545 | - * |
|
| 546 | - * @psalm-param \Closure(\OCP\IUser):?bool $callback |
|
| 547 | - * @param string $search |
|
| 548 | - * @param boolean $onlySeen when true only users that have a lastLogin entry |
|
| 549 | - * in the preferences table will be affected |
|
| 550 | - * @since 9.0.0 |
|
| 551 | - */ |
|
| 552 | - public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = false) { |
|
| 553 | - if ($onlySeen) { |
|
| 554 | - $this->callForSeenUsers($callback); |
|
| 555 | - } else { |
|
| 556 | - foreach ($this->getBackends() as $backend) { |
|
| 557 | - $limit = 500; |
|
| 558 | - $offset = 0; |
|
| 559 | - do { |
|
| 560 | - $users = $backend->getUsers($search, $limit, $offset); |
|
| 561 | - foreach ($users as $uid) { |
|
| 562 | - if (!$backend->userExists($uid)) { |
|
| 563 | - continue; |
|
| 564 | - } |
|
| 565 | - $user = $this->getUserObject($uid, $backend, false); |
|
| 566 | - $return = $callback($user); |
|
| 567 | - if ($return === false) { |
|
| 568 | - break; |
|
| 569 | - } |
|
| 570 | - } |
|
| 571 | - $offset += $limit; |
|
| 572 | - } while (count($users) >= $limit); |
|
| 573 | - } |
|
| 574 | - } |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - /** |
|
| 578 | - * returns how many users are disabled |
|
| 579 | - * |
|
| 580 | - * @return int |
|
| 581 | - * @since 12.0.0 |
|
| 582 | - */ |
|
| 583 | - public function countDisabledUsers(): int { |
|
| 584 | - $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 585 | - $queryBuilder->select($queryBuilder->func()->count('*')) |
|
| 586 | - ->from('preferences') |
|
| 587 | - ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core'))) |
|
| 588 | - ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled'))) |
|
| 589 | - ->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR)); |
|
| 590 | - |
|
| 591 | - |
|
| 592 | - $result = $queryBuilder->executeQuery(); |
|
| 593 | - $count = $result->fetchOne(); |
|
| 594 | - $result->closeCursor(); |
|
| 595 | - |
|
| 596 | - if ($count !== false) { |
|
| 597 | - $count = (int)$count; |
|
| 598 | - } else { |
|
| 599 | - $count = 0; |
|
| 600 | - } |
|
| 601 | - |
|
| 602 | - return $count; |
|
| 603 | - } |
|
| 604 | - |
|
| 605 | - /** |
|
| 606 | - * returns how many users have logged in once |
|
| 607 | - * |
|
| 608 | - * @return int |
|
| 609 | - * @since 11.0.0 |
|
| 610 | - */ |
|
| 611 | - public function countSeenUsers() { |
|
| 612 | - $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 613 | - $queryBuilder->select($queryBuilder->func()->count('*')) |
|
| 614 | - ->from('preferences') |
|
| 615 | - ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('login'))) |
|
| 616 | - ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('lastLogin'))); |
|
| 617 | - |
|
| 618 | - $query = $queryBuilder->executeQuery(); |
|
| 619 | - |
|
| 620 | - $result = (int)$query->fetchOne(); |
|
| 621 | - $query->closeCursor(); |
|
| 622 | - |
|
| 623 | - return $result; |
|
| 624 | - } |
|
| 625 | - |
|
| 626 | - public function callForSeenUsers(\Closure $callback) { |
|
| 627 | - $users = $this->getSeenUsers(); |
|
| 628 | - foreach ($users as $user) { |
|
| 629 | - $return = $callback($user); |
|
| 630 | - if ($return === false) { |
|
| 631 | - return; |
|
| 632 | - } |
|
| 633 | - } |
|
| 634 | - } |
|
| 635 | - |
|
| 636 | - /** |
|
| 637 | - * Getting all userIds that have a lastLogin value requires checking the |
|
| 638 | - * value in php because on oracle you cannot use a clob in a where clause, |
|
| 639 | - * preventing us from doing a not null or length(value) > 0 check. |
|
| 640 | - * |
|
| 641 | - * @param int $limit |
|
| 642 | - * @param int $offset |
|
| 643 | - * @return string[] with user ids |
|
| 644 | - */ |
|
| 645 | - private function getSeenUserIds($limit = null, $offset = null) { |
|
| 646 | - $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 647 | - $queryBuilder->select(['userid']) |
|
| 648 | - ->from('preferences') |
|
| 649 | - ->where($queryBuilder->expr()->eq( |
|
| 650 | - 'appid', $queryBuilder->createNamedParameter('login')) |
|
| 651 | - ) |
|
| 652 | - ->andWhere($queryBuilder->expr()->eq( |
|
| 653 | - 'configkey', $queryBuilder->createNamedParameter('lastLogin')) |
|
| 654 | - ) |
|
| 655 | - ->andWhere($queryBuilder->expr()->isNotNull('configvalue') |
|
| 656 | - ); |
|
| 657 | - |
|
| 658 | - if ($limit !== null) { |
|
| 659 | - $queryBuilder->setMaxResults($limit); |
|
| 660 | - } |
|
| 661 | - if ($offset !== null) { |
|
| 662 | - $queryBuilder->setFirstResult($offset); |
|
| 663 | - } |
|
| 664 | - $query = $queryBuilder->executeQuery(); |
|
| 665 | - $result = []; |
|
| 666 | - |
|
| 667 | - while ($row = $query->fetch()) { |
|
| 668 | - $result[] = $row['userid']; |
|
| 669 | - } |
|
| 670 | - |
|
| 671 | - $query->closeCursor(); |
|
| 672 | - |
|
| 673 | - return $result; |
|
| 674 | - } |
|
| 675 | - |
|
| 676 | - /** |
|
| 677 | - * @param string $email |
|
| 678 | - * @return IUser[] |
|
| 679 | - * @since 9.1.0 |
|
| 680 | - */ |
|
| 681 | - public function getByEmail($email) { |
|
| 682 | - // looking for 'email' only (and not primary_mail) is intentional |
|
| 683 | - $userIds = $this->config->getUsersForUserValueCaseInsensitive('settings', 'email', $email); |
|
| 684 | - |
|
| 685 | - $users = array_map(function ($uid) { |
|
| 686 | - return $this->get($uid); |
|
| 687 | - }, $userIds); |
|
| 688 | - |
|
| 689 | - return array_values(array_filter($users, function ($u) { |
|
| 690 | - return ($u instanceof IUser); |
|
| 691 | - })); |
|
| 692 | - } |
|
| 693 | - |
|
| 694 | - /** |
|
| 695 | - * @param string $uid |
|
| 696 | - * @param bool $checkDataDirectory |
|
| 697 | - * @throws \InvalidArgumentException Message is an already translated string with a reason why the id is not valid |
|
| 698 | - * @since 26.0.0 |
|
| 699 | - */ |
|
| 700 | - public function validateUserId(string $uid, bool $checkDataDirectory = false): void { |
|
| 701 | - $l = Server::get(IFactory::class)->get('lib'); |
|
| 702 | - |
|
| 703 | - // Check the ID for bad characters |
|
| 704 | - // Allowed are: "a-z", "A-Z", "0-9", spaces and "_.@-'" |
|
| 705 | - if (preg_match('/[^a-zA-Z0-9 _.@\-\']/', $uid)) { |
|
| 706 | - throw new \InvalidArgumentException($l->t('Only the following characters are allowed in an Login:' |
|
| 707 | - . ' "a-z", "A-Z", "0-9", spaces and "_.@-\'"')); |
|
| 708 | - } |
|
| 709 | - |
|
| 710 | - // No empty user ID |
|
| 711 | - if (trim($uid) === '') { |
|
| 712 | - throw new \InvalidArgumentException($l->t('A valid Login must be provided')); |
|
| 713 | - } |
|
| 714 | - |
|
| 715 | - // No whitespace at the beginning or at the end |
|
| 716 | - if (trim($uid) !== $uid) { |
|
| 717 | - throw new \InvalidArgumentException($l->t('Login contains whitespace at the beginning or at the end')); |
|
| 718 | - } |
|
| 719 | - |
|
| 720 | - // User ID only consists of 1 or 2 dots (directory traversal) |
|
| 721 | - if ($uid === '.' || $uid === '..') { |
|
| 722 | - throw new \InvalidArgumentException($l->t('Login must not consist of dots only')); |
|
| 723 | - } |
|
| 724 | - |
|
| 725 | - // User ID is too long |
|
| 726 | - if (strlen($uid) > IUser::MAX_USERID_LENGTH) { |
|
| 727 | - // TRANSLATORS User ID is too long |
|
| 728 | - throw new \InvalidArgumentException($l->t('Username is too long')); |
|
| 729 | - } |
|
| 730 | - |
|
| 731 | - if (!$this->verifyUid($uid, $checkDataDirectory)) { |
|
| 732 | - throw new \InvalidArgumentException($l->t('Login is invalid because files already exist for this user')); |
|
| 733 | - } |
|
| 734 | - } |
|
| 735 | - |
|
| 736 | - /** |
|
| 737 | - * Gets the list of user ids sorted by lastLogin, from most recent to least recent |
|
| 738 | - * |
|
| 739 | - * @param int|null $limit how many users to fetch (default: 25, max: 100) |
|
| 740 | - * @param int $offset from which offset to fetch |
|
| 741 | - * @param string $search search users based on search params |
|
| 742 | - * @return list<string> list of user IDs |
|
| 743 | - */ |
|
| 744 | - public function getLastLoggedInUsers(?int $limit = null, int $offset = 0, string $search = ''): array { |
|
| 745 | - // We can't load all users who already logged in |
|
| 746 | - $limit = min(100, $limit ?: 25); |
|
| 747 | - |
|
| 748 | - $connection = \OC::$server->getDatabaseConnection(); |
|
| 749 | - $queryBuilder = $connection->getQueryBuilder(); |
|
| 750 | - $queryBuilder->select('pref_login.userid') |
|
| 751 | - ->from('preferences', 'pref_login') |
|
| 752 | - ->where($queryBuilder->expr()->eq('pref_login.appid', $queryBuilder->expr()->literal('login'))) |
|
| 753 | - ->andWhere($queryBuilder->expr()->eq('pref_login.configkey', $queryBuilder->expr()->literal('lastLogin'))) |
|
| 754 | - ->setFirstResult($offset) |
|
| 755 | - ->setMaxResults($limit) |
|
| 756 | - ; |
|
| 757 | - |
|
| 758 | - // Oracle don't want to run ORDER BY on CLOB column |
|
| 759 | - $loginOrder = $connection->getDatabasePlatform() instanceof OraclePlatform |
|
| 760 | - ? $queryBuilder->expr()->castColumn('pref_login.configvalue', IQueryBuilder::PARAM_INT) |
|
| 761 | - : 'pref_login.configvalue'; |
|
| 762 | - $queryBuilder |
|
| 763 | - ->orderBy($loginOrder, 'DESC') |
|
| 764 | - ->addOrderBy($queryBuilder->func()->lower('pref_login.userid'), 'ASC'); |
|
| 765 | - |
|
| 766 | - if ($search !== '') { |
|
| 767 | - $displayNameMatches = $this->searchDisplayName($search); |
|
| 768 | - $matchedUids = array_map(static fn (IUser $u): string => $u->getUID(), $displayNameMatches); |
|
| 769 | - |
|
| 770 | - $queryBuilder |
|
| 771 | - ->leftJoin('pref_login', 'preferences', 'pref_email', $queryBuilder->expr()->andX( |
|
| 772 | - $queryBuilder->expr()->eq('pref_login.userid', 'pref_email.userid'), |
|
| 773 | - $queryBuilder->expr()->eq('pref_email.appid', $queryBuilder->expr()->literal('settings')), |
|
| 774 | - $queryBuilder->expr()->eq('pref_email.configkey', $queryBuilder->expr()->literal('email')), |
|
| 775 | - )) |
|
| 776 | - ->andWhere($queryBuilder->expr()->orX( |
|
| 777 | - $queryBuilder->expr()->in('pref_login.userid', $queryBuilder->createNamedParameter($matchedUids, IQueryBuilder::PARAM_STR_ARRAY)), |
|
| 778 | - )); |
|
| 779 | - } |
|
| 780 | - |
|
| 781 | - /** @var list<string> */ |
|
| 782 | - $list = $queryBuilder->executeQuery()->fetchAll(\PDO::FETCH_COLUMN); |
|
| 783 | - |
|
| 784 | - return $list; |
|
| 785 | - } |
|
| 786 | - |
|
| 787 | - private function verifyUid(string $uid, bool $checkDataDirectory = false): bool { |
|
| 788 | - $appdata = 'appdata_' . $this->config->getSystemValueString('instanceid'); |
|
| 789 | - |
|
| 790 | - if (\in_array($uid, [ |
|
| 791 | - '.htaccess', |
|
| 792 | - 'files_external', |
|
| 793 | - '__groupfolders', |
|
| 794 | - '.ncdata', |
|
| 795 | - 'owncloud.log', |
|
| 796 | - 'nextcloud.log', |
|
| 797 | - 'updater.log', |
|
| 798 | - 'audit.log', |
|
| 799 | - $appdata], true)) { |
|
| 800 | - return false; |
|
| 801 | - } |
|
| 802 | - |
|
| 803 | - if (!$checkDataDirectory) { |
|
| 804 | - return true; |
|
| 805 | - } |
|
| 806 | - |
|
| 807 | - $dataDirectory = $this->config->getSystemValueString('datadirectory', \OC::$SERVERROOT . '/data'); |
|
| 808 | - |
|
| 809 | - return !file_exists(rtrim($dataDirectory, '/') . '/' . $uid); |
|
| 810 | - } |
|
| 811 | - |
|
| 812 | - public function getDisplayNameCache(): DisplayNameCache { |
|
| 813 | - return $this->displayNameCache; |
|
| 814 | - } |
|
| 815 | - |
|
| 816 | - public function getSeenUsers(int $offset = 0, ?int $limit = null): \Iterator { |
|
| 817 | - $maxBatchSize = 1000; |
|
| 818 | - |
|
| 819 | - do { |
|
| 820 | - if ($limit !== null) { |
|
| 821 | - $batchSize = min($limit, $maxBatchSize); |
|
| 822 | - $limit -= $batchSize; |
|
| 823 | - } else { |
|
| 824 | - $batchSize = $maxBatchSize; |
|
| 825 | - } |
|
| 826 | - |
|
| 827 | - $userIds = $this->getSeenUserIds($batchSize, $offset); |
|
| 828 | - $offset += $batchSize; |
|
| 829 | - |
|
| 830 | - foreach ($userIds as $userId) { |
|
| 831 | - foreach ($this->backends as $backend) { |
|
| 832 | - if ($backend->userExists($userId)) { |
|
| 833 | - $user = new LazyUser($userId, $this, null, $backend); |
|
| 834 | - yield $user; |
|
| 835 | - break; |
|
| 836 | - } |
|
| 837 | - } |
|
| 838 | - } |
|
| 839 | - } while (count($userIds) === $batchSize && $limit !== 0); |
|
| 840 | - } |
|
| 56 | + /** |
|
| 57 | + * @var UserInterface[] $backends |
|
| 58 | + */ |
|
| 59 | + private array $backends = []; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @var array<string,\OC\User\User> $cachedUsers |
|
| 63 | + */ |
|
| 64 | + private array $cachedUsers = []; |
|
| 65 | + |
|
| 66 | + private ICache $cache; |
|
| 67 | + |
|
| 68 | + private DisplayNameCache $displayNameCache; |
|
| 69 | + |
|
| 70 | + public function __construct( |
|
| 71 | + private IConfig $config, |
|
| 72 | + ICacheFactory $cacheFactory, |
|
| 73 | + private IEventDispatcher $eventDispatcher, |
|
| 74 | + private LoggerInterface $logger, |
|
| 75 | + ) { |
|
| 76 | + $this->cache = new WithLocalCache($cacheFactory->createDistributed('user_backend_map')); |
|
| 77 | + $this->listen('\OC\User', 'postDelete', function (IUser $user): void { |
|
| 78 | + unset($this->cachedUsers[$user->getUID()]); |
|
| 79 | + }); |
|
| 80 | + $this->displayNameCache = new DisplayNameCache($cacheFactory, $this); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Get the active backends |
|
| 85 | + * @return UserInterface[] |
|
| 86 | + */ |
|
| 87 | + public function getBackends(): array { |
|
| 88 | + return $this->backends; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + public function registerBackend(UserInterface $backend): void { |
|
| 92 | + $this->backends[] = $backend; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + public function removeBackend(UserInterface $backend): void { |
|
| 96 | + $this->cachedUsers = []; |
|
| 97 | + if (($i = array_search($backend, $this->backends)) !== false) { |
|
| 98 | + unset($this->backends[$i]); |
|
| 99 | + } |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + public function clearBackends(): void { |
|
| 103 | + $this->cachedUsers = []; |
|
| 104 | + $this->backends = []; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * get a user by user id |
|
| 109 | + * |
|
| 110 | + * @param string $uid |
|
| 111 | + * @return \OC\User\User|null Either the user or null if the specified user does not exist |
|
| 112 | + */ |
|
| 113 | + public function get($uid) { |
|
| 114 | + if (is_null($uid) || $uid === '' || $uid === false) { |
|
| 115 | + return null; |
|
| 116 | + } |
|
| 117 | + if (isset($this->cachedUsers[$uid])) { //check the cache first to prevent having to loop over the backends |
|
| 118 | + return $this->cachedUsers[$uid]; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + if (strlen($uid) > IUser::MAX_USERID_LENGTH) { |
|
| 122 | + return null; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + $cachedBackend = $this->cache->get(sha1($uid)); |
|
| 126 | + if ($cachedBackend !== null && isset($this->backends[$cachedBackend])) { |
|
| 127 | + // Cache has the info of the user backend already, so ask that one directly |
|
| 128 | + $backend = $this->backends[$cachedBackend]; |
|
| 129 | + if ($backend->userExists($uid)) { |
|
| 130 | + return $this->getUserObject($uid, $backend); |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + foreach ($this->backends as $i => $backend) { |
|
| 135 | + if ($i === $cachedBackend) { |
|
| 136 | + // Tried that one already |
|
| 137 | + continue; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + if ($backend->userExists($uid)) { |
|
| 141 | + // Hash $uid to ensure that only valid characters are used for the cache key |
|
| 142 | + $this->cache->set(sha1($uid), $i, 300); |
|
| 143 | + return $this->getUserObject($uid, $backend); |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | + return null; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + public function getDisplayName(string $uid): ?string { |
|
| 150 | + return $this->displayNameCache->getDisplayName($uid); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * get or construct the user object |
|
| 155 | + * |
|
| 156 | + * @param string $uid |
|
| 157 | + * @param \OCP\UserInterface $backend |
|
| 158 | + * @param bool $cacheUser If false the newly created user object will not be cached |
|
| 159 | + * @return \OC\User\User |
|
| 160 | + */ |
|
| 161 | + public function getUserObject($uid, $backend, $cacheUser = true) { |
|
| 162 | + if ($backend instanceof IGetRealUIDBackend) { |
|
| 163 | + $uid = $backend->getRealUID($uid); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + if (isset($this->cachedUsers[$uid])) { |
|
| 167 | + return $this->cachedUsers[$uid]; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + $user = new User($uid, $backend, $this->eventDispatcher, $this, $this->config); |
|
| 171 | + if ($cacheUser) { |
|
| 172 | + $this->cachedUsers[$uid] = $user; |
|
| 173 | + } |
|
| 174 | + return $user; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * check if a user exists |
|
| 179 | + * |
|
| 180 | + * @param string $uid |
|
| 181 | + * @return bool |
|
| 182 | + */ |
|
| 183 | + public function userExists($uid) { |
|
| 184 | + if (strlen($uid) > IUser::MAX_USERID_LENGTH) { |
|
| 185 | + return false; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + $user = $this->get($uid); |
|
| 189 | + return ($user !== null); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Check if the password is valid for the user |
|
| 194 | + * |
|
| 195 | + * @param string $loginName |
|
| 196 | + * @param string $password |
|
| 197 | + * @return IUser|false the User object on success, false otherwise |
|
| 198 | + */ |
|
| 199 | + public function checkPassword($loginName, $password) { |
|
| 200 | + $result = $this->checkPasswordNoLogging($loginName, $password); |
|
| 201 | + |
|
| 202 | + if ($result === false) { |
|
| 203 | + $this->logger->warning('Login failed: \'' . $loginName . '\' (Remote IP: \'' . \OC::$server->getRequest()->getRemoteAddress() . '\')', ['app' => 'core']); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + return $result; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Check if the password is valid for the user |
|
| 211 | + * |
|
| 212 | + * @internal |
|
| 213 | + * @param string $loginName |
|
| 214 | + * @param string $password |
|
| 215 | + * @return IUser|false the User object on success, false otherwise |
|
| 216 | + */ |
|
| 217 | + public function checkPasswordNoLogging($loginName, $password) { |
|
| 218 | + $loginName = str_replace("\0", '', $loginName); |
|
| 219 | + $password = str_replace("\0", '', $password); |
|
| 220 | + |
|
| 221 | + $cachedBackend = $this->cache->get($loginName); |
|
| 222 | + if ($cachedBackend !== null && isset($this->backends[$cachedBackend])) { |
|
| 223 | + $backends = [$this->backends[$cachedBackend]]; |
|
| 224 | + } else { |
|
| 225 | + $backends = $this->backends; |
|
| 226 | + } |
|
| 227 | + foreach ($backends as $backend) { |
|
| 228 | + if ($backend instanceof ICheckPasswordBackend || $backend->implementsActions(Backend::CHECK_PASSWORD)) { |
|
| 229 | + /** @var ICheckPasswordBackend $backend */ |
|
| 230 | + $uid = $backend->checkPassword($loginName, $password); |
|
| 231 | + if ($uid !== false) { |
|
| 232 | + return $this->getUserObject($uid, $backend); |
|
| 233 | + } |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + // since http basic auth doesn't provide a standard way of handling non ascii password we allow password to be urlencoded |
|
| 238 | + // we only do this decoding after using the plain password fails to maintain compatibility with any password that happens |
|
| 239 | + // to contain urlencoded patterns by "accident". |
|
| 240 | + $password = urldecode($password); |
|
| 241 | + |
|
| 242 | + foreach ($backends as $backend) { |
|
| 243 | + if ($backend instanceof ICheckPasswordBackend || $backend->implementsActions(Backend::CHECK_PASSWORD)) { |
|
| 244 | + /** @var ICheckPasswordBackend|UserInterface $backend */ |
|
| 245 | + $uid = $backend->checkPassword($loginName, $password); |
|
| 246 | + if ($uid !== false) { |
|
| 247 | + return $this->getUserObject($uid, $backend); |
|
| 248 | + } |
|
| 249 | + } |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + return false; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * Search by user id |
|
| 257 | + * |
|
| 258 | + * @param string $pattern |
|
| 259 | + * @param int $limit |
|
| 260 | + * @param int $offset |
|
| 261 | + * @return IUser[] |
|
| 262 | + * @deprecated 27.0.0, use searchDisplayName instead |
|
| 263 | + */ |
|
| 264 | + public function search($pattern, $limit = null, $offset = null) { |
|
| 265 | + $users = []; |
|
| 266 | + foreach ($this->backends as $backend) { |
|
| 267 | + $backendUsers = $backend->getUsers($pattern, $limit, $offset); |
|
| 268 | + if (is_array($backendUsers)) { |
|
| 269 | + foreach ($backendUsers as $uid) { |
|
| 270 | + $users[$uid] = new LazyUser($uid, $this, null, $backend); |
|
| 271 | + } |
|
| 272 | + } |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + uasort($users, function (IUser $a, IUser $b) { |
|
| 276 | + return strcasecmp($a->getUID(), $b->getUID()); |
|
| 277 | + }); |
|
| 278 | + return $users; |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * Search by displayName |
|
| 283 | + * |
|
| 284 | + * @param string $pattern |
|
| 285 | + * @param int $limit |
|
| 286 | + * @param int $offset |
|
| 287 | + * @return IUser[] |
|
| 288 | + */ |
|
| 289 | + public function searchDisplayName($pattern, $limit = null, $offset = null) { |
|
| 290 | + $users = []; |
|
| 291 | + foreach ($this->backends as $backend) { |
|
| 292 | + $backendUsers = $backend->getDisplayNames($pattern, $limit, $offset); |
|
| 293 | + if (is_array($backendUsers)) { |
|
| 294 | + foreach ($backendUsers as $uid => $displayName) { |
|
| 295 | + $users[] = new LazyUser($uid, $this, $displayName, $backend); |
|
| 296 | + } |
|
| 297 | + } |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + usort($users, function (IUser $a, IUser $b) { |
|
| 301 | + return strcasecmp($a->getDisplayName(), $b->getDisplayName()); |
|
| 302 | + }); |
|
| 303 | + return $users; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * @return IUser[] |
|
| 308 | + */ |
|
| 309 | + public function getDisabledUsers(?int $limit = null, int $offset = 0, string $search = ''): array { |
|
| 310 | + $users = $this->config->getUsersForUserValue('core', 'enabled', 'false'); |
|
| 311 | + $users = array_combine( |
|
| 312 | + $users, |
|
| 313 | + array_map( |
|
| 314 | + fn (string $uid): IUser => new LazyUser($uid, $this), |
|
| 315 | + $users |
|
| 316 | + ) |
|
| 317 | + ); |
|
| 318 | + if ($search !== '') { |
|
| 319 | + $users = array_filter( |
|
| 320 | + $users, |
|
| 321 | + function (IUser $user) use ($search): bool { |
|
| 322 | + try { |
|
| 323 | + return mb_stripos($user->getUID(), $search) !== false |
|
| 324 | + || mb_stripos($user->getDisplayName(), $search) !== false |
|
| 325 | + || mb_stripos($user->getEMailAddress() ?? '', $search) !== false; |
|
| 326 | + } catch (NoUserException $ex) { |
|
| 327 | + $this->logger->error('Error while filtering disabled users', ['exception' => $ex, 'userUID' => $user->getUID()]); |
|
| 328 | + return false; |
|
| 329 | + } |
|
| 330 | + }); |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + $tempLimit = ($limit === null ? null : $limit + $offset); |
|
| 334 | + foreach ($this->backends as $backend) { |
|
| 335 | + if (($tempLimit !== null) && (count($users) >= $tempLimit)) { |
|
| 336 | + break; |
|
| 337 | + } |
|
| 338 | + if ($backend instanceof IProvideEnabledStateBackend) { |
|
| 339 | + $backendUsers = $backend->getDisabledUserList(($tempLimit === null ? null : $tempLimit - count($users)), 0, $search); |
|
| 340 | + foreach ($backendUsers as $uid) { |
|
| 341 | + $users[$uid] = new LazyUser($uid, $this, null, $backend); |
|
| 342 | + } |
|
| 343 | + } |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + return array_slice($users, $offset, $limit); |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + /** |
|
| 350 | + * Search known users (from phonebook sync) by displayName |
|
| 351 | + * |
|
| 352 | + * @param string $searcher |
|
| 353 | + * @param string $pattern |
|
| 354 | + * @param int|null $limit |
|
| 355 | + * @param int|null $offset |
|
| 356 | + * @return IUser[] |
|
| 357 | + */ |
|
| 358 | + public function searchKnownUsersByDisplayName(string $searcher, string $pattern, ?int $limit = null, ?int $offset = null): array { |
|
| 359 | + $users = []; |
|
| 360 | + foreach ($this->backends as $backend) { |
|
| 361 | + if ($backend instanceof ISearchKnownUsersBackend) { |
|
| 362 | + $backendUsers = $backend->searchKnownUsersByDisplayName($searcher, $pattern, $limit, $offset); |
|
| 363 | + } else { |
|
| 364 | + // Better than nothing, but filtering after pagination can remove lots of results. |
|
| 365 | + $backendUsers = $backend->getDisplayNames($pattern, $limit, $offset); |
|
| 366 | + } |
|
| 367 | + if (is_array($backendUsers)) { |
|
| 368 | + foreach ($backendUsers as $uid => $displayName) { |
|
| 369 | + $users[] = $this->getUserObject($uid, $backend); |
|
| 370 | + } |
|
| 371 | + } |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + usort($users, function ($a, $b) { |
|
| 375 | + /** |
|
| 376 | + * @var IUser $a |
|
| 377 | + * @var IUser $b |
|
| 378 | + */ |
|
| 379 | + return strcasecmp($a->getDisplayName(), $b->getDisplayName()); |
|
| 380 | + }); |
|
| 381 | + return $users; |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + /** |
|
| 385 | + * @param string $uid |
|
| 386 | + * @param string $password |
|
| 387 | + * @return false|IUser the created user or false |
|
| 388 | + * @throws \InvalidArgumentException |
|
| 389 | + * @throws HintException |
|
| 390 | + */ |
|
| 391 | + public function createUser($uid, $password) { |
|
| 392 | + // DI injection is not used here as IRegistry needs the user manager itself for user count and thus it would create a cyclic dependency |
|
| 393 | + /** @var IAssertion $assertion */ |
|
| 394 | + $assertion = \OC::$server->get(IAssertion::class); |
|
| 395 | + $assertion->createUserIsLegit(); |
|
| 396 | + |
|
| 397 | + $localBackends = []; |
|
| 398 | + foreach ($this->backends as $backend) { |
|
| 399 | + if ($backend instanceof Database) { |
|
| 400 | + // First check if there is another user backend |
|
| 401 | + $localBackends[] = $backend; |
|
| 402 | + continue; |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + if ($backend->implementsActions(Backend::CREATE_USER)) { |
|
| 406 | + return $this->createUserFromBackend($uid, $password, $backend); |
|
| 407 | + } |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + foreach ($localBackends as $backend) { |
|
| 411 | + if ($backend->implementsActions(Backend::CREATE_USER)) { |
|
| 412 | + return $this->createUserFromBackend($uid, $password, $backend); |
|
| 413 | + } |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + return false; |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + /** |
|
| 420 | + * @param string $uid |
|
| 421 | + * @param string $password |
|
| 422 | + * @param UserInterface $backend |
|
| 423 | + * @return IUser|false |
|
| 424 | + * @throws \InvalidArgumentException |
|
| 425 | + */ |
|
| 426 | + public function createUserFromBackend($uid, $password, UserInterface $backend) { |
|
| 427 | + $l = \OCP\Util::getL10N('lib'); |
|
| 428 | + |
|
| 429 | + $this->validateUserId($uid, true); |
|
| 430 | + |
|
| 431 | + // No empty password |
|
| 432 | + if (trim($password) === '') { |
|
| 433 | + throw new \InvalidArgumentException($l->t('A valid password must be provided')); |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + // Check if user already exists |
|
| 437 | + if ($this->userExists($uid)) { |
|
| 438 | + throw new \InvalidArgumentException($l->t('The Login is already being used')); |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + /** @deprecated 21.0.0 use BeforeUserCreatedEvent event with the IEventDispatcher instead */ |
|
| 442 | + $this->emit('\OC\User', 'preCreateUser', [$uid, $password]); |
|
| 443 | + $this->eventDispatcher->dispatchTyped(new BeforeUserCreatedEvent($uid, $password)); |
|
| 444 | + $state = $backend->createUser($uid, $password); |
|
| 445 | + if ($state === false) { |
|
| 446 | + throw new \InvalidArgumentException($l->t('Could not create account')); |
|
| 447 | + } |
|
| 448 | + $user = $this->getUserObject($uid, $backend); |
|
| 449 | + if ($user instanceof IUser) { |
|
| 450 | + /** @deprecated 21.0.0 use UserCreatedEvent event with the IEventDispatcher instead */ |
|
| 451 | + $this->emit('\OC\User', 'postCreateUser', [$user, $password]); |
|
| 452 | + $this->eventDispatcher->dispatchTyped(new UserCreatedEvent($user, $password)); |
|
| 453 | + return $user; |
|
| 454 | + } |
|
| 455 | + return false; |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + /** |
|
| 459 | + * returns how many users per backend exist (if supported by backend) |
|
| 460 | + * |
|
| 461 | + * @param boolean $hasLoggedIn when true only users that have a lastLogin |
|
| 462 | + * entry in the preferences table will be affected |
|
| 463 | + * @return array<string, int> an array of backend class as key and count number as value |
|
| 464 | + */ |
|
| 465 | + public function countUsers() { |
|
| 466 | + $userCountStatistics = []; |
|
| 467 | + foreach ($this->backends as $backend) { |
|
| 468 | + if ($backend instanceof ICountUsersBackend || $backend->implementsActions(Backend::COUNT_USERS)) { |
|
| 469 | + /** @var ICountUsersBackend|IUserBackend $backend */ |
|
| 470 | + $backendUsers = $backend->countUsers(); |
|
| 471 | + if ($backendUsers !== false) { |
|
| 472 | + if ($backend instanceof IUserBackend) { |
|
| 473 | + $name = $backend->getBackendName(); |
|
| 474 | + } else { |
|
| 475 | + $name = get_class($backend); |
|
| 476 | + } |
|
| 477 | + if (isset($userCountStatistics[$name])) { |
|
| 478 | + $userCountStatistics[$name] += $backendUsers; |
|
| 479 | + } else { |
|
| 480 | + $userCountStatistics[$name] = $backendUsers; |
|
| 481 | + } |
|
| 482 | + } |
|
| 483 | + } |
|
| 484 | + } |
|
| 485 | + return $userCountStatistics; |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + public function countUsersTotal(int $limit = 0, bool $onlyMappedUsers = false): int|false { |
|
| 489 | + $userCount = false; |
|
| 490 | + |
|
| 491 | + foreach ($this->backends as $backend) { |
|
| 492 | + if ($onlyMappedUsers && $backend instanceof ICountMappedUsersBackend) { |
|
| 493 | + $backendUsers = $backend->countMappedUsers(); |
|
| 494 | + } elseif ($backend instanceof ILimitAwareCountUsersBackend) { |
|
| 495 | + $backendUsers = $backend->countUsers($limit); |
|
| 496 | + } elseif ($backend instanceof ICountUsersBackend || $backend->implementsActions(Backend::COUNT_USERS)) { |
|
| 497 | + /** @var ICountUsersBackend $backend */ |
|
| 498 | + $backendUsers = $backend->countUsers(); |
|
| 499 | + } else { |
|
| 500 | + $this->logger->debug('Skip backend for user count: ' . get_class($backend)); |
|
| 501 | + continue; |
|
| 502 | + } |
|
| 503 | + if ($backendUsers !== false) { |
|
| 504 | + $userCount = (int)$userCount + $backendUsers; |
|
| 505 | + if ($limit > 0) { |
|
| 506 | + if ($userCount >= $limit) { |
|
| 507 | + break; |
|
| 508 | + } |
|
| 509 | + $limit -= $userCount; |
|
| 510 | + } |
|
| 511 | + } else { |
|
| 512 | + $this->logger->warning('Can not determine user count for ' . get_class($backend)); |
|
| 513 | + } |
|
| 514 | + } |
|
| 515 | + return $userCount; |
|
| 516 | + } |
|
| 517 | + |
|
| 518 | + /** |
|
| 519 | + * returns how many users per backend exist in the requested groups (if supported by backend) |
|
| 520 | + * |
|
| 521 | + * @param IGroup[] $groups an array of groups to search in |
|
| 522 | + * @param int $limit limit to stop counting |
|
| 523 | + * @return array{int,int} total number of users, and number of disabled users in the given groups, below $limit. If limit is reached, -1 is returned for number of disabled users |
|
| 524 | + */ |
|
| 525 | + public function countUsersAndDisabledUsersOfGroups(array $groups, int $limit): array { |
|
| 526 | + $users = []; |
|
| 527 | + $disabled = []; |
|
| 528 | + foreach ($groups as $group) { |
|
| 529 | + foreach ($group->getUsers() as $user) { |
|
| 530 | + $users[$user->getUID()] = 1; |
|
| 531 | + if (!$user->isEnabled()) { |
|
| 532 | + $disabled[$user->getUID()] = 1; |
|
| 533 | + } |
|
| 534 | + if (count($users) >= $limit) { |
|
| 535 | + return [count($users),-1]; |
|
| 536 | + } |
|
| 537 | + } |
|
| 538 | + } |
|
| 539 | + return [count($users),count($disabled)]; |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + /** |
|
| 543 | + * The callback is executed for each user on each backend. |
|
| 544 | + * If the callback returns false no further users will be retrieved. |
|
| 545 | + * |
|
| 546 | + * @psalm-param \Closure(\OCP\IUser):?bool $callback |
|
| 547 | + * @param string $search |
|
| 548 | + * @param boolean $onlySeen when true only users that have a lastLogin entry |
|
| 549 | + * in the preferences table will be affected |
|
| 550 | + * @since 9.0.0 |
|
| 551 | + */ |
|
| 552 | + public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = false) { |
|
| 553 | + if ($onlySeen) { |
|
| 554 | + $this->callForSeenUsers($callback); |
|
| 555 | + } else { |
|
| 556 | + foreach ($this->getBackends() as $backend) { |
|
| 557 | + $limit = 500; |
|
| 558 | + $offset = 0; |
|
| 559 | + do { |
|
| 560 | + $users = $backend->getUsers($search, $limit, $offset); |
|
| 561 | + foreach ($users as $uid) { |
|
| 562 | + if (!$backend->userExists($uid)) { |
|
| 563 | + continue; |
|
| 564 | + } |
|
| 565 | + $user = $this->getUserObject($uid, $backend, false); |
|
| 566 | + $return = $callback($user); |
|
| 567 | + if ($return === false) { |
|
| 568 | + break; |
|
| 569 | + } |
|
| 570 | + } |
|
| 571 | + $offset += $limit; |
|
| 572 | + } while (count($users) >= $limit); |
|
| 573 | + } |
|
| 574 | + } |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + /** |
|
| 578 | + * returns how many users are disabled |
|
| 579 | + * |
|
| 580 | + * @return int |
|
| 581 | + * @since 12.0.0 |
|
| 582 | + */ |
|
| 583 | + public function countDisabledUsers(): int { |
|
| 584 | + $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 585 | + $queryBuilder->select($queryBuilder->func()->count('*')) |
|
| 586 | + ->from('preferences') |
|
| 587 | + ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core'))) |
|
| 588 | + ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled'))) |
|
| 589 | + ->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR)); |
|
| 590 | + |
|
| 591 | + |
|
| 592 | + $result = $queryBuilder->executeQuery(); |
|
| 593 | + $count = $result->fetchOne(); |
|
| 594 | + $result->closeCursor(); |
|
| 595 | + |
|
| 596 | + if ($count !== false) { |
|
| 597 | + $count = (int)$count; |
|
| 598 | + } else { |
|
| 599 | + $count = 0; |
|
| 600 | + } |
|
| 601 | + |
|
| 602 | + return $count; |
|
| 603 | + } |
|
| 604 | + |
|
| 605 | + /** |
|
| 606 | + * returns how many users have logged in once |
|
| 607 | + * |
|
| 608 | + * @return int |
|
| 609 | + * @since 11.0.0 |
|
| 610 | + */ |
|
| 611 | + public function countSeenUsers() { |
|
| 612 | + $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 613 | + $queryBuilder->select($queryBuilder->func()->count('*')) |
|
| 614 | + ->from('preferences') |
|
| 615 | + ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('login'))) |
|
| 616 | + ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('lastLogin'))); |
|
| 617 | + |
|
| 618 | + $query = $queryBuilder->executeQuery(); |
|
| 619 | + |
|
| 620 | + $result = (int)$query->fetchOne(); |
|
| 621 | + $query->closeCursor(); |
|
| 622 | + |
|
| 623 | + return $result; |
|
| 624 | + } |
|
| 625 | + |
|
| 626 | + public function callForSeenUsers(\Closure $callback) { |
|
| 627 | + $users = $this->getSeenUsers(); |
|
| 628 | + foreach ($users as $user) { |
|
| 629 | + $return = $callback($user); |
|
| 630 | + if ($return === false) { |
|
| 631 | + return; |
|
| 632 | + } |
|
| 633 | + } |
|
| 634 | + } |
|
| 635 | + |
|
| 636 | + /** |
|
| 637 | + * Getting all userIds that have a lastLogin value requires checking the |
|
| 638 | + * value in php because on oracle you cannot use a clob in a where clause, |
|
| 639 | + * preventing us from doing a not null or length(value) > 0 check. |
|
| 640 | + * |
|
| 641 | + * @param int $limit |
|
| 642 | + * @param int $offset |
|
| 643 | + * @return string[] with user ids |
|
| 644 | + */ |
|
| 645 | + private function getSeenUserIds($limit = null, $offset = null) { |
|
| 646 | + $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 647 | + $queryBuilder->select(['userid']) |
|
| 648 | + ->from('preferences') |
|
| 649 | + ->where($queryBuilder->expr()->eq( |
|
| 650 | + 'appid', $queryBuilder->createNamedParameter('login')) |
|
| 651 | + ) |
|
| 652 | + ->andWhere($queryBuilder->expr()->eq( |
|
| 653 | + 'configkey', $queryBuilder->createNamedParameter('lastLogin')) |
|
| 654 | + ) |
|
| 655 | + ->andWhere($queryBuilder->expr()->isNotNull('configvalue') |
|
| 656 | + ); |
|
| 657 | + |
|
| 658 | + if ($limit !== null) { |
|
| 659 | + $queryBuilder->setMaxResults($limit); |
|
| 660 | + } |
|
| 661 | + if ($offset !== null) { |
|
| 662 | + $queryBuilder->setFirstResult($offset); |
|
| 663 | + } |
|
| 664 | + $query = $queryBuilder->executeQuery(); |
|
| 665 | + $result = []; |
|
| 666 | + |
|
| 667 | + while ($row = $query->fetch()) { |
|
| 668 | + $result[] = $row['userid']; |
|
| 669 | + } |
|
| 670 | + |
|
| 671 | + $query->closeCursor(); |
|
| 672 | + |
|
| 673 | + return $result; |
|
| 674 | + } |
|
| 675 | + |
|
| 676 | + /** |
|
| 677 | + * @param string $email |
|
| 678 | + * @return IUser[] |
|
| 679 | + * @since 9.1.0 |
|
| 680 | + */ |
|
| 681 | + public function getByEmail($email) { |
|
| 682 | + // looking for 'email' only (and not primary_mail) is intentional |
|
| 683 | + $userIds = $this->config->getUsersForUserValueCaseInsensitive('settings', 'email', $email); |
|
| 684 | + |
|
| 685 | + $users = array_map(function ($uid) { |
|
| 686 | + return $this->get($uid); |
|
| 687 | + }, $userIds); |
|
| 688 | + |
|
| 689 | + return array_values(array_filter($users, function ($u) { |
|
| 690 | + return ($u instanceof IUser); |
|
| 691 | + })); |
|
| 692 | + } |
|
| 693 | + |
|
| 694 | + /** |
|
| 695 | + * @param string $uid |
|
| 696 | + * @param bool $checkDataDirectory |
|
| 697 | + * @throws \InvalidArgumentException Message is an already translated string with a reason why the id is not valid |
|
| 698 | + * @since 26.0.0 |
|
| 699 | + */ |
|
| 700 | + public function validateUserId(string $uid, bool $checkDataDirectory = false): void { |
|
| 701 | + $l = Server::get(IFactory::class)->get('lib'); |
|
| 702 | + |
|
| 703 | + // Check the ID for bad characters |
|
| 704 | + // Allowed are: "a-z", "A-Z", "0-9", spaces and "_.@-'" |
|
| 705 | + if (preg_match('/[^a-zA-Z0-9 _.@\-\']/', $uid)) { |
|
| 706 | + throw new \InvalidArgumentException($l->t('Only the following characters are allowed in an Login:' |
|
| 707 | + . ' "a-z", "A-Z", "0-9", spaces and "_.@-\'"')); |
|
| 708 | + } |
|
| 709 | + |
|
| 710 | + // No empty user ID |
|
| 711 | + if (trim($uid) === '') { |
|
| 712 | + throw new \InvalidArgumentException($l->t('A valid Login must be provided')); |
|
| 713 | + } |
|
| 714 | + |
|
| 715 | + // No whitespace at the beginning or at the end |
|
| 716 | + if (trim($uid) !== $uid) { |
|
| 717 | + throw new \InvalidArgumentException($l->t('Login contains whitespace at the beginning or at the end')); |
|
| 718 | + } |
|
| 719 | + |
|
| 720 | + // User ID only consists of 1 or 2 dots (directory traversal) |
|
| 721 | + if ($uid === '.' || $uid === '..') { |
|
| 722 | + throw new \InvalidArgumentException($l->t('Login must not consist of dots only')); |
|
| 723 | + } |
|
| 724 | + |
|
| 725 | + // User ID is too long |
|
| 726 | + if (strlen($uid) > IUser::MAX_USERID_LENGTH) { |
|
| 727 | + // TRANSLATORS User ID is too long |
|
| 728 | + throw new \InvalidArgumentException($l->t('Username is too long')); |
|
| 729 | + } |
|
| 730 | + |
|
| 731 | + if (!$this->verifyUid($uid, $checkDataDirectory)) { |
|
| 732 | + throw new \InvalidArgumentException($l->t('Login is invalid because files already exist for this user')); |
|
| 733 | + } |
|
| 734 | + } |
|
| 735 | + |
|
| 736 | + /** |
|
| 737 | + * Gets the list of user ids sorted by lastLogin, from most recent to least recent |
|
| 738 | + * |
|
| 739 | + * @param int|null $limit how many users to fetch (default: 25, max: 100) |
|
| 740 | + * @param int $offset from which offset to fetch |
|
| 741 | + * @param string $search search users based on search params |
|
| 742 | + * @return list<string> list of user IDs |
|
| 743 | + */ |
|
| 744 | + public function getLastLoggedInUsers(?int $limit = null, int $offset = 0, string $search = ''): array { |
|
| 745 | + // We can't load all users who already logged in |
|
| 746 | + $limit = min(100, $limit ?: 25); |
|
| 747 | + |
|
| 748 | + $connection = \OC::$server->getDatabaseConnection(); |
|
| 749 | + $queryBuilder = $connection->getQueryBuilder(); |
|
| 750 | + $queryBuilder->select('pref_login.userid') |
|
| 751 | + ->from('preferences', 'pref_login') |
|
| 752 | + ->where($queryBuilder->expr()->eq('pref_login.appid', $queryBuilder->expr()->literal('login'))) |
|
| 753 | + ->andWhere($queryBuilder->expr()->eq('pref_login.configkey', $queryBuilder->expr()->literal('lastLogin'))) |
|
| 754 | + ->setFirstResult($offset) |
|
| 755 | + ->setMaxResults($limit) |
|
| 756 | + ; |
|
| 757 | + |
|
| 758 | + // Oracle don't want to run ORDER BY on CLOB column |
|
| 759 | + $loginOrder = $connection->getDatabasePlatform() instanceof OraclePlatform |
|
| 760 | + ? $queryBuilder->expr()->castColumn('pref_login.configvalue', IQueryBuilder::PARAM_INT) |
|
| 761 | + : 'pref_login.configvalue'; |
|
| 762 | + $queryBuilder |
|
| 763 | + ->orderBy($loginOrder, 'DESC') |
|
| 764 | + ->addOrderBy($queryBuilder->func()->lower('pref_login.userid'), 'ASC'); |
|
| 765 | + |
|
| 766 | + if ($search !== '') { |
|
| 767 | + $displayNameMatches = $this->searchDisplayName($search); |
|
| 768 | + $matchedUids = array_map(static fn (IUser $u): string => $u->getUID(), $displayNameMatches); |
|
| 769 | + |
|
| 770 | + $queryBuilder |
|
| 771 | + ->leftJoin('pref_login', 'preferences', 'pref_email', $queryBuilder->expr()->andX( |
|
| 772 | + $queryBuilder->expr()->eq('pref_login.userid', 'pref_email.userid'), |
|
| 773 | + $queryBuilder->expr()->eq('pref_email.appid', $queryBuilder->expr()->literal('settings')), |
|
| 774 | + $queryBuilder->expr()->eq('pref_email.configkey', $queryBuilder->expr()->literal('email')), |
|
| 775 | + )) |
|
| 776 | + ->andWhere($queryBuilder->expr()->orX( |
|
| 777 | + $queryBuilder->expr()->in('pref_login.userid', $queryBuilder->createNamedParameter($matchedUids, IQueryBuilder::PARAM_STR_ARRAY)), |
|
| 778 | + )); |
|
| 779 | + } |
|
| 780 | + |
|
| 781 | + /** @var list<string> */ |
|
| 782 | + $list = $queryBuilder->executeQuery()->fetchAll(\PDO::FETCH_COLUMN); |
|
| 783 | + |
|
| 784 | + return $list; |
|
| 785 | + } |
|
| 786 | + |
|
| 787 | + private function verifyUid(string $uid, bool $checkDataDirectory = false): bool { |
|
| 788 | + $appdata = 'appdata_' . $this->config->getSystemValueString('instanceid'); |
|
| 789 | + |
|
| 790 | + if (\in_array($uid, [ |
|
| 791 | + '.htaccess', |
|
| 792 | + 'files_external', |
|
| 793 | + '__groupfolders', |
|
| 794 | + '.ncdata', |
|
| 795 | + 'owncloud.log', |
|
| 796 | + 'nextcloud.log', |
|
| 797 | + 'updater.log', |
|
| 798 | + 'audit.log', |
|
| 799 | + $appdata], true)) { |
|
| 800 | + return false; |
|
| 801 | + } |
|
| 802 | + |
|
| 803 | + if (!$checkDataDirectory) { |
|
| 804 | + return true; |
|
| 805 | + } |
|
| 806 | + |
|
| 807 | + $dataDirectory = $this->config->getSystemValueString('datadirectory', \OC::$SERVERROOT . '/data'); |
|
| 808 | + |
|
| 809 | + return !file_exists(rtrim($dataDirectory, '/') . '/' . $uid); |
|
| 810 | + } |
|
| 811 | + |
|
| 812 | + public function getDisplayNameCache(): DisplayNameCache { |
|
| 813 | + return $this->displayNameCache; |
|
| 814 | + } |
|
| 815 | + |
|
| 816 | + public function getSeenUsers(int $offset = 0, ?int $limit = null): \Iterator { |
|
| 817 | + $maxBatchSize = 1000; |
|
| 818 | + |
|
| 819 | + do { |
|
| 820 | + if ($limit !== null) { |
|
| 821 | + $batchSize = min($limit, $maxBatchSize); |
|
| 822 | + $limit -= $batchSize; |
|
| 823 | + } else { |
|
| 824 | + $batchSize = $maxBatchSize; |
|
| 825 | + } |
|
| 826 | + |
|
| 827 | + $userIds = $this->getSeenUserIds($batchSize, $offset); |
|
| 828 | + $offset += $batchSize; |
|
| 829 | + |
|
| 830 | + foreach ($userIds as $userId) { |
|
| 831 | + foreach ($this->backends as $backend) { |
|
| 832 | + if ($backend->userExists($userId)) { |
|
| 833 | + $user = new LazyUser($userId, $this, null, $backend); |
|
| 834 | + yield $user; |
|
| 835 | + break; |
|
| 836 | + } |
|
| 837 | + } |
|
| 838 | + } |
|
| 839 | + } while (count($userIds) === $batchSize && $limit !== 0); |
|
| 840 | + } |
|
| 841 | 841 | } |
@@ -22,118 +22,118 @@ |
||
| 22 | 22 | use Psr\Log\LoggerInterface; |
| 23 | 23 | |
| 24 | 24 | class ExpireTrash extends TimedJob { |
| 25 | - public const TOGGLE_CONFIG_KEY_NAME = 'background_job_expire_trash'; |
|
| 26 | - public const OFFSET_CONFIG_KEY_NAME = 'background_job_expire_trash_offset'; |
|
| 27 | - private const THIRTY_MINUTES = 30 * 60; |
|
| 28 | - private const USER_BATCH_SIZE = 10; |
|
| 29 | - |
|
| 30 | - public function __construct( |
|
| 31 | - private IAppConfig $appConfig, |
|
| 32 | - private IUserManager $userManager, |
|
| 33 | - private Expiration $expiration, |
|
| 34 | - private LoggerInterface $logger, |
|
| 35 | - private SetupManager $setupManager, |
|
| 36 | - private ILockingProvider $lockingProvider, |
|
| 37 | - ITimeFactory $time, |
|
| 38 | - ) { |
|
| 39 | - parent::__construct($time); |
|
| 40 | - $this->setInterval(self::THIRTY_MINUTES); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - protected function run($argument) { |
|
| 44 | - $backgroundJob = $this->appConfig->getValueBool(Application::APP_ID, self::TOGGLE_CONFIG_KEY_NAME, true); |
|
| 45 | - if (!$backgroundJob) { |
|
| 46 | - return; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - $maxAge = $this->expiration->getMaxAgeAsTimestamp(); |
|
| 50 | - if (!$maxAge) { |
|
| 51 | - return; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - $startTime = time(); |
|
| 55 | - |
|
| 56 | - // Process users in batches of 10, but don't run for more than 30 minutes |
|
| 57 | - while (time() < $startTime + self::THIRTY_MINUTES) { |
|
| 58 | - $offset = $this->getNextOffset(); |
|
| 59 | - $users = $this->userManager->getSeenUsers($offset, self::USER_BATCH_SIZE); |
|
| 60 | - $count = 0; |
|
| 61 | - |
|
| 62 | - foreach ($users as $user) { |
|
| 63 | - $uid = $user->getUID(); |
|
| 64 | - $count++; |
|
| 65 | - |
|
| 66 | - try { |
|
| 67 | - if ($this->setupFS($user)) { |
|
| 68 | - $dirContent = Helper::getTrashFiles('/', $uid, 'mtime'); |
|
| 69 | - Trashbin::deleteExpiredFiles($dirContent, $uid); |
|
| 70 | - } |
|
| 71 | - } catch (\Throwable $e) { |
|
| 72 | - $this->logger->error('Error while expiring trashbin for user ' . $uid, ['exception' => $e]); |
|
| 73 | - } finally { |
|
| 74 | - $this->setupManager->tearDown(); |
|
| 75 | - } |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - // If the last batch was not full it means that we reached the end of the user list. |
|
| 79 | - if ($count < self::USER_BATCH_SIZE) { |
|
| 80 | - $this->resetOffset(); |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Act on behalf on trash item owner |
|
| 87 | - */ |
|
| 88 | - protected function setupFS(IUser $user): bool { |
|
| 89 | - $this->setupManager->setupForUser($user); |
|
| 90 | - |
|
| 91 | - // Check if this user has a trashbin directory |
|
| 92 | - $view = new View('/' . $user->getUID()); |
|
| 93 | - if (!$view->is_dir('/files_trashbin/files')) { |
|
| 94 | - return false; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - return true; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - private function getNextOffset(): int { |
|
| 101 | - return $this->runMutexOperation(function () { |
|
| 102 | - $this->appConfig->clearCache(); |
|
| 103 | - |
|
| 104 | - $offset = $this->appConfig->getValueInt(Application::APP_ID, self::OFFSET_CONFIG_KEY_NAME, 0); |
|
| 105 | - $this->appConfig->setValueInt(Application::APP_ID, self::OFFSET_CONFIG_KEY_NAME, $offset + self::USER_BATCH_SIZE); |
|
| 106 | - |
|
| 107 | - return $offset; |
|
| 108 | - }); |
|
| 109 | - |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - private function resetOffset() { |
|
| 113 | - $this->runMutexOperation(function () { |
|
| 114 | - $this->appConfig->setValueInt(Application::APP_ID, self::OFFSET_CONFIG_KEY_NAME, 0); |
|
| 115 | - }); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - private function runMutexOperation($operation): mixed { |
|
| 119 | - $acquired = false; |
|
| 120 | - |
|
| 121 | - while ($acquired === false) { |
|
| 122 | - try { |
|
| 123 | - $this->lockingProvider->acquireLock(self::OFFSET_CONFIG_KEY_NAME, ILockingProvider::LOCK_EXCLUSIVE, 'Expire trashbin background job offset'); |
|
| 124 | - $acquired = true; |
|
| 125 | - } catch (\OCP\Lock\LockedException $e) { |
|
| 126 | - // wait a bit and try again |
|
| 127 | - usleep(100000); |
|
| 128 | - } |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - try { |
|
| 132 | - $result = $operation(); |
|
| 133 | - } finally { |
|
| 134 | - $this->lockingProvider->releaseLock(self::OFFSET_CONFIG_KEY_NAME, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - return $result; |
|
| 138 | - } |
|
| 25 | + public const TOGGLE_CONFIG_KEY_NAME = 'background_job_expire_trash'; |
|
| 26 | + public const OFFSET_CONFIG_KEY_NAME = 'background_job_expire_trash_offset'; |
|
| 27 | + private const THIRTY_MINUTES = 30 * 60; |
|
| 28 | + private const USER_BATCH_SIZE = 10; |
|
| 29 | + |
|
| 30 | + public function __construct( |
|
| 31 | + private IAppConfig $appConfig, |
|
| 32 | + private IUserManager $userManager, |
|
| 33 | + private Expiration $expiration, |
|
| 34 | + private LoggerInterface $logger, |
|
| 35 | + private SetupManager $setupManager, |
|
| 36 | + private ILockingProvider $lockingProvider, |
|
| 37 | + ITimeFactory $time, |
|
| 38 | + ) { |
|
| 39 | + parent::__construct($time); |
|
| 40 | + $this->setInterval(self::THIRTY_MINUTES); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + protected function run($argument) { |
|
| 44 | + $backgroundJob = $this->appConfig->getValueBool(Application::APP_ID, self::TOGGLE_CONFIG_KEY_NAME, true); |
|
| 45 | + if (!$backgroundJob) { |
|
| 46 | + return; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + $maxAge = $this->expiration->getMaxAgeAsTimestamp(); |
|
| 50 | + if (!$maxAge) { |
|
| 51 | + return; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + $startTime = time(); |
|
| 55 | + |
|
| 56 | + // Process users in batches of 10, but don't run for more than 30 minutes |
|
| 57 | + while (time() < $startTime + self::THIRTY_MINUTES) { |
|
| 58 | + $offset = $this->getNextOffset(); |
|
| 59 | + $users = $this->userManager->getSeenUsers($offset, self::USER_BATCH_SIZE); |
|
| 60 | + $count = 0; |
|
| 61 | + |
|
| 62 | + foreach ($users as $user) { |
|
| 63 | + $uid = $user->getUID(); |
|
| 64 | + $count++; |
|
| 65 | + |
|
| 66 | + try { |
|
| 67 | + if ($this->setupFS($user)) { |
|
| 68 | + $dirContent = Helper::getTrashFiles('/', $uid, 'mtime'); |
|
| 69 | + Trashbin::deleteExpiredFiles($dirContent, $uid); |
|
| 70 | + } |
|
| 71 | + } catch (\Throwable $e) { |
|
| 72 | + $this->logger->error('Error while expiring trashbin for user ' . $uid, ['exception' => $e]); |
|
| 73 | + } finally { |
|
| 74 | + $this->setupManager->tearDown(); |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + // If the last batch was not full it means that we reached the end of the user list. |
|
| 79 | + if ($count < self::USER_BATCH_SIZE) { |
|
| 80 | + $this->resetOffset(); |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Act on behalf on trash item owner |
|
| 87 | + */ |
|
| 88 | + protected function setupFS(IUser $user): bool { |
|
| 89 | + $this->setupManager->setupForUser($user); |
|
| 90 | + |
|
| 91 | + // Check if this user has a trashbin directory |
|
| 92 | + $view = new View('/' . $user->getUID()); |
|
| 93 | + if (!$view->is_dir('/files_trashbin/files')) { |
|
| 94 | + return false; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + return true; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + private function getNextOffset(): int { |
|
| 101 | + return $this->runMutexOperation(function () { |
|
| 102 | + $this->appConfig->clearCache(); |
|
| 103 | + |
|
| 104 | + $offset = $this->appConfig->getValueInt(Application::APP_ID, self::OFFSET_CONFIG_KEY_NAME, 0); |
|
| 105 | + $this->appConfig->setValueInt(Application::APP_ID, self::OFFSET_CONFIG_KEY_NAME, $offset + self::USER_BATCH_SIZE); |
|
| 106 | + |
|
| 107 | + return $offset; |
|
| 108 | + }); |
|
| 109 | + |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + private function resetOffset() { |
|
| 113 | + $this->runMutexOperation(function () { |
|
| 114 | + $this->appConfig->setValueInt(Application::APP_ID, self::OFFSET_CONFIG_KEY_NAME, 0); |
|
| 115 | + }); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + private function runMutexOperation($operation): mixed { |
|
| 119 | + $acquired = false; |
|
| 120 | + |
|
| 121 | + while ($acquired === false) { |
|
| 122 | + try { |
|
| 123 | + $this->lockingProvider->acquireLock(self::OFFSET_CONFIG_KEY_NAME, ILockingProvider::LOCK_EXCLUSIVE, 'Expire trashbin background job offset'); |
|
| 124 | + $acquired = true; |
|
| 125 | + } catch (\OCP\Lock\LockedException $e) { |
|
| 126 | + // wait a bit and try again |
|
| 127 | + usleep(100000); |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + try { |
|
| 132 | + $result = $operation(); |
|
| 133 | + } finally { |
|
| 134 | + $this->lockingProvider->releaseLock(self::OFFSET_CONFIG_KEY_NAME, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + return $result; |
|
| 138 | + } |
|
| 139 | 139 | } |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | Trashbin::deleteExpiredFiles($dirContent, $uid); |
| 70 | 70 | } |
| 71 | 71 | } catch (\Throwable $e) { |
| 72 | - $this->logger->error('Error while expiring trashbin for user ' . $uid, ['exception' => $e]); |
|
| 72 | + $this->logger->error('Error while expiring trashbin for user '.$uid, ['exception' => $e]); |
|
| 73 | 73 | } finally { |
| 74 | 74 | $this->setupManager->tearDown(); |
| 75 | 75 | } |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | $this->setupManager->setupForUser($user); |
| 90 | 90 | |
| 91 | 91 | // Check if this user has a trashbin directory |
| 92 | - $view = new View('/' . $user->getUID()); |
|
| 92 | + $view = new View('/'.$user->getUID()); |
|
| 93 | 93 | if (!$view->is_dir('/files_trashbin/files')) { |
| 94 | 94 | return false; |
| 95 | 95 | } |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | private function getNextOffset(): int { |
| 101 | - return $this->runMutexOperation(function () { |
|
| 101 | + return $this->runMutexOperation(function() { |
|
| 102 | 102 | $this->appConfig->clearCache(); |
| 103 | 103 | |
| 104 | 104 | $offset = $this->appConfig->getValueInt(Application::APP_ID, self::OFFSET_CONFIG_KEY_NAME, 0); |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | private function resetOffset() { |
| 113 | - $this->runMutexOperation(function () { |
|
| 113 | + $this->runMutexOperation(function() { |
|
| 114 | 114 | $this->appConfig->setValueInt(Application::APP_ID, self::OFFSET_CONFIG_KEY_NAME, 0); |
| 115 | 115 | }); |
| 116 | 116 | } |
@@ -23,72 +23,72 @@ |
||
| 23 | 23 | use Test\TestCase; |
| 24 | 24 | |
| 25 | 25 | class ExpireTrashTest extends TestCase { |
| 26 | - private IAppConfig&MockObject $appConfig; |
|
| 27 | - private IUserManager&MockObject $userManager; |
|
| 28 | - private Expiration&MockObject $expiration; |
|
| 29 | - private IJobList&MockObject $jobList; |
|
| 30 | - private LoggerInterface&MockObject $logger; |
|
| 31 | - private ITimeFactory&MockObject $time; |
|
| 32 | - private SetupManager&MockObject $setupManager; |
|
| 33 | - private ILockingProvider&MockObject $lockingProvider; |
|
| 26 | + private IAppConfig&MockObject $appConfig; |
|
| 27 | + private IUserManager&MockObject $userManager; |
|
| 28 | + private Expiration&MockObject $expiration; |
|
| 29 | + private IJobList&MockObject $jobList; |
|
| 30 | + private LoggerInterface&MockObject $logger; |
|
| 31 | + private ITimeFactory&MockObject $time; |
|
| 32 | + private SetupManager&MockObject $setupManager; |
|
| 33 | + private ILockingProvider&MockObject $lockingProvider; |
|
| 34 | 34 | |
| 35 | - protected function setUp(): void { |
|
| 36 | - parent::setUp(); |
|
| 35 | + protected function setUp(): void { |
|
| 36 | + parent::setUp(); |
|
| 37 | 37 | |
| 38 | - $this->appConfig = $this->createMock(IAppConfig::class); |
|
| 39 | - $this->userManager = $this->createMock(IUserManager::class); |
|
| 40 | - $this->expiration = $this->createMock(Expiration::class); |
|
| 41 | - $this->jobList = $this->createMock(IJobList::class); |
|
| 42 | - $this->logger = $this->createMock(LoggerInterface::class); |
|
| 43 | - $this->setupManager = $this->createMock(SetupManager::class); |
|
| 44 | - $this->lockingProvider = $this->createMock(ILockingProvider::class); |
|
| 38 | + $this->appConfig = $this->createMock(IAppConfig::class); |
|
| 39 | + $this->userManager = $this->createMock(IUserManager::class); |
|
| 40 | + $this->expiration = $this->createMock(Expiration::class); |
|
| 41 | + $this->jobList = $this->createMock(IJobList::class); |
|
| 42 | + $this->logger = $this->createMock(LoggerInterface::class); |
|
| 43 | + $this->setupManager = $this->createMock(SetupManager::class); |
|
| 44 | + $this->lockingProvider = $this->createMock(ILockingProvider::class); |
|
| 45 | 45 | |
| 46 | - $this->time = $this->createMock(ITimeFactory::class); |
|
| 47 | - $this->time->method('getTime') |
|
| 48 | - ->willReturn(999999999); |
|
| 46 | + $this->time = $this->createMock(ITimeFactory::class); |
|
| 47 | + $this->time->method('getTime') |
|
| 48 | + ->willReturn(999999999); |
|
| 49 | 49 | |
| 50 | - $this->jobList->expects($this->once()) |
|
| 51 | - ->method('setLastRun'); |
|
| 52 | - $this->jobList->expects($this->once()) |
|
| 53 | - ->method('setExecutionTime'); |
|
| 54 | - } |
|
| 50 | + $this->jobList->expects($this->once()) |
|
| 51 | + ->method('setLastRun'); |
|
| 52 | + $this->jobList->expects($this->once()) |
|
| 53 | + ->method('setExecutionTime'); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - public function testConstructAndRun(): void { |
|
| 57 | - $this->appConfig->method('getValueBool') |
|
| 58 | - ->with(Application::APP_ID, ExpireTrash::TOGGLE_CONFIG_KEY_NAME, true) |
|
| 59 | - ->willReturn(true); |
|
| 60 | - $this->appConfig->method('getValueInt') |
|
| 61 | - ->with(Application::APP_ID, ExpireTrash::OFFSET_CONFIG_KEY_NAME, 0) |
|
| 62 | - ->willReturn(0); |
|
| 56 | + public function testConstructAndRun(): void { |
|
| 57 | + $this->appConfig->method('getValueBool') |
|
| 58 | + ->with(Application::APP_ID, ExpireTrash::TOGGLE_CONFIG_KEY_NAME, true) |
|
| 59 | + ->willReturn(true); |
|
| 60 | + $this->appConfig->method('getValueInt') |
|
| 61 | + ->with(Application::APP_ID, ExpireTrash::OFFSET_CONFIG_KEY_NAME, 0) |
|
| 62 | + ->willReturn(0); |
|
| 63 | 63 | |
| 64 | - $job = new ExpireTrash( |
|
| 65 | - $this->appConfig, |
|
| 66 | - $this->userManager, |
|
| 67 | - $this->expiration, |
|
| 68 | - $this->logger, |
|
| 69 | - $this->setupManager, |
|
| 70 | - $this->lockingProvider, |
|
| 71 | - $this->time, |
|
| 72 | - ); |
|
| 73 | - $job->start($this->jobList); |
|
| 74 | - } |
|
| 64 | + $job = new ExpireTrash( |
|
| 65 | + $this->appConfig, |
|
| 66 | + $this->userManager, |
|
| 67 | + $this->expiration, |
|
| 68 | + $this->logger, |
|
| 69 | + $this->setupManager, |
|
| 70 | + $this->lockingProvider, |
|
| 71 | + $this->time, |
|
| 72 | + ); |
|
| 73 | + $job->start($this->jobList); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - public function testBackgroundJobDeactivated(): void { |
|
| 77 | - $this->appConfig->method('getValueBool') |
|
| 78 | - ->with(Application::APP_ID, ExpireTrash::TOGGLE_CONFIG_KEY_NAME, true) |
|
| 79 | - ->willReturn(false); |
|
| 80 | - $this->expiration->expects($this->never()) |
|
| 81 | - ->method('getMaxAgeAsTimestamp'); |
|
| 76 | + public function testBackgroundJobDeactivated(): void { |
|
| 77 | + $this->appConfig->method('getValueBool') |
|
| 78 | + ->with(Application::APP_ID, ExpireTrash::TOGGLE_CONFIG_KEY_NAME, true) |
|
| 79 | + ->willReturn(false); |
|
| 80 | + $this->expiration->expects($this->never()) |
|
| 81 | + ->method('getMaxAgeAsTimestamp'); |
|
| 82 | 82 | |
| 83 | - $job = new ExpireTrash( |
|
| 84 | - $this->appConfig, |
|
| 85 | - $this->userManager, |
|
| 86 | - $this->expiration, |
|
| 87 | - $this->logger, |
|
| 88 | - $this->setupManager, |
|
| 89 | - $this->lockingProvider, |
|
| 90 | - $this->time, |
|
| 91 | - ); |
|
| 92 | - $job->start($this->jobList); |
|
| 93 | - } |
|
| 83 | + $job = new ExpireTrash( |
|
| 84 | + $this->appConfig, |
|
| 85 | + $this->userManager, |
|
| 86 | + $this->expiration, |
|
| 87 | + $this->logger, |
|
| 88 | + $this->setupManager, |
|
| 89 | + $this->lockingProvider, |
|
| 90 | + $this->time, |
|
| 91 | + ); |
|
| 92 | + $job->start($this->jobList); |
|
| 93 | + } |
|
| 94 | 94 | } |