Skip to content

Commit d808f9c

Browse files
Add typed events for all user hooks and legacy events
Signed-off-by: Christoph Wurst <[email protected]>
1 parent 68d5991 commit d808f9c

16 files changed

+903
-8
lines changed

lib/composer/composer/autoload_classmap.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,21 @@
452452
'OCP\\User\\Backend\\IProvideAvatarBackend' => $baseDir . '/lib/public/User/Backend/IProvideAvatarBackend.php',
453453
'OCP\\User\\Backend\\ISetDisplayNameBackend' => $baseDir . '/lib/public/User/Backend/ISetDisplayNameBackend.php',
454454
'OCP\\User\\Backend\\ISetPasswordBackend' => $baseDir . '/lib/public/User/Backend/ISetPasswordBackend.php',
455+
'OCP\\User\\Events\\BeforePasswordUpdatedEvent' => $baseDir . '/lib/public/User/Events/BeforePasswordUpdatedEvent.php',
456+
'OCP\\User\\Events\\BeforeUserCreatedEvent' => $baseDir . '/lib/public/User/Events/BeforeUserCreatedEvent.php',
457+
'OCP\\User\\Events\\BeforeUserDeletedEvent' => $baseDir . '/lib/public/User/Events/BeforeUserDeletedEvent.php',
458+
'OCP\\User\\Events\\BeforeUserLoggedInEvent' => $baseDir . '/lib/public/User/Events/BeforeUserLoggedInEvent.php',
459+
'OCP\\User\\Events\\BeforeUserLoggedInWithCookieEvent' => $baseDir . '/lib/public/User/Events/BeforeUserLoggedInWithCookieEvent.php',
460+
'OCP\\User\\Events\\BeforeUserLoggedOutEvent' => $baseDir . '/lib/public/User/Events/BeforeUserLoggedOutEvent.php',
455461
'OCP\\User\\Events\\CreateUserEvent' => $baseDir . '/lib/public/User/Events/CreateUserEvent.php',
462+
'OCP\\User\\Events\\PasswordUpdatedEvent' => $baseDir . '/lib/public/User/Events/PasswordUpdatedEvent.php',
456463
'OCP\\User\\Events\\PostLoginEvent' => $baseDir . '/lib/public/User/Events/PostLoginEvent.php',
464+
'OCP\\User\\Events\\UserChangedEvent' => $baseDir . '/lib/public/User/Events/UserChangedEvent.php',
457465
'OCP\\User\\Events\\UserCreatedEvent' => $baseDir . '/lib/public/User/Events/UserCreatedEvent.php',
466+
'OCP\\User\\Events\\UserDeletedEvent' => $baseDir . '/lib/public/User/Events/UserDeletedEvent.php',
467+
'OCP\\User\\Events\\UserLoggedInEvent' => $baseDir . '/lib/public/User/Events/UserLoggedInEvent.php',
468+
'OCP\\User\\Events\\UserLoggedInWithCookieEvent' => $baseDir . '/lib/public/User/Events/UserLoggedInWithCookieEvent.php',
469+
'OCP\\User\\Events\\UserLoggedOutEvent' => $baseDir . '/lib/public/User/Events/UserLoggedOutEvent.php',
458470
'OCP\\Util' => $baseDir . '/lib/public/Util.php',
459471
'OCP\\WorkflowEngine\\EntityContext\\IDisplayName' => $baseDir . '/lib/public/WorkflowEngine/EntityContext/IDisplayName.php',
460472
'OCP\\WorkflowEngine\\EntityContext\\IDisplayText' => $baseDir . '/lib/public/WorkflowEngine/EntityContext/IDisplayText.php',

