Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
}
46 changes: 46 additions & 0 deletions lib/Listener/BeforeUserCreatedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\WordPressLoginBackend\Listener;

use InvalidArgumentException;
use OCA\WordPressLoginBackend\Db\WordPressUsers;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IGroupManager;
use OCP\IUserSession;
use OCP\User\Events\BeforeUserCreatedEvent;

/**
* @template-implements IEventListener<Event|BeforeUserCreatedEvent>
*/
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');
}
}
}
92 changes: 92 additions & 0 deletions tests/Unit/Listener/BeforeUserCreatedListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\WordPressLoginBackend\Tests\Unit\Listener;

use InvalidArgumentException;
use OCA\WordPressLoginBackend\Db\WordPressUsers;
use OCA\WordPressLoginBackend\Listener\BeforeUserCreatedListener;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserSession;
use OCP\User\Events\BeforeUserCreatedEvent;
use PDO;
use PHPUnit\Framework\TestCase;

class BeforeUserCreatedListenerTest extends TestCase {
private string $databaseFile;
private PDO $database;
private array $admins = [];
private array $delegatedAdmins = [];

protected function setUp(): void {
$this->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'));
}
}
24 changes: 24 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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(
Expand Down
Loading