@@ -46,431 +46,431 @@ |
||
46 | 46 | use Symfony\Component\EventDispatcher\GenericEvent; |
47 | 47 | |
48 | 48 | class User implements IUser { |
49 | - /** @var string */ |
|
50 | - private $uid; |
|
51 | - |
|
52 | - /** @var string */ |
|
53 | - private $displayName; |
|
54 | - |
|
55 | - /** @var UserInterface|null */ |
|
56 | - private $backend; |
|
57 | - /** @var EventDispatcherInterface */ |
|
58 | - private $dispatcher; |
|
59 | - |
|
60 | - /** @var bool */ |
|
61 | - private $enabled; |
|
62 | - |
|
63 | - /** @var Emitter|Manager */ |
|
64 | - private $emitter; |
|
65 | - |
|
66 | - /** @var string */ |
|
67 | - private $home; |
|
68 | - |
|
69 | - /** @var int */ |
|
70 | - private $lastLogin; |
|
71 | - |
|
72 | - /** @var \OCP\IConfig */ |
|
73 | - private $config; |
|
74 | - |
|
75 | - /** @var IAvatarManager */ |
|
76 | - private $avatarManager; |
|
77 | - |
|
78 | - /** @var IURLGenerator */ |
|
79 | - private $urlGenerator; |
|
80 | - |
|
81 | - public function __construct(string $uid, ?UserInterface $backend, EventDispatcherInterface $dispatcher, $emitter = null, IConfig $config = null, $urlGenerator = null) { |
|
82 | - $this->uid = $uid; |
|
83 | - $this->backend = $backend; |
|
84 | - $this->dispatcher = $dispatcher; |
|
85 | - $this->emitter = $emitter; |
|
86 | - if(is_null($config)) { |
|
87 | - $config = \OC::$server->getConfig(); |
|
88 | - } |
|
89 | - $this->config = $config; |
|
90 | - $this->urlGenerator = $urlGenerator; |
|
91 | - $enabled = $this->config->getUserValue($uid, 'core', 'enabled', 'true'); |
|
92 | - $this->enabled = ($enabled === 'true'); |
|
93 | - $this->lastLogin = $this->config->getUserValue($uid, 'login', 'lastLogin', 0); |
|
94 | - if (is_null($this->urlGenerator)) { |
|
95 | - $this->urlGenerator = \OC::$server->getURLGenerator(); |
|
96 | - } |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * get the user id |
|
101 | - * |
|
102 | - * @return string |
|
103 | - */ |
|
104 | - public function getUID() { |
|
105 | - return $this->uid; |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * get the display name for the user, if no specific display name is set it will fallback to the user id |
|
110 | - * |
|
111 | - * @return string |
|
112 | - */ |
|
113 | - public function getDisplayName() { |
|
114 | - if (!isset($this->displayName)) { |
|
115 | - $displayName = ''; |
|
116 | - if ($this->backend && $this->backend->implementsActions(Backend::GET_DISPLAYNAME)) { |
|
117 | - // get display name and strip whitespace from the beginning and end of it |
|
118 | - $backendDisplayName = $this->backend->getDisplayName($this->uid); |
|
119 | - if (is_string($backendDisplayName)) { |
|
120 | - $displayName = trim($backendDisplayName); |
|
121 | - } |
|
122 | - } |
|
123 | - |
|
124 | - if (!empty($displayName)) { |
|
125 | - $this->displayName = $displayName; |
|
126 | - } else { |
|
127 | - $this->displayName = $this->uid; |
|
128 | - } |
|
129 | - } |
|
130 | - return $this->displayName; |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * set the displayname for the user |
|
135 | - * |
|
136 | - * @param string $displayName |
|
137 | - * @return bool |
|
138 | - */ |
|
139 | - public function setDisplayName($displayName) { |
|
140 | - $displayName = trim($displayName); |
|
141 | - $oldDisplayName = $this->getDisplayName(); |
|
142 | - if ($this->backend->implementsActions(Backend::SET_DISPLAYNAME) && !empty($displayName) && $displayName !== $oldDisplayName) { |
|
143 | - $result = $this->backend->setDisplayName($this->uid, $displayName); |
|
144 | - if ($result) { |
|
145 | - $this->displayName = $displayName; |
|
146 | - $this->triggerChange('displayName', $displayName, $oldDisplayName); |
|
147 | - } |
|
148 | - return $result !== false; |
|
149 | - } |
|
150 | - return false; |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * set the email address of the user |
|
155 | - * |
|
156 | - * @param string|null $mailAddress |
|
157 | - * @return void |
|
158 | - * @since 9.0.0 |
|
159 | - */ |
|
160 | - public function setEMailAddress($mailAddress) { |
|
161 | - $oldMailAddress = $this->getEMailAddress(); |
|
162 | - if($oldMailAddress !== $mailAddress) { |
|
163 | - if($mailAddress === '') { |
|
164 | - $this->config->deleteUserValue($this->uid, 'settings', 'email'); |
|
165 | - } else { |
|
166 | - $this->config->setUserValue($this->uid, 'settings', 'email', $mailAddress); |
|
167 | - } |
|
168 | - $this->triggerChange('eMailAddress', $mailAddress, $oldMailAddress); |
|
169 | - } |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * returns the timestamp of the user's last login or 0 if the user did never |
|
174 | - * login |
|
175 | - * |
|
176 | - * @return int |
|
177 | - */ |
|
178 | - public function getLastLogin() { |
|
179 | - return $this->lastLogin; |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * updates the timestamp of the most recent login of this user |
|
184 | - */ |
|
185 | - public function updateLastLoginTimestamp() { |
|
186 | - $firstTimeLogin = ($this->lastLogin === 0); |
|
187 | - $this->lastLogin = time(); |
|
188 | - $this->config->setUserValue( |
|
189 | - $this->uid, 'login', 'lastLogin', $this->lastLogin); |
|
190 | - |
|
191 | - return $firstTimeLogin; |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Delete the user |
|
196 | - * |
|
197 | - * @return bool |
|
198 | - */ |
|
199 | - public function delete() { |
|
200 | - $this->dispatcher->dispatch(IUser::class . '::preDelete', new GenericEvent($this)); |
|
201 | - if ($this->emitter) { |
|
202 | - $this->emitter->emit('\OC\User', 'preDelete', array($this)); |
|
203 | - } |
|
204 | - // get the home now because it won't return it after user deletion |
|
205 | - $homePath = $this->getHome(); |
|
206 | - $result = $this->backend->deleteUser($this->uid); |
|
207 | - if ($result) { |
|
208 | - |
|
209 | - // FIXME: Feels like an hack - suggestions? |
|
210 | - |
|
211 | - $groupManager = \OC::$server->getGroupManager(); |
|
212 | - // We have to delete the user from all groups |
|
213 | - foreach ($groupManager->getUserGroupIds($this) as $groupId) { |
|
214 | - $group = $groupManager->get($groupId); |
|
215 | - if ($group) { |
|
216 | - \OC_Hook::emit("OC_Group", "pre_removeFromGroup", ["run" => true, "uid" => $this->uid, "gid" => $groupId]); |
|
217 | - $group->removeUser($this); |
|
218 | - \OC_Hook::emit("OC_User", "post_removeFromGroup", ["uid" => $this->uid, "gid" => $groupId]); |
|
219 | - } |
|
220 | - } |
|
221 | - // Delete the user's keys in preferences |
|
222 | - \OC::$server->getConfig()->deleteAllUserValues($this->uid); |
|
223 | - |
|
224 | - // Delete user files in /data/ |
|
225 | - if ($homePath !== false) { |
|
226 | - // FIXME: this operates directly on FS, should use View instead... |
|
227 | - // also this is not testable/mockable... |
|
228 | - \OC_Helper::rmdirr($homePath); |
|
229 | - } |
|
230 | - |
|
231 | - // Delete the users entry in the storage table |
|
232 | - Storage::remove('home::' . $this->uid); |
|
233 | - |
|
234 | - \OC::$server->getCommentsManager()->deleteReferencesOfActor('users', $this->uid); |
|
235 | - \OC::$server->getCommentsManager()->deleteReadMarksFromUser($this); |
|
236 | - |
|
237 | - $notification = \OC::$server->getNotificationManager()->createNotification(); |
|
238 | - $notification->setUser($this->uid); |
|
239 | - \OC::$server->getNotificationManager()->markProcessed($notification); |
|
240 | - |
|
241 | - /** @var AccountManager $accountManager */ |
|
242 | - $accountManager = \OC::$server->query(AccountManager::class); |
|
243 | - $accountManager->deleteUser($this); |
|
244 | - |
|
245 | - $this->dispatcher->dispatch(IUser::class . '::postDelete', new GenericEvent($this)); |
|
246 | - if ($this->emitter) { |
|
247 | - $this->emitter->emit('\OC\User', 'postDelete', array($this)); |
|
248 | - } |
|
249 | - } |
|
250 | - return !($result === false); |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * Set the password of the user |
|
255 | - * |
|
256 | - * @param string $password |
|
257 | - * @param string $recoveryPassword for the encryption app to reset encryption keys |
|
258 | - * @return bool |
|
259 | - */ |
|
260 | - public function setPassword($password, $recoveryPassword = null) { |
|
261 | - $this->dispatcher->dispatch(IUser::class . '::preSetPassword', new GenericEvent($this, [ |
|
262 | - 'password' => $password, |
|
263 | - 'recoveryPassword' => $recoveryPassword, |
|
264 | - ])); |
|
265 | - if ($this->emitter) { |
|
266 | - $this->emitter->emit('\OC\User', 'preSetPassword', array($this, $password, $recoveryPassword)); |
|
267 | - } |
|
268 | - if ($this->backend->implementsActions(Backend::SET_PASSWORD)) { |
|
269 | - $result = $this->backend->setPassword($this->uid, $password); |
|
270 | - $this->dispatcher->dispatch(IUser::class . '::postSetPassword', new GenericEvent($this, [ |
|
271 | - 'password' => $password, |
|
272 | - 'recoveryPassword' => $recoveryPassword, |
|
273 | - ])); |
|
274 | - if ($this->emitter) { |
|
275 | - $this->emitter->emit('\OC\User', 'postSetPassword', array($this, $password, $recoveryPassword)); |
|
276 | - } |
|
277 | - return !($result === false); |
|
278 | - } else { |
|
279 | - return false; |
|
280 | - } |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * get the users home folder to mount |
|
285 | - * |
|
286 | - * @return string |
|
287 | - */ |
|
288 | - public function getHome() { |
|
289 | - if (!$this->home) { |
|
290 | - if ($this->backend->implementsActions(Backend::GET_HOME) and $home = $this->backend->getHome($this->uid)) { |
|
291 | - $this->home = $home; |
|
292 | - } elseif ($this->config) { |
|
293 | - $this->home = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/' . $this->uid; |
|
294 | - } else { |
|
295 | - $this->home = \OC::$SERVERROOT . '/data/' . $this->uid; |
|
296 | - } |
|
297 | - } |
|
298 | - return $this->home; |
|
299 | - } |
|
300 | - |
|
301 | - /** |
|
302 | - * Get the name of the backend class the user is connected with |
|
303 | - * |
|
304 | - * @return string |
|
305 | - */ |
|
306 | - public function getBackendClassName() { |
|
307 | - if($this->backend instanceof IUserBackend) { |
|
308 | - return $this->backend->getBackendName(); |
|
309 | - } |
|
310 | - return get_class($this->backend); |
|
311 | - } |
|
312 | - |
|
313 | - public function getBackend() { |
|
314 | - return $this->backend; |
|
315 | - } |
|
316 | - |
|
317 | - /** |
|
318 | - * check if the backend allows the user to change his avatar on Personal page |
|
319 | - * |
|
320 | - * @return bool |
|
321 | - */ |
|
322 | - public function canChangeAvatar() { |
|
323 | - if ($this->backend->implementsActions(Backend::PROVIDE_AVATAR)) { |
|
324 | - return $this->backend->canChangeAvatar($this->uid); |
|
325 | - } |
|
326 | - return true; |
|
327 | - } |
|
328 | - |
|
329 | - /** |
|
330 | - * check if the backend supports changing passwords |
|
331 | - * |
|
332 | - * @return bool |
|
333 | - */ |
|
334 | - public function canChangePassword() { |
|
335 | - return $this->backend->implementsActions(Backend::SET_PASSWORD); |
|
336 | - } |
|
337 | - |
|
338 | - /** |
|
339 | - * check if the backend supports changing display names |
|
340 | - * |
|
341 | - * @return bool |
|
342 | - */ |
|
343 | - public function canChangeDisplayName() { |
|
344 | - if ($this->config->getSystemValue('allow_user_to_change_display_name') === false) { |
|
345 | - return false; |
|
346 | - } |
|
347 | - return $this->backend->implementsActions(Backend::SET_DISPLAYNAME); |
|
348 | - } |
|
349 | - |
|
350 | - /** |
|
351 | - * check if the user is enabled |
|
352 | - * |
|
353 | - * @return bool |
|
354 | - */ |
|
355 | - public function isEnabled() { |
|
356 | - return $this->enabled; |
|
357 | - } |
|
358 | - |
|
359 | - /** |
|
360 | - * set the enabled status for the user |
|
361 | - * |
|
362 | - * @param bool $enabled |
|
363 | - */ |
|
364 | - public function setEnabled(bool $enabled = true) { |
|
365 | - $oldStatus = $this->isEnabled(); |
|
366 | - $this->enabled = $enabled; |
|
367 | - if ($oldStatus !== $this->enabled) { |
|
368 | - // TODO: First change the value, then trigger the event as done for all other properties. |
|
369 | - $this->triggerChange('enabled', $enabled, $oldStatus); |
|
370 | - $this->config->setUserValue($this->uid, 'core', 'enabled', $enabled ? 'true' : 'false'); |
|
371 | - } |
|
372 | - } |
|
373 | - |
|
374 | - /** |
|
375 | - * get the users email address |
|
376 | - * |
|
377 | - * @return string|null |
|
378 | - * @since 9.0.0 |
|
379 | - */ |
|
380 | - public function getEMailAddress() { |
|
381 | - return $this->config->getUserValue($this->uid, 'settings', 'email', null); |
|
382 | - } |
|
383 | - |
|
384 | - /** |
|
385 | - * get the users' quota |
|
386 | - * |
|
387 | - * @return string |
|
388 | - * @since 9.0.0 |
|
389 | - */ |
|
390 | - public function getQuota() { |
|
391 | - $quota = $this->config->getUserValue($this->uid, 'files', 'quota', 'default'); |
|
392 | - if($quota === 'default') { |
|
393 | - $quota = $this->config->getAppValue('files', 'default_quota', 'none'); |
|
394 | - } |
|
395 | - return $quota; |
|
396 | - } |
|
397 | - |
|
398 | - /** |
|
399 | - * set the users' quota |
|
400 | - * |
|
401 | - * @param string $quota |
|
402 | - * @return void |
|
403 | - * @since 9.0.0 |
|
404 | - */ |
|
405 | - public function setQuota($quota) { |
|
406 | - $oldQuota = $this->config->getUserValue($this->uid, 'files', 'quota', ''); |
|
407 | - if($quota !== 'none' and $quota !== 'default') { |
|
408 | - $quota = OC_Helper::computerFileSize($quota); |
|
409 | - $quota = OC_Helper::humanFileSize($quota); |
|
410 | - } |
|
411 | - if($quota !== $oldQuota) { |
|
412 | - $this->config->setUserValue($this->uid, 'files', 'quota', $quota); |
|
413 | - $this->triggerChange('quota', $quota, $oldQuota); |
|
414 | - } |
|
415 | - } |
|
416 | - |
|
417 | - /** |
|
418 | - * get the avatar image if it exists |
|
419 | - * |
|
420 | - * @param int $size |
|
421 | - * @return IImage|null |
|
422 | - * @since 9.0.0 |
|
423 | - */ |
|
424 | - public function getAvatarImage($size) { |
|
425 | - // delay the initialization |
|
426 | - if (is_null($this->avatarManager)) { |
|
427 | - $this->avatarManager = \OC::$server->getAvatarManager(); |
|
428 | - } |
|
429 | - |
|
430 | - $avatar = $this->avatarManager->getAvatar($this->uid); |
|
431 | - $image = $avatar->get(-1); |
|
432 | - if ($image) { |
|
433 | - return $image; |
|
434 | - } |
|
435 | - |
|
436 | - return null; |
|
437 | - } |
|
438 | - |
|
439 | - /** |
|
440 | - * get the federation cloud id |
|
441 | - * |
|
442 | - * @return string |
|
443 | - * @since 9.0.0 |
|
444 | - */ |
|
445 | - public function getCloudId() { |
|
446 | - $uid = $this->getUID(); |
|
447 | - $server = $this->urlGenerator->getAbsoluteURL('/'); |
|
448 | - $server = rtrim( $this->removeProtocolFromUrl($server), '/'); |
|
449 | - return \OC::$server->getCloudIdManager()->getCloudId($uid, $server)->getId(); |
|
450 | - } |
|
451 | - |
|
452 | - /** |
|
453 | - * @param string $url |
|
454 | - * @return string |
|
455 | - */ |
|
456 | - private function removeProtocolFromUrl($url) { |
|
457 | - if (strpos($url, 'https://') === 0) { |
|
458 | - return substr($url, strlen('https://')); |
|
459 | - } else if (strpos($url, 'http://') === 0) { |
|
460 | - return substr($url, strlen('http://')); |
|
461 | - } |
|
462 | - |
|
463 | - return $url; |
|
464 | - } |
|
465 | - |
|
466 | - public function triggerChange($feature, $value = null, $oldValue = null) { |
|
467 | - $this->dispatcher->dispatch(IUser::class . '::changeUser', new GenericEvent($this, [ |
|
468 | - 'feature' => $feature, |
|
469 | - 'value' => $value, |
|
470 | - 'oldValue' => $oldValue, |
|
471 | - ])); |
|
472 | - if ($this->emitter) { |
|
473 | - $this->emitter->emit('\OC\User', 'changeUser', array($this, $feature, $value, $oldValue)); |
|
474 | - } |
|
475 | - } |
|
49 | + /** @var string */ |
|
50 | + private $uid; |
|
51 | + |
|
52 | + /** @var string */ |
|
53 | + private $displayName; |
|
54 | + |
|
55 | + /** @var UserInterface|null */ |
|
56 | + private $backend; |
|
57 | + /** @var EventDispatcherInterface */ |
|
58 | + private $dispatcher; |
|
59 | + |
|
60 | + /** @var bool */ |
|
61 | + private $enabled; |
|
62 | + |
|
63 | + /** @var Emitter|Manager */ |
|
64 | + private $emitter; |
|
65 | + |
|
66 | + /** @var string */ |
|
67 | + private $home; |
|
68 | + |
|
69 | + /** @var int */ |
|
70 | + private $lastLogin; |
|
71 | + |
|
72 | + /** @var \OCP\IConfig */ |
|
73 | + private $config; |
|
74 | + |
|
75 | + /** @var IAvatarManager */ |
|
76 | + private $avatarManager; |
|
77 | + |
|
78 | + /** @var IURLGenerator */ |
|
79 | + private $urlGenerator; |
|
80 | + |
|
81 | + public function __construct(string $uid, ?UserInterface $backend, EventDispatcherInterface $dispatcher, $emitter = null, IConfig $config = null, $urlGenerator = null) { |
|
82 | + $this->uid = $uid; |
|
83 | + $this->backend = $backend; |
|
84 | + $this->dispatcher = $dispatcher; |
|
85 | + $this->emitter = $emitter; |
|
86 | + if(is_null($config)) { |
|
87 | + $config = \OC::$server->getConfig(); |
|
88 | + } |
|
89 | + $this->config = $config; |
|
90 | + $this->urlGenerator = $urlGenerator; |
|
91 | + $enabled = $this->config->getUserValue($uid, 'core', 'enabled', 'true'); |
|
92 | + $this->enabled = ($enabled === 'true'); |
|
93 | + $this->lastLogin = $this->config->getUserValue($uid, 'login', 'lastLogin', 0); |
|
94 | + if (is_null($this->urlGenerator)) { |
|
95 | + $this->urlGenerator = \OC::$server->getURLGenerator(); |
|
96 | + } |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * get the user id |
|
101 | + * |
|
102 | + * @return string |
|
103 | + */ |
|
104 | + public function getUID() { |
|
105 | + return $this->uid; |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * get the display name for the user, if no specific display name is set it will fallback to the user id |
|
110 | + * |
|
111 | + * @return string |
|
112 | + */ |
|
113 | + public function getDisplayName() { |
|
114 | + if (!isset($this->displayName)) { |
|
115 | + $displayName = ''; |
|
116 | + if ($this->backend && $this->backend->implementsActions(Backend::GET_DISPLAYNAME)) { |
|
117 | + // get display name and strip whitespace from the beginning and end of it |
|
118 | + $backendDisplayName = $this->backend->getDisplayName($this->uid); |
|
119 | + if (is_string($backendDisplayName)) { |
|
120 | + $displayName = trim($backendDisplayName); |
|
121 | + } |
|
122 | + } |
|
123 | + |
|
124 | + if (!empty($displayName)) { |
|
125 | + $this->displayName = $displayName; |
|
126 | + } else { |
|
127 | + $this->displayName = $this->uid; |
|
128 | + } |
|
129 | + } |
|
130 | + return $this->displayName; |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * set the displayname for the user |
|
135 | + * |
|
136 | + * @param string $displayName |
|
137 | + * @return bool |
|
138 | + */ |
|
139 | + public function setDisplayName($displayName) { |
|
140 | + $displayName = trim($displayName); |
|
141 | + $oldDisplayName = $this->getDisplayName(); |
|
142 | + if ($this->backend->implementsActions(Backend::SET_DISPLAYNAME) && !empty($displayName) && $displayName !== $oldDisplayName) { |
|
143 | + $result = $this->backend->setDisplayName($this->uid, $displayName); |
|
144 | + if ($result) { |
|
145 | + $this->displayName = $displayName; |
|
146 | + $this->triggerChange('displayName', $displayName, $oldDisplayName); |
|
147 | + } |
|
148 | + return $result !== false; |
|
149 | + } |
|
150 | + return false; |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * set the email address of the user |
|
155 | + * |
|
156 | + * @param string|null $mailAddress |
|
157 | + * @return void |
|
158 | + * @since 9.0.0 |
|
159 | + */ |
|
160 | + public function setEMailAddress($mailAddress) { |
|
161 | + $oldMailAddress = $this->getEMailAddress(); |
|
162 | + if($oldMailAddress !== $mailAddress) { |
|
163 | + if($mailAddress === '') { |
|
164 | + $this->config->deleteUserValue($this->uid, 'settings', 'email'); |
|
165 | + } else { |
|
166 | + $this->config->setUserValue($this->uid, 'settings', 'email', $mailAddress); |
|
167 | + } |
|
168 | + $this->triggerChange('eMailAddress', $mailAddress, $oldMailAddress); |
|
169 | + } |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * returns the timestamp of the user's last login or 0 if the user did never |
|
174 | + * login |
|
175 | + * |
|
176 | + * @return int |
|
177 | + */ |
|
178 | + public function getLastLogin() { |
|
179 | + return $this->lastLogin; |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * updates the timestamp of the most recent login of this user |
|
184 | + */ |
|
185 | + public function updateLastLoginTimestamp() { |
|
186 | + $firstTimeLogin = ($this->lastLogin === 0); |
|
187 | + $this->lastLogin = time(); |
|
188 | + $this->config->setUserValue( |
|
189 | + $this->uid, 'login', 'lastLogin', $this->lastLogin); |
|
190 | + |
|
191 | + return $firstTimeLogin; |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Delete the user |
|
196 | + * |
|
197 | + * @return bool |
|
198 | + */ |
|
199 | + public function delete() { |
|
200 | + $this->dispatcher->dispatch(IUser::class . '::preDelete', new GenericEvent($this)); |
|
201 | + if ($this->emitter) { |
|
202 | + $this->emitter->emit('\OC\User', 'preDelete', array($this)); |
|
203 | + } |
|
204 | + // get the home now because it won't return it after user deletion |
|
205 | + $homePath = $this->getHome(); |
|
206 | + $result = $this->backend->deleteUser($this->uid); |
|
207 | + if ($result) { |
|
208 | + |
|
209 | + // FIXME: Feels like an hack - suggestions? |
|
210 | + |
|
211 | + $groupManager = \OC::$server->getGroupManager(); |
|
212 | + // We have to delete the user from all groups |
|
213 | + foreach ($groupManager->getUserGroupIds($this) as $groupId) { |
|
214 | + $group = $groupManager->get($groupId); |
|
215 | + if ($group) { |
|
216 | + \OC_Hook::emit("OC_Group", "pre_removeFromGroup", ["run" => true, "uid" => $this->uid, "gid" => $groupId]); |
|
217 | + $group->removeUser($this); |
|
218 | + \OC_Hook::emit("OC_User", "post_removeFromGroup", ["uid" => $this->uid, "gid" => $groupId]); |
|
219 | + } |
|
220 | + } |
|
221 | + // Delete the user's keys in preferences |
|
222 | + \OC::$server->getConfig()->deleteAllUserValues($this->uid); |
|
223 | + |
|
224 | + // Delete user files in /data/ |
|
225 | + if ($homePath !== false) { |
|
226 | + // FIXME: this operates directly on FS, should use View instead... |
|
227 | + // also this is not testable/mockable... |
|
228 | + \OC_Helper::rmdirr($homePath); |
|
229 | + } |
|
230 | + |
|
231 | + // Delete the users entry in the storage table |
|
232 | + Storage::remove('home::' . $this->uid); |
|
233 | + |
|
234 | + \OC::$server->getCommentsManager()->deleteReferencesOfActor('users', $this->uid); |
|
235 | + \OC::$server->getCommentsManager()->deleteReadMarksFromUser($this); |
|
236 | + |
|
237 | + $notification = \OC::$server->getNotificationManager()->createNotification(); |
|
238 | + $notification->setUser($this->uid); |
|
239 | + \OC::$server->getNotificationManager()->markProcessed($notification); |
|
240 | + |
|
241 | + /** @var AccountManager $accountManager */ |
|
242 | + $accountManager = \OC::$server->query(AccountManager::class); |
|
243 | + $accountManager->deleteUser($this); |
|
244 | + |
|
245 | + $this->dispatcher->dispatch(IUser::class . '::postDelete', new GenericEvent($this)); |
|
246 | + if ($this->emitter) { |
|
247 | + $this->emitter->emit('\OC\User', 'postDelete', array($this)); |
|
248 | + } |
|
249 | + } |
|
250 | + return !($result === false); |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * Set the password of the user |
|
255 | + * |
|
256 | + * @param string $password |
|
257 | + * @param string $recoveryPassword for the encryption app to reset encryption keys |
|
258 | + * @return bool |
|
259 | + */ |
|
260 | + public function setPassword($password, $recoveryPassword = null) { |
|
261 | + $this->dispatcher->dispatch(IUser::class . '::preSetPassword', new GenericEvent($this, [ |
|
262 | + 'password' => $password, |
|
263 | + 'recoveryPassword' => $recoveryPassword, |
|
264 | + ])); |
|
265 | + if ($this->emitter) { |
|
266 | + $this->emitter->emit('\OC\User', 'preSetPassword', array($this, $password, $recoveryPassword)); |
|
267 | + } |
|
268 | + if ($this->backend->implementsActions(Backend::SET_PASSWORD)) { |
|
269 | + $result = $this->backend->setPassword($this->uid, $password); |
|
270 | + $this->dispatcher->dispatch(IUser::class . '::postSetPassword', new GenericEvent($this, [ |
|
271 | + 'password' => $password, |
|
272 | + 'recoveryPassword' => $recoveryPassword, |
|
273 | + ])); |
|
274 | + if ($this->emitter) { |
|
275 | + $this->emitter->emit('\OC\User', 'postSetPassword', array($this, $password, $recoveryPassword)); |
|
276 | + } |
|
277 | + return !($result === false); |
|
278 | + } else { |
|
279 | + return false; |
|
280 | + } |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * get the users home folder to mount |
|
285 | + * |
|
286 | + * @return string |
|
287 | + */ |
|
288 | + public function getHome() { |
|
289 | + if (!$this->home) { |
|
290 | + if ($this->backend->implementsActions(Backend::GET_HOME) and $home = $this->backend->getHome($this->uid)) { |
|
291 | + $this->home = $home; |
|
292 | + } elseif ($this->config) { |
|
293 | + $this->home = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/' . $this->uid; |
|
294 | + } else { |
|
295 | + $this->home = \OC::$SERVERROOT . '/data/' . $this->uid; |
|
296 | + } |
|
297 | + } |
|
298 | + return $this->home; |
|
299 | + } |
|
300 | + |
|
301 | + /** |
|
302 | + * Get the name of the backend class the user is connected with |
|
303 | + * |
|
304 | + * @return string |
|
305 | + */ |
|
306 | + public function getBackendClassName() { |
|
307 | + if($this->backend instanceof IUserBackend) { |
|
308 | + return $this->backend->getBackendName(); |
|
309 | + } |
|
310 | + return get_class($this->backend); |
|
311 | + } |
|
312 | + |
|
313 | + public function getBackend() { |
|
314 | + return $this->backend; |
|
315 | + } |
|
316 | + |
|
317 | + /** |
|
318 | + * check if the backend allows the user to change his avatar on Personal page |
|
319 | + * |
|
320 | + * @return bool |
|
321 | + */ |
|
322 | + public function canChangeAvatar() { |
|
323 | + if ($this->backend->implementsActions(Backend::PROVIDE_AVATAR)) { |
|
324 | + return $this->backend->canChangeAvatar($this->uid); |
|
325 | + } |
|
326 | + return true; |
|
327 | + } |
|
328 | + |
|
329 | + /** |
|
330 | + * check if the backend supports changing passwords |
|
331 | + * |
|
332 | + * @return bool |
|
333 | + */ |
|
334 | + public function canChangePassword() { |
|
335 | + return $this->backend->implementsActions(Backend::SET_PASSWORD); |
|
336 | + } |
|
337 | + |
|
338 | + /** |
|
339 | + * check if the backend supports changing display names |
|
340 | + * |
|
341 | + * @return bool |
|
342 | + */ |
|
343 | + public function canChangeDisplayName() { |
|
344 | + if ($this->config->getSystemValue('allow_user_to_change_display_name') === false) { |
|
345 | + return false; |
|
346 | + } |
|
347 | + return $this->backend->implementsActions(Backend::SET_DISPLAYNAME); |
|
348 | + } |
|
349 | + |
|
350 | + /** |
|
351 | + * check if the user is enabled |
|
352 | + * |
|
353 | + * @return bool |
|
354 | + */ |
|
355 | + public function isEnabled() { |
|
356 | + return $this->enabled; |
|
357 | + } |
|
358 | + |
|
359 | + /** |
|
360 | + * set the enabled status for the user |
|
361 | + * |
|
362 | + * @param bool $enabled |
|
363 | + */ |
|
364 | + public function setEnabled(bool $enabled = true) { |
|
365 | + $oldStatus = $this->isEnabled(); |
|
366 | + $this->enabled = $enabled; |
|
367 | + if ($oldStatus !== $this->enabled) { |
|
368 | + // TODO: First change the value, then trigger the event as done for all other properties. |
|
369 | + $this->triggerChange('enabled', $enabled, $oldStatus); |
|
370 | + $this->config->setUserValue($this->uid, 'core', 'enabled', $enabled ? 'true' : 'false'); |
|
371 | + } |
|
372 | + } |
|
373 | + |
|
374 | + /** |
|
375 | + * get the users email address |
|
376 | + * |
|
377 | + * @return string|null |
|
378 | + * @since 9.0.0 |
|
379 | + */ |
|
380 | + public function getEMailAddress() { |
|
381 | + return $this->config->getUserValue($this->uid, 'settings', 'email', null); |
|
382 | + } |
|
383 | + |
|
384 | + /** |
|
385 | + * get the users' quota |
|
386 | + * |
|
387 | + * @return string |
|
388 | + * @since 9.0.0 |
|
389 | + */ |
|
390 | + public function getQuota() { |
|
391 | + $quota = $this->config->getUserValue($this->uid, 'files', 'quota', 'default'); |
|
392 | + if($quota === 'default') { |
|
393 | + $quota = $this->config->getAppValue('files', 'default_quota', 'none'); |
|
394 | + } |
|
395 | + return $quota; |
|
396 | + } |
|
397 | + |
|
398 | + /** |
|
399 | + * set the users' quota |
|
400 | + * |
|
401 | + * @param string $quota |
|
402 | + * @return void |
|
403 | + * @since 9.0.0 |
|
404 | + */ |
|
405 | + public function setQuota($quota) { |
|
406 | + $oldQuota = $this->config->getUserValue($this->uid, 'files', 'quota', ''); |
|
407 | + if($quota !== 'none' and $quota !== 'default') { |
|
408 | + $quota = OC_Helper::computerFileSize($quota); |
|
409 | + $quota = OC_Helper::humanFileSize($quota); |
|
410 | + } |
|
411 | + if($quota !== $oldQuota) { |
|
412 | + $this->config->setUserValue($this->uid, 'files', 'quota', $quota); |
|
413 | + $this->triggerChange('quota', $quota, $oldQuota); |
|
414 | + } |
|
415 | + } |
|
416 | + |
|
417 | + /** |
|
418 | + * get the avatar image if it exists |
|
419 | + * |
|
420 | + * @param int $size |
|
421 | + * @return IImage|null |
|
422 | + * @since 9.0.0 |
|
423 | + */ |
|
424 | + public function getAvatarImage($size) { |
|
425 | + // delay the initialization |
|
426 | + if (is_null($this->avatarManager)) { |
|
427 | + $this->avatarManager = \OC::$server->getAvatarManager(); |
|
428 | + } |
|
429 | + |
|
430 | + $avatar = $this->avatarManager->getAvatar($this->uid); |
|
431 | + $image = $avatar->get(-1); |
|
432 | + if ($image) { |
|
433 | + return $image; |
|
434 | + } |
|
435 | + |
|
436 | + return null; |
|
437 | + } |
|
438 | + |
|
439 | + /** |
|
440 | + * get the federation cloud id |
|
441 | + * |
|
442 | + * @return string |
|
443 | + * @since 9.0.0 |
|
444 | + */ |
|
445 | + public function getCloudId() { |
|
446 | + $uid = $this->getUID(); |
|
447 | + $server = $this->urlGenerator->getAbsoluteURL('/'); |
|
448 | + $server = rtrim( $this->removeProtocolFromUrl($server), '/'); |
|
449 | + return \OC::$server->getCloudIdManager()->getCloudId($uid, $server)->getId(); |
|
450 | + } |
|
451 | + |
|
452 | + /** |
|
453 | + * @param string $url |
|
454 | + * @return string |
|
455 | + */ |
|
456 | + private function removeProtocolFromUrl($url) { |
|
457 | + if (strpos($url, 'https://') === 0) { |
|
458 | + return substr($url, strlen('https://')); |
|
459 | + } else if (strpos($url, 'http://') === 0) { |
|
460 | + return substr($url, strlen('http://')); |
|
461 | + } |
|
462 | + |
|
463 | + return $url; |
|
464 | + } |
|
465 | + |
|
466 | + public function triggerChange($feature, $value = null, $oldValue = null) { |
|
467 | + $this->dispatcher->dispatch(IUser::class . '::changeUser', new GenericEvent($this, [ |
|
468 | + 'feature' => $feature, |
|
469 | + 'value' => $value, |
|
470 | + 'oldValue' => $oldValue, |
|
471 | + ])); |
|
472 | + if ($this->emitter) { |
|
473 | + $this->emitter->emit('\OC\User', 'changeUser', array($this, $feature, $value, $oldValue)); |
|
474 | + } |
|
475 | + } |
|
476 | 476 | } |