Skip to content

Commit 2e3f923

Browse files
Use typed password events
Requires nextcloud/server#18019 Requires nextcloud/password_policy#90 Signed-off-by: Christoph Wurst <[email protected]>
1 parent 7beca45 commit 2e3f923

2 files changed

Lines changed: 9 additions & 10 deletions

File tree

lib/GuestManager.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use OCP\IUserBackend;
3333
use OCP\IUserManager;
3434
use OCP\IUserSession;
35+
use OCP\Security\Events\GenerateSecurePasswordEvent;
3536
use OCP\Security\ICrypto;
3637
use OCP\Security\ISecureRandom;
3738
use OCP\Share\IManager;
@@ -97,11 +98,11 @@ public function isGuest($user = null) {
9798
}
9899

99100
public function createGuest(IUser $createdBy, $userId, $email, $displayName = '', $language = '') {
100-
$passwordEvent = new Event(null, ['password' => $this->secureRandom->generate(20)]);
101-
$this->eventDispatcher->dispatch('OCP\PasswordPolicy::generate', $passwordEvent);
101+
$passwordEvent = new GenerateSecurePasswordEvent();
102+
$this->eventDispatcher->dispatchTyped($passwordEvent);
102103
$this->userBackend->createUser(
103104
$userId,
104-
$passwordEvent->getArgument('password')
105+
$passwordEvent->getPassword() ?? $this->secureRandom->generate(20)
105106
);
106107

107108
$this->config->setUserValue($userId, 'settings', 'email', $email);

lib/UserBackend.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
namespace OCA\Guests;
2424

2525
use OC\Cache\CappedMemoryCache;
26+
use OCP\EventDispatcher\IEventDispatcher;
2627
use OCP\IDBConnection;
28+
use OCP\Security\Events\ValidatePasswordPolicyEvent;
2729
use OCP\Security\IHasher;
2830
use OCP\User\Backend\ABackend;
2931
use OCP\User\Backend\ICheckPasswordBackend;
@@ -32,8 +34,6 @@
3234
use OCP\User\Backend\IGetHomeBackend;
3335
use OCP\User\Backend\ISetDisplayNameBackend;
3436
use OCP\User\Backend\ISetPasswordBackend;
35-
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
36-
use Symfony\Component\EventDispatcher\GenericEvent;
3737

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

5656
public function __construct(
57-
EventDispatcherInterface $eventDispatcher,
57+
IEventDispatcher $eventDispatcher,
5858
IDBConnection $connection,
5959
Config $config,
6060
IHasher $hasher
@@ -82,8 +82,7 @@ public function setAllowListing(bool $allow) {
8282
*/
8383
public function createUser(string $uid, string $password): bool {
8484
if (!$this->userExists($uid)) {
85-
$event = new GenericEvent($password);
86-
$this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event);
85+
$this->eventDispatcher->dispatchTyped(new ValidatePasswordPolicyEvent($password));
8786

8887
$qb = $this->dbConn->getQueryBuilder();
8988
$qb->insert('guests_users')
@@ -137,8 +136,7 @@ public function deleteUser($uid) {
137136
*/
138137
public function setPassword(string $uid, string $password): bool {
139138
if ($this->userExists($uid)) {
140-
$event = new GenericEvent($password);
141-
$this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event);
139+
$this->eventDispatcher->dispatchTyped(new ValidatePasswordPolicyEvent($password));
142140

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

0 commit comments

Comments
 (0)