lib/composer/composer/autoload_static.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,9 +481,21 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
481481
'OCP\\User\\Backend\\IProvideAvatarBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/IProvideAvatarBackend.php',
482482
'OCP\\User\\Backend\\ISetDisplayNameBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/ISetDisplayNameBackend.php',
483483
'OCP\\User\\Backend\\ISetPasswordBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/ISetPasswordBackend.php',
484+
'OCP\\User\\Events\\BeforePasswordUpdatedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/BeforePasswordUpdatedEvent.php',
485+
'OCP\\User\\Events\\BeforeUserCreatedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/BeforeUserCreatedEvent.php',
486+
'OCP\\User\\Events\\BeforeUserDeletedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/BeforeUserDeletedEvent.php',
487+
'OCP\\User\\Events\\BeforeUserLoggedInEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/BeforeUserLoggedInEvent.php',
488+
'OCP\\User\\Events\\BeforeUserLoggedInWithCookieEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/BeforeUserLoggedInWithCookieEvent.php',
489+
'OCP\\User\\Events\\BeforeUserLoggedOutEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/BeforeUserLoggedOutEvent.php',
484490
'OCP\\User\\Events\\CreateUserEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/CreateUserEvent.php',
491+
'OCP\\User\\Events\\PasswordUpdatedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/PasswordUpdatedEvent.php',
485492
'OCP\\User\\Events\\PostLoginEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/PostLoginEvent.php',
493+
'OCP\\User\\Events\\UserChangedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserChangedEvent.php',
486494
'OCP\\User\\Events\\UserCreatedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserCreatedEvent.php',
495+
'OCP\\User\\Events\\UserDeletedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserDeletedEvent.php',
496+
'OCP\\User\\Events\\UserLoggedInEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserLoggedInEvent.php',
497+
'OCP\\User\\Events\\UserLoggedInWithCookieEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserLoggedInWithCookieEvent.php',
498+
'OCP\\User\\Events\\UserLoggedOutEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserLoggedOutEvent.php',
487499
'OCP\\Util' => __DIR__ . '/../../..' . '/lib/public/Util.php',
488500
'OCP\\WorkflowEngine\\EntityContext\\IDisplayName' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/EntityContext/IDisplayName.php',
489501
'OCP\\WorkflowEngine\\EntityContext\\IDisplayText' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/EntityContext/IDisplayText.php',

lib/private/Server.php

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@
113113
use OC\Security\CertificateManager;
114114
use OC\Security\CredentialsManager;
115115
use OC\Security\Crypto;
116-
use OC\Security\CSP\ContentSecurityPolicyManager;
117116
use OC\Security\CSP\ContentSecurityPolicyNonceManager;
118117
use OC\Security\CSRF\CsrfTokenGenerator;
119118
use OC\Security\CSRF\CsrfTokenManager;
@@ -163,6 +162,19 @@
163162
use OCP\RichObjectStrings\IValidator;
164163
use OCP\Security\IContentSecurityPolicyManager;
165164
use OCP\Share\IShareHelper;
165+
use OCP\User\Events\BeforePasswordUpdatedEvent;
166+
use OCP\User\Events\BeforeUserCreatedEvent;
167+
use OCP\User\Events\BeforeUserDeletedEvent;
168+
use OCP\User\Events\BeforeUserLoggedInEvent;
169+
use OCP\User\Events\BeforeUserLoggedInWithCookieEvent;
170+
use OCP\User\Events\BeforeUserLoggedOutEvent;
171+
use OCP\User\Events\PasswordUpdatedEvent;
172+
use OCP\User\Events\UserChangedEvent;
173+
use OCP\User\Events\UserCreatedEvent;
174+
use OCP\User\Events\UserDeletedEvent;
175+
use OCP\User\Events\UserLoggedInEvent;
176+
use OCP\User\Events\UserLoggedInWithCookieEvent;
177+
use OCP\User\Events\UserLoggedOutEvent;
166178
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
167179
use Symfony\Component\EventDispatcher\GenericEvent;
168180

@@ -380,7 +392,7 @@ public function __construct($webRoot, \OC\Config $config) {
380392
$defaultTokenProvider = null;
381393
}
382394

383-
$dispatcher = $c->getEventDispatcher();
395+
$legacyDispatcher = $c->getEventDispatcher();
384396

