diff --git a/README.md b/README.md index 6dca470..775f01f 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,9 @@ password, and only their password changes are written back to WordPress. The WordPress user is the one whose `user_login` is exactly the account uid. See [Linking existing accounts](#linking-existing-accounts). +While logged in, only administrators and delegated administrators can create +an account whose uid is a WordPress login. + ### Custom SQL queries (optional) You can override default SQL queries using the `wordpress_queries` system key, diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index e4a6a5b..3eb4d71 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -6,11 +6,13 @@ namespace OCA\WordPressLoginBackend\AppInfo; +use OCA\WordPressLoginBackend\Listener\BeforeUserCreatedListener; use OCA\WordPressLoginBackend\Listener\PasswordUpdatedListener; use \OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\User\Events\BeforeUserCreatedEvent; use OCP\User\Events\PasswordUpdatedEvent; class Application extends App implements IBootstrap { @@ -29,5 +31,6 @@ public function boot(IBootContext $context): void { public function register(IRegistrationContext $context): void { $context->registerEventListener(PasswordUpdatedEvent::class, PasswordUpdatedListener::class); + $context->registerEventListener(BeforeUserCreatedEvent::class, BeforeUserCreatedListener::class); } } diff --git a/lib/Listener/BeforeUserCreatedListener.php b/lib/Listener/BeforeUserCreatedListener.php new file mode 100644 index 0000000..d00b290 --- /dev/null +++ b/lib/Listener/BeforeUserCreatedListener.php @@ -0,0 +1,46 @@ + + */ +class BeforeUserCreatedListener implements IEventListener { + public function __construct( + private IUserSession $userSession, + private IGroupManager $groupManager, + private WordPressUsers $wordPressUsers, + ) { + } + + public function handle(Event $event): void { + if (!$event instanceof BeforeUserCreatedEvent) { + return; + } + $creator = $this->userSession->getUser(); + if ($creator === null) { + return; + } + $creatorUid = $creator->getUID(); + if ($this->groupManager->isAdmin($creatorUid) || $this->groupManager->isDelegatedAdmin($creatorUid)) { + return; + } + if ($this->wordPressUsers->findByLogin($event->getUid()) !== null) { + throw new InvalidArgumentException('This account id is reserved'); + } + } +} diff --git a/tests/Unit/Listener/BeforeUserCreatedListenerTest.php b/tests/Unit/Listener/BeforeUserCreatedListenerTest.php new file mode 100644 index 0000000..c7841f2 --- /dev/null +++ b/tests/Unit/Listener/BeforeUserCreatedListenerTest.php @@ -0,0 +1,92 @@ +databaseFile = tempnam(sys_get_temp_dir(), 'wordpress'); + $this->database = new PDO('sqlite:' . $this->databaseFile); + $this->database->exec('CREATE TABLE wp_users (ID INTEGER PRIMARY KEY, user_login TEXT COLLATE NOCASE, user_email TEXT, user_pass TEXT, display_name TEXT)'); + $this->database->exec('CREATE TABLE wp_wc_orders (customer_id INTEGER, status TEXT, type TEXT)'); + $this->database->exec("INSERT INTO wp_users (user_login, user_email, user_pass, display_name) VALUES ('ana', 'ana@example.org', 'hash', 'Ana')"); + } + + protected function tearDown(): void { + unlink($this->databaseFile); + } + + public function testRefusesASubAdminCreatingAWordPressLogin(): void { + $this->expectException(InvalidArgumentException::class); + + $this->createUser('bruno', 'ana'); + } + + public function testLetsASubAdminCreateAnUidThatIsNotAWordPressLogin(): void { + $this->expectNotToPerformAssertions(); + + $this->createUser('bruno', 'carla'); + } + + public function testLetsAnAdminCreateAWordPressLogin(): void { + $this->admins[] = 'admin'; + $this->expectNotToPerformAssertions(); + + $this->createUser('admin', 'ana'); + } + + public function testLetsADelegatedAdminCreateAWordPressLogin(): void { + $this->delegatedAdmins[] = 'manager'; + $this->expectNotToPerformAssertions(); + + $this->createUser('manager', 'ana'); + } + + public function testLetsACreationWithoutSessionCreateAWordPressLogin(): void { + $this->expectNotToPerformAssertions(); + + $this->createUser(null, 'ana'); + } + + private function createUser(?string $creatorUid, string $uid): void { + $config = $this->createMock(IConfig::class); + $config->method('getSystemValue')->willReturnMap([ + ['wordpress_dsn', '', 'sqlite:' . $this->databaseFile], + ['wordpress_queries', [], []], + ]); + $creator = null; + if ($creatorUid !== null) { + $creator = $this->createMock(IUser::class); + $creator->method('getUID')->willReturn($creatorUid); + } + $userSession = $this->createMock(IUserSession::class); + $userSession->method('getUser')->willReturn($creator); + $groupManager = $this->createMock(IGroupManager::class); + $groupManager->method('isAdmin')->willReturnCallback(fn (string $userId) => in_array($userId, $this->admins, true)); + $groupManager->method('isDelegatedAdmin')->willReturnCallback(fn (string $userId) => in_array($userId, $this->delegatedAdmins, true)); + + $listener = new BeforeUserCreatedListener($userSession, $groupManager, new WordPressUsers($config)); + $listener->handle(new BeforeUserCreatedEvent($uid, 'password')); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index f9e3d47..fe3ab0a 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -42,6 +42,16 @@ public function getUsers(); if (!interface_exists(IGroupManager::class)) { interface IGroupManager { public function search(string $search, ?int $limit = null, ?int $offset = 0); + + public function isAdmin($userId); + + public function isDelegatedAdmin(string $userId): bool; + } + } + + if (!interface_exists(IUserSession::class)) { + interface IUserSession { + public function getUser(); } } @@ -198,6 +208,20 @@ public function handle(Event $event): void; use OCP\EventDispatcher\Event; use OCP\IUser; + if (!class_exists(BeforeUserCreatedEvent::class)) { + class BeforeUserCreatedEvent extends Event { + public function __construct( + private string $uid, + private string $password, + ) { + } + + public function getUid(): string { + return $this->uid; + } + } + } + if (!class_exists(PasswordUpdatedEvent::class)) { class PasswordUpdatedEvent extends Event { public function __construct(