Skip to content

Commit 5c0ba9a

Browse files
Merge pull request #56732 from nextcloud/techdebt/noid/migrate-settings-pii-listener-to-events
fix(settings): Migrate PII listener to IEventListener
2 parents 631f471 + 3dd06f8 commit 5c0ba9a

File tree

3 files changed

+35
-52
lines changed

3 files changed

+35
-52
lines changed

apps/settings/lib/AppInfo/Application.php

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@
8585
use OCP\AppFramework\Bootstrap\IBootstrap;
8686
use OCP\AppFramework\Bootstrap\IRegistrationContext;
8787
use OCP\AppFramework\IAppContainer;
88-
use OCP\AppFramework\QueryException;
8988
use OCP\Defaults;
9089
use OCP\Group\Events\GroupDeletedEvent;
9190
use OCP\Group\Events\UserAddedEvent;
@@ -94,6 +93,8 @@
9493
use OCP\Settings\Events\DeclarativeSettingsGetValueEvent;
9594
use OCP\Settings\Events\DeclarativeSettingsSetValueEvent;
9695
use OCP\Settings\IManager;
96+
use OCP\User\Events\PasswordUpdatedEvent;
97+
use OCP\User\Events\UserChangedEvent;
9798
use OCP\Util;
9899

99100
class Application extends App implements IBootstrap {
@@ -121,6 +122,8 @@ public function register(IRegistrationContext $context): void {
121122
$context->registerEventListener(UserAddedEvent::class, UserAddedToGroupActivityListener::class);
122123
$context->registerEventListener(UserRemovedEvent::class, UserRemovedFromGroupActivityListener::class);
123124
$context->registerEventListener(GroupDeletedEvent::class, GroupRemovedListener::class);
125+
$context->registerEventListener(PasswordUpdatedEvent::class, Hooks::class);
126+
$context->registerEventListener(UserChangedEvent::class, Hooks::class);
124127

125128
// Register Mail Provider listeners
126129
$context->registerEventListener(DeclarativeSettingsGetValueEvent::class, MailProviderListener::class);
@@ -223,37 +226,5 @@ public function register(IRegistrationContext $context): void {
223226
}
224227

225228
public function boot(IBootContext $context): void {
226-
Util::connectHook('OC_User', 'post_setPassword', $this, 'onChangePassword');
227-
Util::connectHook('OC_User', 'changeUser', $this, 'onChangeInfo');
228-
}
229-
230-
/**
231-
* @param array $parameters
232-
* @throws \InvalidArgumentException
233-
* @throws \BadMethodCallException
234-
* @throws \Exception
235-
* @throws QueryException
236-
*/
237-
public function onChangePassword(array $parameters) {
238-
/** @var Hooks $hooks */
239-
$hooks = $this->getContainer()->query(Hooks::class);
240-
$hooks->onChangePassword($parameters['uid']);
241-
}
242-
243-
/**
244-
* @param array $parameters
245-
* @throws \InvalidArgumentException
246-
* @throws \BadMethodCallException
247-
* @throws \Exception
248-
* @throws QueryException
249-
*/
250-
public function onChangeInfo(array $parameters) {
251-
if ($parameters['feature'] !== 'eMailAddress') {
252-
return;
253-
}
254-
255-
/** @var Hooks $hooks */
256-
$hooks = $this->getContainer()->query(Hooks::class);
257-
$hooks->onChangeEmail($parameters['user'], $parameters['old_value']);
258229
}
259230
}

apps/settings/lib/Hooks.php

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
57
* SPDX-License-Identifier: AGPL-3.0-or-later
@@ -9,6 +11,8 @@
911
use OCA\Settings\Activity\Provider;
1012
use OCP\Activity\IManager as IActivityManager;
1113
use OCP\Defaults;
14+
use OCP\EventDispatcher\Event;
15+
use OCP\EventDispatcher\IEventListener;
1216
use OCP\IConfig;
1317
use OCP\IGroupManager;
1418
use OCP\IURLGenerator;
@@ -17,8 +21,13 @@
1721
use OCP\IUserSession;
1822
use OCP\L10N\IFactory;
1923
use OCP\Mail\IMailer;
24+
use OCP\User\Events\PasswordUpdatedEvent;
25+
use OCP\User\Events\UserChangedEvent;
2026

21-
class Hooks {
27+
/**
28+
* @template-implements IEventListener<PasswordUpdatedEvent|UserChangedEvent>
29+
*/
30+
class Hooks implements IEventListener {
2231

2332
public function __construct(
2433
protected IActivityManager $activityManager,
@@ -33,16 +42,19 @@ public function __construct(
3342
) {
3443
}
3544

36-
/**
37-
* @param string $uid
38-
* @throws \InvalidArgumentException
39-
* @throws \BadMethodCallException
40-
* @throws \Exception
41-
*/
42-
public function onChangePassword($uid) {
43-
$user = $this->userManager->get($uid);
45+
public function handle(Event $event): void {
46+
if ($event instanceof PasswordUpdatedEvent) {
47+
$this->onChangePassword($event);
48+
}
49+
if ($event instanceof UserChangedEvent) {
50+
$this->onChangeEmail($event);
51+
}
52+
}
4453

45-
if (!$user instanceof IUser || $user->getLastLogin() === 0) {
54+
public function onChangePassword(PasswordUpdatedEvent $handle): void {
55+
$user = $handle->getUser();
56+
57+
if ($user->getLastLogin() === 0) {
4658
// User didn't login, so don't create activities and emails.
4759
return;
4860
}
@@ -89,6 +101,7 @@ public function onChangePassword($uid) {
89101
'displayname' => $user->getDisplayName(),
90102
'emailAddress' => $user->getEMailAddress(),
91103
'instanceUrl' => $instanceUrl,
104+
'event' => $handle,
92105
]);
93106

94107
$template->setSubject($l->t('Password for %1$s changed on %2$s', [$user->getDisplayName(), $instanceName]));
@@ -105,13 +118,14 @@ public function onChangePassword($uid) {
105118
}
106119
}
107120

108-
/**
109-
* @param IUser $user
110-
* @param string|null $oldMailAddress
111-
* @throws \InvalidArgumentException
112-
* @throws \BadMethodCallException
113-
*/
114-
public function onChangeEmail(IUser $user, $oldMailAddress) {
121+
public function onChangeEmail(UserChangedEvent $handle): void {
122+
if ($handle->getFeature() !== 'eMailAddress') {
123+
return;
124+
}
125+
126+
$oldMailAddress = $handle->getOldValue();
127+
$user = $handle->getUser();
128+
115129
if ($oldMailAddress === $user->getEMailAddress()
116130
|| $user->getLastLogin() === 0) {
117131
// Email didn't really change or user didn't login,

build/psalm-baseline.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,8 +2274,6 @@
22742274
<code><![CDATA[IAppContainer]]></code>
22752275
</DeprecatedInterface>
22762276
<DeprecatedMethod>
2277-
<code><![CDATA[Util::connectHook('OC_User', 'changeUser', $this, 'onChangeInfo')]]></code>
2278-
<code><![CDATA[Util::connectHook('OC_User', 'post_setPassword', $this, 'onChangePassword')]]></code>
22792277
<code><![CDATA[getConfig]]></code>
22802278
<code><![CDATA[getCrypto]]></code>
22812279
<code><![CDATA[getL10NFactory]]></code>

0 commit comments

Comments
 (0)