Skip to content

Commit

Permalink
Add typed create user events
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Wurst <[email protected]>
Signed-off-by: Roeland Jago Douma <[email protected]>
  • Loading branch information
ChristophWurst authored and rullzer committed Nov 29, 2019
1 parent e5c95ee commit 7ebc026
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 40 deletions.
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,9 @@
'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',
Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 0 additions & 3 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,6 @@ public function __construct($webRoot, \OC\Config $config) {
});
$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);

Expand Down
22 changes: 17 additions & 5 deletions lib/private/User/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@

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;

Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -349,13 +359,15 @@ public function createUserFromBackend($uid, $password, UserInterface $backend) {
}

$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'));
}
$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;
}
Expand Down Expand Up @@ -460,11 +472,11 @@ public function countDisabledUsers(): int {
->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 {
Expand Down Expand Up @@ -494,7 +506,7 @@ public function countDisabledUsersOfGroups(array $groups): int {
$result = $queryBuilder->execute();
$count = $result->fetchColumn();
$result->closeCursor();

if ($count !== false) {
$count = (int)$count;
} else {
Expand Down
63 changes: 63 additions & 0 deletions lib/public/User/Events/CreateUserEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php declare(strict_types=1);

/**
* @copyright 2019 Christoph Wurst <[email protected]>
*
* @author 2019 Christoph Wurst <[email protected]>
*
* @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 <http://www.gnu.org/licenses/>.
*/

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;
}

}
71 changes: 71 additions & 0 deletions lib/public/User/Events/UserCreatedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php declare(strict_types=1);

/**
* @copyright 2019 Christoph Wurst <[email protected]>
*
* @author 2019 Christoph Wurst <[email protected]>
*
* @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 <http://www.gnu.org/licenses/>.
*/

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;
}

}
3 changes: 2 additions & 1 deletion tests/lib/Files/Config/UserMountCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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;
Expand Down Expand Up @@ -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', '');
Expand Down
7 changes: 4 additions & 3 deletions tests/lib/Files/Storage/Wrapper/EncryptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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;
Expand Down Expand Up @@ -131,7 +132,7 @@ protected function setUp(): void {

$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')
Expand Down Expand Up @@ -568,7 +569,7 @@ public function testGetHeader($path, $strippedPathExists, $strippedPath) {
->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
Expand Down Expand Up @@ -637,7 +638,7 @@ public function testGetHeaderAddLegacyModule($header, $isEncrypted, $exists, $ex
->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')
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/Files/Stream/EncryptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand Down Expand Up @@ -48,7 +49,7 @@ protected function getStream($fileName, $mode, $unencryptedSize, $wrapper = '\OC
$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')
Expand Down
Loading

0 comments on commit 7ebc026

Please sign in to comment.