From cc80339b39d998c615a5648d2c86b1e431a8b023 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Wed, 20 Nov 2019 14:22:00 +0100 Subject: [PATCH] Add typed create user events Signed-off-by: Christoph Wurst Signed-off-by: Roeland Jago Douma --- apps/user_ldap/tests/LDAPProviderTest.php | 139 +++++++++--------- lib/composer/composer/autoload_classmap.php | 2 + lib/composer/composer/autoload_static.php | 2 + lib/private/Server.php | 3 - lib/private/User/Manager.php | 22 ++- lib/public/User/Events/CreateUserEvent.php | 63 ++++++++ lib/public/User/Events/UserCreatedEvent.php | 71 +++++++++ tests/lib/Files/Config/UserMountCacheTest.php | 3 +- .../Files/Storage/Wrapper/EncryptionTest.php | 7 +- tests/lib/Files/Stream/EncryptionTest.php | 3 +- tests/lib/User/ManagerTest.php | 78 +++++----- tests/lib/User/SessionTest.php | 52 +++++-- 12 files changed, 308 insertions(+), 137 deletions(-) create mode 100644 lib/public/User/Events/CreateUserEvent.php create mode 100644 lib/public/User/Events/UserCreatedEvent.php diff --git a/apps/user_ldap/tests/LDAPProviderTest.php b/apps/user_ldap/tests/LDAPProviderTest.php index 03756e85cc..3b2365062e 100644 --- a/apps/user_ldap/tests/LDAPProviderTest.php +++ b/apps/user_ldap/tests/LDAPProviderTest.php @@ -30,6 +30,7 @@ use OCA\User_LDAP\Access; use OCA\User_LDAP\Connection; use OCA\User_LDAP\IGroupLDAP; use OCA\User_LDAP\IUserLDAP; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; use OCP\IServerContainer; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -46,7 +47,7 @@ class LDAPProviderTest extends \Test\TestCase { protected function setUp(): void { parent::setUp(); } - + private function getServerMock(IUserLDAP $userBackend, IGroupLDAP $groupBackend) { $server = $this->getMockBuilder('OC\Server') ->setMethods(['getUserManager', 'getBackends', 'getGroupManager']) @@ -71,7 +72,11 @@ class LDAPProviderTest extends \Test\TestCase { private function getUserManagerMock(IUserLDAP $userBackend) { $userManager = $this->getMockBuilder(Manager::class) ->setMethods(['getBackends']) - ->setConstructorArgs([$this->createMock(IConfig::class), $this->createMock(EventDispatcherInterface::class)]) + ->setConstructorArgs([ + $this->createMock(IConfig::class), + $this->createMock(EventDispatcherInterface::class), + $this->createMock(IEventDispatcher::class) + ]) ->getMock(); $userManager->expects($this->any()) ->method('getBackends') @@ -92,9 +97,9 @@ class LDAPProviderTest extends \Test\TestCase { private function getDefaultGroupBackendMock() { $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP') - ->disableOriginalConstructor() + ->disableOriginalConstructor() ->getMock(); - + return $groupBackend; } @@ -102,8 +107,8 @@ class LDAPProviderTest extends \Test\TestCase { $factory = new \OCA\User_LDAP\LDAPProviderFactory($serverContainer); return $factory->getLDAPProvider(); } - - + + public function testGetUserDNUserIDNotFound() { $this->expectException(\Exception::class); $this->expectExceptionMessage('User id not found in LDAP'); @@ -113,13 +118,13 @@ class LDAPProviderTest extends \Test\TestCase { ->disableOriginalConstructor() ->getMock(); $userBackend->expects($this->any())->method('userExists')->willReturn(false); - + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - + $ldapProvider = $this->getLDAPProvider($server); $ldapProvider->getUserDN('nonexisting_user'); } - + public function testGetUserDN() { $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') ->setMethods(['userExists', 'getLDAPAccess', 'username2dn']) @@ -134,15 +139,15 @@ class LDAPProviderTest extends \Test\TestCase { $userBackend->expects($this->any()) ->method($this->anything()) ->willReturnSelf(); - + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - + $ldapProvider = $this->getLDAPProvider($server); - $this->assertEquals('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org', + $this->assertEquals('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org', $ldapProvider->getUserDN('existing_user')); } - + public function testGetGroupDNGroupIDNotFound() { $this->expectException(\Exception::class); $this->expectExceptionMessage('Group id not found in LDAP'); @@ -190,7 +195,7 @@ class LDAPProviderTest extends \Test\TestCase { $ldapProvider = $this->getLDAPProvider($server); $this->assertEquals('cn=existing_group,ou=Are Sufficient To,ou=Test,dc=example,dc=org', $ldapProvider->getGroupDN('existing_group')); - } + } public function testGetUserName() { $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') @@ -200,27 +205,27 @@ class LDAPProviderTest extends \Test\TestCase { $userBackend->expects($this->any()) ->method('dn2UserName') ->willReturn('existing_user'); - + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - + $ldapProvider = $this->getLDAPProvider($server); - $this->assertEquals('existing_user', + $this->assertEquals('existing_user', $ldapProvider->getUserName('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org')); } - + public function testDNasBaseParameter() { $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') ->setMethods([]) ->disableOriginalConstructor() ->getMock(); - + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - + $helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig()); - + $ldapProvider = $this->getLDAPProvider($server); $this->assertEquals( - $helper->DNasBaseParameter('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'), + $helper->DNasBaseParameter('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'), $ldapProvider->DNasBaseParameter('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org')); } @@ -229,18 +234,18 @@ class LDAPProviderTest extends \Test\TestCase { ->setMethods([]) ->disableOriginalConstructor() ->getMock(); - + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - + $helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig()); - + $ldapProvider = $this->getLDAPProvider($server); $this->assertEquals( - $helper->sanitizeDN('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'), + $helper->sanitizeDN('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'), $ldapProvider->sanitizeDN('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org')); } - - + + public function testGetLDAPConnectionUserIDNotFound() { $this->expectException(\Exception::class); $this->expectExceptionMessage('User id not found in LDAP'); @@ -250,13 +255,13 @@ class LDAPProviderTest extends \Test\TestCase { ->disableOriginalConstructor() ->getMock(); $userBackend->expects($this->any())->method('userExists')->willReturn(false); - + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - + $ldapProvider = $this->getLDAPProvider($server); $ldapProvider->getLDAPConnection('nonexisting_user'); } - + public function testGetLDAPConnection() { $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') ->setMethods(['userExists', 'getNewLDAPConnection']) @@ -268,14 +273,14 @@ class LDAPProviderTest extends \Test\TestCase { $userBackend->expects($this->any()) ->method('getNewLDAPConnection') ->willReturn(true); - + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - + $ldapProvider = $this->getLDAPProvider($server); $this->assertTrue($ldapProvider->getLDAPConnection('existing_user')); } - + public function testGetGroupLDAPConnectionGroupIDNotFound() { $this->expectException(\Exception::class); $this->expectExceptionMessage('Group id not found in LDAP'); @@ -320,8 +325,8 @@ class LDAPProviderTest extends \Test\TestCase { $ldapProvider = $this->getLDAPProvider($server); $this->assertTrue($ldapProvider->getGroupLDAPConnection('existing_group')); } - - + + public function testGetLDAPBaseUsersUserIDNotFound() { $this->expectException(\Exception::class); $this->expectExceptionMessage('User id not found in LDAP'); @@ -331,13 +336,13 @@ class LDAPProviderTest extends \Test\TestCase { ->disableOriginalConstructor() ->getMock(); $userBackend->expects($this->any())->method('userExists')->willReturn(false); - + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - + $ldapProvider = $this->getLDAPProvider($server); $ldapProvider->getLDAPBaseUsers('nonexisting_user'); } - + public function testGetLDAPBaseUsers() { $bases = [ 'ou=users,ou=foobar,dc=example,dc=org', @@ -379,12 +384,12 @@ class LDAPProviderTest extends \Test\TestCase { ->willReturn($access); $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - + $ldapProvider = $this->getLDAPProvider($server); $this->assertEquals($bases[1], $ldapProvider->getLDAPBaseUsers('existing_user')); } - - + + public function testGetLDAPBaseGroupsUserIDNotFound() { $this->expectException(\Exception::class); $this->expectExceptionMessage('User id not found in LDAP'); @@ -394,13 +399,13 @@ class LDAPProviderTest extends \Test\TestCase { ->disableOriginalConstructor() ->getMock(); $userBackend->expects($this->any())->method('userExists')->willReturn(false); - + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - + $ldapProvider = $this->getLDAPProvider($server); $ldapProvider->getLDAPBaseGroups('nonexisting_user'); } - + public function testGetLDAPBaseGroups() { $bases = [ 'ou=groupd,ou=foobar,dc=example,dc=org', @@ -435,12 +440,12 @@ class LDAPProviderTest extends \Test\TestCase { ->willReturn($access); $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - + $ldapProvider = $this->getLDAPProvider($server); $this->assertEquals($bases[0], $ldapProvider->getLDAPBaseGroups('existing_user')); } - - + + public function testClearCacheUserIDNotFound() { $this->expectException(\Exception::class); $this->expectExceptionMessage('User id not found in LDAP'); @@ -450,13 +455,13 @@ class LDAPProviderTest extends \Test\TestCase { ->disableOriginalConstructor() ->getMock(); $userBackend->expects($this->any())->method('userExists')->willReturn(false); - + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - + $ldapProvider = $this->getLDAPProvider($server); $ldapProvider->clearCache('nonexisting_user'); } - + public function testClearCache() { $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'clearCache']) @@ -471,15 +476,15 @@ class LDAPProviderTest extends \Test\TestCase { $userBackend->expects($this->any()) ->method($this->anything()) ->willReturnSelf(); - + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - + $ldapProvider = $this->getLDAPProvider($server); $ldapProvider->clearCache('existing_user'); $this->addToAssertionCount(1); } - + public function testClearGroupCacheGroupIDNotFound() { $this->expectException(\Exception::class); $this->expectExceptionMessage('Group id not found in LDAP'); @@ -523,7 +528,7 @@ class LDAPProviderTest extends \Test\TestCase { $ldapProvider->clearGroupCache('existing_group'); $this->addToAssertionCount(1); } - + public function testDnExists() { $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') ->setMethods(['dn2UserName']) @@ -532,40 +537,40 @@ class LDAPProviderTest extends \Test\TestCase { $userBackend->expects($this->any()) ->method('dn2UserName') ->willReturn('existing_user'); - + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - + $ldapProvider = $this->getLDAPProvider($server); $this->assertTrue($ldapProvider->dnExists('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org')); } - + public function testFlagRecord() { $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') ->setMethods([]) ->disableOriginalConstructor() ->getMock(); - + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - + $ldapProvider = $this->getLDAPProvider($server); $ldapProvider->flagRecord('existing_user'); $this->addToAssertionCount(1); } - + public function testUnflagRecord() { $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') ->setMethods([]) ->disableOriginalConstructor() ->getMock(); - + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - + $ldapProvider = $this->getLDAPProvider($server); $ldapProvider->unflagRecord('existing_user'); $this->addToAssertionCount(1); } - + public function testGetLDAPDisplayNameFieldUserIDNotFound() { $this->expectException(\Exception::class); $this->expectExceptionMessage('User id not found in LDAP'); @@ -603,7 +608,7 @@ class LDAPProviderTest extends \Test\TestCase { $this->assertEquals('displayName', $ldapProvider->getLDAPDisplayNameField('existing_user')); } - + public function testGetLDAPEmailFieldUserIDNotFound() { $this->expectException(\Exception::class); $this->expectExceptionMessage('User id not found in LDAP'); @@ -641,7 +646,7 @@ class LDAPProviderTest extends \Test\TestCase { $this->assertEquals('mail', $ldapProvider->getLDAPEmailField('existing_user')); } - + public function testGetLDAPGroupMemberAssocUserIDNotFound() { $this->expectException(\Exception::class); $this->expectExceptionMessage('Group id not found in LDAP'); @@ -687,6 +692,6 @@ class LDAPProviderTest extends \Test\TestCase { $ldapProvider = $this->getLDAPProvider($server); $this->assertEquals('assoc_type', $ldapProvider->getLDAPGroupMemberAssoc('existing_group')); - } + } } diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index cd6b4ca1a8..98bb406c44 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -450,7 +450,9 @@ return array( '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\\UserCreatedEvent' => $baseDir . '/lib/public/User/Events/UserCreatedEvent.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', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 7fae2d92c3..4f10468078 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -479,7 +479,9 @@ 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\\UserCreatedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserCreatedEvent.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', diff --git a/lib/private/Server.php b/lib/private/Server.php index 9fb197fcb1..fa2a521b6b 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -317,9 +317,6 @@ class Server extends ServerContainer implements IServerContainer { }); $this->registerAlias('LazyRootFolder', \OCP\Files\IRootFolder::class); - $this->registerService(\OC\User\Manager::class, function (Server $c) { - return new \OC\User\Manager($c->getConfig(), $c->getEventDispatcher()); - }); $this->registerAlias('UserManager', \OC\User\Manager::class); $this->registerAlias(\OCP\IUserManager::class, \OC\User\Manager::class); diff --git a/lib/private/User/Manager.php b/lib/private/User/Manager.php index da2d354b7e..29cae3da79 100644 --- a/lib/private/User/Manager.php +++ b/lib/private/User/Manager.php @@ -33,12 +33,15 @@ namespace OC\User; use OC\Hooks\PublicEmitter; use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; use OCP\IGroup; use OCP\IUser; use OCP\IUserBackend; use OCP\IUserManager; use OCP\User\Backend\IGetRealUIDBackend; +use OCP\User\Events\CreateUserEvent; +use OCP\User\Events\UserCreatedEvent; use OCP\UserInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -72,17 +75,24 @@ class Manager extends PublicEmitter implements IUserManager { /** @var IConfig */ private $config; + /** @var EventDispatcherInterface */ private $dispatcher; - public function __construct(IConfig $config, EventDispatcherInterface $dispatcher) { + /** @var IEventDispatcher */ + private $eventDispatcher; + + public function __construct(IConfig $config, + EventDispatcherInterface $oldDispatcher, + IEventDispatcher $eventDispatcher) { $this->config = $config; - $this->dispatcher = $dispatcher; + $this->dispatcher = $oldDispatcher; $cachedUsers = &$this->cachedUsers; $this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) { /** @var \OC\User\User $user */ unset($cachedUsers[$user->getUID()]); }); + $this->eventDispatcher = $eventDispatcher; } /** @@ -349,6 +359,7 @@ class Manager extends PublicEmitter implements IUserManager { } $this->emit('\OC\User', 'preCreateUser', [$uid, $password]); + $this->eventDispatcher->dispatchTyped(new CreateUserEvent($uid, $password)); $state = $backend->createUser($uid, $password); if($state === false) { throw new \InvalidArgumentException($l->t('Could not create user')); @@ -356,6 +367,7 @@ class Manager extends PublicEmitter implements IUserManager { $user = $this->getUserObject($uid, $backend); if ($user instanceof IUser) { $this->emit('\OC\User', 'postCreateUser', [$user, $password]); + $this->eventDispatcher->dispatchTyped(new UserCreatedEvent($user, $password)); } return $user; } @@ -460,11 +472,11 @@ class Manager extends PublicEmitter implements IUserManager { ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled'))) ->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR)); - + $result = $queryBuilder->execute(); $count = $result->fetchColumn(); $result->closeCursor(); - + if ($count !== false) { $count = (int)$count; } else { @@ -494,7 +506,7 @@ class Manager extends PublicEmitter implements IUserManager { $result = $queryBuilder->execute(); $count = $result->fetchColumn(); $result->closeCursor(); - + if ($count !== false) { $count = (int)$count; } else { diff --git a/lib/public/User/Events/CreateUserEvent.php b/lib/public/User/Events/CreateUserEvent.php new file mode 100644 index 0000000000..877899fcba --- /dev/null +++ b/lib/public/User/Events/CreateUserEvent.php @@ -0,0 +1,63 @@ + + * + * @author 2019 Christoph Wurst + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace OCP\User\Events; + +use OCP\EventDispatcher\Event; + +/** + * @since 18.0.0 + */ +class CreateUserEvent extends Event { + + /** @var string */ + private $uid; + + /** @var string */ + private $password; + + /** + * @since 18.0.0 + */ + public function __construct(string $uid, + string $password) { + parent::__construct(); + $this->uid = $uid; + $this->password = $password; + } + + /** + * @since 18.0.0 + */ + public function getUid(): string { + return $this->uid; + } + + /** + * @since 18.0.0 + */ + public function getPassword(): string { + return $this->password; + } + +} diff --git a/lib/public/User/Events/UserCreatedEvent.php b/lib/public/User/Events/UserCreatedEvent.php new file mode 100644 index 0000000000..53debf5ff5 --- /dev/null +++ b/lib/public/User/Events/UserCreatedEvent.php @@ -0,0 +1,71 @@ + + * + * @author 2019 Christoph Wurst + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace OCP\User\Events; + +use OCP\EventDispatcher\Event; +use OCP\IUser; + +/** + * @since 18.0.0 + */ +class UserCreatedEvent extends Event { + + /** @var IUser */ + private $user; + + /** @var string */ + private $password; + + /** + * @since 18.0.0 + */ + public function __construct(IUser $user, + string $password) { + parent::__construct(); + $this->user = $user; + $this->password = $password; + } + + /** + * @since 18.0.0 + */ + public function getUser(): IUser { + return $this->user; + } + + /** + * @since 18.0.0 + */ + public function getUid(): string { + return $this->user->getUID(); + } + + /** + * @since 18.0.0 + */ + public function getPassword(): string { + return $this->password; + } + +} diff --git a/tests/lib/Files/Config/UserMountCacheTest.php b/tests/lib/Files/Config/UserMountCacheTest.php index d63693a655..2f61f634ff 100644 --- a/tests/lib/Files/Config/UserMountCacheTest.php +++ b/tests/lib/Files/Config/UserMountCacheTest.php @@ -13,6 +13,7 @@ use OC\Files\Mount\MountPoint; use OC\Files\Storage\Storage; use OC\Log; use OC\User\Manager; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Config\ICachedMountInfo; use OCP\IConfig; use OCP\IDBConnection; @@ -45,7 +46,7 @@ class UserMountCacheTest extends TestCase { protected function setUp(): void { $this->fileIds = []; $this->connection = \OC::$server->getDatabaseConnection(); - $this->userManager = new Manager($this->createMock(IConfig::class), $this->createMock(EventDispatcherInterface::class)); + $this->userManager = new Manager($this->createMock(IConfig::class), $this->createMock(EventDispatcherInterface::class), $this->createMock(IEventDispatcher::class)); $userBackend = new Dummy(); $userBackend->createUser('u1', ''); $userBackend->createUser('u2', ''); diff --git a/tests/lib/Files/Storage/Wrapper/EncryptionTest.php b/tests/lib/Files/Storage/Wrapper/EncryptionTest.php index 95ee0ada24..a9d678e76e 100644 --- a/tests/lib/Files/Storage/Wrapper/EncryptionTest.php +++ b/tests/lib/Files/Storage/Wrapper/EncryptionTest.php @@ -14,6 +14,7 @@ use OC\User\Manager; use OCP\Encryption\IEncryptionModule; use OCP\Encryption\IFile; use OCP\Encryption\Keys\IStorage; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Cache\ICache; use OCP\Files\Mount\IMountPoint; use OCP\IConfig; @@ -131,7 +132,7 @@ class EncryptionTest extends Storage { $this->util = $this->getMockBuilder('\OC\Encryption\Util') ->setMethods(['getUidAndFilename', 'isFile', 'isExcluded']) - ->setConstructorArgs([new View(), new Manager($this->config, $this->createMock(EventDispatcherInterface::class)), $this->groupManager, $this->config, $this->arrayCache]) + ->setConstructorArgs([new View(), new Manager($this->config, $this->createMock(EventDispatcherInterface::class), $this->createMock(IEventDispatcher::class)), $this->groupManager, $this->config, $this->arrayCache]) ->getMock(); $this->util->expects($this->any()) ->method('getUidAndFilename') @@ -568,7 +569,7 @@ class EncryptionTest extends Storage { ->setConstructorArgs( [ new View(), - new Manager($this->config, $this->createMock(EventDispatcherInterface::class)), + new Manager($this->config, $this->createMock(EventDispatcherInterface::class), $this->createMock(IEventDispatcher::class)), $this->groupManager, $this->config, $this->arrayCache @@ -637,7 +638,7 @@ class EncryptionTest extends Storage { ->willReturn($exists); $util = $this->getMockBuilder('\OC\Encryption\Util') - ->setConstructorArgs([new View(), new Manager($this->config, $this->createMock(EventDispatcherInterface::class)), $this->groupManager, $this->config, $this->arrayCache]) + ->setConstructorArgs([new View(), new Manager($this->config, $this->createMock(EventDispatcherInterface::class), $this->createMock(IEventDispatcher::class)), $this->groupManager, $this->config, $this->arrayCache]) ->getMock(); $cache = $this->getMockBuilder('\OC\Files\Cache\Cache') diff --git a/tests/lib/Files/Stream/EncryptionTest.php b/tests/lib/Files/Stream/EncryptionTest.php index 3ded6ef91d..fd5e8c6ca9 100644 --- a/tests/lib/Files/Stream/EncryptionTest.php +++ b/tests/lib/Files/Stream/EncryptionTest.php @@ -5,6 +5,7 @@ namespace Test\Files\Stream; use OC\Files\Cache\CacheEntry; use OC\Files\View; use OC\Memcache\ArrayCache; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Cache\ICache; use OCP\IConfig; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -48,7 +49,7 @@ class EncryptionTest extends \Test\TestCase { $file->expects($this->any())->method('getAccessList')->willReturn([]); $util = $this->getMockBuilder('\OC\Encryption\Util') ->setMethods(['getUidAndFilename']) - ->setConstructorArgs([new View(), new \OC\User\Manager($config, $this->createMock(EventDispatcherInterface::class)), $groupManager, $config, $arrayCache]) + ->setConstructorArgs([new View(), new \OC\User\Manager($config, $this->createMock(EventDispatcherInterface::class), $this->createMock(IEventDispatcher::class)), $groupManager, $config, $arrayCache]) ->getMock(); $util->expects($this->any()) ->method('getUidAndFilename') diff --git a/tests/lib/User/ManagerTest.php b/tests/lib/User/ManagerTest.php index 58a6b55065..40197101fd 100644 --- a/tests/lib/User/ManagerTest.php +++ b/tests/lib/User/ManagerTest.php @@ -11,6 +11,7 @@ namespace Test\User; use OC\AllConfig; use OC\User\Database; use OC\User\Manager; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; use OCP\IUser; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -28,18 +29,21 @@ class ManagerTest extends TestCase { /** @var IConfig */ private $config; /** @var EventDispatcherInterface */ - private $dispatcher; + private $oldDispatcher; + /** @var IEventDispatcher */ + private $eventDispatcher; protected function setUp(): void { parent::setUp(); $this->config = $this->createMock(IConfig::class); - $this->dispatcher = $this->createMock(EventDispatcherInterface::class); + $this->oldDispatcher = $this->createMock(EventDispatcherInterface::class); + $this->eventDispatcher = $this->createMock(IEventDispatcher::class); } public function testGetBackends() { $userDummyBackend = $this->createMock(\Test\Util\User\Dummy::class); - $manager = new \OC\User\Manager($this->config, $this->dispatcher); + $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->eventDispatcher); $manager->registerBackend($userDummyBackend); $this->assertEquals([$userDummyBackend], $manager->getBackends()); $dummyDatabaseBackend = $this->createMock(Database::class); @@ -58,7 +62,7 @@ class ManagerTest extends TestCase { ->with($this->equalTo('foo')) ->will($this->returnValue(true)); - $manager = new \OC\User\Manager($this->config, $this->dispatcher); + $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->eventDispatcher); $manager->registerBackend($backend); $this->assertTrue($manager->userExists('foo')); @@ -74,14 +78,14 @@ class ManagerTest extends TestCase { ->with($this->equalTo('foo')) ->will($this->returnValue(false)); - $manager = new \OC\User\Manager($this->config, $this->dispatcher); + $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->eventDispatcher); $manager->registerBackend($backend); $this->assertFalse($manager->userExists('foo')); } public function testUserExistsNoBackends() { - $manager = new \OC\User\Manager($this->config, $this->dispatcher); + $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->eventDispatcher); $this->assertFalse($manager->userExists('foo')); } @@ -105,7 +109,7 @@ class ManagerTest extends TestCase { ->with($this->equalTo('foo')) ->will($this->returnValue(true)); - $manager = new \OC\User\Manager($this->config, $this->dispatcher); + $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->eventDispatcher); $manager->registerBackend($backend1); $manager->registerBackend($backend2); @@ -129,7 +133,7 @@ class ManagerTest extends TestCase { $backend2->expects($this->never()) ->method('userExists'); - $manager = new \OC\User\Manager($this->config, $this->dispatcher); + $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->eventDispatcher); $manager->registerBackend($backend1); $manager->registerBackend($backend2); @@ -156,7 +160,7 @@ class ManagerTest extends TestCase { } })); - $manager = new \OC\User\Manager($this->config, $this->dispatcher); + $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->eventDispatcher); $manager->registerBackend($backend); $user = $manager->checkPassword('foo', 'bar'); @@ -175,7 +179,7 @@ class ManagerTest extends TestCase { ->method('implementsActions') ->will($this->returnValue(false)); - $manager = new \OC\User\Manager($this->config, $this->dispatcher); + $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->eventDispatcher); $manager->registerBackend($backend); $this->assertFalse($manager->checkPassword('foo', 'bar')); @@ -193,7 +197,7 @@ class ManagerTest extends TestCase { $backend->expects($this->never()) ->method('loginName2UserName'); - $manager = new \OC\User\Manager($this->config, $this->dispatcher); + $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->eventDispatcher); $manager->registerBackend($backend); $this->assertEquals('foo', $manager->get('foo')->getUID()); @@ -209,7 +213,7 @@ class ManagerTest extends TestCase { ->with($this->equalTo('foo')) ->will($this->returnValue(false)); - $manager = new \OC\User\Manager($this->config, $this->dispatcher); + $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->eventDispatcher); $manager->registerBackend($backend); $this->assertEquals(null, $manager->get('foo')); @@ -227,7 +231,7 @@ class ManagerTest extends TestCase { $backend->expects($this->never()) ->method('loginName2UserName'); - $manager = new \OC\User\Manager($this->config, $this->dispatcher); + $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->eventDispatcher); $manager->registerBackend($backend); $this->assertEquals('bLeNdEr', $manager->get('bLeNdEr')->getUID()); @@ -245,7 +249,7 @@ class ManagerTest extends TestCase { $backend->expects($this->never()) ->method('loginName2UserName'); - $manager = new \OC\User\Manager($this->config, $this->dispatcher); + $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->eventDispatcher); $manager->registerBackend($backend); $result = $manager->search('fo'); @@ -279,7 +283,7 @@ class ManagerTest extends TestCase { $backend2->expects($this->never()) ->method('loginName2UserName'); - $manager = new \OC\User\Manager($this->config, $this->dispatcher); + $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->eventDispatcher); $manager->registerBackend($backend1); $manager->registerBackend($backend2); @@ -333,7 +337,7 @@ class ManagerTest extends TestCase { ->willReturn(true); - $manager = new \OC\User\Manager($this->config, $this->dispatcher); + $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->eventDispatcher); $manager->registerBackend($backend); $this->expectException(\InvalidArgumentException::class, $exception); @@ -360,14 +364,14 @@ class ManagerTest extends TestCase { $backend->expects($this->never()) ->method('loginName2UserName'); - $manager = new \OC\User\Manager($this->config, $this->dispatcher); + $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->eventDispatcher); $manager->registerBackend($backend); $user = $manager->createUser('foo', 'bar'); $this->assertEquals('foo', $user->getUID()); } - + public function testCreateUserSingleBackendExists() { $this->expectException(\Exception::class); @@ -387,7 +391,7 @@ class ManagerTest extends TestCase { ->with($this->equalTo('foo')) ->will($this->returnValue(true)); - $manager = new \OC\User\Manager($this->config, $this->dispatcher); + $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->eventDispatcher); $manager->registerBackend($backend); $manager->createUser('foo', 'bar'); @@ -408,19 +412,19 @@ class ManagerTest extends TestCase { $backend->expects($this->never()) ->method('userExists'); - $manager = new \OC\User\Manager($this->config, $this->dispatcher); + $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->eventDispatcher); $manager->registerBackend($backend); $this->assertFalse($manager->createUser('foo', 'bar')); } public function testCreateUserNoBackends() { - $manager = new \OC\User\Manager($this->config, $this->dispatcher); + $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->eventDispatcher); $this->assertFalse($manager->createUser('foo', 'bar')); } - + public function testCreateUserFromBackendWithBackendError() { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Could not create user'); @@ -435,11 +439,11 @@ class ManagerTest extends TestCase { ->with('MyUid', 'MyPassword') ->willReturn(false); - $manager = new Manager($config, $this->dispatcher); + $manager = new Manager($this->config, $this->oldDispatcher, $this->eventDispatcher); $manager->createUserFromBackend('MyUid', 'MyPassword', $backend); } - + public function testCreateUserTwoBackendExists() { $this->expectException(\Exception::class); @@ -475,7 +479,7 @@ class ManagerTest extends TestCase { ->with($this->equalTo('foo')) ->will($this->returnValue(true)); - $manager = new \OC\User\Manager($this->config, $this->dispatcher); + $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->eventDispatcher); $manager->registerBackend($backend1); $manager->registerBackend($backend2); @@ -483,7 +487,7 @@ class ManagerTest extends TestCase { } public function testCountUsersNoBackend() { - $manager = new \OC\User\Manager($this->config, $this->dispatcher); + $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->eventDispatcher); $result = $manager->countUsers(); $this->assertTrue(is_array($result)); @@ -508,7 +512,7 @@ class ManagerTest extends TestCase { ->method('getBackendName') ->will($this->returnValue('Mock_Test_Util_User_Dummy')); - $manager = new \OC\User\Manager($this->config, $this->dispatcher); + $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->eventDispatcher); $manager->registerBackend($backend); $result = $manager->countUsers(); @@ -549,7 +553,7 @@ class ManagerTest extends TestCase { ->method('getBackendName') ->will($this->returnValue('Mock_Test_Util_User_Dummy')); - $manager = new \OC\User\Manager($this->config, $this->dispatcher); + $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->eventDispatcher); $manager->registerBackend($backend1); $manager->registerBackend($backend2); @@ -650,21 +654,7 @@ class ManagerTest extends TestCase { } public function testDeleteUser() { - $config = $this->getMockBuilder(IConfig::class) - ->disableOriginalConstructor() - ->getMock(); - $config - ->expects($this->at(0)) - ->method('getUserValue') - ->with('foo', 'core', 'enabled') - ->will($this->returnValue(true)); - $config - ->expects($this->at(1)) - ->method('getUserValue') - ->with('foo', 'login', 'lastLogin') - ->will($this->returnValue(0)); - - $manager = new \OC\User\Manager($config, $this->dispatcher); + $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->eventDispatcher); $backend = new \Test\Util\User\Dummy(); $manager->registerBackend($backend); @@ -698,7 +688,7 @@ class ManagerTest extends TestCase { ->with($this->equalTo('uid2')) ->will($this->returnValue(true)); - $manager = new \OC\User\Manager($config, $this->dispatcher); + $manager = new \OC\User\Manager($config, $this->oldDispatcher, $this->eventDispatcher); $manager->registerBackend($backend); $users = $manager->getByEmail('test@example.com'); diff --git a/tests/lib/User/SessionTest.php b/tests/lib/User/SessionTest.php index 3567aa9cf6..e7df7578c7 100644 --- a/tests/lib/User/SessionTest.php +++ b/tests/lib/User/SessionTest.php @@ -228,7 +228,11 @@ class SessionTest extends \Test\TestCase { $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']); $manager = $this->getMockBuilder(Manager::class) ->setMethods($mockedManagerMethods) - ->setConstructorArgs([$this->config, $this->createMock(EventDispatcherInterface::class)]) + ->setConstructorArgs([ + $this->config, + $this->createMock(EventDispatcherInterface::class), + $this->createMock(IEventDispatcher::class) + ]) ->getMock(); $backend = $this->createMock(\Test\Util\User\Dummy::class); @@ -271,7 +275,7 @@ class SessionTest extends \Test\TestCase { $this->assertEquals($user, $userSession->getUser()); } - + public function testLoginValidPasswordDisabled() { $this->expectException(\OC\User\LoginException::class); @@ -290,11 +294,13 @@ class SessionTest extends \Test\TestCase { $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']); $manager = $this->getMockBuilder(Manager::class) ->setMethods($mockedManagerMethods) - ->setConstructorArgs([$this->config, $this->createMock(EventDispatcherInterface::class)]) + ->setConstructorArgs([ + $this->config, + $this->createMock(EventDispatcherInterface::class), + $this->createMock(IEventDispatcher::class) + ]) ->getMock(); - $backend = $this->createMock(\Test\Util\User\Dummy::class); - $user = $this->createMock(IUser::class); $user->expects($this->any()) ->method('isEnabled') @@ -321,7 +327,11 @@ class SessionTest extends \Test\TestCase { $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']); $manager = $this->getMockBuilder(Manager::class) ->setMethods($mockedManagerMethods) - ->setConstructorArgs([$this->config, $this->createMock(EventDispatcherInterface::class)]) + ->setConstructorArgs([ + $this->config, + $this->createMock(EventDispatcherInterface::class), + $this->createMock(IEventDispatcher::class) + ]) ->getMock(); $backend = $this->createMock(\Test\Util\User\Dummy::class); $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); @@ -404,7 +414,7 @@ class SessionTest extends \Test\TestCase { $userSession->login('foo', 'bar'); } - + public function testLogClientInNoTokenPasswordWith2fa() { $this->expectException(\OC\Authentication\Exceptions\PasswordLoginForbiddenException::class); @@ -508,7 +518,7 @@ class SessionTest extends \Test\TestCase { $this->assertTrue($userSession->logClientIn('john', 'I-AM-AN-APP-PASSWORD', $request, $this->throttler)); } - + public function testLogClientInNoTokenPasswordNo2fa() { $this->expectException(\OC\Authentication\Exceptions\PasswordLoginForbiddenException::class); @@ -560,7 +570,11 @@ class SessionTest extends \Test\TestCase { $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']); $manager = $this->getMockBuilder(Manager::class) ->setMethods($mockedManagerMethods) - ->setConstructorArgs([$this->config, $this->createMock(EventDispatcherInterface::class)]) + ->setConstructorArgs([ + $this->config, + $this->createMock(EventDispatcherInterface::class), + $this->createMock(IEventDispatcher::class) + ]) ->getMock(); $userSession = $this->getMockBuilder(Session::class) //override, otherwise tests will fail because of setcookie() @@ -645,7 +659,11 @@ class SessionTest extends \Test\TestCase { $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']); $manager = $this->getMockBuilder(Manager::class) ->setMethods($mockedManagerMethods) - ->setConstructorArgs([$this->config, $this->createMock(EventDispatcherInterface::class)]) + ->setConstructorArgs([ + $this->config, + $this->createMock(EventDispatcherInterface::class), + $this->createMock(IEventDispatcher::class) + ]) ->getMock(); $userSession = $this->getMockBuilder(Session::class) //override, otherwise tests will fail because of setcookie() @@ -705,7 +723,11 @@ class SessionTest extends \Test\TestCase { $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']); $manager = $this->getMockBuilder(Manager::class) ->setMethods($mockedManagerMethods) - ->setConstructorArgs([$this->config, $this->createMock(EventDispatcherInterface::class)]) + ->setConstructorArgs([ + $this->config, + $this->createMock(EventDispatcherInterface::class), + $this->createMock(IEventDispatcher::class) + ]) ->getMock(); $userSession = $this->getMockBuilder(Session::class) //override, otherwise tests will fail because of setcookie() @@ -753,7 +775,11 @@ class SessionTest extends \Test\TestCase { $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']); $manager = $this->getMockBuilder(Manager::class) ->setMethods($mockedManagerMethods) - ->setConstructorArgs([$this->config, $this->createMock(EventDispatcherInterface::class)]) + ->setConstructorArgs([ + $this->config, + $this->createMock(EventDispatcherInterface::class), + $this->createMock(IEventDispatcher::class) + ]) ->getMock(); $userSession = $this->getMockBuilder(Session::class) //override, otherwise tests will fail because of setcookie() @@ -973,7 +999,7 @@ class SessionTest extends \Test\TestCase { $this->assertFalse($userSession->createSessionToken($request, $uid, $loginName, $password)); } - + public function testTryTokenLoginWithDisabledUser() { $this->expectException(\OC\User\LoginException::class);