Skip to content

Commit 22eb442

Browse files
committed
feat(users): Make User object constructor argument mandatory
Signed-off-by: Louis Chmn <louis@chmn.me>
1 parent 68dca70 commit 22eb442

3 files changed

Lines changed: 84 additions & 66 deletions

File tree

‎lib/private/User/User.php‎

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@
5050
class User implements IUser {
5151
private const CONFIG_KEY_MANAGERS = 'manager';
5252

53-
private IConfig $config;
54-
private IURLGenerator $urlGenerator;
5553
protected ?IAccountManager $accountManager = null;
5654

5755
private ?string $displayName = null;
@@ -63,15 +61,13 @@ class User implements IUser {
6361
private ?IAvatarManager $avatarManager = null;
6462

6563
public function __construct(
66-
private string $uid,
67-
private ?UserInterface $backend,
68-
private IEventDispatcher $dispatcher,
69-
private Emitter|Manager|null $emitter = null,
70-
?IConfig $config = null,
71-
$urlGenerator = null,
64+
private readonly string $uid,
65+
private readonly ?UserInterface $backend,
66+
private readonly IEventDispatcher $dispatcher,
67+
private readonly Emitter|Manager $emitter,
68+
private readonly IConfig $config,
69+
private readonly IURLGenerator $urlGenerator,
7270
) {
73-
$this->config = $config ?? Server::get(IConfig::class);
74-
$this->urlGenerator = $urlGenerator ?? Server::get(IURLGenerator::class);
7571
}
7672

7773
#[\Override]
@@ -251,10 +247,8 @@ public function delete(): bool {
251247
return false;
252248
}
253249

254-
if ($this->emitter) {
255-
/** @deprecated 21.0.0 use BeforeUserDeletedEvent event with the IEventDispatcher instead */
256-
$this->emitter->emit('\OC\User', 'preDelete', [$this]);
257-
}
250+
/** @deprecated 21.0.0 use BeforeUserDeletedEvent event with the IEventDispatcher instead */
251+
$this->emitter->emit('\OC\User', 'preDelete', [$this]);
258252
$this->dispatcher->dispatchTyped(new BeforeUserDeletedEvent($this));
259253

260254
// Set delete flag on the user - this is needed to ensure that the user data is removed if there happen any exception in the backend
@@ -315,10 +309,8 @@ public function delete(): bool {
315309
throw $e;
316310
}
317311

318-
if ($this->emitter !== null) {
319-
/** @deprecated 21.0.0 use UserDeletedEvent event with the IEventDispatcher instead */
320-
$this->emitter->emit('\OC\User', 'postDelete', [$this]);
321-
}
312+
/** @deprecated 21.0.0 use UserDeletedEvent event with the IEventDispatcher instead */
313+
$this->emitter->emit('\OC\User', 'postDelete', [$this]);
322314
$this->dispatcher->dispatchTyped(new UserDeletedEvent($this));
323315

324316
// Finally we can unset the delete flag and all other states
@@ -336,19 +328,15 @@ public function delete(): bool {
336328
#[\Override]
337329
public function setPassword($password, $recoveryPassword = null): bool {
338330
$this->dispatcher->dispatchTyped(new BeforePasswordUpdatedEvent($this, $password, $recoveryPassword));
339-
if ($this->emitter) {
340-
$this->emitter->emit('\OC\User', 'preSetPassword', [$this, $password, $recoveryPassword]);
341-
}
331+
$this->emitter->emit('\OC\User', 'preSetPassword', [$this, $password, $recoveryPassword]);
342332
if ($this->backend->implementsActions(Backend::SET_PASSWORD)) {
343333
/** @var ISetPasswordBackend $backend */
344334
$backend = $this->backend;
345335
$result = $backend->setPassword($this->uid, $password);
346336

347337
if ($result !== false) {
348338
$this->dispatcher->dispatchTyped(new PasswordUpdatedEvent($this, $password, $recoveryPassword));
349-
if ($this->emitter) {
350-
$this->emitter->emit('\OC\User', 'postSetPassword', [$this, $password, $recoveryPassword]);
351-
}
339+
$this->emitter->emit('\OC\User', 'postSetPassword', [$this, $password, $recoveryPassword]);
352340
}
353341

354342
return !($result === false);
@@ -676,8 +664,6 @@ private function removeProtocolFromUrl(string $url): string {
676664

677665
public function triggerChange($feature, $value = null, $oldValue = null): void {
678666
$this->dispatcher->dispatchTyped(new UserChangedEvent($this, $feature, $value, $oldValue));
679-
if ($this->emitter) {
680-
$this->emitter->emit('\OC\User', 'changeUser', [$this, $feature, $value, $oldValue]);
681-
}
667+
$this->emitter->emit('\OC\User', 'changeUser', [$this, $feature, $value, $oldValue]);
682668
}
683669
}

‎tests/lib/Traits/UserTrait.php‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,27 @@
88

99
namespace Test\Traits;
1010

11+
use OC\User\Manager;
1112
use OC\User\User;
1213
use OCP\EventDispatcher\IEventDispatcher;
14+
use OCP\IConfig;
15+
use OCP\IURLGenerator;
1316
use OCP\IUser;
1417
use OCP\IUserManager;
1518
use OCP\Server;
16-
use OCP\UserInterface;
1719

1820
class DummyUser extends User {
1921
public function __construct(
2022
private string $uid,
2123
) {
22-
parent::__construct($this->uid, null, Server::get(IEventDispatcher::class));
24+
parent::__construct(
25+
$this->uid,
26+
null,
27+
Server::get(IEventDispatcher::class),
28+
Server::get(Manager::class),
29+
Server::get(IConfig::class),
30+
Server::get(IURLGenerator::class),
31+
);
2332
}
2433

2534
#[\Override]

0 commit comments

Comments
 (0)