385397
$userSession = new \OC\User\Session(
386398
$manager,
@@ -395,45 +407,99 @@ public function __construct($webRoot, \OC\Config $config) {
395407
);
396408
$userSession->listen('\OC\User', 'preCreateUser', function ($uid, $password) {
397409
\OC_Hook::emit('OC_User', 'pre_createUser', array('run' => true, 'uid' => $uid, 'password' => $password));
410+
411+
/** @var IEventDispatcher $dispatcher */
412+
$dispatcher = $this->query(IEventDispatcher::class);
413+
$dispatcher->dispatchTyped(new BeforeUserCreatedEvent($uid, $password));
398414
});
399415
$userSession->listen('\OC\User', 'postCreateUser', function ($user, $password) {
400416
/** @var $user \OC\User\User */
401417
\OC_Hook::emit('OC_User', 'post_createUser', array('uid' => $user->getUID(), 'password' => $password));
418+
419+
/** @var IEventDispatcher $dispatcher */
420+
$dispatcher = $this->query(IEventDispatcher::class);
421+
$dispatcher->dispatchTyped(new UserCreatedEvent($user, $password));
402422
});
403-
$userSession->listen('\OC\User', 'preDelete', function ($user) use ($dispatcher) {
423+
$userSession->listen('\OC\User', 'preDelete', function ($user) use ($legacyDispatcher) {
404424
/** @var $user \OC\User\User */
405425
\OC_Hook::emit('OC_User', 'pre_deleteUser', array('run' => true, 'uid' => $user->getUID()));
406-
$dispatcher->dispatch('OCP\IUser::preDelete', new GenericEvent($user));
426+
$legacyDispatcher->dispatch('OCP\IUser::preDelete', new GenericEvent($user));
427+
428+
/** @var IEventDispatcher $dispatcher */
429+
$dispatcher = $this->query(IEventDispatcher::class);
430+
$dispatcher->dispatchTyped(new BeforeUserDeletedEvent($user));
407431
});
408432
$userSession->listen('\OC\User', 'postDelete', function ($user) {
409433
/** @var $user \OC\User\User */
410434
\OC_Hook::emit('OC_User', 'post_deleteUser', array('uid' => $user->getUID()));
435+
436+
/** @var IEventDispatcher $dispatcher */
437+
$dispatcher = $this->query(IEventDispatcher::class);
438+
$dispatcher->dispatchTyped(new UserDeletedEvent($user));
411439
});
412440
$userSession->listen('\OC\User', 'preSetPassword', function ($user, $password, $recoveryPassword) {
413441
/** @var $user \OC\User\User */
414442
\OC_Hook::emit('OC_User', 'pre_setPassword', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword));
443+
444+
/** @var IEventDispatcher $dispatcher */
445+
$dispatcher = $this->query(IEventDispatcher::class);
446+
$dispatcher->dispatchTyped(new BeforePasswordUpdatedEvent($user, $password, $recoveryPassword));
415447
});
416448
$userSession->listen('\OC\User', 'postSetPassword', function ($user, $password, $recoveryPassword) {
417449
/** @var $user \OC\User\User */
418450
\OC_Hook::emit('OC_User', 'post_setPassword', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword));
451+
452+
/** @var IEventDispatcher $dispatcher */
453+
$dispatcher = $this->query(IEventDispatcher::class);
454+
$dispatcher->dispatchTyped(new PasswordUpdatedEvent($user, $password, $recoveryPassword));
419455
});
420456
$userSession->listen('\OC\User', 'preLogin', function ($uid, $password) {
421457
\OC_Hook::emit('OC_User', 'pre_login', array('run' => true, 'uid' => $uid, 'password' => $password));
458+
459+
/** @var IEventDispatcher $dispatcher */
460+
$dispatcher = $this->query(IEventDispatcher::class);
461+
$dispatcher->dispatchTyped(new BeforeUserLoggedInEvent($uid, $password));
422462
});
423463
$userSession->listen('\OC\User', 'postLogin', function ($user, $password, $isTokenLogin) {
424464
/** @var $user \OC\User\User */
425465
\OC_Hook::emit('OC_User', 'post_login', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'isTokenLogin' => $isTokenLogin));
466+
467+
/** @var IEventDispatcher $dispatcher */
468+
$dispatcher = $this->query(IEventDispatcher::class);
469+
$dispatcher->dispatchTyped(new UserLoggedInEvent($user, $password, $isTokenLogin));
470+
});
471+
$userSession->listen('\OC\User', 'preRememberedLogin', function ($uid) {
472+
/** @var IEventDispatcher $dispatcher */
473+
$dispatcher = $this->query(IEventDispatcher::class);
474+
$dispatcher->dispatchTyped(new BeforeUserLoggedInWithCookieEvent($uid));
426475
});
427476
$userSession->listen('\OC\User', 'postRememberedLogin', function ($user, $password) {
428477
/** @var $user \OC\User\User */
429478
\OC_Hook::emit('OC_User', 'post_login', array('run' => true, 'uid' => $user->getUID(), 'password' => $password));
479+
480+
/** @var IEventDispatcher $dispatcher */
481+
$dispatcher = $this->query(IEventDispatcher::class);
482+
$dispatcher->dispatchTyped(new UserLoggedInWithCookieEvent($user, $password));
430483
});
431-
$userSession->listen('\OC\User', 'logout', function () {
484+
$userSession->listen('\OC\User', 'logout', function ($user) {
432485
\OC_Hook::emit('OC_User', 'logout', array());
486+
487+
/** @var IEventDispatcher $dispatcher */
488+
$dispatcher = $this->query(IEventDispatcher::class);
489+
$dispatcher->dispatchTyped(new BeforeUserLoggedOutEvent($user));
490+
});
491+
$userSession->listen('\OC\User', 'postLogout', function ($user) {
492+
/** @var IEventDispatcher $dispatcher */
493+
$dispatcher = $this->query(IEventDispatcher::class);
494+
$dispatcher->dispatchTyped(new UserLoggedOutEvent($user));
433495
});
434496
$userSession->listen('\OC\User', 'changeUser', function ($user, $feature, $value, $oldValue) {
435497
/** @var $user \OC\User\User */
436498
\OC_Hook::emit('OC_User', 'changeUser', array('run' => true, 'user' => $user, 'feature' => $feature, 'value' => $value, 'old_value' => $oldValue));
499+
500+
/** @var IEventDispatcher $dispatcher */
501+
$dispatcher = $this->query(IEventDispatcher::class);
502+
$dispatcher->dispatchTyped(new UserChangedEvent($user, $feature, $value, $oldValue));
437503
});
438504
return $userSession;
439505
});

