Skip to content

Commit 0e70391

Browse files
committed
refactor(hooks): Remove all non-used user hooks
And replace the few remaining by converting the event to hooks instead of the other way around. Signed-off-by: Carl Schwan <carlschwan@kde.org>
1 parent 1fcaa3b commit 0e70391

33 files changed

Lines changed: 240 additions & 388 deletions

File tree

apps/dashboard/composer/composer/installed.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
'name' => '__root__',
44
'pretty_version' => 'dev-master',
55
'version' => 'dev-master',
6-
'reference' => 'b1797842784b250fb01ed5e3bf130705eb94751b',
6+
'reference' => '707699d6351faa181d14cf50abe6be4343938400',
77
'type' => 'library',
88
'install_path' => __DIR__ . '/../',
99
'aliases' => array(),
@@ -13,7 +13,7 @@
1313
'__root__' => array(
1414
'pretty_version' => 'dev-master',
1515
'version' => 'dev-master',
16-
'reference' => 'b1797842784b250fb01ed5e3bf130705eb94751b',
16+
'reference' => '707699d6351faa181d14cf50abe6be4343938400',
1717
'type' => 'library',
1818
'install_path' => __DIR__ . '/../',
1919
'aliases' => array(),

apps/files_sharing/lib/AppInfo/Application.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ function () use ($c) {
121121
$context->registerEventListener(UserRemovedEvent::class, SharesUpdatedListener::class);
122122
$context->registerEventListener(UserShareAccessUpdatedEvent::class, SharesUpdatedListener::class);
123123

124+
$context->registerEventListener(UserDeletedEvent::class, SharesUpdatedListener::class);
125+
124126
$context->registerConfigLexicon(ConfigLexicon::class);
125127
}
126128

apps/files_sharing/lib/Helper.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
use OCP\Util;
1616

1717
class Helper {
18-
public static function registerHooks() {
18+
public static function registerHooks(): void {
1919
Util::connectHook('OC_Filesystem', 'post_rename', '\OCA\Files_Sharing\Updater', 'renameHook');
2020
Util::connectHook('OC_Filesystem', 'post_delete', '\OCA\Files_Sharing\Hooks', 'unshareChildren');
21-
22-
Util::connectHook('OC_User', 'post_deleteUser', '\OCA\Files_Sharing\Hooks', 'deleteUser');
2321
}
2422

2523
/**

apps/files_sharing/lib/Hooks.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,8 @@
99

1010
use OC\Files\Filesystem;
1111
use OC\Files\View;
12-
use OCP\IUserManager;
13-
use OCP\Server;
1412

1513
class Hooks {
16-
public static function deleteUser(array $params): void {
17-
$userManager = Server::get(IUserManager::class);
18-
$user = $userManager->get($params['uid']);
19-
if ($user === null) {
20-
return;
21-
}
22-
23-
$manager = Server::get(External\Manager::class);
24-
$manager->removeUserShares($user);
25-
}
26-
2714
public static function unshareChildren(array $params): void {
2815
$path = Filesystem::getView()->getAbsolutePath($params['path']);
2916
$view = new View('/');

apps/files_sharing/lib/Listener/SharesUpdatedListener.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace OCA\Files_Sharing\Listener;
1010

1111
use OCA\Files_Sharing\Event\UserShareAccessUpdatedEvent;
12+
use OCA\Files_Sharing\External\Manager as ExternalManager;
1213
use OCA\Files_Sharing\MountProvider;
1314
use OCA\Files_Sharing\ShareTargetValidator;
1415
use OCP\EventDispatcher\Event;
@@ -23,11 +24,12 @@
2324
use OCP\Share\Events\ShareCreatedEvent;
2425
use OCP\Share\Events\ShareTransferredEvent;
2526
use OCP\Share\IManager;
27+
use OCP\User\Events\UserDeletedEvent;
2628

2729
/**
2830
* Listen to various events that can change what shares a user has access to
2931
*
30-
* @template-implements IEventListener<UserAddedEvent|UserRemovedEvent|ShareCreatedEvent|ShareTransferredEvent|BeforeShareDeletedEvent|UserShareAccessUpdatedEvent>
32+
* @template-implements IEventListener<UserAddedEvent|UserRemovedEvent|ShareCreatedEvent|ShareTransferredEvent|BeforeShareDeletedEvent|UserShareAccessUpdatedEvent|UserDeletedEvent>
3133
*/
3234
class SharesUpdatedListener implements IEventListener {
3335
private array $inUpdate = [];
@@ -38,6 +40,7 @@ public function __construct(
3840
private readonly MountProvider $shareMountProvider,
3941
private readonly ShareTargetValidator $shareTargetValidator,
4042
private readonly IStorageFactory $storageFactory,
43+
private readonly ExternalManager $externalManager,
4144
) {
4245
}
4346
public function handle(Event $event): void {
@@ -62,6 +65,10 @@ public function handle(Event $event): void {
6265
$this->updateForUser($user, false, [$event->getShare()]);
6366
}
6467
}
68+
69+
if ($event instanceof UserDeletedEvent) {
70+
$this->deleteUser($event);
71+
}
6572
}
6673

6774
private function updateForUser(IUser $user, bool $verifyMountPoints, array $ignoreShares = []): void {
@@ -97,4 +104,8 @@ private function updateForUser(IUser $user, bool $verifyMountPoints, array $igno
97104

98105
unset($this->inUpdate[$user->getUID()]);
99106
}
107+
108+
public function deleteUser(UserDeletedEvent $event): void {
109+
$this->externalManager->removeUserShares($event->getUser());
110+
}
100111
}

apps/user_ldap/lib/AppInfo/Application.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ function (ContainerInterface $c) {
6767
$c->get(IUserManager::class),
6868
$c->get(INotificationManager::class),
6969
$c->get(IShareManager::class),
70+
$c->get(IEventDispatcher::class),
7071
);
7172
},
7273
// the instance is specific to a lazy bound Access instance, thus cannot be shared.

apps/user_ldap/lib/User/Manager.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use OCP\AppFramework\Services\IAppConfig;
1212
use OCP\Cache\CappedMemoryCache;
1313
use OCP\Config\IUserConfig;
14+
use OCP\EventDispatcher\IEventDispatcher;
1415
use OCP\IAvatarManager;
1516
use OCP\IConfig;
1617
use OCP\IDBConnection;
@@ -44,6 +45,7 @@ public function __construct(
4445
protected IUserManager $userManager,
4546
protected INotificationManager $notificationManager,
4647
private IManager $shareManager,
48+
private IEventDispatcher $eventDispatcher,
4749
) {
4850
$this->usersByDN = new CappedMemoryCache();
4951
$this->usersByUid = new CappedMemoryCache();
@@ -52,9 +54,8 @@ public function __construct(
5254
/**
5355
* Binds manager to an instance of Access.
5456
* It needs to be assigned first before the manager can be used.
55-
* @param Access
5657
*/
57-
public function setLdapAccess(Access $access) {
58+
public function setLdapAccess(Access $access): void {
5859
$this->access = $access;
5960
}
6061

@@ -63,24 +64,22 @@ public function setLdapAccess(Access $access) {
6364
* property array
6465
* @param string $dn the DN of the user
6566
* @param string $uid the internal (owncloud) username
66-
* @return User
6767
*/
68-
private function createAndCache($dn, $uid) {
68+
private function createAndCache(string $dn, string $uid): User {
6969
$this->checkAccess();
7070
$user = new User($uid, $dn, $this->access, $this->ocConfig, $this->userConfig, $this->appConfig,
7171
clone $this->image, $this->logger,
7272
$this->avatarManager, $this->userManager,
73-
$this->notificationManager);
73+
$this->notificationManager, $this->eventDispatcher);
7474
$this->usersByDN[$dn] = $user;
7575
$this->usersByUid[$uid] = $user;
7676
return $user;
7777
}
7878

7979
/**
80-
* removes a user entry from the cache
81-
* @param $uid
80+
* Removes a user entry from the cache.
8281
*/
83-
public function invalidate($uid) {
82+
public function invalidate(string $uid): void {
8483
if (!isset($this->usersByUid[$uid])) {
8584
return;
8685
}
@@ -93,9 +92,8 @@ public function invalidate($uid) {
9392
* @brief checks whether the Access instance has been set
9493
* @throws \Exception if Access has not been set
9594
* @psalm-assert !null $this->access
96-
* @return null
9795
*/
98-
private function checkAccess() {
96+
private function checkAccess(): void {
9997
if (is_null($this->access)) {
10098
throw new \Exception('LDAP Access instance must be set first');
10199
}

apps/user_ldap/lib/User/User.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use OCP\Accounts\PropertyDoesNotExistException;
1919
use OCP\AppFramework\Services\IAppConfig;
2020
use OCP\Config\IUserConfig;
21+
use OCP\EventDispatcher\IEventDispatcher;
2122
use OCP\IAvatarManager;
2223
use OCP\IConfig;
2324
use OCP\Image;
@@ -27,6 +28,8 @@
2728
use OCP\Notification\IManager as INotificationManager;
2829
use OCP\PreConditionNotMetException;
2930
use OCP\Server;
31+
use OCP\User\Events\UserLoggedInEvent;
32+
use OCP\User\Events\UserLoggedInWithCookieEvent;
3033
use OCP\Util;
3134
use Psr\Log\LoggerInterface;
3235

@@ -66,6 +69,7 @@ public function __construct(
6669
protected IAvatarManager $avatarManager,
6770
protected IUserManager $userManager,
6871
protected INotificationManager $notificationManager,
72+
IEventDispatcher $eventDispatcher,
6973
) {
7074
if ($uid === '') {
7175
$logger->error("uid for '$dn' must not be an empty string", ['app' => 'user_ldap']);
@@ -74,7 +78,13 @@ public function __construct(
7478
$this->connection = $this->access->getConnection();
7579
$this->birthdateParser = new BirthdateParserService();
7680

77-
Util::connectHook('OC_User', 'post_login', $this, 'handlePasswordExpiry');
81+
$eventDispatcher->addListener(UserLoggedInWithCookieEvent::class, function (UserLoggedInWithCookieEvent $event) {
82+
$this->handlePasswordExpiry($event->getUser());
83+
});
84+
85+
$eventDispatcher->addListener(UserLoggedInEvent::class, function (UserLoggedInEvent $event) {
86+
$this->handlePasswordExpiry($event->getUser());
87+
});
7888
}
7989

8090
/**
@@ -735,15 +745,15 @@ public function updateExtStorageHome(?string $valueFromLDAP = null):string {
735745
}
736746

737747
/**
738-
* called by a post_login hook to handle password expiry
748+
* Called by a LoggedIn events to handle password expiry
739749
*/
740-
public function handlePasswordExpiry(array $params): void {
750+
public function handlePasswordExpiry(IUser $user): void {
751+
$uid = $user->getUID();
741752
$ppolicyDN = $this->connection->ldapDefaultPPolicyDN;
742753
if (empty($ppolicyDN) || ((int)$this->connection->turnOnPasswordChange !== 1)) {
743754
//password expiry handling disabled
744755
return;
745756
}
746-
$uid = $params['uid'];
747757
if (isset($uid) && $uid === $this->getUsername()) {
748758
//retrieve relevant user attributes
749759
$result = $this->access->search('objectclass=*', $this->dn, ['pwdpolicysubentry', 'pwdgraceusetime', 'pwdreset', 'pwdchangedtime']);

apps/user_ldap/tests/AccessTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ private function getConnectorAndLdapMock(): array {
111111
$this->createMock(Image::class),
112112
$this->createMock(IUserManager::class),
113113
$this->createMock(INotificationManager::class),
114-
$this->shareManager])
115-
->getMock();
114+
$this->shareManager,
115+
$this->dispatcher,
116+
])->getMock();
116117
$helper = Server::get(Helper::class);
117118

118119
return [$lw, $connector, $um, $helper];

apps/user_ldap/tests/User/ManagerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use OCA\User_LDAP\User\User;
1616
use OCP\AppFramework\Services\IAppConfig;
1717
use OCP\Config\IUserConfig;
18+
use OCP\EventDispatcher\IEventDispatcher;
1819
use OCP\IAvatarManager;
1920
use OCP\IConfig;
2021
use OCP\IDBConnection;
@@ -79,7 +80,8 @@ protected function setUp(): void {
7980
$this->image,
8081
$this->ncUserManager,
8182
$this->notificationManager,
82-
$this->shareManager
83+
$this->shareManager,
84+
$this->createMock(IEventDispatcher::class),
8385
);
8486

8587
$this->manager->setLdapAccess($this->access);

0 commit comments

Comments
 (0)