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 <[email protected]>
  • Loading branch information
ChristophWurst committed Nov 19, 2019
1 parent 7beca45 commit 2e3f923
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
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

0 comments on commit 2e3f923

Please sign in to comment.