Skip to content

Commit

Permalink
Add typed events for all user hooks and legacy events
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
  • Loading branch information
ChristophWurst committed Dec 10, 2019
1 parent c9d77b7 commit 6dfb31a
Show file tree
Hide file tree
Showing 30 changed files with 1,038 additions and 374 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OCA\Files_External\NotFoundException;
use OCA\Files_External\Service\StoragesService;
use OCA\Files_External\Service\UserGlobalStoragesService;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserSession;
Expand Down Expand Up @@ -65,7 +66,7 @@ protected function setUp(): void {

$this->globalStoragesService = $this->service;

$this->user = new \OC\User\User(self::USER_ID, null, \OC::$server->getEventDispatcher());
$this->user = new \OC\User\User(self::USER_ID, null, \OC::$server->query(IEventDispatcher::class));
/** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject $userSession */
$userSession = $this->createMock(IUserSession::class);
$userSession
Expand Down
2 changes: 0 additions & 2 deletions apps/user_ldap/tests/LDAPProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IServerContainer;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* Class LDAPProviderTest
Expand Down Expand Up @@ -78,7 +77,6 @@ private function getUserManagerMock(IUserLDAP $userBackend) {
->setMethods(['getBackends'])
->setConstructorArgs([
$this->createMock(IConfig::class),
$this->createMock(EventDispatcherInterface::class),
$this->createMock(IEventDispatcher::class)
])
->getMock();
Expand Down
14 changes: 12 additions & 2 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,19 @@
'OCP\\User\\Backend\\IProvideAvatarBackend' => $baseDir . '/lib/public/User/Backend/IProvideAvatarBackend.php',
'OCP\\User\\Backend\\ISetDisplayNameBackend' => $baseDir . '/lib/public/User/Backend/ISetDisplayNameBackend.php',
'OCP\\User\\Backend\\ISetPasswordBackend' => $baseDir . '/lib/public/User/Backend/ISetPasswordBackend.php',
'OCP\\User\\Events\\CreateUserEvent' => $baseDir . '/lib/public/User/Events/CreateUserEvent.php',
'OCP\\User\\Events\\PostLoginEvent' => $baseDir . '/lib/public/User/Events/PostLoginEvent.php',
'OCP\\User\\Events\\BeforePasswordUpdatedEvent' => $baseDir . '/lib/public/User/Events/BeforePasswordUpdatedEvent.php',
'OCP\\User\\Events\\BeforeUserCreatedEvent' => $baseDir . '/lib/public/User/Events/BeforeUserCreatedEvent.php',
'OCP\\User\\Events\\BeforeUserDeletedEvent' => $baseDir . '/lib/public/User/Events/BeforeUserDeletedEvent.php',
'OCP\\User\\Events\\BeforeUserLoggedInEvent' => $baseDir . '/lib/public/User/Events/BeforeUserLoggedInEvent.php',
'OCP\\User\\Events\\BeforeUserLoggedInWithCookieEvent' => $baseDir . '/lib/public/User/Events/BeforeUserLoggedInWithCookieEvent.php',
'OCP\\User\\Events\\BeforeUserLoggedOutEvent' => $baseDir . '/lib/public/User/Events/BeforeUserLoggedOutEvent.php',
'OCP\\User\\Events\\PasswordUpdatedEvent' => $baseDir . '/lib/public/User/Events/PasswordUpdatedEvent.php',
'OCP\\User\\Events\\UserChangedEvent' => $baseDir . '/lib/public/User/Events/UserChangedEvent.php',
'OCP\\User\\Events\\UserCreatedEvent' => $baseDir . '/lib/public/User/Events/UserCreatedEvent.php',
'OCP\\User\\Events\\UserDeletedEvent' => $baseDir . '/lib/public/User/Events/UserDeletedEvent.php',
'OCP\\User\\Events\\UserLoggedInEvent' => $baseDir . '/lib/public/User/Events/UserLoggedInEvent.php',
'OCP\\User\\Events\\UserLoggedInWithCookieEvent' => $baseDir . '/lib/public/User/Events/UserLoggedInWithCookieEvent.php',
'OCP\\User\\Events\\UserLoggedOutEvent' => $baseDir . '/lib/public/User/Events/UserLoggedOutEvent.php',
'OCP\\Util' => $baseDir . '/lib/public/Util.php',
'OCP\\WorkflowEngine\\EntityContext\\IDisplayName' => $baseDir . '/lib/public/WorkflowEngine/EntityContext/IDisplayName.php',
'OCP\\WorkflowEngine\\EntityContext\\IDisplayText' => $baseDir . '/lib/public/WorkflowEngine/EntityContext/IDisplayText.php',
Expand Down
14 changes: 12 additions & 2 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,9 +481,19 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\User\\Backend\\IProvideAvatarBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/IProvideAvatarBackend.php',
'OCP\\User\\Backend\\ISetDisplayNameBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/ISetDisplayNameBackend.php',
'OCP\\User\\Backend\\ISetPasswordBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/ISetPasswordBackend.php',
'OCP\\User\\Events\\CreateUserEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/CreateUserEvent.php',
'OCP\\User\\Events\\PostLoginEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/PostLoginEvent.php',
'OCP\\User\\Events\\BeforePasswordUpdatedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/BeforePasswordUpdatedEvent.php',
'OCP\\User\\Events\\BeforeUserCreatedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/BeforeUserCreatedEvent.php',
'OCP\\User\\Events\\BeforeUserDeletedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/BeforeUserDeletedEvent.php',
'OCP\\User\\Events\\BeforeUserLoggedInEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/BeforeUserLoggedInEvent.php',
'OCP\\User\\Events\\BeforeUserLoggedInWithCookieEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/BeforeUserLoggedInWithCookieEvent.php',
'OCP\\User\\Events\\BeforeUserLoggedOutEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/BeforeUserLoggedOutEvent.php',
'OCP\\User\\Events\\PasswordUpdatedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/PasswordUpdatedEvent.php',
'OCP\\User\\Events\\UserChangedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserChangedEvent.php',
'OCP\\User\\Events\\UserCreatedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserCreatedEvent.php',
'OCP\\User\\Events\\UserDeletedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserDeletedEvent.php',
'OCP\\User\\Events\\UserLoggedInEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserLoggedInEvent.php',
'OCP\\User\\Events\\UserLoggedInWithCookieEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserLoggedInWithCookieEvent.php',
'OCP\\User\\Events\\UserLoggedOutEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserLoggedOutEvent.php',
'OCP\\Util' => __DIR__ . '/../../..' . '/lib/public/Util.php',
'OCP\\WorkflowEngine\\EntityContext\\IDisplayName' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/EntityContext/IDisplayName.php',
'OCP\\WorkflowEngine\\EntityContext\\IDisplayText' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/EntityContext/IDisplayText.php',
Expand Down
4 changes: 2 additions & 2 deletions lib/private/Files/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
use OC\User\User;
use OCA\Files_Sharing\SharedMount;
use OCP\Constants;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\EmptyFileNameException;
use OCP\Files\FileNameTooLongException;
Expand Down Expand Up @@ -1308,9 +1309,8 @@ private function getUserObjectForOwner($ownerId) {
$owner = $this->userManager->get($ownerId);
if ($owner instanceof IUser) {
return $owner;
} else {
return new User($ownerId, null, \OC::$server->getEventDispatcher());
}
return new User($ownerId, null, \OC::$server->query(IEventDispatcher::class));
}

/**
Expand Down
162 changes: 129 additions & 33 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@
use OCP\RichObjectStrings\IValidator;
use OCP\Security\IContentSecurityPolicyManager;
use OCP\Share\IShareHelper;
use OCP\User\Events\BeforePasswordUpdatedEvent;
use OCP\User\Events\BeforeUserCreatedEvent;
use OCP\User\Events\BeforeUserDeletedEvent;
use OCP\User\Events\BeforeUserLoggedInEvent;
use OCP\User\Events\BeforeUserLoggedInWithCookieEvent;
use OCP\User\Events\BeforeUserLoggedOutEvent;
use OCP\User\Events\PasswordUpdatedEvent;
use OCP\User\Events\UserChangedEvent;
use OCP\User\Events\UserCreatedEvent;
use OCP\User\Events\UserDeletedEvent;
use OCP\User\Events\UserLoggedInEvent;
use OCP\User\Events\UserLoggedInWithCookieEvent;
use OCP\User\Events\UserLoggedOutEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

Expand Down Expand Up @@ -380,8 +393,9 @@ public function __construct($webRoot, \OC\Config $config) {
$defaultTokenProvider = null;
}

$dispatcher = $c->getEventDispatcher();

$symfonyDispatcher = $c->getEventDispatcher();
/** @var IEventDispatcher $eventDispatcher */
$eventDispatcher = $c->query(IEventDispatcher::class);
$userSession = new \OC\User\Session(
$manager,
$session,
Expand All @@ -393,48 +407,129 @@ public function __construct($webRoot, \OC\Config $config) {
$c->getLogger(),
$c->query(IEventDispatcher::class)
);
$userSession->listen('\OC\User', 'preCreateUser', function ($uid, $password) {
\OC_Hook::emit('OC_User', 'pre_createUser', array('run' => true, 'uid' => $uid, 'password' => $password));

// Convert new, typed events to legacy Symfony events and hooks
$eventDispatcher->addListener(BeforeUserCreatedEvent::class, function(BeforeUserCreatedEvent $event) use ($c) {
$c->getUserManager()->emit('\OC\User', 'preCreateUser', [$event->getUid(), $event->getPassword()]);
\OC_Hook::emit('OC_User', 'pre_createUser', [
'run' => true,
'uid' => $event->getUid(),
'password' => $event->getPassword(),
]);
});
$eventDispatcher->addListener(UserCreatedEvent::class, function(UserCreatedEvent $event) use ($c) {
$c->getUserManager()->emit('\OC\User', 'postCreateUser', [$event->getUser(), $event->getPassword()]);
\OC_Hook::emit('OC_User', 'post_createUser', [
'uid' => $event->getUid(),
'password' => $event->getPassword(),
]);
});
$userSession->listen('\OC\User', 'postCreateUser', function ($user, $password) {
/** @var $user \OC\User\User */
\OC_Hook::emit('OC_User', 'post_createUser', array('uid' => $user->getUID(), 'password' => $password));
$eventDispatcher->addListener(BeforePasswordUpdatedEvent::class, function(BeforePasswordUpdatedEvent $event) use ($symfonyDispatcher, $c) {
$symfonyDispatcher->dispatch(IUser::class . '::preSetPassword', new GenericEvent($event->getUser(), [
'password' => $event->getPassword(),
'recoveryPassword' => $event->getRecoveryPassword(),
]));
$c->getUserManager()->emit('\OC\User', 'preSetPassword', [
$event->getUser(),
$event->getPassword(),
$event->getRecoveryPassword()
]);
});
$userSession->listen('\OC\User', 'preDelete', function ($user) use ($dispatcher) {
/** @var $user \OC\User\User */
\OC_Hook::emit('OC_User', 'pre_deleteUser', array('run' => true, 'uid' => $user->getUID()));
$dispatcher->dispatch('OCP\IUser::preDelete', new GenericEvent($user));
$eventDispatcher->addListener(PasswordUpdatedEvent::class, function(PasswordUpdatedEvent $event) use ($symfonyDispatcher, $c) {
$symfonyDispatcher->dispatch(IUser::class . '::postSetPassword', new GenericEvent($event->getUser(), [
'password' => $event->getPassword(),
'recoveryPassword' => $event->getRecoveryPassword(),
]));
$c->getUserManager()->emit('\OC\User', 'postSetPassword', [
$event->getUser(),
$event->getPassword(),
$event->getRecoveryPassword()
]);
\OC_Hook::emit('OC_User', 'post_setPassword', [
'run' => true,
'uid' => $event->getUser()->getUID(),
'password' => $event->getPassword(),
'recoveryPassword' => $event->getRecoveryPassword(),
]);
});
$userSession->listen('\OC\User', 'postDelete', function ($user) {
/** @var $user \OC\User\User */
\OC_Hook::emit('OC_User', 'post_deleteUser', array('uid' => $user->getUID()));
$eventDispatcher->addListener(BeforeUserDeletedEvent::class, function(BeforeUserDeletedEvent $event) use ($symfonyDispatcher, $c) {
$symfonyDispatcher->dispatch(IUser::class . '::preDelete', new GenericEvent($event->getUser()));
$c->getUserManager()->emit('\OC\User', 'preDelete', [$event->getUser()]);
\OC_Hook::emit('OC_User', 'pre_deleteUser', ['run' => true, 'uid' => $event->getUser()->getUID()]);
});
$userSession->listen('\OC\User', 'preSetPassword', function ($user, $password, $recoveryPassword) {
/** @var $user \OC\User\User */
\OC_Hook::emit('OC_User', 'pre_setPassword', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword));
$eventDispatcher->addListener(UserDeletedEvent::class, function(UserDeletedEvent $event) use ($symfonyDispatcher, $c) {
$symfonyDispatcher->dispatch(IUser::class . '::postDelete', new GenericEvent($event->getUser()));
$c->getUserManager()->emit('\OC\User', 'postDelete', [$event->getUser()]);
\OC_Hook::emit('OC_User', 'post_deleteUser', ['uid' => $event->getUser()->getUID()]);
});
$userSession->listen('\OC\User', 'postSetPassword', function ($user, $password, $recoveryPassword) {
/** @var $user \OC\User\User */
\OC_Hook::emit('OC_User', 'post_setPassword', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword));
$eventDispatcher->addListener(UserChangedEvent::class, function(UserChangedEvent $event) use ($c, $symfonyDispatcher) {
$symfonyDispatcher->dispatch(IUser::class . '::changeUser', new GenericEvent($event->getUser(), [
'feature' => $event->getFeature(),
'value' => $event->getValue(),
'oldValue' => $event->getOldValue(),
]));
$c->getUserManager()->emit('\OC\User', 'changeUser', [
$event->getUser(),
$event->getFeature(),
$event->getValue(),
$event->getOldValue(),
]);
\OC_Hook::emit('OC_User', 'changeUser', [
'run' => true,
'user' => $event->getUser(),
'feature' => $event->getFeature(),
'value' => $event->getValue(),
'old_value' => $event->getOldValue(),
]);
});
$userSession->listen('\OC\User', 'preLogin', function ($uid, $password) {
\OC_Hook::emit('OC_User', 'pre_login', array('run' => true, 'uid' => $uid, 'password' => $password));
$eventDispatcher->addListener(BeforeUserLoggedInEvent::class, function(BeforeUserLoggedInEvent $event) use ($c) {
$c->getUserManager()->emit('\OC\User', 'preLogin', [
$event->getUsername(),
$event->getPassword(),
]);
\OC_Hook::emit('OC_User', 'pre_login', [
'run' => true,
'uid' => $event->getUsername(),
'password' => $event->getPassword(),
]);
});
$userSession->listen('\OC\User', 'postLogin', function ($user, $password, $isTokenLogin) {
/** @var $user \OC\User\User */
\OC_Hook::emit('OC_User', 'post_login', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'isTokenLogin' => $isTokenLogin));
$eventDispatcher->addListener(UserLoggedInEvent::class, function(UserLoggedInEvent $event) use ($c) {
$c->getUserManager()->emit('\OC\User', 'postLogin', [
$event->getUser(),
$event->getPassword(),
$event->isTokenLogin(),
]);
\OC_Hook::emit('OC_User', 'post_login', [
'run' => true,
'uid' => $event->getUser()->getUID(),
'password' => $event->getPassword(),
'isTokenLogin' => $event->isTokenLogin(),
]);
});
$userSession->listen('\OC\User', 'postRememberedLogin', function ($user, $password) {
/** @var $user \OC\User\User */
\OC_Hook::emit('OC_User', 'post_login', array('run' => true, 'uid' => $user->getUID(), 'password' => $password));
$eventDispatcher->addListener(BeforeUserLoggedInWithCookieEvent::class, function(BeforeUserLoggedInWithCookieEvent $event) use ($c) {
$c->getUserManager()->emit('\OC\User', 'preRememberedLogin', [
$event->getUsername(),
]);
});
$userSession->listen('\OC\User', 'logout', function () {
\OC_Hook::emit('OC_User', 'logout', array());
$eventDispatcher->addListener(UserLoggedInWithCookieEvent::class, function(UserLoggedInWithCookieEvent $event) use ($c) {
$c->getUserManager()->emit('\OC\User', 'postRememberedLogin', [
$event->getUser(),
$event->getPassword(),
]);
\OC_Hook::emit('OC_User', 'post_login', [
'run' => true,
'uid' => $event->getUser()->getUID(),
'password' => $event->getPassword(),
]);
});
$userSession->listen('\OC\User', 'changeUser', function ($user, $feature, $value, $oldValue) {
/** @var $user \OC\User\User */
\OC_Hook::emit('OC_User', 'changeUser', array('run' => true, 'user' => $user, 'feature' => $feature, 'value' => $value, 'old_value' => $oldValue));
$eventDispatcher->addListener(BeforeUserLoggedOutEvent::class, function(BeforeUserLoggedOutEvent $event) use ($c) {
$c->getUserManager()->emit('\OC\User', 'logout');
\OC_Hook::emit('OC_User', 'logout', []);
});
$eventDispatcher->addListener(UserLoggedOutEvent::class, function(UserLoggedOutEvent $event) use ($c) {
$c->getUserManager()->emit('\OC\User', 'postLogout');
});

return $userSession;
});
$this->registerAlias(\OCP\IUserSession::class, \OC\User\Session::class);
Expand Down Expand Up @@ -1848,6 +1943,7 @@ public function getCapabilitiesManager() {
*
* @return EventDispatcherInterface
* @since 8.2.0
* @deprecated inject or query \OCP\EventDispatcher\IEventDispatcher instead
*/
public function getEventDispatcher() {
return $this->query(\OC\EventDispatcher\SymfonyAdapter::class);
Expand Down
Loading

0 comments on commit 6dfb31a

Please sign in to comment.