@@ -51,576 +51,576 @@ |
||
51 | 51 | use OCP\Util; |
52 | 52 | |
53 | 53 | class User_LDAP extends BackendUtility implements \OCP\IUserBackend, \OCP\UserInterface, IUserLDAP { |
54 | - /** @var \OCP\IConfig */ |
|
55 | - protected $ocConfig; |
|
56 | - |
|
57 | - /** @var INotificationManager */ |
|
58 | - protected $notificationManager; |
|
59 | - |
|
60 | - /** @var string */ |
|
61 | - protected $currentUserInDeletionProcess; |
|
62 | - |
|
63 | - /** @var UserPluginManager */ |
|
64 | - protected $userPluginManager; |
|
65 | - |
|
66 | - /** |
|
67 | - * @param Access $access |
|
68 | - * @param \OCP\IConfig $ocConfig |
|
69 | - * @param \OCP\Notification\IManager $notificationManager |
|
70 | - * @param IUserSession $userSession |
|
71 | - */ |
|
72 | - public function __construct(Access $access, IConfig $ocConfig, INotificationManager $notificationManager, IUserSession $userSession, UserPluginManager $userPluginManager) { |
|
73 | - parent::__construct($access); |
|
74 | - $this->ocConfig = $ocConfig; |
|
75 | - $this->notificationManager = $notificationManager; |
|
76 | - $this->userPluginManager = $userPluginManager; |
|
77 | - $this->registerHooks($userSession); |
|
78 | - } |
|
79 | - |
|
80 | - protected function registerHooks(IUserSession $userSession) { |
|
81 | - $userSession->listen('\OC\User', 'preDelete', [$this, 'preDeleteUser']); |
|
82 | - $userSession->listen('\OC\User', 'postDelete', [$this, 'postDeleteUser']); |
|
83 | - } |
|
84 | - |
|
85 | - public function preDeleteUser(IUser $user) { |
|
86 | - $this->currentUserInDeletionProcess = $user->getUID(); |
|
87 | - } |
|
88 | - |
|
89 | - public function postDeleteUser() { |
|
90 | - $this->currentUserInDeletionProcess = null; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * checks whether the user is allowed to change his avatar in Nextcloud |
|
95 | - * @param string $uid the Nextcloud user name |
|
96 | - * @return boolean either the user can or cannot |
|
97 | - */ |
|
98 | - public function canChangeAvatar($uid) { |
|
99 | - if ($this->userPluginManager->implementsActions(Backend::PROVIDE_AVATAR)) { |
|
100 | - return $this->userPluginManager->canChangeAvatar($uid); |
|
101 | - } |
|
102 | - |
|
103 | - $user = $this->access->userManager->get($uid); |
|
104 | - if(!$user instanceof User) { |
|
105 | - return false; |
|
106 | - } |
|
107 | - if($user->getAvatarImage() === false) { |
|
108 | - return true; |
|
109 | - } |
|
110 | - |
|
111 | - return false; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * returns the username for the given login name, if available |
|
116 | - * |
|
117 | - * @param string $loginName |
|
118 | - * @return string|false |
|
119 | - */ |
|
120 | - public function loginName2UserName($loginName) { |
|
121 | - $cacheKey = 'loginName2UserName-'.$loginName; |
|
122 | - $username = $this->access->connection->getFromCache($cacheKey); |
|
123 | - if(!is_null($username)) { |
|
124 | - return $username; |
|
125 | - } |
|
126 | - |
|
127 | - try { |
|
128 | - $ldapRecord = $this->getLDAPUserByLoginName($loginName); |
|
129 | - $user = $this->access->userManager->get($ldapRecord['dn'][0]); |
|
130 | - if($user instanceof OfflineUser) { |
|
131 | - // this path is not really possible, however get() is documented |
|
132 | - // to return User or OfflineUser so we are very defensive here. |
|
133 | - $this->access->connection->writeToCache($cacheKey, false); |
|
134 | - return false; |
|
135 | - } |
|
136 | - $username = $user->getUsername(); |
|
137 | - $this->access->connection->writeToCache($cacheKey, $username); |
|
138 | - return $username; |
|
139 | - } catch (NotOnLDAP $e) { |
|
140 | - $this->access->connection->writeToCache($cacheKey, false); |
|
141 | - return false; |
|
142 | - } |
|
143 | - } |
|
54 | + /** @var \OCP\IConfig */ |
|
55 | + protected $ocConfig; |
|
56 | + |
|
57 | + /** @var INotificationManager */ |
|
58 | + protected $notificationManager; |
|
59 | + |
|
60 | + /** @var string */ |
|
61 | + protected $currentUserInDeletionProcess; |
|
62 | + |
|
63 | + /** @var UserPluginManager */ |
|
64 | + protected $userPluginManager; |
|
65 | + |
|
66 | + /** |
|
67 | + * @param Access $access |
|
68 | + * @param \OCP\IConfig $ocConfig |
|
69 | + * @param \OCP\Notification\IManager $notificationManager |
|
70 | + * @param IUserSession $userSession |
|
71 | + */ |
|
72 | + public function __construct(Access $access, IConfig $ocConfig, INotificationManager $notificationManager, IUserSession $userSession, UserPluginManager $userPluginManager) { |
|
73 | + parent::__construct($access); |
|
74 | + $this->ocConfig = $ocConfig; |
|
75 | + $this->notificationManager = $notificationManager; |
|
76 | + $this->userPluginManager = $userPluginManager; |
|
77 | + $this->registerHooks($userSession); |
|
78 | + } |
|
79 | + |
|
80 | + protected function registerHooks(IUserSession $userSession) { |
|
81 | + $userSession->listen('\OC\User', 'preDelete', [$this, 'preDeleteUser']); |
|
82 | + $userSession->listen('\OC\User', 'postDelete', [$this, 'postDeleteUser']); |
|
83 | + } |
|
84 | + |
|
85 | + public function preDeleteUser(IUser $user) { |
|
86 | + $this->currentUserInDeletionProcess = $user->getUID(); |
|
87 | + } |
|
88 | + |
|
89 | + public function postDeleteUser() { |
|
90 | + $this->currentUserInDeletionProcess = null; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * checks whether the user is allowed to change his avatar in Nextcloud |
|
95 | + * @param string $uid the Nextcloud user name |
|
96 | + * @return boolean either the user can or cannot |
|
97 | + */ |
|
98 | + public function canChangeAvatar($uid) { |
|
99 | + if ($this->userPluginManager->implementsActions(Backend::PROVIDE_AVATAR)) { |
|
100 | + return $this->userPluginManager->canChangeAvatar($uid); |
|
101 | + } |
|
102 | + |
|
103 | + $user = $this->access->userManager->get($uid); |
|
104 | + if(!$user instanceof User) { |
|
105 | + return false; |
|
106 | + } |
|
107 | + if($user->getAvatarImage() === false) { |
|
108 | + return true; |
|
109 | + } |
|
110 | + |
|
111 | + return false; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * returns the username for the given login name, if available |
|
116 | + * |
|
117 | + * @param string $loginName |
|
118 | + * @return string|false |
|
119 | + */ |
|
120 | + public function loginName2UserName($loginName) { |
|
121 | + $cacheKey = 'loginName2UserName-'.$loginName; |
|
122 | + $username = $this->access->connection->getFromCache($cacheKey); |
|
123 | + if(!is_null($username)) { |
|
124 | + return $username; |
|
125 | + } |
|
126 | + |
|
127 | + try { |
|
128 | + $ldapRecord = $this->getLDAPUserByLoginName($loginName); |
|
129 | + $user = $this->access->userManager->get($ldapRecord['dn'][0]); |
|
130 | + if($user instanceof OfflineUser) { |
|
131 | + // this path is not really possible, however get() is documented |
|
132 | + // to return User or OfflineUser so we are very defensive here. |
|
133 | + $this->access->connection->writeToCache($cacheKey, false); |
|
134 | + return false; |
|
135 | + } |
|
136 | + $username = $user->getUsername(); |
|
137 | + $this->access->connection->writeToCache($cacheKey, $username); |
|
138 | + return $username; |
|
139 | + } catch (NotOnLDAP $e) { |
|
140 | + $this->access->connection->writeToCache($cacheKey, false); |
|
141 | + return false; |
|
142 | + } |
|
143 | + } |
|
144 | 144 | |
145 | - /** |
|
146 | - * returns the username for the given LDAP DN, if available |
|
147 | - * |
|
148 | - * @param string $dn |
|
149 | - * @return string|false with the username |
|
150 | - */ |
|
151 | - public function dn2UserName($dn) { |
|
152 | - return $this->access->dn2username($dn); |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * returns an LDAP record based on a given login name |
|
157 | - * |
|
158 | - * @param string $loginName |
|
159 | - * @return array |
|
160 | - * @throws NotOnLDAP |
|
161 | - */ |
|
162 | - public function getLDAPUserByLoginName($loginName) { |
|
163 | - //find out dn of the user name |
|
164 | - $attrs = $this->access->userManager->getAttributes(); |
|
165 | - $users = $this->access->fetchUsersByLoginName($loginName, $attrs); |
|
166 | - if(count($users) < 1) { |
|
167 | - throw new NotOnLDAP('No user available for the given login name on ' . |
|
168 | - $this->access->connection->ldapHost . ':' . $this->access->connection->ldapPort); |
|
169 | - } |
|
170 | - return $users[0]; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Check if the password is correct without logging in the user |
|
175 | - * |
|
176 | - * @param string $uid The username |
|
177 | - * @param string $password The password |
|
178 | - * @return false|string |
|
179 | - */ |
|
180 | - public function checkPassword($uid, $password) { |
|
181 | - try { |
|
182 | - $ldapRecord = $this->getLDAPUserByLoginName($uid); |
|
183 | - } catch(NotOnLDAP $e) { |
|
184 | - if($this->ocConfig->getSystemValue('loglevel', Util::WARN) === Util::DEBUG) { |
|
185 | - \OC::$server->getLogger()->logException($e, ['app' => 'user_ldap']); |
|
186 | - } |
|
187 | - return false; |
|
188 | - } |
|
189 | - $dn = $ldapRecord['dn'][0]; |
|
190 | - $user = $this->access->userManager->get($dn); |
|
191 | - |
|
192 | - if(!$user instanceof User) { |
|
193 | - Util::writeLog('user_ldap', |
|
194 | - 'LDAP Login: Could not get user object for DN ' . $dn . |
|
195 | - '. Maybe the LDAP entry has no set display name attribute?', |
|
196 | - Util::WARN); |
|
197 | - return false; |
|
198 | - } |
|
199 | - if($user->getUsername() !== false) { |
|
200 | - //are the credentials OK? |
|
201 | - if(!$this->access->areCredentialsValid($dn, $password)) { |
|
202 | - return false; |
|
203 | - } |
|
204 | - |
|
205 | - $this->access->cacheUserExists($user->getUsername()); |
|
206 | - $user->processAttributes($ldapRecord); |
|
207 | - $user->markLogin(); |
|
208 | - |
|
209 | - return $user->getUsername(); |
|
210 | - } |
|
211 | - |
|
212 | - return false; |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * Set password |
|
217 | - * @param string $uid The username |
|
218 | - * @param string $password The new password |
|
219 | - * @return bool |
|
220 | - */ |
|
221 | - public function setPassword($uid, $password) { |
|
222 | - if ($this->userPluginManager->implementsActions(Backend::SET_PASSWORD)) { |
|
223 | - return $this->userPluginManager->setPassword($uid, $password); |
|
224 | - } |
|
225 | - |
|
226 | - $user = $this->access->userManager->get($uid); |
|
227 | - |
|
228 | - if(!$user instanceof User) { |
|
229 | - throw new \Exception('LDAP setPassword: Could not get user object for uid ' . $uid . |
|
230 | - '. Maybe the LDAP entry has no set display name attribute?'); |
|
231 | - } |
|
232 | - if($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) { |
|
233 | - $ldapDefaultPPolicyDN = $this->access->connection->ldapDefaultPPolicyDN; |
|
234 | - $turnOnPasswordChange = $this->access->connection->turnOnPasswordChange; |
|
235 | - if (!empty($ldapDefaultPPolicyDN) && ((int)$turnOnPasswordChange === 1)) { |
|
236 | - //remove last password expiry warning if any |
|
237 | - $notification = $this->notificationManager->createNotification(); |
|
238 | - $notification->setApp('user_ldap') |
|
239 | - ->setUser($uid) |
|
240 | - ->setObject('pwd_exp_warn', $uid) |
|
241 | - ; |
|
242 | - $this->notificationManager->markProcessed($notification); |
|
243 | - } |
|
244 | - return true; |
|
245 | - } |
|
246 | - |
|
247 | - return false; |
|
248 | - } |
|
249 | - |
|
250 | - /** |
|
251 | - * Get a list of all users |
|
252 | - * |
|
253 | - * @param string $search |
|
254 | - * @param integer $limit |
|
255 | - * @param integer $offset |
|
256 | - * @return string[] an array of all uids |
|
257 | - */ |
|
258 | - public function getUsers($search = '', $limit = 10, $offset = 0) { |
|
259 | - $search = $this->access->escapeFilterPart($search, true); |
|
260 | - $cachekey = 'getUsers-'.$search.'-'.$limit.'-'.$offset; |
|
261 | - |
|
262 | - //check if users are cached, if so return |
|
263 | - $ldap_users = $this->access->connection->getFromCache($cachekey); |
|
264 | - if(!is_null($ldap_users)) { |
|
265 | - return $ldap_users; |
|
266 | - } |
|
267 | - |
|
268 | - // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
|
269 | - // error. With a limit of 0, we get 0 results. So we pass null. |
|
270 | - if($limit <= 0) { |
|
271 | - $limit = null; |
|
272 | - } |
|
273 | - $filter = $this->access->combineFilterWithAnd(array( |
|
274 | - $this->access->connection->ldapUserFilter, |
|
275 | - $this->access->connection->ldapUserDisplayName . '=*', |
|
276 | - $this->access->getFilterPartForUserSearch($search) |
|
277 | - )); |
|
278 | - |
|
279 | - Util::writeLog('user_ldap', |
|
280 | - 'getUsers: Options: search '.$search.' limit '.$limit.' offset '.$offset.' Filter: '.$filter, |
|
281 | - Util::DEBUG); |
|
282 | - //do the search and translate results to Nextcloud names |
|
283 | - $ldap_users = $this->access->fetchListOfUsers( |
|
284 | - $filter, |
|
285 | - $this->access->userManager->getAttributes(true), |
|
286 | - $limit, $offset); |
|
287 | - $ldap_users = $this->access->nextcloudUserNames($ldap_users); |
|
288 | - Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', Util::DEBUG); |
|
289 | - |
|
290 | - $this->access->connection->writeToCache($cachekey, $ldap_users); |
|
291 | - return $ldap_users; |
|
292 | - } |
|
293 | - |
|
294 | - /** |
|
295 | - * checks whether a user is still available on LDAP |
|
296 | - * |
|
297 | - * @param string|\OCA\User_LDAP\User\User $user either the Nextcloud user |
|
298 | - * name or an instance of that user |
|
299 | - * @return bool |
|
300 | - * @throws \Exception |
|
301 | - * @throws \OC\ServerNotAvailableException |
|
302 | - */ |
|
303 | - public function userExistsOnLDAP($user) { |
|
304 | - if(is_string($user)) { |
|
305 | - $user = $this->access->userManager->get($user); |
|
306 | - } |
|
307 | - if(is_null($user)) { |
|
308 | - return false; |
|
309 | - } |
|
310 | - |
|
311 | - $dn = $user->getDN(); |
|
312 | - //check if user really still exists by reading its entry |
|
313 | - if(!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) { |
|
314 | - $lcr = $this->access->connection->getConnectionResource(); |
|
315 | - if(is_null($lcr)) { |
|
316 | - throw new \Exception('No LDAP Connection to server ' . $this->access->connection->ldapHost); |
|
317 | - } |
|
318 | - |
|
319 | - try { |
|
320 | - $uuid = $this->access->getUserMapper()->getUUIDByDN($dn); |
|
321 | - if (!$uuid) { |
|
322 | - return false; |
|
323 | - } |
|
324 | - $newDn = $this->access->getUserDnByUuid($uuid); |
|
325 | - //check if renamed user is still valid by reapplying the ldap filter |
|
326 | - if (!is_array($this->access->readAttribute($newDn, '', $this->access->connection->ldapUserFilter))) { |
|
327 | - return false; |
|
328 | - } |
|
329 | - $this->access->getUserMapper()->setDNbyUUID($newDn, $uuid); |
|
330 | - return true; |
|
331 | - } catch (ServerNotAvailableException $e) { |
|
332 | - throw $e; |
|
333 | - } catch (\Exception $e) { |
|
334 | - return false; |
|
335 | - } |
|
336 | - } |
|
337 | - |
|
338 | - if($user instanceof OfflineUser) { |
|
339 | - $user->unmark(); |
|
340 | - } |
|
341 | - |
|
342 | - return true; |
|
343 | - } |
|
344 | - |
|
345 | - /** |
|
346 | - * check if a user exists |
|
347 | - * @param string $uid the username |
|
348 | - * @return boolean |
|
349 | - * @throws \Exception when connection could not be established |
|
350 | - */ |
|
351 | - public function userExists($uid) { |
|
352 | - $userExists = $this->access->connection->getFromCache('userExists'.$uid); |
|
353 | - if(!is_null($userExists)) { |
|
354 | - return (bool)$userExists; |
|
355 | - } |
|
356 | - //getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking. |
|
357 | - $user = $this->access->userManager->get($uid); |
|
358 | - |
|
359 | - if(is_null($user)) { |
|
360 | - Util::writeLog('user_ldap', 'No DN found for '.$uid.' on '. |
|
361 | - $this->access->connection->ldapHost, Util::DEBUG); |
|
362 | - $this->access->connection->writeToCache('userExists'.$uid, false); |
|
363 | - return false; |
|
364 | - } else if($user instanceof OfflineUser) { |
|
365 | - //express check for users marked as deleted. Returning true is |
|
366 | - //necessary for cleanup |
|
367 | - return true; |
|
368 | - } |
|
369 | - |
|
370 | - $result = $this->userExistsOnLDAP($user); |
|
371 | - $this->access->connection->writeToCache('userExists'.$uid, $result); |
|
372 | - if($result === true) { |
|
373 | - $user->update(); |
|
374 | - } |
|
375 | - return $result; |
|
376 | - } |
|
377 | - |
|
378 | - /** |
|
379 | - * returns whether a user was deleted in LDAP |
|
380 | - * |
|
381 | - * @param string $uid The username of the user to delete |
|
382 | - * @return bool |
|
383 | - */ |
|
384 | - public function deleteUser($uid) { |
|
385 | - if ($this->userPluginManager->canDeleteUser()) { |
|
386 | - return $this->userPluginManager->deleteUser($uid); |
|
387 | - } |
|
388 | - |
|
389 | - $marked = $this->ocConfig->getUserValue($uid, 'user_ldap', 'isDeleted', 0); |
|
390 | - if((int)$marked === 0) { |
|
391 | - \OC::$server->getLogger()->notice( |
|
392 | - 'User '.$uid . ' is not marked as deleted, not cleaning up.', |
|
393 | - array('app' => 'user_ldap')); |
|
394 | - return false; |
|
395 | - } |
|
396 | - \OC::$server->getLogger()->info('Cleaning up after user ' . $uid, |
|
397 | - array('app' => 'user_ldap')); |
|
398 | - |
|
399 | - $this->access->getUserMapper()->unmap($uid); // we don't emit revoke signals here, since it is implicit to delete signals fired from core |
|
400 | - $this->access->userManager->invalidate($uid); |
|
401 | - return true; |
|
402 | - } |
|
403 | - |
|
404 | - /** |
|
405 | - * get the user's home directory |
|
406 | - * |
|
407 | - * @param string $uid the username |
|
408 | - * @return bool|string |
|
409 | - * @throws NoUserException |
|
410 | - * @throws \Exception |
|
411 | - */ |
|
412 | - public function getHome($uid) { |
|
413 | - // user Exists check required as it is not done in user proxy! |
|
414 | - if(!$this->userExists($uid)) { |
|
415 | - return false; |
|
416 | - } |
|
417 | - |
|
418 | - if ($this->userPluginManager->implementsActions(Backend::GET_HOME)) { |
|
419 | - return $this->userPluginManager->getHome($uid); |
|
420 | - } |
|
421 | - |
|
422 | - $cacheKey = 'getHome'.$uid; |
|
423 | - $path = $this->access->connection->getFromCache($cacheKey); |
|
424 | - if(!is_null($path)) { |
|
425 | - return $path; |
|
426 | - } |
|
427 | - |
|
428 | - // early return path if it is a deleted user |
|
429 | - $user = $this->access->userManager->get($uid); |
|
430 | - if($user instanceof OfflineUser) { |
|
431 | - if($this->currentUserInDeletionProcess !== null |
|
432 | - && $this->currentUserInDeletionProcess === $user->getOCName() |
|
433 | - ) { |
|
434 | - return $user->getHomePath(); |
|
435 | - } else { |
|
436 | - throw new NoUserException($uid . ' is not a valid user anymore'); |
|
437 | - } |
|
438 | - } else if ($user === null) { |
|
439 | - throw new NoUserException($uid . ' is not a valid user anymore'); |
|
440 | - } |
|
441 | - |
|
442 | - $path = $user->getHomePath(); |
|
443 | - $this->access->cacheUserHome($uid, $path); |
|
444 | - |
|
445 | - return $path; |
|
446 | - } |
|
447 | - |
|
448 | - /** |
|
449 | - * get display name of the user |
|
450 | - * @param string $uid user ID of the user |
|
451 | - * @return string|false display name |
|
452 | - */ |
|
453 | - public function getDisplayName($uid) { |
|
454 | - if ($this->userPluginManager->implementsActions(Backend::GET_DISPLAYNAME)) { |
|
455 | - return $this->userPluginManager->getDisplayName($uid); |
|
456 | - } |
|
457 | - |
|
458 | - if(!$this->userExists($uid)) { |
|
459 | - return false; |
|
460 | - } |
|
461 | - |
|
462 | - $cacheKey = 'getDisplayName'.$uid; |
|
463 | - if(!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { |
|
464 | - return $displayName; |
|
465 | - } |
|
466 | - |
|
467 | - //Check whether the display name is configured to have a 2nd feature |
|
468 | - $additionalAttribute = $this->access->connection->ldapUserDisplayName2; |
|
469 | - $displayName2 = ''; |
|
470 | - if ($additionalAttribute !== '') { |
|
471 | - $displayName2 = $this->access->readAttribute( |
|
472 | - $this->access->username2dn($uid), |
|
473 | - $additionalAttribute); |
|
474 | - } |
|
475 | - |
|
476 | - $displayName = $this->access->readAttribute( |
|
477 | - $this->access->username2dn($uid), |
|
478 | - $this->access->connection->ldapUserDisplayName); |
|
479 | - |
|
480 | - if($displayName && (count($displayName) > 0)) { |
|
481 | - $displayName = $displayName[0]; |
|
482 | - |
|
483 | - if (is_array($displayName2)){ |
|
484 | - $displayName2 = count($displayName2) > 0 ? $displayName2[0] : ''; |
|
485 | - } |
|
486 | - |
|
487 | - $user = $this->access->userManager->get($uid); |
|
488 | - if ($user instanceof User) { |
|
489 | - $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); |
|
490 | - $this->access->connection->writeToCache($cacheKey, $displayName); |
|
491 | - } |
|
492 | - if ($user instanceof OfflineUser) { |
|
493 | - /** @var OfflineUser $user*/ |
|
494 | - $displayName = $user->getDisplayName(); |
|
495 | - } |
|
496 | - return $displayName; |
|
497 | - } |
|
498 | - |
|
499 | - return null; |
|
500 | - } |
|
501 | - |
|
502 | - /** |
|
503 | - * set display name of the user |
|
504 | - * @param string $uid user ID of the user |
|
505 | - * @param string $displayName new display name of the user |
|
506 | - * @return string|false display name |
|
507 | - */ |
|
508 | - public function setDisplayName($uid, $displayName) { |
|
509 | - if ($this->userPluginManager->implementsActions(Backend::SET_DISPLAYNAME)) { |
|
510 | - return $this->userPluginManager->setDisplayName($uid, $displayName); |
|
511 | - } |
|
512 | - return false; |
|
513 | - } |
|
514 | - |
|
515 | - /** |
|
516 | - * Get a list of all display names |
|
517 | - * |
|
518 | - * @param string $search |
|
519 | - * @param string|null $limit |
|
520 | - * @param string|null $offset |
|
521 | - * @return array an array of all displayNames (value) and the corresponding uids (key) |
|
522 | - */ |
|
523 | - public function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
524 | - $cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset; |
|
525 | - if(!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) { |
|
526 | - return $displayNames; |
|
527 | - } |
|
528 | - |
|
529 | - $displayNames = array(); |
|
530 | - $users = $this->getUsers($search, $limit, $offset); |
|
531 | - foreach ($users as $user) { |
|
532 | - $displayNames[$user] = $this->getDisplayName($user); |
|
533 | - } |
|
534 | - $this->access->connection->writeToCache($cacheKey, $displayNames); |
|
535 | - return $displayNames; |
|
536 | - } |
|
537 | - |
|
538 | - /** |
|
539 | - * Check if backend implements actions |
|
540 | - * @param int $actions bitwise-or'ed actions |
|
541 | - * @return boolean |
|
542 | - * |
|
543 | - * Returns the supported actions as int to be |
|
544 | - * compared with \OC\User\Backend::CREATE_USER etc. |
|
545 | - */ |
|
546 | - public function implementsActions($actions) { |
|
547 | - return (bool)((Backend::CHECK_PASSWORD |
|
548 | - | Backend::GET_HOME |
|
549 | - | Backend::GET_DISPLAYNAME |
|
550 | - | Backend::PROVIDE_AVATAR |
|
551 | - | Backend::COUNT_USERS |
|
552 | - | (((int)$this->access->connection->turnOnPasswordChange === 1)? Backend::SET_PASSWORD :0) |
|
553 | - | $this->userPluginManager->getImplementedActions()) |
|
554 | - & $actions); |
|
555 | - } |
|
556 | - |
|
557 | - /** |
|
558 | - * @return bool |
|
559 | - */ |
|
560 | - public function hasUserListings() { |
|
561 | - return true; |
|
562 | - } |
|
563 | - |
|
564 | - /** |
|
565 | - * counts the users in LDAP |
|
566 | - * |
|
567 | - * @return int|bool |
|
568 | - */ |
|
569 | - public function countUsers() { |
|
570 | - if ($this->userPluginManager->implementsActions(Backend::COUNT_USERS)) { |
|
571 | - return $this->userPluginManager->countUsers(); |
|
572 | - } |
|
573 | - |
|
574 | - $filter = $this->access->getFilterForUserCount(); |
|
575 | - $cacheKey = 'countUsers-'.$filter; |
|
576 | - if(!is_null($entries = $this->access->connection->getFromCache($cacheKey))) { |
|
577 | - return $entries; |
|
578 | - } |
|
579 | - $entries = $this->access->countUsers($filter); |
|
580 | - $this->access->connection->writeToCache($cacheKey, $entries); |
|
581 | - return $entries; |
|
582 | - } |
|
583 | - |
|
584 | - /** |
|
585 | - * Backend name to be shown in user management |
|
586 | - * @return string the name of the backend to be shown |
|
587 | - */ |
|
588 | - public function getBackendName(){ |
|
589 | - return 'LDAP'; |
|
590 | - } |
|
145 | + /** |
|
146 | + * returns the username for the given LDAP DN, if available |
|
147 | + * |
|
148 | + * @param string $dn |
|
149 | + * @return string|false with the username |
|
150 | + */ |
|
151 | + public function dn2UserName($dn) { |
|
152 | + return $this->access->dn2username($dn); |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * returns an LDAP record based on a given login name |
|
157 | + * |
|
158 | + * @param string $loginName |
|
159 | + * @return array |
|
160 | + * @throws NotOnLDAP |
|
161 | + */ |
|
162 | + public function getLDAPUserByLoginName($loginName) { |
|
163 | + //find out dn of the user name |
|
164 | + $attrs = $this->access->userManager->getAttributes(); |
|
165 | + $users = $this->access->fetchUsersByLoginName($loginName, $attrs); |
|
166 | + if(count($users) < 1) { |
|
167 | + throw new NotOnLDAP('No user available for the given login name on ' . |
|
168 | + $this->access->connection->ldapHost . ':' . $this->access->connection->ldapPort); |
|
169 | + } |
|
170 | + return $users[0]; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Check if the password is correct without logging in the user |
|
175 | + * |
|
176 | + * @param string $uid The username |
|
177 | + * @param string $password The password |
|
178 | + * @return false|string |
|
179 | + */ |
|
180 | + public function checkPassword($uid, $password) { |
|
181 | + try { |
|
182 | + $ldapRecord = $this->getLDAPUserByLoginName($uid); |
|
183 | + } catch(NotOnLDAP $e) { |
|
184 | + if($this->ocConfig->getSystemValue('loglevel', Util::WARN) === Util::DEBUG) { |
|
185 | + \OC::$server->getLogger()->logException($e, ['app' => 'user_ldap']); |
|
186 | + } |
|
187 | + return false; |
|
188 | + } |
|
189 | + $dn = $ldapRecord['dn'][0]; |
|
190 | + $user = $this->access->userManager->get($dn); |
|
191 | + |
|
192 | + if(!$user instanceof User) { |
|
193 | + Util::writeLog('user_ldap', |
|
194 | + 'LDAP Login: Could not get user object for DN ' . $dn . |
|
195 | + '. Maybe the LDAP entry has no set display name attribute?', |
|
196 | + Util::WARN); |
|
197 | + return false; |
|
198 | + } |
|
199 | + if($user->getUsername() !== false) { |
|
200 | + //are the credentials OK? |
|
201 | + if(!$this->access->areCredentialsValid($dn, $password)) { |
|
202 | + return false; |
|
203 | + } |
|
204 | + |
|
205 | + $this->access->cacheUserExists($user->getUsername()); |
|
206 | + $user->processAttributes($ldapRecord); |
|
207 | + $user->markLogin(); |
|
208 | + |
|
209 | + return $user->getUsername(); |
|
210 | + } |
|
211 | + |
|
212 | + return false; |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * Set password |
|
217 | + * @param string $uid The username |
|
218 | + * @param string $password The new password |
|
219 | + * @return bool |
|
220 | + */ |
|
221 | + public function setPassword($uid, $password) { |
|
222 | + if ($this->userPluginManager->implementsActions(Backend::SET_PASSWORD)) { |
|
223 | + return $this->userPluginManager->setPassword($uid, $password); |
|
224 | + } |
|
225 | + |
|
226 | + $user = $this->access->userManager->get($uid); |
|
227 | + |
|
228 | + if(!$user instanceof User) { |
|
229 | + throw new \Exception('LDAP setPassword: Could not get user object for uid ' . $uid . |
|
230 | + '. Maybe the LDAP entry has no set display name attribute?'); |
|
231 | + } |
|
232 | + if($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) { |
|
233 | + $ldapDefaultPPolicyDN = $this->access->connection->ldapDefaultPPolicyDN; |
|
234 | + $turnOnPasswordChange = $this->access->connection->turnOnPasswordChange; |
|
235 | + if (!empty($ldapDefaultPPolicyDN) && ((int)$turnOnPasswordChange === 1)) { |
|
236 | + //remove last password expiry warning if any |
|
237 | + $notification = $this->notificationManager->createNotification(); |
|
238 | + $notification->setApp('user_ldap') |
|
239 | + ->setUser($uid) |
|
240 | + ->setObject('pwd_exp_warn', $uid) |
|
241 | + ; |
|
242 | + $this->notificationManager->markProcessed($notification); |
|
243 | + } |
|
244 | + return true; |
|
245 | + } |
|
246 | + |
|
247 | + return false; |
|
248 | + } |
|
249 | + |
|
250 | + /** |
|
251 | + * Get a list of all users |
|
252 | + * |
|
253 | + * @param string $search |
|
254 | + * @param integer $limit |
|
255 | + * @param integer $offset |
|
256 | + * @return string[] an array of all uids |
|
257 | + */ |
|
258 | + public function getUsers($search = '', $limit = 10, $offset = 0) { |
|
259 | + $search = $this->access->escapeFilterPart($search, true); |
|
260 | + $cachekey = 'getUsers-'.$search.'-'.$limit.'-'.$offset; |
|
261 | + |
|
262 | + //check if users are cached, if so return |
|
263 | + $ldap_users = $this->access->connection->getFromCache($cachekey); |
|
264 | + if(!is_null($ldap_users)) { |
|
265 | + return $ldap_users; |
|
266 | + } |
|
267 | + |
|
268 | + // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
|
269 | + // error. With a limit of 0, we get 0 results. So we pass null. |
|
270 | + if($limit <= 0) { |
|
271 | + $limit = null; |
|
272 | + } |
|
273 | + $filter = $this->access->combineFilterWithAnd(array( |
|
274 | + $this->access->connection->ldapUserFilter, |
|
275 | + $this->access->connection->ldapUserDisplayName . '=*', |
|
276 | + $this->access->getFilterPartForUserSearch($search) |
|
277 | + )); |
|
278 | + |
|
279 | + Util::writeLog('user_ldap', |
|
280 | + 'getUsers: Options: search '.$search.' limit '.$limit.' offset '.$offset.' Filter: '.$filter, |
|
281 | + Util::DEBUG); |
|
282 | + //do the search and translate results to Nextcloud names |
|
283 | + $ldap_users = $this->access->fetchListOfUsers( |
|
284 | + $filter, |
|
285 | + $this->access->userManager->getAttributes(true), |
|
286 | + $limit, $offset); |
|
287 | + $ldap_users = $this->access->nextcloudUserNames($ldap_users); |
|
288 | + Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', Util::DEBUG); |
|
289 | + |
|
290 | + $this->access->connection->writeToCache($cachekey, $ldap_users); |
|
291 | + return $ldap_users; |
|
292 | + } |
|
293 | + |
|
294 | + /** |
|
295 | + * checks whether a user is still available on LDAP |
|
296 | + * |
|
297 | + * @param string|\OCA\User_LDAP\User\User $user either the Nextcloud user |
|
298 | + * name or an instance of that user |
|
299 | + * @return bool |
|
300 | + * @throws \Exception |
|
301 | + * @throws \OC\ServerNotAvailableException |
|
302 | + */ |
|
303 | + public function userExistsOnLDAP($user) { |
|
304 | + if(is_string($user)) { |
|
305 | + $user = $this->access->userManager->get($user); |
|
306 | + } |
|
307 | + if(is_null($user)) { |
|
308 | + return false; |
|
309 | + } |
|
310 | + |
|
311 | + $dn = $user->getDN(); |
|
312 | + //check if user really still exists by reading its entry |
|
313 | + if(!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) { |
|
314 | + $lcr = $this->access->connection->getConnectionResource(); |
|
315 | + if(is_null($lcr)) { |
|
316 | + throw new \Exception('No LDAP Connection to server ' . $this->access->connection->ldapHost); |
|
317 | + } |
|
318 | + |
|
319 | + try { |
|
320 | + $uuid = $this->access->getUserMapper()->getUUIDByDN($dn); |
|
321 | + if (!$uuid) { |
|
322 | + return false; |
|
323 | + } |
|
324 | + $newDn = $this->access->getUserDnByUuid($uuid); |
|
325 | + //check if renamed user is still valid by reapplying the ldap filter |
|
326 | + if (!is_array($this->access->readAttribute($newDn, '', $this->access->connection->ldapUserFilter))) { |
|
327 | + return false; |
|
328 | + } |
|
329 | + $this->access->getUserMapper()->setDNbyUUID($newDn, $uuid); |
|
330 | + return true; |
|
331 | + } catch (ServerNotAvailableException $e) { |
|
332 | + throw $e; |
|
333 | + } catch (\Exception $e) { |
|
334 | + return false; |
|
335 | + } |
|
336 | + } |
|
337 | + |
|
338 | + if($user instanceof OfflineUser) { |
|
339 | + $user->unmark(); |
|
340 | + } |
|
341 | + |
|
342 | + return true; |
|
343 | + } |
|
344 | + |
|
345 | + /** |
|
346 | + * check if a user exists |
|
347 | + * @param string $uid the username |
|
348 | + * @return boolean |
|
349 | + * @throws \Exception when connection could not be established |
|
350 | + */ |
|
351 | + public function userExists($uid) { |
|
352 | + $userExists = $this->access->connection->getFromCache('userExists'.$uid); |
|
353 | + if(!is_null($userExists)) { |
|
354 | + return (bool)$userExists; |
|
355 | + } |
|
356 | + //getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking. |
|
357 | + $user = $this->access->userManager->get($uid); |
|
358 | + |
|
359 | + if(is_null($user)) { |
|
360 | + Util::writeLog('user_ldap', 'No DN found for '.$uid.' on '. |
|
361 | + $this->access->connection->ldapHost, Util::DEBUG); |
|
362 | + $this->access->connection->writeToCache('userExists'.$uid, false); |
|
363 | + return false; |
|
364 | + } else if($user instanceof OfflineUser) { |
|
365 | + //express check for users marked as deleted. Returning true is |
|
366 | + //necessary for cleanup |
|
367 | + return true; |
|
368 | + } |
|
369 | + |
|
370 | + $result = $this->userExistsOnLDAP($user); |
|
371 | + $this->access->connection->writeToCache('userExists'.$uid, $result); |
|
372 | + if($result === true) { |
|
373 | + $user->update(); |
|
374 | + } |
|
375 | + return $result; |
|
376 | + } |
|
377 | + |
|
378 | + /** |
|
379 | + * returns whether a user was deleted in LDAP |
|
380 | + * |
|
381 | + * @param string $uid The username of the user to delete |
|
382 | + * @return bool |
|
383 | + */ |
|
384 | + public function deleteUser($uid) { |
|
385 | + if ($this->userPluginManager->canDeleteUser()) { |
|
386 | + return $this->userPluginManager->deleteUser($uid); |
|
387 | + } |
|
388 | + |
|
389 | + $marked = $this->ocConfig->getUserValue($uid, 'user_ldap', 'isDeleted', 0); |
|
390 | + if((int)$marked === 0) { |
|
391 | + \OC::$server->getLogger()->notice( |
|
392 | + 'User '.$uid . ' is not marked as deleted, not cleaning up.', |
|
393 | + array('app' => 'user_ldap')); |
|
394 | + return false; |
|
395 | + } |
|
396 | + \OC::$server->getLogger()->info('Cleaning up after user ' . $uid, |
|
397 | + array('app' => 'user_ldap')); |
|
398 | + |
|
399 | + $this->access->getUserMapper()->unmap($uid); // we don't emit revoke signals here, since it is implicit to delete signals fired from core |
|
400 | + $this->access->userManager->invalidate($uid); |
|
401 | + return true; |
|
402 | + } |
|
403 | + |
|
404 | + /** |
|
405 | + * get the user's home directory |
|
406 | + * |
|
407 | + * @param string $uid the username |
|
408 | + * @return bool|string |
|
409 | + * @throws NoUserException |
|
410 | + * @throws \Exception |
|
411 | + */ |
|
412 | + public function getHome($uid) { |
|
413 | + // user Exists check required as it is not done in user proxy! |
|
414 | + if(!$this->userExists($uid)) { |
|
415 | + return false; |
|
416 | + } |
|
417 | + |
|
418 | + if ($this->userPluginManager->implementsActions(Backend::GET_HOME)) { |
|
419 | + return $this->userPluginManager->getHome($uid); |
|
420 | + } |
|
421 | + |
|
422 | + $cacheKey = 'getHome'.$uid; |
|
423 | + $path = $this->access->connection->getFromCache($cacheKey); |
|
424 | + if(!is_null($path)) { |
|
425 | + return $path; |
|
426 | + } |
|
427 | + |
|
428 | + // early return path if it is a deleted user |
|
429 | + $user = $this->access->userManager->get($uid); |
|
430 | + if($user instanceof OfflineUser) { |
|
431 | + if($this->currentUserInDeletionProcess !== null |
|
432 | + && $this->currentUserInDeletionProcess === $user->getOCName() |
|
433 | + ) { |
|
434 | + return $user->getHomePath(); |
|
435 | + } else { |
|
436 | + throw new NoUserException($uid . ' is not a valid user anymore'); |
|
437 | + } |
|
438 | + } else if ($user === null) { |
|
439 | + throw new NoUserException($uid . ' is not a valid user anymore'); |
|
440 | + } |
|
441 | + |
|
442 | + $path = $user->getHomePath(); |
|
443 | + $this->access->cacheUserHome($uid, $path); |
|
444 | + |
|
445 | + return $path; |
|
446 | + } |
|
447 | + |
|
448 | + /** |
|
449 | + * get display name of the user |
|
450 | + * @param string $uid user ID of the user |
|
451 | + * @return string|false display name |
|
452 | + */ |
|
453 | + public function getDisplayName($uid) { |
|
454 | + if ($this->userPluginManager->implementsActions(Backend::GET_DISPLAYNAME)) { |
|
455 | + return $this->userPluginManager->getDisplayName($uid); |
|
456 | + } |
|
457 | + |
|
458 | + if(!$this->userExists($uid)) { |
|
459 | + return false; |
|
460 | + } |
|
461 | + |
|
462 | + $cacheKey = 'getDisplayName'.$uid; |
|
463 | + if(!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { |
|
464 | + return $displayName; |
|
465 | + } |
|
466 | + |
|
467 | + //Check whether the display name is configured to have a 2nd feature |
|
468 | + $additionalAttribute = $this->access->connection->ldapUserDisplayName2; |
|
469 | + $displayName2 = ''; |
|
470 | + if ($additionalAttribute !== '') { |
|
471 | + $displayName2 = $this->access->readAttribute( |
|
472 | + $this->access->username2dn($uid), |
|
473 | + $additionalAttribute); |
|
474 | + } |
|
475 | + |
|
476 | + $displayName = $this->access->readAttribute( |
|
477 | + $this->access->username2dn($uid), |
|
478 | + $this->access->connection->ldapUserDisplayName); |
|
479 | + |
|
480 | + if($displayName && (count($displayName) > 0)) { |
|
481 | + $displayName = $displayName[0]; |
|
482 | + |
|
483 | + if (is_array($displayName2)){ |
|
484 | + $displayName2 = count($displayName2) > 0 ? $displayName2[0] : ''; |
|
485 | + } |
|
486 | + |
|
487 | + $user = $this->access->userManager->get($uid); |
|
488 | + if ($user instanceof User) { |
|
489 | + $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); |
|
490 | + $this->access->connection->writeToCache($cacheKey, $displayName); |
|
491 | + } |
|
492 | + if ($user instanceof OfflineUser) { |
|
493 | + /** @var OfflineUser $user*/ |
|
494 | + $displayName = $user->getDisplayName(); |
|
495 | + } |
|
496 | + return $displayName; |
|
497 | + } |
|
498 | + |
|
499 | + return null; |
|
500 | + } |
|
501 | + |
|
502 | + /** |
|
503 | + * set display name of the user |
|
504 | + * @param string $uid user ID of the user |
|
505 | + * @param string $displayName new display name of the user |
|
506 | + * @return string|false display name |
|
507 | + */ |
|
508 | + public function setDisplayName($uid, $displayName) { |
|
509 | + if ($this->userPluginManager->implementsActions(Backend::SET_DISPLAYNAME)) { |
|
510 | + return $this->userPluginManager->setDisplayName($uid, $displayName); |
|
511 | + } |
|
512 | + return false; |
|
513 | + } |
|
514 | + |
|
515 | + /** |
|
516 | + * Get a list of all display names |
|
517 | + * |
|
518 | + * @param string $search |
|
519 | + * @param string|null $limit |
|
520 | + * @param string|null $offset |
|
521 | + * @return array an array of all displayNames (value) and the corresponding uids (key) |
|
522 | + */ |
|
523 | + public function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
524 | + $cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset; |
|
525 | + if(!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) { |
|
526 | + return $displayNames; |
|
527 | + } |
|
528 | + |
|
529 | + $displayNames = array(); |
|
530 | + $users = $this->getUsers($search, $limit, $offset); |
|
531 | + foreach ($users as $user) { |
|
532 | + $displayNames[$user] = $this->getDisplayName($user); |
|
533 | + } |
|
534 | + $this->access->connection->writeToCache($cacheKey, $displayNames); |
|
535 | + return $displayNames; |
|
536 | + } |
|
537 | + |
|
538 | + /** |
|
539 | + * Check if backend implements actions |
|
540 | + * @param int $actions bitwise-or'ed actions |
|
541 | + * @return boolean |
|
542 | + * |
|
543 | + * Returns the supported actions as int to be |
|
544 | + * compared with \OC\User\Backend::CREATE_USER etc. |
|
545 | + */ |
|
546 | + public function implementsActions($actions) { |
|
547 | + return (bool)((Backend::CHECK_PASSWORD |
|
548 | + | Backend::GET_HOME |
|
549 | + | Backend::GET_DISPLAYNAME |
|
550 | + | Backend::PROVIDE_AVATAR |
|
551 | + | Backend::COUNT_USERS |
|
552 | + | (((int)$this->access->connection->turnOnPasswordChange === 1)? Backend::SET_PASSWORD :0) |
|
553 | + | $this->userPluginManager->getImplementedActions()) |
|
554 | + & $actions); |
|
555 | + } |
|
556 | + |
|
557 | + /** |
|
558 | + * @return bool |
|
559 | + */ |
|
560 | + public function hasUserListings() { |
|
561 | + return true; |
|
562 | + } |
|
563 | + |
|
564 | + /** |
|
565 | + * counts the users in LDAP |
|
566 | + * |
|
567 | + * @return int|bool |
|
568 | + */ |
|
569 | + public function countUsers() { |
|
570 | + if ($this->userPluginManager->implementsActions(Backend::COUNT_USERS)) { |
|
571 | + return $this->userPluginManager->countUsers(); |
|
572 | + } |
|
573 | + |
|
574 | + $filter = $this->access->getFilterForUserCount(); |
|
575 | + $cacheKey = 'countUsers-'.$filter; |
|
576 | + if(!is_null($entries = $this->access->connection->getFromCache($cacheKey))) { |
|
577 | + return $entries; |
|
578 | + } |
|
579 | + $entries = $this->access->countUsers($filter); |
|
580 | + $this->access->connection->writeToCache($cacheKey, $entries); |
|
581 | + return $entries; |
|
582 | + } |
|
583 | + |
|
584 | + /** |
|
585 | + * Backend name to be shown in user management |
|
586 | + * @return string the name of the backend to be shown |
|
587 | + */ |
|
588 | + public function getBackendName(){ |
|
589 | + return 'LDAP'; |
|
590 | + } |
|
591 | 591 | |
592 | - /** |
|
593 | - * Return access for LDAP interaction. |
|
594 | - * @param string $uid |
|
595 | - * @return Access instance of Access for LDAP interaction |
|
596 | - */ |
|
597 | - public function getLDAPAccess($uid) { |
|
598 | - return $this->access; |
|
599 | - } |
|
592 | + /** |
|
593 | + * Return access for LDAP interaction. |
|
594 | + * @param string $uid |
|
595 | + * @return Access instance of Access for LDAP interaction |
|
596 | + */ |
|
597 | + public function getLDAPAccess($uid) { |
|
598 | + return $this->access; |
|
599 | + } |
|
600 | 600 | |
601 | - /** |
|
602 | - * Return LDAP connection resource from a cloned connection. |
|
603 | - * The cloned connection needs to be closed manually. |
|
604 | - * of the current access. |
|
605 | - * @param string $uid |
|
606 | - * @return resource of the LDAP connection |
|
607 | - */ |
|
608 | - public function getNewLDAPConnection($uid) { |
|
609 | - $connection = clone $this->access->getConnection(); |
|
610 | - return $connection->getConnectionResource(); |
|
611 | - } |
|
612 | - |
|
613 | - /** |
|
614 | - * create new user |
|
615 | - * @param string $username username of the new user |
|
616 | - * @param string $password password of the new user |
|
617 | - * @return bool was the user created? |
|
618 | - */ |
|
619 | - public function createUser($username, $password) { |
|
620 | - if ($this->userPluginManager->implementsActions(Backend::CREATE_USER)) { |
|
621 | - return $this->userPluginManager->createUser($username, $password); |
|
622 | - } |
|
623 | - return false; |
|
624 | - } |
|
601 | + /** |
|
602 | + * Return LDAP connection resource from a cloned connection. |
|
603 | + * The cloned connection needs to be closed manually. |
|
604 | + * of the current access. |
|
605 | + * @param string $uid |
|
606 | + * @return resource of the LDAP connection |
|
607 | + */ |
|
608 | + public function getNewLDAPConnection($uid) { |
|
609 | + $connection = clone $this->access->getConnection(); |
|
610 | + return $connection->getConnectionResource(); |
|
611 | + } |
|
612 | + |
|
613 | + /** |
|
614 | + * create new user |
|
615 | + * @param string $username username of the new user |
|
616 | + * @param string $password password of the new user |
|
617 | + * @return bool was the user created? |
|
618 | + */ |
|
619 | + public function createUser($username, $password) { |
|
620 | + if ($this->userPluginManager->implementsActions(Backend::CREATE_USER)) { |
|
621 | + return $this->userPluginManager->createUser($username, $password); |
|
622 | + } |
|
623 | + return false; |
|
624 | + } |
|
625 | 625 | |
626 | 626 | } |
@@ -29,293 +29,293 @@ |
||
29 | 29 | * @package OCA\User_LDAP\Mapping |
30 | 30 | */ |
31 | 31 | abstract class AbstractMapping { |
32 | - /** |
|
33 | - * @var \OCP\IDBConnection $dbc |
|
34 | - */ |
|
35 | - protected $dbc; |
|
32 | + /** |
|
33 | + * @var \OCP\IDBConnection $dbc |
|
34 | + */ |
|
35 | + protected $dbc; |
|
36 | 36 | |
37 | - /** |
|
38 | - * returns the DB table name which holds the mappings |
|
39 | - * @return string |
|
40 | - */ |
|
41 | - abstract protected function getTableName(); |
|
37 | + /** |
|
38 | + * returns the DB table name which holds the mappings |
|
39 | + * @return string |
|
40 | + */ |
|
41 | + abstract protected function getTableName(); |
|
42 | 42 | |
43 | - /** |
|
44 | - * @param \OCP\IDBConnection $dbc |
|
45 | - */ |
|
46 | - public function __construct(\OCP\IDBConnection $dbc) { |
|
47 | - $this->dbc = $dbc; |
|
48 | - } |
|
43 | + /** |
|
44 | + * @param \OCP\IDBConnection $dbc |
|
45 | + */ |
|
46 | + public function __construct(\OCP\IDBConnection $dbc) { |
|
47 | + $this->dbc = $dbc; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * checks whether a provided string represents an existing table col |
|
52 | - * @param string $col |
|
53 | - * @return bool |
|
54 | - */ |
|
55 | - public function isColNameValid($col) { |
|
56 | - switch($col) { |
|
57 | - case 'ldap_dn': |
|
58 | - case 'owncloud_name': |
|
59 | - case 'directory_uuid': |
|
60 | - return true; |
|
61 | - default: |
|
62 | - return false; |
|
63 | - } |
|
64 | - } |
|
50 | + /** |
|
51 | + * checks whether a provided string represents an existing table col |
|
52 | + * @param string $col |
|
53 | + * @return bool |
|
54 | + */ |
|
55 | + public function isColNameValid($col) { |
|
56 | + switch($col) { |
|
57 | + case 'ldap_dn': |
|
58 | + case 'owncloud_name': |
|
59 | + case 'directory_uuid': |
|
60 | + return true; |
|
61 | + default: |
|
62 | + return false; |
|
63 | + } |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Gets the value of one column based on a provided value of another column |
|
68 | - * @param string $fetchCol |
|
69 | - * @param string $compareCol |
|
70 | - * @param string $search |
|
71 | - * @throws \Exception |
|
72 | - * @return string|false |
|
73 | - */ |
|
74 | - protected function getXbyY($fetchCol, $compareCol, $search) { |
|
75 | - if(!$this->isColNameValid($fetchCol)) { |
|
76 | - //this is used internally only, but we don't want to risk |
|
77 | - //having SQL injection at all. |
|
78 | - throw new \Exception('Invalid Column Name'); |
|
79 | - } |
|
80 | - $query = $this->dbc->prepare(' |
|
66 | + /** |
|
67 | + * Gets the value of one column based on a provided value of another column |
|
68 | + * @param string $fetchCol |
|
69 | + * @param string $compareCol |
|
70 | + * @param string $search |
|
71 | + * @throws \Exception |
|
72 | + * @return string|false |
|
73 | + */ |
|
74 | + protected function getXbyY($fetchCol, $compareCol, $search) { |
|
75 | + if(!$this->isColNameValid($fetchCol)) { |
|
76 | + //this is used internally only, but we don't want to risk |
|
77 | + //having SQL injection at all. |
|
78 | + throw new \Exception('Invalid Column Name'); |
|
79 | + } |
|
80 | + $query = $this->dbc->prepare(' |
|
81 | 81 | SELECT `' . $fetchCol . '` |
82 | 82 | FROM `'. $this->getTableName() .'` |
83 | 83 | WHERE `' . $compareCol . '` = ? |
84 | 84 | '); |
85 | 85 | |
86 | - $res = $query->execute(array($search)); |
|
87 | - if($res !== false) { |
|
88 | - return $query->fetchColumn(); |
|
89 | - } |
|
86 | + $res = $query->execute(array($search)); |
|
87 | + if($res !== false) { |
|
88 | + return $query->fetchColumn(); |
|
89 | + } |
|
90 | 90 | |
91 | - return false; |
|
92 | - } |
|
91 | + return false; |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * Performs a DELETE or UPDATE query to the database. |
|
96 | - * @param \Doctrine\DBAL\Driver\Statement $query |
|
97 | - * @param array $parameters |
|
98 | - * @return bool true if at least one row was modified, false otherwise |
|
99 | - */ |
|
100 | - protected function modify($query, $parameters) { |
|
101 | - $result = $query->execute($parameters); |
|
102 | - return ($result === true && $query->rowCount() > 0); |
|
103 | - } |
|
94 | + /** |
|
95 | + * Performs a DELETE or UPDATE query to the database. |
|
96 | + * @param \Doctrine\DBAL\Driver\Statement $query |
|
97 | + * @param array $parameters |
|
98 | + * @return bool true if at least one row was modified, false otherwise |
|
99 | + */ |
|
100 | + protected function modify($query, $parameters) { |
|
101 | + $result = $query->execute($parameters); |
|
102 | + return ($result === true && $query->rowCount() > 0); |
|
103 | + } |
|
104 | 104 | |
105 | - /** |
|
106 | - * Gets the LDAP DN based on the provided name. |
|
107 | - * Replaces Access::ocname2dn |
|
108 | - * @param string $name |
|
109 | - * @return string|false |
|
110 | - */ |
|
111 | - public function getDNByName($name) { |
|
112 | - return $this->getXbyY('ldap_dn', 'owncloud_name', $name); |
|
113 | - } |
|
105 | + /** |
|
106 | + * Gets the LDAP DN based on the provided name. |
|
107 | + * Replaces Access::ocname2dn |
|
108 | + * @param string $name |
|
109 | + * @return string|false |
|
110 | + */ |
|
111 | + public function getDNByName($name) { |
|
112 | + return $this->getXbyY('ldap_dn', 'owncloud_name', $name); |
|
113 | + } |
|
114 | 114 | |
115 | - /** |
|
116 | - * Updates the DN based on the given UUID |
|
117 | - * @param string $fdn |
|
118 | - * @param string $uuid |
|
119 | - * @return bool |
|
120 | - */ |
|
121 | - public function setDNbyUUID($fdn, $uuid) { |
|
122 | - $query = $this->dbc->prepare(' |
|
115 | + /** |
|
116 | + * Updates the DN based on the given UUID |
|
117 | + * @param string $fdn |
|
118 | + * @param string $uuid |
|
119 | + * @return bool |
|
120 | + */ |
|
121 | + public function setDNbyUUID($fdn, $uuid) { |
|
122 | + $query = $this->dbc->prepare(' |
|
123 | 123 | UPDATE `' . $this->getTableName() . '` |
124 | 124 | SET `ldap_dn` = ? |
125 | 125 | WHERE `directory_uuid` = ? |
126 | 126 | '); |
127 | 127 | |
128 | - return $this->modify($query, array($fdn, $uuid)); |
|
129 | - } |
|
128 | + return $this->modify($query, array($fdn, $uuid)); |
|
129 | + } |
|
130 | 130 | |
131 | - /** |
|
132 | - * Updates the UUID based on the given DN |
|
133 | - * |
|
134 | - * required by Migration/UUIDFix |
|
135 | - * |
|
136 | - * @param $uuid |
|
137 | - * @param $fdn |
|
138 | - * @return bool |
|
139 | - */ |
|
140 | - public function setUUIDbyDN($uuid, $fdn) { |
|
141 | - $query = $this->dbc->prepare(' |
|
131 | + /** |
|
132 | + * Updates the UUID based on the given DN |
|
133 | + * |
|
134 | + * required by Migration/UUIDFix |
|
135 | + * |
|
136 | + * @param $uuid |
|
137 | + * @param $fdn |
|
138 | + * @return bool |
|
139 | + */ |
|
140 | + public function setUUIDbyDN($uuid, $fdn) { |
|
141 | + $query = $this->dbc->prepare(' |
|
142 | 142 | UPDATE `' . $this->getTableName() . '` |
143 | 143 | SET `directory_uuid` = ? |
144 | 144 | WHERE `ldap_dn` = ? |
145 | 145 | '); |
146 | 146 | |
147 | - return $this->modify($query, [$uuid, $fdn]); |
|
148 | - } |
|
147 | + return $this->modify($query, [$uuid, $fdn]); |
|
148 | + } |
|
149 | 149 | |
150 | - /** |
|
151 | - * Gets the name based on the provided LDAP DN. |
|
152 | - * @param string $fdn |
|
153 | - * @return string|false |
|
154 | - */ |
|
155 | - public function getNameByDN($fdn) { |
|
156 | - return $this->getXbyY('owncloud_name', 'ldap_dn', $fdn); |
|
157 | - } |
|
150 | + /** |
|
151 | + * Gets the name based on the provided LDAP DN. |
|
152 | + * @param string $fdn |
|
153 | + * @return string|false |
|
154 | + */ |
|
155 | + public function getNameByDN($fdn) { |
|
156 | + return $this->getXbyY('owncloud_name', 'ldap_dn', $fdn); |
|
157 | + } |
|
158 | 158 | |
159 | - /** |
|
160 | - * Searches mapped names by the giving string in the name column |
|
161 | - * @param string $search |
|
162 | - * @param string $prefixMatch |
|
163 | - * @param string $postfixMatch |
|
164 | - * @return string[] |
|
165 | - */ |
|
166 | - public function getNamesBySearch($search, $prefixMatch = "", $postfixMatch = "") { |
|
167 | - $query = $this->dbc->prepare(' |
|
159 | + /** |
|
160 | + * Searches mapped names by the giving string in the name column |
|
161 | + * @param string $search |
|
162 | + * @param string $prefixMatch |
|
163 | + * @param string $postfixMatch |
|
164 | + * @return string[] |
|
165 | + */ |
|
166 | + public function getNamesBySearch($search, $prefixMatch = "", $postfixMatch = "") { |
|
167 | + $query = $this->dbc->prepare(' |
|
168 | 168 | SELECT `owncloud_name` |
169 | 169 | FROM `'. $this->getTableName() .'` |
170 | 170 | WHERE `owncloud_name` LIKE ? |
171 | 171 | '); |
172 | 172 | |
173 | - $res = $query->execute(array($prefixMatch.$this->dbc->escapeLikeParameter($search).$postfixMatch)); |
|
174 | - $names = array(); |
|
175 | - if($res !== false) { |
|
176 | - while($row = $query->fetch()) { |
|
177 | - $names[] = $row['owncloud_name']; |
|
178 | - } |
|
179 | - } |
|
180 | - return $names; |
|
181 | - } |
|
173 | + $res = $query->execute(array($prefixMatch.$this->dbc->escapeLikeParameter($search).$postfixMatch)); |
|
174 | + $names = array(); |
|
175 | + if($res !== false) { |
|
176 | + while($row = $query->fetch()) { |
|
177 | + $names[] = $row['owncloud_name']; |
|
178 | + } |
|
179 | + } |
|
180 | + return $names; |
|
181 | + } |
|
182 | 182 | |
183 | - /** |
|
184 | - * Gets the name based on the provided LDAP UUID. |
|
185 | - * @param string $uuid |
|
186 | - * @return string|false |
|
187 | - */ |
|
188 | - public function getNameByUUID($uuid) { |
|
189 | - return $this->getXbyY('owncloud_name', 'directory_uuid', $uuid); |
|
190 | - } |
|
183 | + /** |
|
184 | + * Gets the name based on the provided LDAP UUID. |
|
185 | + * @param string $uuid |
|
186 | + * @return string|false |
|
187 | + */ |
|
188 | + public function getNameByUUID($uuid) { |
|
189 | + return $this->getXbyY('owncloud_name', 'directory_uuid', $uuid); |
|
190 | + } |
|
191 | 191 | |
192 | - /** |
|
193 | - * Gets the UUID based on the provided LDAP DN |
|
194 | - * @param string $dn |
|
195 | - * @return false|string |
|
196 | - * @throws \Exception |
|
197 | - */ |
|
198 | - public function getUUIDByDN($dn) { |
|
199 | - return $this->getXbyY('directory_uuid', 'ldap_dn', $dn); |
|
200 | - } |
|
192 | + /** |
|
193 | + * Gets the UUID based on the provided LDAP DN |
|
194 | + * @param string $dn |
|
195 | + * @return false|string |
|
196 | + * @throws \Exception |
|
197 | + */ |
|
198 | + public function getUUIDByDN($dn) { |
|
199 | + return $this->getXbyY('directory_uuid', 'ldap_dn', $dn); |
|
200 | + } |
|
201 | 201 | |
202 | - /** |
|
203 | - * gets a piece of the mapping list |
|
204 | - * @param int $offset |
|
205 | - * @param int $limit |
|
206 | - * @return array |
|
207 | - */ |
|
208 | - public function getList($offset = null, $limit = null) { |
|
209 | - $query = $this->dbc->prepare(' |
|
202 | + /** |
|
203 | + * gets a piece of the mapping list |
|
204 | + * @param int $offset |
|
205 | + * @param int $limit |
|
206 | + * @return array |
|
207 | + */ |
|
208 | + public function getList($offset = null, $limit = null) { |
|
209 | + $query = $this->dbc->prepare(' |
|
210 | 210 | SELECT |
211 | 211 | `ldap_dn` AS `dn`, |
212 | 212 | `owncloud_name` AS `name`, |
213 | 213 | `directory_uuid` AS `uuid` |
214 | 214 | FROM `' . $this->getTableName() . '`', |
215 | - $limit, |
|
216 | - $offset |
|
217 | - ); |
|
215 | + $limit, |
|
216 | + $offset |
|
217 | + ); |
|
218 | 218 | |
219 | - $query->execute(); |
|
220 | - return $query->fetchAll(); |
|
221 | - } |
|
219 | + $query->execute(); |
|
220 | + return $query->fetchAll(); |
|
221 | + } |
|
222 | 222 | |
223 | - /** |
|
224 | - * attempts to map the given entry |
|
225 | - * @param string $fdn fully distinguished name (from LDAP) |
|
226 | - * @param string $name |
|
227 | - * @param string $uuid a unique identifier as used in LDAP |
|
228 | - * @return bool |
|
229 | - */ |
|
230 | - public function map($fdn, $name, $uuid) { |
|
231 | - if(mb_strlen($fdn) > 255) { |
|
232 | - \OC::$server->getLogger()->error( |
|
233 | - 'Cannot map, because the DN exceeds 255 characters: {dn}', |
|
234 | - [ |
|
235 | - 'app' => 'user_ldap', |
|
236 | - 'dn' => $fdn, |
|
237 | - ] |
|
238 | - ); |
|
239 | - return false; |
|
240 | - } |
|
223 | + /** |
|
224 | + * attempts to map the given entry |
|
225 | + * @param string $fdn fully distinguished name (from LDAP) |
|
226 | + * @param string $name |
|
227 | + * @param string $uuid a unique identifier as used in LDAP |
|
228 | + * @return bool |
|
229 | + */ |
|
230 | + public function map($fdn, $name, $uuid) { |
|
231 | + if(mb_strlen($fdn) > 255) { |
|
232 | + \OC::$server->getLogger()->error( |
|
233 | + 'Cannot map, because the DN exceeds 255 characters: {dn}', |
|
234 | + [ |
|
235 | + 'app' => 'user_ldap', |
|
236 | + 'dn' => $fdn, |
|
237 | + ] |
|
238 | + ); |
|
239 | + return false; |
|
240 | + } |
|
241 | 241 | |
242 | - $row = array( |
|
243 | - 'ldap_dn' => $fdn, |
|
244 | - 'owncloud_name' => $name, |
|
245 | - 'directory_uuid' => $uuid |
|
246 | - ); |
|
242 | + $row = array( |
|
243 | + 'ldap_dn' => $fdn, |
|
244 | + 'owncloud_name' => $name, |
|
245 | + 'directory_uuid' => $uuid |
|
246 | + ); |
|
247 | 247 | |
248 | - try { |
|
249 | - $result = $this->dbc->insertIfNotExist($this->getTableName(), $row); |
|
250 | - // insertIfNotExist returns values as int |
|
251 | - return (bool)$result; |
|
252 | - } catch (\Exception $e) { |
|
253 | - return false; |
|
254 | - } |
|
255 | - } |
|
248 | + try { |
|
249 | + $result = $this->dbc->insertIfNotExist($this->getTableName(), $row); |
|
250 | + // insertIfNotExist returns values as int |
|
251 | + return (bool)$result; |
|
252 | + } catch (\Exception $e) { |
|
253 | + return false; |
|
254 | + } |
|
255 | + } |
|
256 | 256 | |
257 | - /** |
|
258 | - * removes a mapping based on the owncloud_name of the entry |
|
259 | - * @param string $name |
|
260 | - * @return bool |
|
261 | - */ |
|
262 | - public function unmap($name) { |
|
263 | - $query = $this->dbc->prepare(' |
|
257 | + /** |
|
258 | + * removes a mapping based on the owncloud_name of the entry |
|
259 | + * @param string $name |
|
260 | + * @return bool |
|
261 | + */ |
|
262 | + public function unmap($name) { |
|
263 | + $query = $this->dbc->prepare(' |
|
264 | 264 | DELETE FROM `'. $this->getTableName() .'` |
265 | 265 | WHERE `owncloud_name` = ?'); |
266 | 266 | |
267 | - return $this->modify($query, array($name)); |
|
268 | - } |
|
267 | + return $this->modify($query, array($name)); |
|
268 | + } |
|
269 | 269 | |
270 | - /** |
|
271 | - * Truncate's the mapping table |
|
272 | - * @return bool |
|
273 | - */ |
|
274 | - public function clear() { |
|
275 | - $sql = $this->dbc |
|
276 | - ->getDatabasePlatform() |
|
277 | - ->getTruncateTableSQL('`' . $this->getTableName() . '`'); |
|
278 | - return $this->dbc->prepare($sql)->execute(); |
|
279 | - } |
|
270 | + /** |
|
271 | + * Truncate's the mapping table |
|
272 | + * @return bool |
|
273 | + */ |
|
274 | + public function clear() { |
|
275 | + $sql = $this->dbc |
|
276 | + ->getDatabasePlatform() |
|
277 | + ->getTruncateTableSQL('`' . $this->getTableName() . '`'); |
|
278 | + return $this->dbc->prepare($sql)->execute(); |
|
279 | + } |
|
280 | 280 | |
281 | - /** |
|
282 | - * clears the mapping table one by one and executing a callback with |
|
283 | - * each row's id (=owncloud_name col) |
|
284 | - * |
|
285 | - * @param callable $preCallback |
|
286 | - * @param callable $postCallback |
|
287 | - * @return bool true on success, false when at least one row was not |
|
288 | - * deleted |
|
289 | - */ |
|
290 | - public function clearCb(Callable $preCallback, Callable $postCallback): bool { |
|
291 | - $picker = $this->dbc->getQueryBuilder(); |
|
292 | - $picker->select('owncloud_name') |
|
293 | - ->from($this->getTableName()); |
|
294 | - $cursor = $picker->execute(); |
|
295 | - $result = true; |
|
296 | - while($id = $cursor->fetchColumn(0)) { |
|
297 | - $preCallback($id); |
|
298 | - if($isUnmapped = $this->unmap($id)) { |
|
299 | - $postCallback($id); |
|
300 | - } |
|
301 | - $result &= $isUnmapped; |
|
302 | - } |
|
303 | - $cursor->closeCursor(); |
|
304 | - return $result; |
|
305 | - } |
|
281 | + /** |
|
282 | + * clears the mapping table one by one and executing a callback with |
|
283 | + * each row's id (=owncloud_name col) |
|
284 | + * |
|
285 | + * @param callable $preCallback |
|
286 | + * @param callable $postCallback |
|
287 | + * @return bool true on success, false when at least one row was not |
|
288 | + * deleted |
|
289 | + */ |
|
290 | + public function clearCb(Callable $preCallback, Callable $postCallback): bool { |
|
291 | + $picker = $this->dbc->getQueryBuilder(); |
|
292 | + $picker->select('owncloud_name') |
|
293 | + ->from($this->getTableName()); |
|
294 | + $cursor = $picker->execute(); |
|
295 | + $result = true; |
|
296 | + while($id = $cursor->fetchColumn(0)) { |
|
297 | + $preCallback($id); |
|
298 | + if($isUnmapped = $this->unmap($id)) { |
|
299 | + $postCallback($id); |
|
300 | + } |
|
301 | + $result &= $isUnmapped; |
|
302 | + } |
|
303 | + $cursor->closeCursor(); |
|
304 | + return $result; |
|
305 | + } |
|
306 | 306 | |
307 | - /** |
|
308 | - * returns the number of entries in the mappings table |
|
309 | - * |
|
310 | - * @return int |
|
311 | - */ |
|
312 | - public function count() { |
|
313 | - $qb = $this->dbc->getQueryBuilder(); |
|
314 | - $query = $qb->select($qb->createFunction('COUNT(`ldap_dn`)')) |
|
315 | - ->from($this->getTableName()); |
|
316 | - $res = $query->execute(); |
|
317 | - $count = $res->fetchColumn(); |
|
318 | - $res->closeCursor(); |
|
319 | - return (int)$count; |
|
320 | - } |
|
307 | + /** |
|
308 | + * returns the number of entries in the mappings table |
|
309 | + * |
|
310 | + * @return int |
|
311 | + */ |
|
312 | + public function count() { |
|
313 | + $qb = $this->dbc->getQueryBuilder(); |
|
314 | + $query = $qb->select($qb->createFunction('COUNT(`ldap_dn`)')) |
|
315 | + ->from($this->getTableName()); |
|
316 | + $res = $query->execute(); |
|
317 | + $count = $res->fetchColumn(); |
|
318 | + $res->closeCursor(); |
|
319 | + return (int)$count; |
|
320 | + } |
|
321 | 321 | } |
@@ -35,95 +35,95 @@ |
||
35 | 35 | * @package OCA\AdminAudit\Actions |
36 | 36 | */ |
37 | 37 | class UserManagement extends Action { |
38 | - /** |
|
39 | - * Log creation of users |
|
40 | - * |
|
41 | - * @param array $params |
|
42 | - */ |
|
43 | - public function create(array $params) { |
|
44 | - $this->log( |
|
45 | - 'User created: "%s"', |
|
46 | - $params, |
|
47 | - [ |
|
48 | - 'uid', |
|
49 | - ] |
|
50 | - ); |
|
51 | - } |
|
38 | + /** |
|
39 | + * Log creation of users |
|
40 | + * |
|
41 | + * @param array $params |
|
42 | + */ |
|
43 | + public function create(array $params) { |
|
44 | + $this->log( |
|
45 | + 'User created: "%s"', |
|
46 | + $params, |
|
47 | + [ |
|
48 | + 'uid', |
|
49 | + ] |
|
50 | + ); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Log assignments of users (typically user backends) |
|
55 | - * |
|
56 | - * @param string $uid |
|
57 | - */ |
|
58 | - public function announce(string $uid) { |
|
59 | - $this->log( |
|
60 | - 'UserID assgined: "%s"', |
|
61 | - [ 'uid' => $uid ], |
|
62 | - [ 'uid' ] |
|
63 | - ); |
|
64 | - } |
|
53 | + /** |
|
54 | + * Log assignments of users (typically user backends) |
|
55 | + * |
|
56 | + * @param string $uid |
|
57 | + */ |
|
58 | + public function announce(string $uid) { |
|
59 | + $this->log( |
|
60 | + 'UserID assgined: "%s"', |
|
61 | + [ 'uid' => $uid ], |
|
62 | + [ 'uid' ] |
|
63 | + ); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Log deletion of users |
|
68 | - * |
|
69 | - * @param array $params |
|
70 | - */ |
|
71 | - public function delete(array $params) { |
|
72 | - $this->log( |
|
73 | - 'User deleted: "%s"', |
|
74 | - $params, |
|
75 | - [ |
|
76 | - 'uid', |
|
77 | - ] |
|
78 | - ); |
|
79 | - } |
|
66 | + /** |
|
67 | + * Log deletion of users |
|
68 | + * |
|
69 | + * @param array $params |
|
70 | + */ |
|
71 | + public function delete(array $params) { |
|
72 | + $this->log( |
|
73 | + 'User deleted: "%s"', |
|
74 | + $params, |
|
75 | + [ |
|
76 | + 'uid', |
|
77 | + ] |
|
78 | + ); |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * Log unassignments of users (typically user backends, no data removed) |
|
83 | - * |
|
84 | - * @param string $uid |
|
85 | - */ |
|
86 | - public function revoke(string $uid) { |
|
87 | - $this->log( |
|
88 | - 'UserID unassigned: "%s"', |
|
89 | - [ 'uid' => $uid ], |
|
90 | - [ 'uid' ] |
|
91 | - ); |
|
92 | - } |
|
81 | + /** |
|
82 | + * Log unassignments of users (typically user backends, no data removed) |
|
83 | + * |
|
84 | + * @param string $uid |
|
85 | + */ |
|
86 | + public function revoke(string $uid) { |
|
87 | + $this->log( |
|
88 | + 'UserID unassigned: "%s"', |
|
89 | + [ 'uid' => $uid ], |
|
90 | + [ 'uid' ] |
|
91 | + ); |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * Log enabling of users |
|
96 | - * |
|
97 | - * @param array $params |
|
98 | - */ |
|
99 | - public function change(array $params) { |
|
100 | - if ($params['feature'] === 'enabled') { |
|
101 | - $this->log( |
|
102 | - $params['value'] === 'true' ? 'User enabled: "%s"' : 'User disabled: "%s"', |
|
103 | - ['user' => $params['user']->getUID()], |
|
104 | - [ |
|
105 | - 'user', |
|
106 | - ] |
|
107 | - ); |
|
108 | - } |
|
109 | - } |
|
94 | + /** |
|
95 | + * Log enabling of users |
|
96 | + * |
|
97 | + * @param array $params |
|
98 | + */ |
|
99 | + public function change(array $params) { |
|
100 | + if ($params['feature'] === 'enabled') { |
|
101 | + $this->log( |
|
102 | + $params['value'] === 'true' ? 'User enabled: "%s"' : 'User disabled: "%s"', |
|
103 | + ['user' => $params['user']->getUID()], |
|
104 | + [ |
|
105 | + 'user', |
|
106 | + ] |
|
107 | + ); |
|
108 | + } |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * Logs changing of the user scope |
|
113 | - * |
|
114 | - * @param IUser $user |
|
115 | - */ |
|
116 | - public function setPassword(IUser $user) { |
|
117 | - if($user->getBackendClassName() === 'Database') { |
|
118 | - $this->log( |
|
119 | - 'Password of user "%s" has been changed', |
|
120 | - [ |
|
121 | - 'user' => $user->getUID(), |
|
122 | - ], |
|
123 | - [ |
|
124 | - 'user', |
|
125 | - ] |
|
126 | - ); |
|
127 | - } |
|
128 | - } |
|
111 | + /** |
|
112 | + * Logs changing of the user scope |
|
113 | + * |
|
114 | + * @param IUser $user |
|
115 | + */ |
|
116 | + public function setPassword(IUser $user) { |
|
117 | + if($user->getBackendClassName() === 'Database') { |
|
118 | + $this->log( |
|
119 | + 'Password of user "%s" has been changed', |
|
120 | + [ |
|
121 | + 'user' => $user->getUID(), |
|
122 | + ], |
|
123 | + [ |
|
124 | + 'user', |
|
125 | + ] |
|
126 | + ); |
|
127 | + } |
|
128 | + } |
|
129 | 129 | } |
@@ -53,189 +53,189 @@ |
||
53 | 53 | |
54 | 54 | class Application extends App { |
55 | 55 | |
56 | - public function __construct() { |
|
57 | - parent::__construct('admin_audit'); |
|
58 | - } |
|
59 | - |
|
60 | - public function register() { |
|
61 | - $this->registerHooks(); |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Register hooks in order to log them |
|
66 | - */ |
|
67 | - protected function registerHooks() { |
|
68 | - $logger = $this->getContainer()->getServer()->getLogger(); |
|
69 | - |
|
70 | - $this->userManagementHooks($logger); |
|
71 | - $this->groupHooks($logger); |
|
72 | - $this->authHooks($logger); |
|
73 | - |
|
74 | - $this->consoleHooks($logger); |
|
75 | - $this->appHooks($logger); |
|
76 | - |
|
77 | - $this->sharingHooks($logger); |
|
78 | - |
|
79 | - $this->fileHooks($logger); |
|
80 | - $this->trashbinHooks($logger); |
|
81 | - $this->versionsHooks($logger); |
|
82 | - |
|
83 | - $this->securityHooks($logger); |
|
84 | - } |
|
85 | - |
|
86 | - protected function userManagementHooks(ILogger $logger) { |
|
87 | - $userActions = new UserManagement($logger); |
|
88 | - |
|
89 | - Util::connectHook('OC_User', 'post_createUser', $userActions, 'create'); |
|
90 | - Util::connectHook('OC_User', 'post_deleteUser', $userActions, 'delete'); |
|
91 | - Util::connectHook('OC_User', 'changeUser', $userActions, 'change'); |
|
92 | - |
|
93 | - /** @var IUserSession|Session $userSession */ |
|
94 | - $userSession = $this->getContainer()->getServer()->getUserSession(); |
|
95 | - $userSession->listen('\OC\User', 'postSetPassword', [$userActions, 'setPassword']); |
|
96 | - $userSession->listen('\OC\User', 'assignedUserId', [$userActions, 'announce']); |
|
97 | - $userSession->listen('\OC\User', 'postUnassignedUserId', [$userActions, 'revoke']); |
|
98 | - } |
|
99 | - |
|
100 | - protected function groupHooks(ILogger $logger) { |
|
101 | - $groupActions = new GroupManagement($logger); |
|
102 | - |
|
103 | - /** @var IGroupManager|Manager $groupManager */ |
|
104 | - $groupManager = $this->getContainer()->getServer()->getGroupManager(); |
|
105 | - $groupManager->listen('\OC\Group', 'postRemoveUser', [$groupActions, 'removeUser']); |
|
106 | - $groupManager->listen('\OC\Group', 'postAddUser', [$groupActions, 'addUser']); |
|
107 | - $groupManager->listen('\OC\Group', 'postDelete', [$groupActions, 'deleteGroup']); |
|
108 | - $groupManager->listen('\OC\Group', 'postCreate', [$groupActions, 'createGroup']); |
|
109 | - } |
|
110 | - |
|
111 | - protected function sharingHooks(ILogger $logger) { |
|
112 | - $shareActions = new Sharing($logger); |
|
113 | - |
|
114 | - Util::connectHook(Share::class, 'post_shared', $shareActions, 'shared'); |
|
115 | - Util::connectHook(Share::class, 'post_unshare', $shareActions, 'unshare'); |
|
116 | - Util::connectHook(Share::class, 'post_update_permissions', $shareActions, 'updatePermissions'); |
|
117 | - Util::connectHook(Share::class, 'post_update_password', $shareActions, 'updatePassword'); |
|
118 | - Util::connectHook(Share::class, 'post_set_expiration_date', $shareActions, 'updateExpirationDate'); |
|
119 | - Util::connectHook(Share::class, 'share_link_access', $shareActions, 'shareAccessed'); |
|
120 | - } |
|
121 | - |
|
122 | - protected function authHooks(ILogger $logger) { |
|
123 | - $authActions = new Auth($logger); |
|
124 | - |
|
125 | - Util::connectHook('OC_User', 'pre_login', $authActions, 'loginAttempt'); |
|
126 | - Util::connectHook('OC_User', 'post_login', $authActions, 'loginSuccessful'); |
|
127 | - Util::connectHook('OC_User', 'logout', $authActions, 'logout'); |
|
128 | - } |
|
129 | - |
|
130 | - protected function appHooks(ILogger $logger) { |
|
131 | - |
|
132 | - $eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher(); |
|
133 | - $eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE, function(ManagerEvent $event) use ($logger) { |
|
134 | - $appActions = new AppManagement($logger); |
|
135 | - $appActions->enableApp($event->getAppID()); |
|
136 | - }); |
|
137 | - $eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, function(ManagerEvent $event) use ($logger) { |
|
138 | - $appActions = new AppManagement($logger); |
|
139 | - $appActions->enableAppForGroups($event->getAppID(), $event->getGroups()); |
|
140 | - }); |
|
141 | - $eventDispatcher->addListener(ManagerEvent::EVENT_APP_DISABLE, function(ManagerEvent $event) use ($logger) { |
|
142 | - $appActions = new AppManagement($logger); |
|
143 | - $appActions->disableApp($event->getAppID()); |
|
144 | - }); |
|
145 | - |
|
146 | - } |
|
147 | - |
|
148 | - protected function consoleHooks(ILogger $logger) { |
|
149 | - $eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher(); |
|
150 | - $eventDispatcher->addListener(ConsoleEvent::EVENT_RUN, function(ConsoleEvent $event) use ($logger) { |
|
151 | - $appActions = new Console($logger); |
|
152 | - $appActions->runCommand($event->getArguments()); |
|
153 | - }); |
|
154 | - } |
|
155 | - |
|
156 | - protected function fileHooks(ILogger $logger) { |
|
157 | - $fileActions = new Files($logger); |
|
158 | - $eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher(); |
|
159 | - $eventDispatcher->addListener( |
|
160 | - IPreview::EVENT, |
|
161 | - function(GenericEvent $event) use ($fileActions) { |
|
162 | - /** @var File $file */ |
|
163 | - $file = $event->getSubject(); |
|
164 | - $fileActions->preview([ |
|
165 | - 'path' => substr($file->getInternalPath(), 5), |
|
166 | - 'width' => $event->getArguments()['width'], |
|
167 | - 'height' => $event->getArguments()['height'], |
|
168 | - 'crop' => $event->getArguments()['crop'], |
|
169 | - 'mode' => $event->getArguments()['mode'] |
|
170 | - ]); |
|
171 | - } |
|
172 | - ); |
|
173 | - |
|
174 | - Util::connectHook( |
|
175 | - Filesystem::CLASSNAME, |
|
176 | - Filesystem::signal_post_rename, |
|
177 | - $fileActions, |
|
178 | - 'rename' |
|
179 | - ); |
|
180 | - Util::connectHook( |
|
181 | - Filesystem::CLASSNAME, |
|
182 | - Filesystem::signal_post_create, |
|
183 | - $fileActions, |
|
184 | - 'create' |
|
185 | - ); |
|
186 | - Util::connectHook( |
|
187 | - Filesystem::CLASSNAME, |
|
188 | - Filesystem::signal_post_copy, |
|
189 | - $fileActions, |
|
190 | - 'copy' |
|
191 | - ); |
|
192 | - Util::connectHook( |
|
193 | - Filesystem::CLASSNAME, |
|
194 | - Filesystem::signal_post_write, |
|
195 | - $fileActions, |
|
196 | - 'write' |
|
197 | - ); |
|
198 | - Util::connectHook( |
|
199 | - Filesystem::CLASSNAME, |
|
200 | - Filesystem::signal_post_update, |
|
201 | - $fileActions, |
|
202 | - 'update' |
|
203 | - ); |
|
204 | - Util::connectHook( |
|
205 | - Filesystem::CLASSNAME, |
|
206 | - Filesystem::signal_read, |
|
207 | - $fileActions, |
|
208 | - 'read' |
|
209 | - ); |
|
210 | - Util::connectHook( |
|
211 | - Filesystem::CLASSNAME, |
|
212 | - Filesystem::signal_delete, |
|
213 | - $fileActions, |
|
214 | - 'delete' |
|
215 | - ); |
|
216 | - } |
|
217 | - |
|
218 | - protected function versionsHooks(ILogger $logger) { |
|
219 | - $versionsActions = new Versions($logger); |
|
220 | - Util::connectHook('\OCP\Versions', 'rollback', $versionsActions, 'rollback'); |
|
221 | - Util::connectHook('\OCP\Versions', 'delete',$versionsActions, 'delete'); |
|
222 | - } |
|
223 | - |
|
224 | - protected function trashbinHooks(ILogger $logger) { |
|
225 | - $trashActions = new Trashbin($logger); |
|
226 | - Util::connectHook('\OCP\Trashbin', 'preDelete', $trashActions, 'delete'); |
|
227 | - Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', $trashActions, 'restore'); |
|
228 | - } |
|
229 | - |
|
230 | - protected function securityHooks(ILogger $logger) { |
|
231 | - $eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher(); |
|
232 | - $eventDispatcher->addListener(IProvider::EVENT_SUCCESS, function(GenericEvent $event) use ($logger) { |
|
233 | - $security = new Security($logger); |
|
234 | - $security->twofactorSuccess($event->getSubject(), $event->getArguments()); |
|
235 | - }); |
|
236 | - $eventDispatcher->addListener(IProvider::EVENT_FAILED, function(GenericEvent $event) use ($logger) { |
|
237 | - $security = new Security($logger); |
|
238 | - $security->twofactorFailed($event->getSubject(), $event->getArguments()); |
|
239 | - }); |
|
240 | - } |
|
56 | + public function __construct() { |
|
57 | + parent::__construct('admin_audit'); |
|
58 | + } |
|
59 | + |
|
60 | + public function register() { |
|
61 | + $this->registerHooks(); |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Register hooks in order to log them |
|
66 | + */ |
|
67 | + protected function registerHooks() { |
|
68 | + $logger = $this->getContainer()->getServer()->getLogger(); |
|
69 | + |
|
70 | + $this->userManagementHooks($logger); |
|
71 | + $this->groupHooks($logger); |
|
72 | + $this->authHooks($logger); |
|
73 | + |
|
74 | + $this->consoleHooks($logger); |
|
75 | + $this->appHooks($logger); |
|
76 | + |
|
77 | + $this->sharingHooks($logger); |
|
78 | + |
|
79 | + $this->fileHooks($logger); |
|
80 | + $this->trashbinHooks($logger); |
|
81 | + $this->versionsHooks($logger); |
|
82 | + |
|
83 | + $this->securityHooks($logger); |
|
84 | + } |
|
85 | + |
|
86 | + protected function userManagementHooks(ILogger $logger) { |
|
87 | + $userActions = new UserManagement($logger); |
|
88 | + |
|
89 | + Util::connectHook('OC_User', 'post_createUser', $userActions, 'create'); |
|
90 | + Util::connectHook('OC_User', 'post_deleteUser', $userActions, 'delete'); |
|
91 | + Util::connectHook('OC_User', 'changeUser', $userActions, 'change'); |
|
92 | + |
|
93 | + /** @var IUserSession|Session $userSession */ |
|
94 | + $userSession = $this->getContainer()->getServer()->getUserSession(); |
|
95 | + $userSession->listen('\OC\User', 'postSetPassword', [$userActions, 'setPassword']); |
|
96 | + $userSession->listen('\OC\User', 'assignedUserId', [$userActions, 'announce']); |
|
97 | + $userSession->listen('\OC\User', 'postUnassignedUserId', [$userActions, 'revoke']); |
|
98 | + } |
|
99 | + |
|
100 | + protected function groupHooks(ILogger $logger) { |
|
101 | + $groupActions = new GroupManagement($logger); |
|
102 | + |
|
103 | + /** @var IGroupManager|Manager $groupManager */ |
|
104 | + $groupManager = $this->getContainer()->getServer()->getGroupManager(); |
|
105 | + $groupManager->listen('\OC\Group', 'postRemoveUser', [$groupActions, 'removeUser']); |
|
106 | + $groupManager->listen('\OC\Group', 'postAddUser', [$groupActions, 'addUser']); |
|
107 | + $groupManager->listen('\OC\Group', 'postDelete', [$groupActions, 'deleteGroup']); |
|
108 | + $groupManager->listen('\OC\Group', 'postCreate', [$groupActions, 'createGroup']); |
|
109 | + } |
|
110 | + |
|
111 | + protected function sharingHooks(ILogger $logger) { |
|
112 | + $shareActions = new Sharing($logger); |
|
113 | + |
|
114 | + Util::connectHook(Share::class, 'post_shared', $shareActions, 'shared'); |
|
115 | + Util::connectHook(Share::class, 'post_unshare', $shareActions, 'unshare'); |
|
116 | + Util::connectHook(Share::class, 'post_update_permissions', $shareActions, 'updatePermissions'); |
|
117 | + Util::connectHook(Share::class, 'post_update_password', $shareActions, 'updatePassword'); |
|
118 | + Util::connectHook(Share::class, 'post_set_expiration_date', $shareActions, 'updateExpirationDate'); |
|
119 | + Util::connectHook(Share::class, 'share_link_access', $shareActions, 'shareAccessed'); |
|
120 | + } |
|
121 | + |
|
122 | + protected function authHooks(ILogger $logger) { |
|
123 | + $authActions = new Auth($logger); |
|
124 | + |
|
125 | + Util::connectHook('OC_User', 'pre_login', $authActions, 'loginAttempt'); |
|
126 | + Util::connectHook('OC_User', 'post_login', $authActions, 'loginSuccessful'); |
|
127 | + Util::connectHook('OC_User', 'logout', $authActions, 'logout'); |
|
128 | + } |
|
129 | + |
|
130 | + protected function appHooks(ILogger $logger) { |
|
131 | + |
|
132 | + $eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher(); |
|
133 | + $eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE, function(ManagerEvent $event) use ($logger) { |
|
134 | + $appActions = new AppManagement($logger); |
|
135 | + $appActions->enableApp($event->getAppID()); |
|
136 | + }); |
|
137 | + $eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, function(ManagerEvent $event) use ($logger) { |
|
138 | + $appActions = new AppManagement($logger); |
|
139 | + $appActions->enableAppForGroups($event->getAppID(), $event->getGroups()); |
|
140 | + }); |
|
141 | + $eventDispatcher->addListener(ManagerEvent::EVENT_APP_DISABLE, function(ManagerEvent $event) use ($logger) { |
|
142 | + $appActions = new AppManagement($logger); |
|
143 | + $appActions->disableApp($event->getAppID()); |
|
144 | + }); |
|
145 | + |
|
146 | + } |
|
147 | + |
|
148 | + protected function consoleHooks(ILogger $logger) { |
|
149 | + $eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher(); |
|
150 | + $eventDispatcher->addListener(ConsoleEvent::EVENT_RUN, function(ConsoleEvent $event) use ($logger) { |
|
151 | + $appActions = new Console($logger); |
|
152 | + $appActions->runCommand($event->getArguments()); |
|
153 | + }); |
|
154 | + } |
|
155 | + |
|
156 | + protected function fileHooks(ILogger $logger) { |
|
157 | + $fileActions = new Files($logger); |
|
158 | + $eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher(); |
|
159 | + $eventDispatcher->addListener( |
|
160 | + IPreview::EVENT, |
|
161 | + function(GenericEvent $event) use ($fileActions) { |
|
162 | + /** @var File $file */ |
|
163 | + $file = $event->getSubject(); |
|
164 | + $fileActions->preview([ |
|
165 | + 'path' => substr($file->getInternalPath(), 5), |
|
166 | + 'width' => $event->getArguments()['width'], |
|
167 | + 'height' => $event->getArguments()['height'], |
|
168 | + 'crop' => $event->getArguments()['crop'], |
|
169 | + 'mode' => $event->getArguments()['mode'] |
|
170 | + ]); |
|
171 | + } |
|
172 | + ); |
|
173 | + |
|
174 | + Util::connectHook( |
|
175 | + Filesystem::CLASSNAME, |
|
176 | + Filesystem::signal_post_rename, |
|
177 | + $fileActions, |
|
178 | + 'rename' |
|
179 | + ); |
|
180 | + Util::connectHook( |
|
181 | + Filesystem::CLASSNAME, |
|
182 | + Filesystem::signal_post_create, |
|
183 | + $fileActions, |
|
184 | + 'create' |
|
185 | + ); |
|
186 | + Util::connectHook( |
|
187 | + Filesystem::CLASSNAME, |
|
188 | + Filesystem::signal_post_copy, |
|
189 | + $fileActions, |
|
190 | + 'copy' |
|
191 | + ); |
|
192 | + Util::connectHook( |
|
193 | + Filesystem::CLASSNAME, |
|
194 | + Filesystem::signal_post_write, |
|
195 | + $fileActions, |
|
196 | + 'write' |
|
197 | + ); |
|
198 | + Util::connectHook( |
|
199 | + Filesystem::CLASSNAME, |
|
200 | + Filesystem::signal_post_update, |
|
201 | + $fileActions, |
|
202 | + 'update' |
|
203 | + ); |
|
204 | + Util::connectHook( |
|
205 | + Filesystem::CLASSNAME, |
|
206 | + Filesystem::signal_read, |
|
207 | + $fileActions, |
|
208 | + 'read' |
|
209 | + ); |
|
210 | + Util::connectHook( |
|
211 | + Filesystem::CLASSNAME, |
|
212 | + Filesystem::signal_delete, |
|
213 | + $fileActions, |
|
214 | + 'delete' |
|
215 | + ); |
|
216 | + } |
|
217 | + |
|
218 | + protected function versionsHooks(ILogger $logger) { |
|
219 | + $versionsActions = new Versions($logger); |
|
220 | + Util::connectHook('\OCP\Versions', 'rollback', $versionsActions, 'rollback'); |
|
221 | + Util::connectHook('\OCP\Versions', 'delete',$versionsActions, 'delete'); |
|
222 | + } |
|
223 | + |
|
224 | + protected function trashbinHooks(ILogger $logger) { |
|
225 | + $trashActions = new Trashbin($logger); |
|
226 | + Util::connectHook('\OCP\Trashbin', 'preDelete', $trashActions, 'delete'); |
|
227 | + Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', $trashActions, 'restore'); |
|
228 | + } |
|
229 | + |
|
230 | + protected function securityHooks(ILogger $logger) { |
|
231 | + $eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher(); |
|
232 | + $eventDispatcher->addListener(IProvider::EVENT_SUCCESS, function(GenericEvent $event) use ($logger) { |
|
233 | + $security = new Security($logger); |
|
234 | + $security->twofactorSuccess($event->getSubject(), $event->getArguments()); |
|
235 | + }); |
|
236 | + $eventDispatcher->addListener(IProvider::EVENT_FAILED, function(GenericEvent $event) use ($logger) { |
|
237 | + $security = new Security($logger); |
|
238 | + $security->twofactorFailed($event->getSubject(), $event->getArguments()); |
|
239 | + }); |
|
240 | + } |
|
241 | 241 | } |
@@ -34,26 +34,26 @@ |
||
34 | 34 | $subject = (string)$_POST['ldap_clear_mapping']; |
35 | 35 | $mapping = null; |
36 | 36 | try { |
37 | - if($subject === 'user') { |
|
38 | - $mapping = new UserMapping(\OC::$server->getDatabaseConnection()); |
|
39 | - $result = $mapping->clearCb( |
|
40 | - function ($uid) { |
|
41 | - \OC::$server->getUserManager()->emit('\OC\User', 'preUnassignedUserId', [$uid]); |
|
42 | - }, |
|
43 | - function ($uid) { |
|
44 | - \OC::$server->getUserManager()->emit('\OC\User', 'postUnassignedUserId', [$uid]); |
|
45 | - } |
|
46 | - ); |
|
47 | - } else if($subject === 'group') { |
|
48 | - $mapping = new GroupMapping(\OC::$server->getDatabaseConnection()); |
|
49 | - $result = $mapping->clear(); |
|
50 | - } |
|
37 | + if($subject === 'user') { |
|
38 | + $mapping = new UserMapping(\OC::$server->getDatabaseConnection()); |
|
39 | + $result = $mapping->clearCb( |
|
40 | + function ($uid) { |
|
41 | + \OC::$server->getUserManager()->emit('\OC\User', 'preUnassignedUserId', [$uid]); |
|
42 | + }, |
|
43 | + function ($uid) { |
|
44 | + \OC::$server->getUserManager()->emit('\OC\User', 'postUnassignedUserId', [$uid]); |
|
45 | + } |
|
46 | + ); |
|
47 | + } else if($subject === 'group') { |
|
48 | + $mapping = new GroupMapping(\OC::$server->getDatabaseConnection()); |
|
49 | + $result = $mapping->clear(); |
|
50 | + } |
|
51 | 51 | |
52 | - if($mapping === null || !$result) { |
|
53 | - $l = \OC::$server->getL10N('user_ldap'); |
|
54 | - throw new \Exception($l->t('Failed to clear the mappings.')); |
|
55 | - } |
|
56 | - OCP\JSON::success(); |
|
52 | + if($mapping === null || !$result) { |
|
53 | + $l = \OC::$server->getL10N('user_ldap'); |
|
54 | + throw new \Exception($l->t('Failed to clear the mappings.')); |
|
55 | + } |
|
56 | + OCP\JSON::success(); |
|
57 | 57 | } catch (\Exception $e) { |
58 | - OCP\JSON::error(array('message' => $e->getMessage())); |
|
58 | + OCP\JSON::error(array('message' => $e->getMessage())); |
|
59 | 59 | } |
@@ -29,39 +29,39 @@ |
||
29 | 29 | use OCP\IUserManager; |
30 | 30 | |
31 | 31 | class AccessFactory { |
32 | - /** @var ILDAPWrapper */ |
|
33 | - protected $ldap; |
|
34 | - /** @var Manager */ |
|
35 | - protected $userManager; |
|
36 | - /** @var Helper */ |
|
37 | - protected $helper; |
|
38 | - /** @var IConfig */ |
|
39 | - protected $config; |
|
40 | - /** @var IUserManager */ |
|
41 | - private $ncUserManager; |
|
32 | + /** @var ILDAPWrapper */ |
|
33 | + protected $ldap; |
|
34 | + /** @var Manager */ |
|
35 | + protected $userManager; |
|
36 | + /** @var Helper */ |
|
37 | + protected $helper; |
|
38 | + /** @var IConfig */ |
|
39 | + protected $config; |
|
40 | + /** @var IUserManager */ |
|
41 | + private $ncUserManager; |
|
42 | 42 | |
43 | - public function __construct( |
|
44 | - ILDAPWrapper $ldap, |
|
45 | - Manager $userManager, |
|
46 | - Helper $helper, |
|
47 | - IConfig $config, |
|
48 | - IUserManager $ncUserManager) |
|
49 | - { |
|
50 | - $this->ldap = $ldap; |
|
51 | - $this->userManager = $userManager; |
|
52 | - $this->helper = $helper; |
|
53 | - $this->config = $config; |
|
54 | - $this->ncUserManager = $ncUserManager; |
|
55 | - } |
|
43 | + public function __construct( |
|
44 | + ILDAPWrapper $ldap, |
|
45 | + Manager $userManager, |
|
46 | + Helper $helper, |
|
47 | + IConfig $config, |
|
48 | + IUserManager $ncUserManager) |
|
49 | + { |
|
50 | + $this->ldap = $ldap; |
|
51 | + $this->userManager = $userManager; |
|
52 | + $this->helper = $helper; |
|
53 | + $this->config = $config; |
|
54 | + $this->ncUserManager = $ncUserManager; |
|
55 | + } |
|
56 | 56 | |
57 | - public function get(Connection $connection) { |
|
58 | - return new Access( |
|
59 | - $connection, |
|
60 | - $this->ldap, |
|
61 | - $this->userManager, |
|
62 | - $this->helper, |
|
63 | - $this->config, |
|
64 | - $this->ncUserManager |
|
65 | - ); |
|
66 | - } |
|
57 | + public function get(Connection $connection) { |
|
58 | + return new Access( |
|
59 | + $connection, |
|
60 | + $this->ldap, |
|
61 | + $this->userManager, |
|
62 | + $this->helper, |
|
63 | + $this->config, |
|
64 | + $this->ncUserManager |
|
65 | + ); |
|
66 | + } |
|
67 | 67 | } |
@@ -37,170 +37,170 @@ |
||
37 | 37 | use OCA\User_LDAP\User\Manager; |
38 | 38 | |
39 | 39 | abstract class Proxy { |
40 | - static private $accesses = array(); |
|
41 | - private $ldap = null; |
|
42 | - |
|
43 | - /** @var \OCP\ICache|null */ |
|
44 | - private $cache; |
|
45 | - |
|
46 | - /** |
|
47 | - * @param ILDAPWrapper $ldap |
|
48 | - */ |
|
49 | - public function __construct(ILDAPWrapper $ldap) { |
|
50 | - $this->ldap = $ldap; |
|
51 | - $memcache = \OC::$server->getMemCacheFactory(); |
|
52 | - if($memcache->isAvailable()) { |
|
53 | - $this->cache = $memcache->createDistributed(); |
|
54 | - } |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * @param string $configPrefix |
|
59 | - */ |
|
60 | - private function addAccess($configPrefix) { |
|
61 | - static $ocConfig; |
|
62 | - static $fs; |
|
63 | - static $log; |
|
64 | - static $avatarM; |
|
65 | - static $userMap; |
|
66 | - static $groupMap; |
|
67 | - static $db; |
|
68 | - static $coreUserManager; |
|
69 | - static $coreNotificationManager; |
|
70 | - if($fs === null) { |
|
71 | - $ocConfig = \OC::$server->getConfig(); |
|
72 | - $fs = new FilesystemHelper(); |
|
73 | - $log = new LogWrapper(); |
|
74 | - $avatarM = \OC::$server->getAvatarManager(); |
|
75 | - $db = \OC::$server->getDatabaseConnection(); |
|
76 | - $userMap = new UserMapping($db); |
|
77 | - $groupMap = new GroupMapping($db); |
|
78 | - $coreUserManager = \OC::$server->getUserManager(); |
|
79 | - $coreNotificationManager = \OC::$server->getNotificationManager(); |
|
80 | - } |
|
81 | - $userManager = |
|
82 | - new Manager($ocConfig, $fs, $log, $avatarM, new \OCP\Image(), $db, |
|
83 | - $coreUserManager, $coreNotificationManager); |
|
84 | - $connector = new Connection($this->ldap, $configPrefix); |
|
85 | - $access = new Access($connector, $this->ldap, $userManager, new Helper($ocConfig), $ocConfig, $coreUserManager); |
|
86 | - $access->setUserMapper($userMap); |
|
87 | - $access->setGroupMapper($groupMap); |
|
88 | - self::$accesses[$configPrefix] = $access; |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * @param string $configPrefix |
|
93 | - * @return mixed |
|
94 | - */ |
|
95 | - protected function getAccess($configPrefix) { |
|
96 | - if(!isset(self::$accesses[$configPrefix])) { |
|
97 | - $this->addAccess($configPrefix); |
|
98 | - } |
|
99 | - return self::$accesses[$configPrefix]; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * @param string $uid |
|
104 | - * @return string |
|
105 | - */ |
|
106 | - protected function getUserCacheKey($uid) { |
|
107 | - return 'user-'.$uid.'-lastSeenOn'; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * @param string $gid |
|
112 | - * @return string |
|
113 | - */ |
|
114 | - protected function getGroupCacheKey($gid) { |
|
115 | - return 'group-'.$gid.'-lastSeenOn'; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * @param string $id |
|
120 | - * @param string $method |
|
121 | - * @param array $parameters |
|
122 | - * @param bool $passOnWhen |
|
123 | - * @return mixed |
|
124 | - */ |
|
125 | - abstract protected function callOnLastSeenOn($id, $method, $parameters, $passOnWhen); |
|
126 | - |
|
127 | - /** |
|
128 | - * @param string $id |
|
129 | - * @param string $method |
|
130 | - * @param array $parameters |
|
131 | - * @return mixed |
|
132 | - */ |
|
133 | - abstract protected function walkBackends($id, $method, $parameters); |
|
134 | - |
|
135 | - /** |
|
136 | - * @param string $id |
|
137 | - * @return Access |
|
138 | - */ |
|
139 | - abstract public function getLDAPAccess($id); |
|
140 | - |
|
141 | - /** |
|
142 | - * Takes care of the request to the User backend |
|
143 | - * @param string $id |
|
144 | - * @param string $method string, the method of the user backend that shall be called |
|
145 | - * @param array $parameters an array of parameters to be passed |
|
146 | - * @param bool $passOnWhen |
|
147 | - * @return mixed, the result of the specified method |
|
148 | - */ |
|
149 | - protected function handleRequest($id, $method, $parameters, $passOnWhen = false) { |
|
150 | - $result = $this->callOnLastSeenOn($id, $method, $parameters, $passOnWhen); |
|
151 | - if($result === $passOnWhen) { |
|
152 | - $result = $this->walkBackends($id, $method, $parameters); |
|
153 | - } |
|
154 | - return $result; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * @param string|null $key |
|
159 | - * @return string |
|
160 | - */ |
|
161 | - private function getCacheKey($key) { |
|
162 | - $prefix = 'LDAP-Proxy-'; |
|
163 | - if($key === null) { |
|
164 | - return $prefix; |
|
165 | - } |
|
166 | - return $prefix.hash('sha256', $key); |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * @param string $key |
|
171 | - * @return mixed|null |
|
172 | - */ |
|
173 | - public function getFromCache($key) { |
|
174 | - if($this->cache === null) { |
|
175 | - return null; |
|
176 | - } |
|
177 | - |
|
178 | - $key = $this->getCacheKey($key); |
|
179 | - $value = $this->cache->get($key); |
|
180 | - if ($value === null) { |
|
181 | - return null; |
|
182 | - } |
|
183 | - |
|
184 | - return json_decode(base64_decode($value)); |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * @param string $key |
|
189 | - * @param mixed $value |
|
190 | - */ |
|
191 | - public function writeToCache($key, $value) { |
|
192 | - if($this->cache === null) { |
|
193 | - return; |
|
194 | - } |
|
195 | - $key = $this->getCacheKey($key); |
|
196 | - $value = base64_encode(json_encode($value)); |
|
197 | - $this->cache->set($key, $value, 2592000); |
|
198 | - } |
|
199 | - |
|
200 | - public function clearCache() { |
|
201 | - if($this->cache === null) { |
|
202 | - return; |
|
203 | - } |
|
204 | - $this->cache->clear($this->getCacheKey(null)); |
|
205 | - } |
|
40 | + static private $accesses = array(); |
|
41 | + private $ldap = null; |
|
42 | + |
|
43 | + /** @var \OCP\ICache|null */ |
|
44 | + private $cache; |
|
45 | + |
|
46 | + /** |
|
47 | + * @param ILDAPWrapper $ldap |
|
48 | + */ |
|
49 | + public function __construct(ILDAPWrapper $ldap) { |
|
50 | + $this->ldap = $ldap; |
|
51 | + $memcache = \OC::$server->getMemCacheFactory(); |
|
52 | + if($memcache->isAvailable()) { |
|
53 | + $this->cache = $memcache->createDistributed(); |
|
54 | + } |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * @param string $configPrefix |
|
59 | + */ |
|
60 | + private function addAccess($configPrefix) { |
|
61 | + static $ocConfig; |
|
62 | + static $fs; |
|
63 | + static $log; |
|
64 | + static $avatarM; |
|
65 | + static $userMap; |
|
66 | + static $groupMap; |
|
67 | + static $db; |
|
68 | + static $coreUserManager; |
|
69 | + static $coreNotificationManager; |
|
70 | + if($fs === null) { |
|
71 | + $ocConfig = \OC::$server->getConfig(); |
|
72 | + $fs = new FilesystemHelper(); |
|
73 | + $log = new LogWrapper(); |
|
74 | + $avatarM = \OC::$server->getAvatarManager(); |
|
75 | + $db = \OC::$server->getDatabaseConnection(); |
|
76 | + $userMap = new UserMapping($db); |
|
77 | + $groupMap = new GroupMapping($db); |
|
78 | + $coreUserManager = \OC::$server->getUserManager(); |
|
79 | + $coreNotificationManager = \OC::$server->getNotificationManager(); |
|
80 | + } |
|
81 | + $userManager = |
|
82 | + new Manager($ocConfig, $fs, $log, $avatarM, new \OCP\Image(), $db, |
|
83 | + $coreUserManager, $coreNotificationManager); |
|
84 | + $connector = new Connection($this->ldap, $configPrefix); |
|
85 | + $access = new Access($connector, $this->ldap, $userManager, new Helper($ocConfig), $ocConfig, $coreUserManager); |
|
86 | + $access->setUserMapper($userMap); |
|
87 | + $access->setGroupMapper($groupMap); |
|
88 | + self::$accesses[$configPrefix] = $access; |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * @param string $configPrefix |
|
93 | + * @return mixed |
|
94 | + */ |
|
95 | + protected function getAccess($configPrefix) { |
|
96 | + if(!isset(self::$accesses[$configPrefix])) { |
|
97 | + $this->addAccess($configPrefix); |
|
98 | + } |
|
99 | + return self::$accesses[$configPrefix]; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * @param string $uid |
|
104 | + * @return string |
|
105 | + */ |
|
106 | + protected function getUserCacheKey($uid) { |
|
107 | + return 'user-'.$uid.'-lastSeenOn'; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * @param string $gid |
|
112 | + * @return string |
|
113 | + */ |
|
114 | + protected function getGroupCacheKey($gid) { |
|
115 | + return 'group-'.$gid.'-lastSeenOn'; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * @param string $id |
|
120 | + * @param string $method |
|
121 | + * @param array $parameters |
|
122 | + * @param bool $passOnWhen |
|
123 | + * @return mixed |
|
124 | + */ |
|
125 | + abstract protected function callOnLastSeenOn($id, $method, $parameters, $passOnWhen); |
|
126 | + |
|
127 | + /** |
|
128 | + * @param string $id |
|
129 | + * @param string $method |
|
130 | + * @param array $parameters |
|
131 | + * @return mixed |
|
132 | + */ |
|
133 | + abstract protected function walkBackends($id, $method, $parameters); |
|
134 | + |
|
135 | + /** |
|
136 | + * @param string $id |
|
137 | + * @return Access |
|
138 | + */ |
|
139 | + abstract public function getLDAPAccess($id); |
|
140 | + |
|
141 | + /** |
|
142 | + * Takes care of the request to the User backend |
|
143 | + * @param string $id |
|
144 | + * @param string $method string, the method of the user backend that shall be called |
|
145 | + * @param array $parameters an array of parameters to be passed |
|
146 | + * @param bool $passOnWhen |
|
147 | + * @return mixed, the result of the specified method |
|
148 | + */ |
|
149 | + protected function handleRequest($id, $method, $parameters, $passOnWhen = false) { |
|
150 | + $result = $this->callOnLastSeenOn($id, $method, $parameters, $passOnWhen); |
|
151 | + if($result === $passOnWhen) { |
|
152 | + $result = $this->walkBackends($id, $method, $parameters); |
|
153 | + } |
|
154 | + return $result; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * @param string|null $key |
|
159 | + * @return string |
|
160 | + */ |
|
161 | + private function getCacheKey($key) { |
|
162 | + $prefix = 'LDAP-Proxy-'; |
|
163 | + if($key === null) { |
|
164 | + return $prefix; |
|
165 | + } |
|
166 | + return $prefix.hash('sha256', $key); |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * @param string $key |
|
171 | + * @return mixed|null |
|
172 | + */ |
|
173 | + public function getFromCache($key) { |
|
174 | + if($this->cache === null) { |
|
175 | + return null; |
|
176 | + } |
|
177 | + |
|
178 | + $key = $this->getCacheKey($key); |
|
179 | + $value = $this->cache->get($key); |
|
180 | + if ($value === null) { |
|
181 | + return null; |
|
182 | + } |
|
183 | + |
|
184 | + return json_decode(base64_decode($value)); |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * @param string $key |
|
189 | + * @param mixed $value |
|
190 | + */ |
|
191 | + public function writeToCache($key, $value) { |
|
192 | + if($this->cache === null) { |
|
193 | + return; |
|
194 | + } |
|
195 | + $key = $this->getCacheKey($key); |
|
196 | + $value = base64_encode(json_encode($value)); |
|
197 | + $this->cache->set($key, $value, 2592000); |
|
198 | + } |
|
199 | + |
|
200 | + public function clearCache() { |
|
201 | + if($this->cache === null) { |
|
202 | + return; |
|
203 | + } |
|
204 | + $this->cache->clear($this->getCacheKey(null)); |
|
205 | + } |
|
206 | 206 | } |
@@ -45,183 +45,183 @@ |
||
45 | 45 | use OCA\User_LDAP\User\Manager; |
46 | 46 | |
47 | 47 | class UpdateGroups extends \OC\BackgroundJob\TimedJob { |
48 | - static private $groupsFromDB; |
|
49 | - |
|
50 | - static private $groupBE; |
|
51 | - |
|
52 | - public function __construct(){ |
|
53 | - $this->interval = self::getRefreshInterval(); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * @param mixed $argument |
|
58 | - */ |
|
59 | - public function run($argument){ |
|
60 | - self::updateGroups(); |
|
61 | - } |
|
62 | - |
|
63 | - static public function updateGroups() { |
|
64 | - \OCP\Util::writeLog('user_ldap', 'Run background job "updateGroups"', \OCP\Util::DEBUG); |
|
65 | - |
|
66 | - $knownGroups = array_keys(self::getKnownGroups()); |
|
67 | - $actualGroups = self::getGroupBE()->getGroups(); |
|
68 | - |
|
69 | - if(empty($actualGroups) && empty($knownGroups)) { |
|
70 | - \OCP\Util::writeLog('user_ldap', |
|
71 | - 'bgJ "updateGroups" – groups do not seem to be configured properly, aborting.', |
|
72 | - \OCP\Util::INFO); |
|
73 | - return; |
|
74 | - } |
|
75 | - |
|
76 | - self::handleKnownGroups(array_intersect($actualGroups, $knownGroups)); |
|
77 | - self::handleCreatedGroups(array_diff($actualGroups, $knownGroups)); |
|
78 | - self::handleRemovedGroups(array_diff($knownGroups, $actualGroups)); |
|
79 | - |
|
80 | - \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Finished.', \OCP\Util::DEBUG); |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * @return int |
|
85 | - */ |
|
86 | - static private function getRefreshInterval() { |
|
87 | - //defaults to every hour |
|
88 | - return \OC::$server->getConfig()->getAppValue('user_ldap', 'bgjRefreshInterval', 3600); |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * @param string[] $groups |
|
93 | - */ |
|
94 | - static private function handleKnownGroups($groups) { |
|
95 | - \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Dealing with known Groups.', \OCP\Util::DEBUG); |
|
96 | - $query = \OCP\DB::prepare(' |
|
48 | + static private $groupsFromDB; |
|
49 | + |
|
50 | + static private $groupBE; |
|
51 | + |
|
52 | + public function __construct(){ |
|
53 | + $this->interval = self::getRefreshInterval(); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * @param mixed $argument |
|
58 | + */ |
|
59 | + public function run($argument){ |
|
60 | + self::updateGroups(); |
|
61 | + } |
|
62 | + |
|
63 | + static public function updateGroups() { |
|
64 | + \OCP\Util::writeLog('user_ldap', 'Run background job "updateGroups"', \OCP\Util::DEBUG); |
|
65 | + |
|
66 | + $knownGroups = array_keys(self::getKnownGroups()); |
|
67 | + $actualGroups = self::getGroupBE()->getGroups(); |
|
68 | + |
|
69 | + if(empty($actualGroups) && empty($knownGroups)) { |
|
70 | + \OCP\Util::writeLog('user_ldap', |
|
71 | + 'bgJ "updateGroups" – groups do not seem to be configured properly, aborting.', |
|
72 | + \OCP\Util::INFO); |
|
73 | + return; |
|
74 | + } |
|
75 | + |
|
76 | + self::handleKnownGroups(array_intersect($actualGroups, $knownGroups)); |
|
77 | + self::handleCreatedGroups(array_diff($actualGroups, $knownGroups)); |
|
78 | + self::handleRemovedGroups(array_diff($knownGroups, $actualGroups)); |
|
79 | + |
|
80 | + \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Finished.', \OCP\Util::DEBUG); |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * @return int |
|
85 | + */ |
|
86 | + static private function getRefreshInterval() { |
|
87 | + //defaults to every hour |
|
88 | + return \OC::$server->getConfig()->getAppValue('user_ldap', 'bgjRefreshInterval', 3600); |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * @param string[] $groups |
|
93 | + */ |
|
94 | + static private function handleKnownGroups($groups) { |
|
95 | + \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Dealing with known Groups.', \OCP\Util::DEBUG); |
|
96 | + $query = \OCP\DB::prepare(' |
|
97 | 97 | UPDATE `*PREFIX*ldap_group_members` |
98 | 98 | SET `owncloudusers` = ? |
99 | 99 | WHERE `owncloudname` = ? |
100 | 100 | '); |
101 | - foreach($groups as $group) { |
|
102 | - //we assume, that self::$groupsFromDB has been retrieved already |
|
103 | - $knownUsers = unserialize(self::$groupsFromDB[$group]['owncloudusers']); |
|
104 | - $actualUsers = self::getGroupBE()->usersInGroup($group); |
|
105 | - $hasChanged = false; |
|
106 | - foreach(array_diff($knownUsers, $actualUsers) as $removedUser) { |
|
107 | - \OCP\Util::emitHook('OC_User', 'post_removeFromGroup', array('uid' => $removedUser, 'gid' => $group)); |
|
108 | - \OCP\Util::writeLog('user_ldap', |
|
109 | - 'bgJ "updateGroups" – "'.$removedUser.'" removed from "'.$group.'".', |
|
110 | - \OCP\Util::INFO); |
|
111 | - $hasChanged = true; |
|
112 | - } |
|
113 | - foreach(array_diff($actualUsers, $knownUsers) as $addedUser) { |
|
114 | - \OCP\Util::emitHook('OC_User', 'post_addToGroup', array('uid' => $addedUser, 'gid' => $group)); |
|
115 | - \OCP\Util::writeLog('user_ldap', |
|
116 | - 'bgJ "updateGroups" – "'.$addedUser.'" added to "'.$group.'".', |
|
117 | - \OCP\Util::INFO); |
|
118 | - $hasChanged = true; |
|
119 | - } |
|
120 | - if($hasChanged) { |
|
121 | - $query->execute(array(serialize($actualUsers), $group)); |
|
122 | - } |
|
123 | - } |
|
124 | - \OCP\Util::writeLog('user_ldap', |
|
125 | - 'bgJ "updateGroups" – FINISHED dealing with known Groups.', |
|
126 | - \OCP\Util::DEBUG); |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @param string[] $createdGroups |
|
131 | - */ |
|
132 | - static private function handleCreatedGroups($createdGroups) { |
|
133 | - \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with created Groups.', \OCP\Util::DEBUG); |
|
134 | - $query = \OCP\DB::prepare(' |
|
101 | + foreach($groups as $group) { |
|
102 | + //we assume, that self::$groupsFromDB has been retrieved already |
|
103 | + $knownUsers = unserialize(self::$groupsFromDB[$group]['owncloudusers']); |
|
104 | + $actualUsers = self::getGroupBE()->usersInGroup($group); |
|
105 | + $hasChanged = false; |
|
106 | + foreach(array_diff($knownUsers, $actualUsers) as $removedUser) { |
|
107 | + \OCP\Util::emitHook('OC_User', 'post_removeFromGroup', array('uid' => $removedUser, 'gid' => $group)); |
|
108 | + \OCP\Util::writeLog('user_ldap', |
|
109 | + 'bgJ "updateGroups" – "'.$removedUser.'" removed from "'.$group.'".', |
|
110 | + \OCP\Util::INFO); |
|
111 | + $hasChanged = true; |
|
112 | + } |
|
113 | + foreach(array_diff($actualUsers, $knownUsers) as $addedUser) { |
|
114 | + \OCP\Util::emitHook('OC_User', 'post_addToGroup', array('uid' => $addedUser, 'gid' => $group)); |
|
115 | + \OCP\Util::writeLog('user_ldap', |
|
116 | + 'bgJ "updateGroups" – "'.$addedUser.'" added to "'.$group.'".', |
|
117 | + \OCP\Util::INFO); |
|
118 | + $hasChanged = true; |
|
119 | + } |
|
120 | + if($hasChanged) { |
|
121 | + $query->execute(array(serialize($actualUsers), $group)); |
|
122 | + } |
|
123 | + } |
|
124 | + \OCP\Util::writeLog('user_ldap', |
|
125 | + 'bgJ "updateGroups" – FINISHED dealing with known Groups.', |
|
126 | + \OCP\Util::DEBUG); |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @param string[] $createdGroups |
|
131 | + */ |
|
132 | + static private function handleCreatedGroups($createdGroups) { |
|
133 | + \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with created Groups.', \OCP\Util::DEBUG); |
|
134 | + $query = \OCP\DB::prepare(' |
|
135 | 135 | INSERT |
136 | 136 | INTO `*PREFIX*ldap_group_members` (`owncloudname`, `owncloudusers`) |
137 | 137 | VALUES (?, ?) |
138 | 138 | '); |
139 | - foreach($createdGroups as $createdGroup) { |
|
140 | - \OCP\Util::writeLog('user_ldap', |
|
141 | - 'bgJ "updateGroups" – new group "'.$createdGroup.'" found.', |
|
142 | - \OCP\Util::INFO); |
|
143 | - $users = serialize(self::getGroupBE()->usersInGroup($createdGroup)); |
|
144 | - $query->execute(array($createdGroup, $users)); |
|
145 | - } |
|
146 | - \OCP\Util::writeLog('user_ldap', |
|
147 | - 'bgJ "updateGroups" – FINISHED dealing with created Groups.', |
|
148 | - \OCP\Util::DEBUG); |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * @param string[] $removedGroups |
|
153 | - */ |
|
154 | - static private function handleRemovedGroups($removedGroups) { |
|
155 | - \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with removed groups.', \OCP\Util::DEBUG); |
|
156 | - $query = \OCP\DB::prepare(' |
|
139 | + foreach($createdGroups as $createdGroup) { |
|
140 | + \OCP\Util::writeLog('user_ldap', |
|
141 | + 'bgJ "updateGroups" – new group "'.$createdGroup.'" found.', |
|
142 | + \OCP\Util::INFO); |
|
143 | + $users = serialize(self::getGroupBE()->usersInGroup($createdGroup)); |
|
144 | + $query->execute(array($createdGroup, $users)); |
|
145 | + } |
|
146 | + \OCP\Util::writeLog('user_ldap', |
|
147 | + 'bgJ "updateGroups" – FINISHED dealing with created Groups.', |
|
148 | + \OCP\Util::DEBUG); |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * @param string[] $removedGroups |
|
153 | + */ |
|
154 | + static private function handleRemovedGroups($removedGroups) { |
|
155 | + \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with removed groups.', \OCP\Util::DEBUG); |
|
156 | + $query = \OCP\DB::prepare(' |
|
157 | 157 | DELETE |
158 | 158 | FROM `*PREFIX*ldap_group_members` |
159 | 159 | WHERE `owncloudname` = ? |
160 | 160 | '); |
161 | - foreach($removedGroups as $removedGroup) { |
|
162 | - \OCP\Util::writeLog('user_ldap', |
|
163 | - 'bgJ "updateGroups" – group "'.$removedGroup.'" was removed.', |
|
164 | - \OCP\Util::INFO); |
|
165 | - $query->execute(array($removedGroup)); |
|
166 | - } |
|
167 | - \OCP\Util::writeLog('user_ldap', |
|
168 | - 'bgJ "updateGroups" – FINISHED dealing with removed groups.', |
|
169 | - \OCP\Util::DEBUG); |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * @return \OCA\User_LDAP\Group_LDAP|\OCA\User_LDAP\Group_Proxy |
|
174 | - */ |
|
175 | - static private function getGroupBE() { |
|
176 | - if(!is_null(self::$groupBE)) { |
|
177 | - return self::$groupBE; |
|
178 | - } |
|
179 | - $helper = new Helper(\OC::$server->getConfig()); |
|
180 | - $configPrefixes = $helper->getServerConfigurationPrefixes(true); |
|
181 | - $ldapWrapper = new LDAP(); |
|
182 | - if(count($configPrefixes) === 1) { |
|
183 | - //avoid the proxy when there is only one LDAP server configured |
|
184 | - $dbc = \OC::$server->getDatabaseConnection(); |
|
185 | - $userManager = new Manager( |
|
186 | - \OC::$server->getConfig(), |
|
187 | - new FilesystemHelper(), |
|
188 | - new LogWrapper(), |
|
189 | - \OC::$server->getAvatarManager(), |
|
190 | - new \OCP\Image(), |
|
191 | - $dbc, |
|
192 | - \OC::$server->getUserManager(), |
|
193 | - \OC::$server->getNotificationManager()); |
|
194 | - $connector = new Connection($ldapWrapper, $configPrefixes[0]); |
|
195 | - $ldapAccess = new Access($connector, $ldapWrapper, $userManager, $helper, \OC::$server->getConfig(), \OC::$server->getUserManager()); |
|
196 | - $groupMapper = new GroupMapping($dbc); |
|
197 | - $userMapper = new UserMapping($dbc); |
|
198 | - $ldapAccess->setGroupMapper($groupMapper); |
|
199 | - $ldapAccess->setUserMapper($userMapper); |
|
200 | - self::$groupBE = new \OCA\User_LDAP\Group_LDAP($ldapAccess, \OC::$server->query('LDAPGroupPluginManager')); |
|
201 | - } else { |
|
202 | - self::$groupBE = new \OCA\User_LDAP\Group_Proxy($configPrefixes, $ldapWrapper, \OC::$server->query('LDAPGroupPluginManager')); |
|
203 | - } |
|
204 | - |
|
205 | - return self::$groupBE; |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * @return array |
|
210 | - */ |
|
211 | - static private function getKnownGroups() { |
|
212 | - if(is_array(self::$groupsFromDB)) { |
|
213 | - return self::$groupsFromDB; |
|
214 | - } |
|
215 | - $query = \OCP\DB::prepare(' |
|
161 | + foreach($removedGroups as $removedGroup) { |
|
162 | + \OCP\Util::writeLog('user_ldap', |
|
163 | + 'bgJ "updateGroups" – group "'.$removedGroup.'" was removed.', |
|
164 | + \OCP\Util::INFO); |
|
165 | + $query->execute(array($removedGroup)); |
|
166 | + } |
|
167 | + \OCP\Util::writeLog('user_ldap', |
|
168 | + 'bgJ "updateGroups" – FINISHED dealing with removed groups.', |
|
169 | + \OCP\Util::DEBUG); |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * @return \OCA\User_LDAP\Group_LDAP|\OCA\User_LDAP\Group_Proxy |
|
174 | + */ |
|
175 | + static private function getGroupBE() { |
|
176 | + if(!is_null(self::$groupBE)) { |
|
177 | + return self::$groupBE; |
|
178 | + } |
|
179 | + $helper = new Helper(\OC::$server->getConfig()); |
|
180 | + $configPrefixes = $helper->getServerConfigurationPrefixes(true); |
|
181 | + $ldapWrapper = new LDAP(); |
|
182 | + if(count($configPrefixes) === 1) { |
|
183 | + //avoid the proxy when there is only one LDAP server configured |
|
184 | + $dbc = \OC::$server->getDatabaseConnection(); |
|
185 | + $userManager = new Manager( |
|
186 | + \OC::$server->getConfig(), |
|
187 | + new FilesystemHelper(), |
|
188 | + new LogWrapper(), |
|
189 | + \OC::$server->getAvatarManager(), |
|
190 | + new \OCP\Image(), |
|
191 | + $dbc, |
|
192 | + \OC::$server->getUserManager(), |
|
193 | + \OC::$server->getNotificationManager()); |
|
194 | + $connector = new Connection($ldapWrapper, $configPrefixes[0]); |
|
195 | + $ldapAccess = new Access($connector, $ldapWrapper, $userManager, $helper, \OC::$server->getConfig(), \OC::$server->getUserManager()); |
|
196 | + $groupMapper = new GroupMapping($dbc); |
|
197 | + $userMapper = new UserMapping($dbc); |
|
198 | + $ldapAccess->setGroupMapper($groupMapper); |
|
199 | + $ldapAccess->setUserMapper($userMapper); |
|
200 | + self::$groupBE = new \OCA\User_LDAP\Group_LDAP($ldapAccess, \OC::$server->query('LDAPGroupPluginManager')); |
|
201 | + } else { |
|
202 | + self::$groupBE = new \OCA\User_LDAP\Group_Proxy($configPrefixes, $ldapWrapper, \OC::$server->query('LDAPGroupPluginManager')); |
|
203 | + } |
|
204 | + |
|
205 | + return self::$groupBE; |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * @return array |
|
210 | + */ |
|
211 | + static private function getKnownGroups() { |
|
212 | + if(is_array(self::$groupsFromDB)) { |
|
213 | + return self::$groupsFromDB; |
|
214 | + } |
|
215 | + $query = \OCP\DB::prepare(' |
|
216 | 216 | SELECT `owncloudname`, `owncloudusers` |
217 | 217 | FROM `*PREFIX*ldap_group_members` |
218 | 218 | '); |
219 | - $result = $query->execute()->fetchAll(); |
|
220 | - self::$groupsFromDB = array(); |
|
221 | - foreach($result as $dataset) { |
|
222 | - self::$groupsFromDB[$dataset['owncloudname']] = $dataset; |
|
223 | - } |
|
224 | - |
|
225 | - return self::$groupsFromDB; |
|
226 | - } |
|
219 | + $result = $query->execute()->fetchAll(); |
|
220 | + self::$groupsFromDB = array(); |
|
221 | + foreach($result as $dataset) { |
|
222 | + self::$groupsFromDB[$dataset['owncloudname']] = $dataset; |
|
223 | + } |
|
224 | + |
|
225 | + return self::$groupsFromDB; |
|
226 | + } |
|
227 | 227 | } |
@@ -42,343 +42,343 @@ |
||
42 | 42 | use OCP\Notification\IManager; |
43 | 43 | |
44 | 44 | class Sync extends TimedJob { |
45 | - const MAX_INTERVAL = 12 * 60 * 60; // 12h |
|
46 | - const MIN_INTERVAL = 30 * 60; // 30min |
|
47 | - /** @var Helper */ |
|
48 | - protected $ldapHelper; |
|
49 | - /** @var LDAP */ |
|
50 | - protected $ldap; |
|
51 | - /** @var Manager */ |
|
52 | - protected $userManager; |
|
53 | - /** @var UserMapping */ |
|
54 | - protected $mapper; |
|
55 | - /** @var IConfig */ |
|
56 | - protected $config; |
|
57 | - /** @var IAvatarManager */ |
|
58 | - protected $avatarManager; |
|
59 | - /** @var IDBConnection */ |
|
60 | - protected $dbc; |
|
61 | - /** @var IUserManager */ |
|
62 | - protected $ncUserManager; |
|
63 | - /** @var IManager */ |
|
64 | - protected $notificationManager; |
|
65 | - /** @var ConnectionFactory */ |
|
66 | - protected $connectionFactory; |
|
67 | - /** @var AccessFactory */ |
|
68 | - protected $accessFactory; |
|
69 | - |
|
70 | - public function __construct() { |
|
71 | - $this->setInterval( |
|
72 | - \OC::$server->getConfig()->getAppValue( |
|
73 | - 'user_ldap', |
|
74 | - 'background_sync_interval', |
|
75 | - self::MIN_INTERVAL |
|
76 | - ) |
|
77 | - ); |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * updates the interval |
|
82 | - * |
|
83 | - * the idea is to adjust the interval depending on the amount of known users |
|
84 | - * and the attempt to update each user one day. At most it would run every |
|
85 | - * 30 minutes, and at least every 12 hours. |
|
86 | - */ |
|
87 | - public function updateInterval() { |
|
88 | - $minPagingSize = $this->getMinPagingSize(); |
|
89 | - $mappedUsers = $this->mapper->count(); |
|
90 | - |
|
91 | - $runsPerDay = ($minPagingSize === 0 || $mappedUsers === 0) ? self::MAX_INTERVAL |
|
92 | - : $mappedUsers / $minPagingSize; |
|
93 | - $interval = floor(24 * 60 * 60 / $runsPerDay); |
|
94 | - $interval = min(max($interval, self::MIN_INTERVAL), self::MAX_INTERVAL); |
|
95 | - |
|
96 | - $this->config->setAppValue('user_ldap', 'background_sync_interval', $interval); |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * returns the smallest configured paging size |
|
101 | - * @return int |
|
102 | - */ |
|
103 | - protected function getMinPagingSize() { |
|
104 | - $configKeys = $this->config->getAppKeys('user_ldap'); |
|
105 | - $configKeys = array_filter($configKeys, function($key) { |
|
106 | - return strpos($key, 'ldap_paging_size') !== false; |
|
107 | - }); |
|
108 | - $minPagingSize = null; |
|
109 | - foreach ($configKeys as $configKey) { |
|
110 | - $pagingSize = $this->config->getAppValue('user_ldap', $configKey, $minPagingSize); |
|
111 | - $minPagingSize = $minPagingSize === null ? $pagingSize : min($minPagingSize, $pagingSize); |
|
112 | - } |
|
113 | - return (int)$minPagingSize; |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * @param array $argument |
|
118 | - */ |
|
119 | - public function run($argument) { |
|
120 | - $this->setArgument($argument); |
|
121 | - |
|
122 | - $isBackgroundJobModeAjax = $this->config |
|
123 | - ->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'ajax'; |
|
124 | - if($isBackgroundJobModeAjax) { |
|
125 | - return; |
|
126 | - } |
|
127 | - |
|
128 | - $cycleData = $this->getCycle(); |
|
129 | - if($cycleData === null) { |
|
130 | - $cycleData = $this->determineNextCycle(); |
|
131 | - if($cycleData === null) { |
|
132 | - $this->updateInterval(); |
|
133 | - return; |
|
134 | - } |
|
135 | - } |
|
136 | - |
|
137 | - if(!$this->qualifiesToRun($cycleData)) { |
|
138 | - $this->updateInterval(); |
|
139 | - return; |
|
140 | - } |
|
141 | - |
|
142 | - try { |
|
143 | - $expectMoreResults = $this->runCycle($cycleData); |
|
144 | - if ($expectMoreResults) { |
|
145 | - $this->increaseOffset($cycleData); |
|
146 | - } else { |
|
147 | - $this->determineNextCycle($cycleData); |
|
148 | - } |
|
149 | - $this->updateInterval(); |
|
150 | - } catch (ServerNotAvailableException $e) { |
|
151 | - $this->determineNextCycle($cycleData); |
|
152 | - } |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * @param array $cycleData |
|
157 | - * @return bool whether more results are expected from the same configuration |
|
158 | - */ |
|
159 | - public function runCycle($cycleData) { |
|
160 | - $connection = $this->connectionFactory->get($cycleData['prefix']); |
|
161 | - $access = $this->accessFactory->get($connection); |
|
162 | - $access->setUserMapper($this->mapper); |
|
163 | - |
|
164 | - $filter = $access->combineFilterWithAnd(array( |
|
165 | - $access->connection->ldapUserFilter, |
|
166 | - $access->connection->ldapUserDisplayName . '=*', |
|
167 | - $access->getFilterPartForUserSearch('') |
|
168 | - )); |
|
169 | - $results = $access->fetchListOfUsers( |
|
170 | - $filter, |
|
171 | - $access->userManager->getAttributes(), |
|
172 | - $connection->ldapPagingSize, |
|
173 | - $cycleData['offset'], |
|
174 | - true |
|
175 | - ); |
|
176 | - |
|
177 | - if((int)$connection->ldapPagingSize === 0) { |
|
178 | - return false; |
|
179 | - } |
|
180 | - return count($results) >= (int)$connection->ldapPagingSize; |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * returns the info about the current cycle that should be run, if any, |
|
185 | - * otherwise null |
|
186 | - * |
|
187 | - * @return array|null |
|
188 | - */ |
|
189 | - public function getCycle() { |
|
190 | - $prefixes = $this->ldapHelper->getServerConfigurationPrefixes(true); |
|
191 | - if(count($prefixes) === 0) { |
|
192 | - return null; |
|
193 | - } |
|
194 | - |
|
195 | - $cycleData = [ |
|
196 | - 'prefix' => $this->config->getAppValue('user_ldap', 'background_sync_prefix', null), |
|
197 | - 'offset' => (int)$this->config->getAppValue('user_ldap', 'background_sync_offset', 0), |
|
198 | - ]; |
|
199 | - |
|
200 | - if( |
|
201 | - $cycleData['prefix'] !== null |
|
202 | - && in_array($cycleData['prefix'], $prefixes) |
|
203 | - ) { |
|
204 | - return $cycleData; |
|
205 | - } |
|
206 | - |
|
207 | - return null; |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * Save the provided cycle information in the DB |
|
212 | - * |
|
213 | - * @param array $cycleData |
|
214 | - */ |
|
215 | - public function setCycle(array $cycleData) { |
|
216 | - $this->config->setAppValue('user_ldap', 'background_sync_prefix', $cycleData['prefix']); |
|
217 | - $this->config->setAppValue('user_ldap', 'background_sync_offset', $cycleData['offset']); |
|
218 | - } |
|
219 | - |
|
220 | - /** |
|
221 | - * returns data about the next cycle that should run, if any, otherwise |
|
222 | - * null. It also always goes for the next LDAP configuration! |
|
223 | - * |
|
224 | - * @param array|null $cycleData the old cycle |
|
225 | - * @return array|null |
|
226 | - */ |
|
227 | - public function determineNextCycle(array $cycleData = null) { |
|
228 | - $prefixes = $this->ldapHelper->getServerConfigurationPrefixes(true); |
|
229 | - if(count($prefixes) === 0) { |
|
230 | - return null; |
|
231 | - } |
|
232 | - |
|
233 | - // get the next prefix in line and remember it |
|
234 | - $oldPrefix = $cycleData === null ? null : $cycleData['prefix']; |
|
235 | - $prefix = $this->getNextPrefix($oldPrefix); |
|
236 | - if($prefix === null) { |
|
237 | - return null; |
|
238 | - } |
|
239 | - $cycleData['prefix'] = $prefix; |
|
240 | - $cycleData['offset'] = 0; |
|
241 | - $this->setCycle(['prefix' => $prefix, 'offset' => 0]); |
|
242 | - |
|
243 | - return $cycleData; |
|
244 | - } |
|
245 | - |
|
246 | - /** |
|
247 | - * Checks whether the provided cycle should be run. Currently only the |
|
248 | - * last configuration change goes into account (at least one hour). |
|
249 | - * |
|
250 | - * @param $cycleData |
|
251 | - * @return bool |
|
252 | - */ |
|
253 | - public function qualifiesToRun($cycleData) { |
|
254 | - $lastChange = $this->config->getAppValue('user_ldap', $cycleData['prefix'] . '_lastChange', 0); |
|
255 | - if((time() - $lastChange) > 60 * 30) { |
|
256 | - return true; |
|
257 | - } |
|
258 | - return false; |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * increases the offset of the current cycle for the next run |
|
263 | - * |
|
264 | - * @param $cycleData |
|
265 | - */ |
|
266 | - protected function increaseOffset($cycleData) { |
|
267 | - $ldapConfig = new Configuration($cycleData['prefix']); |
|
268 | - $cycleData['offset'] += (int)$ldapConfig->ldapPagingSize; |
|
269 | - $this->setCycle($cycleData); |
|
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * determines the next configuration prefix based on the last one (if any) |
|
274 | - * |
|
275 | - * @param string|null $lastPrefix |
|
276 | - * @return string|null |
|
277 | - */ |
|
278 | - protected function getNextPrefix($lastPrefix) { |
|
279 | - $prefixes = $this->ldapHelper->getServerConfigurationPrefixes(true); |
|
280 | - $noOfPrefixes = count($prefixes); |
|
281 | - if($noOfPrefixes === 0) { |
|
282 | - return null; |
|
283 | - } |
|
284 | - $i = $lastPrefix === null ? false : array_search($lastPrefix, $prefixes, true); |
|
285 | - if($i === false) { |
|
286 | - $i = -1; |
|
287 | - } else { |
|
288 | - $i++; |
|
289 | - } |
|
290 | - |
|
291 | - if(!isset($prefixes[$i])) { |
|
292 | - $i = 0; |
|
293 | - } |
|
294 | - return $prefixes[$i]; |
|
295 | - } |
|
296 | - |
|
297 | - /** |
|
298 | - * "fixes" DI |
|
299 | - * |
|
300 | - * @param array $argument |
|
301 | - */ |
|
302 | - public function setArgument($argument) { |
|
303 | - if(isset($argument['config'])) { |
|
304 | - $this->config = $argument['config']; |
|
305 | - } else { |
|
306 | - $this->config = \OC::$server->getConfig(); |
|
307 | - } |
|
308 | - |
|
309 | - if(isset($argument['helper'])) { |
|
310 | - $this->ldapHelper = $argument['helper']; |
|
311 | - } else { |
|
312 | - $this->ldapHelper = new Helper($this->config); |
|
313 | - } |
|
314 | - |
|
315 | - if(isset($argument['ldapWrapper'])) { |
|
316 | - $this->ldap = $argument['ldapWrapper']; |
|
317 | - } else { |
|
318 | - $this->ldap = new LDAP(); |
|
319 | - } |
|
320 | - |
|
321 | - if(isset($argument['avatarManager'])) { |
|
322 | - $this->avatarManager = $argument['avatarManager']; |
|
323 | - } else { |
|
324 | - $this->avatarManager = \OC::$server->getAvatarManager(); |
|
325 | - } |
|
326 | - |
|
327 | - if(isset($argument['dbc'])) { |
|
328 | - $this->dbc = $argument['dbc']; |
|
329 | - } else { |
|
330 | - $this->dbc = \OC::$server->getDatabaseConnection(); |
|
331 | - } |
|
332 | - |
|
333 | - if(isset($argument['ncUserManager'])) { |
|
334 | - $this->ncUserManager = $argument['ncUserManager']; |
|
335 | - } else { |
|
336 | - $this->ncUserManager = \OC::$server->getUserManager(); |
|
337 | - } |
|
338 | - |
|
339 | - if(isset($argument['notificationManager'])) { |
|
340 | - $this->notificationManager = $argument['notificationManager']; |
|
341 | - } else { |
|
342 | - $this->notificationManager = \OC::$server->getNotificationManager(); |
|
343 | - } |
|
344 | - |
|
345 | - if(isset($argument['userManager'])) { |
|
346 | - $this->userManager = $argument['userManager']; |
|
347 | - } else { |
|
348 | - $this->userManager = new Manager( |
|
349 | - $this->config, |
|
350 | - new FilesystemHelper(), |
|
351 | - new LogWrapper(), |
|
352 | - $this->avatarManager, |
|
353 | - new Image(), |
|
354 | - $this->dbc, |
|
355 | - $this->ncUserManager, |
|
356 | - $this->notificationManager |
|
357 | - ); |
|
358 | - } |
|
359 | - |
|
360 | - if(isset($argument['mapper'])) { |
|
361 | - $this->mapper = $argument['mapper']; |
|
362 | - } else { |
|
363 | - $this->mapper = new UserMapping($this->dbc); |
|
364 | - } |
|
45 | + const MAX_INTERVAL = 12 * 60 * 60; // 12h |
|
46 | + const MIN_INTERVAL = 30 * 60; // 30min |
|
47 | + /** @var Helper */ |
|
48 | + protected $ldapHelper; |
|
49 | + /** @var LDAP */ |
|
50 | + protected $ldap; |
|
51 | + /** @var Manager */ |
|
52 | + protected $userManager; |
|
53 | + /** @var UserMapping */ |
|
54 | + protected $mapper; |
|
55 | + /** @var IConfig */ |
|
56 | + protected $config; |
|
57 | + /** @var IAvatarManager */ |
|
58 | + protected $avatarManager; |
|
59 | + /** @var IDBConnection */ |
|
60 | + protected $dbc; |
|
61 | + /** @var IUserManager */ |
|
62 | + protected $ncUserManager; |
|
63 | + /** @var IManager */ |
|
64 | + protected $notificationManager; |
|
65 | + /** @var ConnectionFactory */ |
|
66 | + protected $connectionFactory; |
|
67 | + /** @var AccessFactory */ |
|
68 | + protected $accessFactory; |
|
69 | + |
|
70 | + public function __construct() { |
|
71 | + $this->setInterval( |
|
72 | + \OC::$server->getConfig()->getAppValue( |
|
73 | + 'user_ldap', |
|
74 | + 'background_sync_interval', |
|
75 | + self::MIN_INTERVAL |
|
76 | + ) |
|
77 | + ); |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * updates the interval |
|
82 | + * |
|
83 | + * the idea is to adjust the interval depending on the amount of known users |
|
84 | + * and the attempt to update each user one day. At most it would run every |
|
85 | + * 30 minutes, and at least every 12 hours. |
|
86 | + */ |
|
87 | + public function updateInterval() { |
|
88 | + $minPagingSize = $this->getMinPagingSize(); |
|
89 | + $mappedUsers = $this->mapper->count(); |
|
90 | + |
|
91 | + $runsPerDay = ($minPagingSize === 0 || $mappedUsers === 0) ? self::MAX_INTERVAL |
|
92 | + : $mappedUsers / $minPagingSize; |
|
93 | + $interval = floor(24 * 60 * 60 / $runsPerDay); |
|
94 | + $interval = min(max($interval, self::MIN_INTERVAL), self::MAX_INTERVAL); |
|
95 | + |
|
96 | + $this->config->setAppValue('user_ldap', 'background_sync_interval', $interval); |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * returns the smallest configured paging size |
|
101 | + * @return int |
|
102 | + */ |
|
103 | + protected function getMinPagingSize() { |
|
104 | + $configKeys = $this->config->getAppKeys('user_ldap'); |
|
105 | + $configKeys = array_filter($configKeys, function($key) { |
|
106 | + return strpos($key, 'ldap_paging_size') !== false; |
|
107 | + }); |
|
108 | + $minPagingSize = null; |
|
109 | + foreach ($configKeys as $configKey) { |
|
110 | + $pagingSize = $this->config->getAppValue('user_ldap', $configKey, $minPagingSize); |
|
111 | + $minPagingSize = $minPagingSize === null ? $pagingSize : min($minPagingSize, $pagingSize); |
|
112 | + } |
|
113 | + return (int)$minPagingSize; |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * @param array $argument |
|
118 | + */ |
|
119 | + public function run($argument) { |
|
120 | + $this->setArgument($argument); |
|
121 | + |
|
122 | + $isBackgroundJobModeAjax = $this->config |
|
123 | + ->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'ajax'; |
|
124 | + if($isBackgroundJobModeAjax) { |
|
125 | + return; |
|
126 | + } |
|
127 | + |
|
128 | + $cycleData = $this->getCycle(); |
|
129 | + if($cycleData === null) { |
|
130 | + $cycleData = $this->determineNextCycle(); |
|
131 | + if($cycleData === null) { |
|
132 | + $this->updateInterval(); |
|
133 | + return; |
|
134 | + } |
|
135 | + } |
|
136 | + |
|
137 | + if(!$this->qualifiesToRun($cycleData)) { |
|
138 | + $this->updateInterval(); |
|
139 | + return; |
|
140 | + } |
|
141 | + |
|
142 | + try { |
|
143 | + $expectMoreResults = $this->runCycle($cycleData); |
|
144 | + if ($expectMoreResults) { |
|
145 | + $this->increaseOffset($cycleData); |
|
146 | + } else { |
|
147 | + $this->determineNextCycle($cycleData); |
|
148 | + } |
|
149 | + $this->updateInterval(); |
|
150 | + } catch (ServerNotAvailableException $e) { |
|
151 | + $this->determineNextCycle($cycleData); |
|
152 | + } |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * @param array $cycleData |
|
157 | + * @return bool whether more results are expected from the same configuration |
|
158 | + */ |
|
159 | + public function runCycle($cycleData) { |
|
160 | + $connection = $this->connectionFactory->get($cycleData['prefix']); |
|
161 | + $access = $this->accessFactory->get($connection); |
|
162 | + $access->setUserMapper($this->mapper); |
|
163 | + |
|
164 | + $filter = $access->combineFilterWithAnd(array( |
|
165 | + $access->connection->ldapUserFilter, |
|
166 | + $access->connection->ldapUserDisplayName . '=*', |
|
167 | + $access->getFilterPartForUserSearch('') |
|
168 | + )); |
|
169 | + $results = $access->fetchListOfUsers( |
|
170 | + $filter, |
|
171 | + $access->userManager->getAttributes(), |
|
172 | + $connection->ldapPagingSize, |
|
173 | + $cycleData['offset'], |
|
174 | + true |
|
175 | + ); |
|
176 | + |
|
177 | + if((int)$connection->ldapPagingSize === 0) { |
|
178 | + return false; |
|
179 | + } |
|
180 | + return count($results) >= (int)$connection->ldapPagingSize; |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * returns the info about the current cycle that should be run, if any, |
|
185 | + * otherwise null |
|
186 | + * |
|
187 | + * @return array|null |
|
188 | + */ |
|
189 | + public function getCycle() { |
|
190 | + $prefixes = $this->ldapHelper->getServerConfigurationPrefixes(true); |
|
191 | + if(count($prefixes) === 0) { |
|
192 | + return null; |
|
193 | + } |
|
194 | + |
|
195 | + $cycleData = [ |
|
196 | + 'prefix' => $this->config->getAppValue('user_ldap', 'background_sync_prefix', null), |
|
197 | + 'offset' => (int)$this->config->getAppValue('user_ldap', 'background_sync_offset', 0), |
|
198 | + ]; |
|
199 | + |
|
200 | + if( |
|
201 | + $cycleData['prefix'] !== null |
|
202 | + && in_array($cycleData['prefix'], $prefixes) |
|
203 | + ) { |
|
204 | + return $cycleData; |
|
205 | + } |
|
206 | + |
|
207 | + return null; |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * Save the provided cycle information in the DB |
|
212 | + * |
|
213 | + * @param array $cycleData |
|
214 | + */ |
|
215 | + public function setCycle(array $cycleData) { |
|
216 | + $this->config->setAppValue('user_ldap', 'background_sync_prefix', $cycleData['prefix']); |
|
217 | + $this->config->setAppValue('user_ldap', 'background_sync_offset', $cycleData['offset']); |
|
218 | + } |
|
219 | + |
|
220 | + /** |
|
221 | + * returns data about the next cycle that should run, if any, otherwise |
|
222 | + * null. It also always goes for the next LDAP configuration! |
|
223 | + * |
|
224 | + * @param array|null $cycleData the old cycle |
|
225 | + * @return array|null |
|
226 | + */ |
|
227 | + public function determineNextCycle(array $cycleData = null) { |
|
228 | + $prefixes = $this->ldapHelper->getServerConfigurationPrefixes(true); |
|
229 | + if(count($prefixes) === 0) { |
|
230 | + return null; |
|
231 | + } |
|
232 | + |
|
233 | + // get the next prefix in line and remember it |
|
234 | + $oldPrefix = $cycleData === null ? null : $cycleData['prefix']; |
|
235 | + $prefix = $this->getNextPrefix($oldPrefix); |
|
236 | + if($prefix === null) { |
|
237 | + return null; |
|
238 | + } |
|
239 | + $cycleData['prefix'] = $prefix; |
|
240 | + $cycleData['offset'] = 0; |
|
241 | + $this->setCycle(['prefix' => $prefix, 'offset' => 0]); |
|
242 | + |
|
243 | + return $cycleData; |
|
244 | + } |
|
245 | + |
|
246 | + /** |
|
247 | + * Checks whether the provided cycle should be run. Currently only the |
|
248 | + * last configuration change goes into account (at least one hour). |
|
249 | + * |
|
250 | + * @param $cycleData |
|
251 | + * @return bool |
|
252 | + */ |
|
253 | + public function qualifiesToRun($cycleData) { |
|
254 | + $lastChange = $this->config->getAppValue('user_ldap', $cycleData['prefix'] . '_lastChange', 0); |
|
255 | + if((time() - $lastChange) > 60 * 30) { |
|
256 | + return true; |
|
257 | + } |
|
258 | + return false; |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * increases the offset of the current cycle for the next run |
|
263 | + * |
|
264 | + * @param $cycleData |
|
265 | + */ |
|
266 | + protected function increaseOffset($cycleData) { |
|
267 | + $ldapConfig = new Configuration($cycleData['prefix']); |
|
268 | + $cycleData['offset'] += (int)$ldapConfig->ldapPagingSize; |
|
269 | + $this->setCycle($cycleData); |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * determines the next configuration prefix based on the last one (if any) |
|
274 | + * |
|
275 | + * @param string|null $lastPrefix |
|
276 | + * @return string|null |
|
277 | + */ |
|
278 | + protected function getNextPrefix($lastPrefix) { |
|
279 | + $prefixes = $this->ldapHelper->getServerConfigurationPrefixes(true); |
|
280 | + $noOfPrefixes = count($prefixes); |
|
281 | + if($noOfPrefixes === 0) { |
|
282 | + return null; |
|
283 | + } |
|
284 | + $i = $lastPrefix === null ? false : array_search($lastPrefix, $prefixes, true); |
|
285 | + if($i === false) { |
|
286 | + $i = -1; |
|
287 | + } else { |
|
288 | + $i++; |
|
289 | + } |
|
290 | + |
|
291 | + if(!isset($prefixes[$i])) { |
|
292 | + $i = 0; |
|
293 | + } |
|
294 | + return $prefixes[$i]; |
|
295 | + } |
|
296 | + |
|
297 | + /** |
|
298 | + * "fixes" DI |
|
299 | + * |
|
300 | + * @param array $argument |
|
301 | + */ |
|
302 | + public function setArgument($argument) { |
|
303 | + if(isset($argument['config'])) { |
|
304 | + $this->config = $argument['config']; |
|
305 | + } else { |
|
306 | + $this->config = \OC::$server->getConfig(); |
|
307 | + } |
|
308 | + |
|
309 | + if(isset($argument['helper'])) { |
|
310 | + $this->ldapHelper = $argument['helper']; |
|
311 | + } else { |
|
312 | + $this->ldapHelper = new Helper($this->config); |
|
313 | + } |
|
314 | + |
|
315 | + if(isset($argument['ldapWrapper'])) { |
|
316 | + $this->ldap = $argument['ldapWrapper']; |
|
317 | + } else { |
|
318 | + $this->ldap = new LDAP(); |
|
319 | + } |
|
320 | + |
|
321 | + if(isset($argument['avatarManager'])) { |
|
322 | + $this->avatarManager = $argument['avatarManager']; |
|
323 | + } else { |
|
324 | + $this->avatarManager = \OC::$server->getAvatarManager(); |
|
325 | + } |
|
326 | + |
|
327 | + if(isset($argument['dbc'])) { |
|
328 | + $this->dbc = $argument['dbc']; |
|
329 | + } else { |
|
330 | + $this->dbc = \OC::$server->getDatabaseConnection(); |
|
331 | + } |
|
332 | + |
|
333 | + if(isset($argument['ncUserManager'])) { |
|
334 | + $this->ncUserManager = $argument['ncUserManager']; |
|
335 | + } else { |
|
336 | + $this->ncUserManager = \OC::$server->getUserManager(); |
|
337 | + } |
|
338 | + |
|
339 | + if(isset($argument['notificationManager'])) { |
|
340 | + $this->notificationManager = $argument['notificationManager']; |
|
341 | + } else { |
|
342 | + $this->notificationManager = \OC::$server->getNotificationManager(); |
|
343 | + } |
|
344 | + |
|
345 | + if(isset($argument['userManager'])) { |
|
346 | + $this->userManager = $argument['userManager']; |
|
347 | + } else { |
|
348 | + $this->userManager = new Manager( |
|
349 | + $this->config, |
|
350 | + new FilesystemHelper(), |
|
351 | + new LogWrapper(), |
|
352 | + $this->avatarManager, |
|
353 | + new Image(), |
|
354 | + $this->dbc, |
|
355 | + $this->ncUserManager, |
|
356 | + $this->notificationManager |
|
357 | + ); |
|
358 | + } |
|
359 | + |
|
360 | + if(isset($argument['mapper'])) { |
|
361 | + $this->mapper = $argument['mapper']; |
|
362 | + } else { |
|
363 | + $this->mapper = new UserMapping($this->dbc); |
|
364 | + } |
|
365 | 365 | |
366 | - if(isset($argument['connectionFactory'])) { |
|
367 | - $this->connectionFactory = $argument['connectionFactory']; |
|
368 | - } else { |
|
369 | - $this->connectionFactory = new ConnectionFactory($this->ldap); |
|
370 | - } |
|
371 | - |
|
372 | - if(isset($argument['accessFactory'])) { |
|
373 | - $this->accessFactory = $argument['accessFactory']; |
|
374 | - } else { |
|
375 | - $this->accessFactory = new AccessFactory( |
|
376 | - $this->ldap, |
|
377 | - $this->userManager, |
|
378 | - $this->ldapHelper, |
|
379 | - $this->config, |
|
380 | - $this->ncUserManager |
|
381 | - ); |
|
382 | - } |
|
383 | - } |
|
366 | + if(isset($argument['connectionFactory'])) { |
|
367 | + $this->connectionFactory = $argument['connectionFactory']; |
|
368 | + } else { |
|
369 | + $this->connectionFactory = new ConnectionFactory($this->ldap); |
|
370 | + } |
|
371 | + |
|
372 | + if(isset($argument['accessFactory'])) { |
|
373 | + $this->accessFactory = $argument['accessFactory']; |
|
374 | + } else { |
|
375 | + $this->accessFactory = new AccessFactory( |
|
376 | + $this->ldap, |
|
377 | + $this->userManager, |
|
378 | + $this->ldapHelper, |
|
379 | + $this->config, |
|
380 | + $this->ncUserManager |
|
381 | + ); |
|
382 | + } |
|
383 | + } |
|
384 | 384 | } |