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