lib/private/User/Session.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -900,9 +900,9 @@ public function createRememberMeToken(IUser $user) {
900900
* logout the user from the session
901901
*/
902902
public function logout() {
903-
$this->manager->emit('\OC\User', 'logout');
904903
$user = $this->getUser();
905-
if (!is_null($user)) {
904+
$this->manager->emit('\OC\User', 'logout', [$user]);
905+
if ($user !== null) {
906906
try {
907907
$this->tokenProvider->invalidateToken($this->session->getId());
908908
} catch (SessionNotAvailableException $ex) {
@@ -914,7 +914,7 @@ public function logout() {
914914
$this->setToken(null);
915915
$this->unsetMagicInCookie();
916916
$this->session->clear();
917-
$this->manager->emit('\OC\User', 'postLogout');
917+
$this->manager->emit('\OC\User', 'postLogout', [$user]);
918918
}
919919

920920
/**
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
* @copyright 2019 Christoph Wurst <[email protected]>
5+
*
6+
* @author 2019 Christoph Wurst <[email protected]>
7+
*
8+
* @license GNU AGPL version 3 or any later version
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Affero General Public License as
12+
* published by the Free Software Foundation, either version 3 of the
13+
* License, or (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*/
23+
24+
namespace OCP\User\Events;
25+
26+
use OCP\EventDispatcher\Event;
27+
use OCP\IUser;
28+
29+
/**
30+
* @since 18.0.0
31+
*/
32+
class BeforePasswordUpdatedEvent extends Event {
33+
34+
/** @var IUser */
35+
private $user;
36+
37+
/** @var string */
38+
private $password;
39+
40+
/** @var string|null */
41+
private $recoveryPassword;
42+
43+
/**
44+
* @param IUser $user
45+
* @param string $password
46+
* @param string|null $recoveryPassword
47+
* @since 18.0.0
48+
*/
49+
public function __construct(IUser $user,
50+
string $password,
51+
string $recoveryPassword = null) {
52+
parent::__construct();
53+
$this->user = $user;
54+
$this->password = $password;
55+
$this->recoveryPassword = $recoveryPassword;
56+
}
57+
58+
/**
59+
* @return IUser
60+
* @since 18.0.0
61+
*/
62+
public function getUser(): IUser {
63+
return $this->user;
64+
}
65+
66+
/**
67+
* @return string
68+
* @since 18.0.0
69+
*/
70+
public function getPassword(): string {
71+
return $this->password;
72+
}
73+
74+
/**
75+
* @return string|null
76+
* @since 18.0.0
77+
*/
78+
public function getRecoveryPassword(): ?string {
79+
return $this->recoveryPassword;
80+
}
81+
82+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright 2019 Christoph Wurst <[email protected]>
7+
*
8+
* @author Christoph Wurst <[email protected]>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCP\User\Events;
28+
29+
use OCP\EventDispatcher\Event;
30+
31+
/**
32+
* @since 18.0.0
33+
*/
34+
class BeforeUserCreatedEvent extends Event {
35+
36+
/** @var string */
37+
private $uid;
38+
39+
/** @var string */
40+
private $password;
41+
42+
/**
43+
* @since 18.0.0
44+
*/
45+
public function __construct(string $uid,
46+
string $password) {
47+
parent::__construct();
48+
$this->uid = $uid;
49+
$this->password = $password;
50+
}
51+
52+
/**
53+
* @since 18.0.0
54+
*/
55+
public function getUid(): string {
56+
return $this->uid;
57+
}
58+
59+
/**
60+
* @since 18.0.0
61+
*/
62+
public function getPassword(): string {
63+
return $this->password;
64+
}
65+
66+
}

0 commit comments

Comments
 (0)