Skip to content

Commit d9112b8

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

7 files changed

Lines changed: 20 additions & 14 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ env:
99
- APP_NAME=guests
1010
- DB=sqlite
1111
matrix:
12-
- CORE_BRANCH=stable17
12+
- CORE_BRANCH=master
1313

1414
branches:
1515
only:

appinfo/info.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Guests users can only access files shared to them and can't create any files out
2222
<screenshot>https://raw.githubusercontent.com/nextcloud/guests/master/screenshots/settings.png</screenshot>
2323
<screenshot>https://raw.githubusercontent.com/nextcloud/guests/master/screenshots/dropdown.png</screenshot>
2424
<dependencies>
25-
<nextcloud min-version="17" max-version="17" />
25+
<nextcloud min-version="18" max-version="18" />
2626
</dependencies>
2727
<commands>
2828
<command>OCA\Guests\Command\ListCommand</command>

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

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"devDependencies": {
3131
"@babel/core": "^7.7.4",
3232
"@babel/preset-env": "^7.7.4",
33+
"@nextcloud/browserslist-config": "^1.0.0",
3334
"babel-loader": "^8.0.6",
3435
"browserslist-config-nextcloud": "0.1.0",
3536
"css-loader": "^3.2.0",

tests/unit/UserBackendTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
use OCA\Guests\Config;
2626
use OCA\Guests\UserBackend;
27+
use OCP\EventDispatcher\IEventDispatcher;
2728
use PHPUnit\Framework\MockObject\MockObject;
28-
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
2929
use Test\TestCase;
3030

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

5454
$this->backend = new UserBackend(
55-
$this->createMock(EventDispatcherInterface::class),
55+
$this->createMock(IEventDispatcher::class),
5656
\OC::$server->getDatabaseConnection(),
5757
$this->config,
5858
\OC::$server->getHasher()

0 commit comments

Comments
 (0)