From d801f5b33a3d22e1df32413a2db555790001f616 Mon Sep 17 00:00:00 2001 From: YvesCesar Date: Fri, 13 Nov 2026 20:14:52 -0400 Subject: [PATCH] feat: add a command to link existing group owners to their WordPress login Signed-off-by: YvesCesar --- README.md | 18 ++++ appinfo/info.xml | 3 + lib/AppInfo/Application.php | 1 + lib/Command/LinkUsers.php | 42 ++++++++ lib/Db/WordPressUsers.php | 50 +++++++++ lib/Service/UserLinker.php | 59 +++++++++++ tests/Unit/Service/UserLinkerTest.php | 139 ++++++++++++++++++++++++++ tests/bootstrap.php | 53 ++++++++++ 8 files changed, 365 insertions(+) create mode 100644 lib/Command/LinkUsers.php create mode 100644 lib/Db/WordPressUsers.php create mode 100644 lib/Service/UserLinker.php create mode 100644 tests/Unit/Service/UserLinkerTest.php diff --git a/README.md b/README.md index 4f09b28..026af5e 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,24 @@ The backend expects these named parameters: - `:search` for disabled-user listing - `:hash` for password updates +### Linking existing accounts + +An account is linked to WordPress by the user preference `wordpress_login` of +the app `wordpress_login_backend`, holding the account uid. Admin Group Manager +records it when it creates the account that owns a group. + +To link the accounts created before that, run: + +```bash +occ wordpress-login-backend:link-users --dry-run +occ wordpress-login-backend:link-users +``` + +The command links each account that is sub-admin of the group with the same id +as its uid and has a WordPress user with exactly that login. Members of the +groups are never linked and are listed as skipped. With `--dry-run` it only +lists the accounts. + ## Support, customization and enterprise If you need help using this app, or if you want custom development, priority diff --git a/appinfo/info.xml b/appinfo/info.xml index 9e02e67..dc6f758 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -23,4 +23,7 @@ Integrate WordPress accounts with Nextcloud]]> + + OCA\WordPressLoginBackend\Command\LinkUsers + diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index de04033..e58e76f 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -15,6 +15,7 @@ class Application extends App implements IBootstrap { public const APP_ID = 'wordpress_login_backend'; + public const LINKED_LOGIN_KEY = 'wordpress_login'; public function __construct(array $urlParams = array()) { parent::__construct(Application::APP_ID, $urlParams); diff --git a/lib/Command/LinkUsers.php b/lib/Command/LinkUsers.php new file mode 100644 index 0000000..f4f3b6c --- /dev/null +++ b/lib/Command/LinkUsers.php @@ -0,0 +1,42 @@ +setName('wordpress-login-backend:link-users') + ->setDescription('Link the accounts that own a group of the same name to the WordPress user with that login') + ->addOption('dry-run', null, InputOption::VALUE_NONE, 'List the accounts without linking them'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + $dryRun = (bool)$input->getOption('dry-run'); + $result = $this->userLinker->link($dryRun); + foreach ($result['linked'] as $uid) { + $output->writeln(($dryRun ? 'Would link: ' : 'Linked: ') . $uid); + } + foreach ($result['skipped'] as $uid) { + $output->writeln('Skipped: ' . $uid); + } + return 0; + } +} diff --git a/lib/Db/WordPressUsers.php b/lib/Db/WordPressUsers.php new file mode 100644 index 0000000..eaaf15f --- /dev/null +++ b/lib/Db/WordPressUsers.php @@ -0,0 +1,50 @@ +queryConfig = new QueryConfig($this->config); + } + + public function findByLogin(string $login): ?array { + $database = $this->getDatabase(); + if (!$database) { + return null; + } + $statement = $database->prepare($this->queryConfig->getWordPressUserQuery()); + $statement->execute(['username' => $login]); + foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) { + if ((string)$row['uid'] === $login) { + return $row; + } + } + return null; + } + + private function getDatabase(): ?PDO { + $dsn = (string)$this->config->getSystemValue('wordpress_dsn', ''); + if ($dsn === '') { + return null; + } + if (!$this->pdo instanceof PDO) { + $this->pdo = new PDO($dsn); + } + return $this->pdo; + } +} diff --git a/lib/Service/UserLinker.php b/lib/Service/UserLinker.php new file mode 100644 index 0000000..0bb78e0 --- /dev/null +++ b/lib/Service/UserLinker.php @@ -0,0 +1,59 @@ +, skipped: list} + */ + public function link(bool $dryRun): array { + $linked = []; + $skipped = []; + foreach ($this->groupManager->search('') as $group) { + $groupId = $group->getGID(); + $owner = $this->userManager->get($groupId); + if ($owner instanceof IUser + && $owner->getUID() === $groupId + && $this->subAdmin->isSubAdminOfGroup($owner, $group) + && $this->wordPressUsers->findByLogin($groupId) !== null) { + $linked[$groupId] = $groupId; + } + foreach ($group->getUsers() as $member) { + $skipped[$member->getUID()] = $member->getUID(); + } + } + $skipped = array_diff_key($skipped, $linked); + if (!$dryRun) { + foreach ($linked as $uid) { + $this->config->setUserValue($uid, Application::APP_ID, Application::LINKED_LOGIN_KEY, $uid); + } + } + return [ + 'linked' => array_values($linked), + 'skipped' => array_values($skipped), + ]; + } +} diff --git a/tests/Unit/Service/UserLinkerTest.php b/tests/Unit/Service/UserLinkerTest.php new file mode 100644 index 0000000..41a9a24 --- /dev/null +++ b/tests/Unit/Service/UserLinkerTest.php @@ -0,0 +1,139 @@ +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, user_email TEXT, user_pass TEXT, display_name TEXT)'); + $this->database->exec('CREATE TABLE wp_wc_orders (customer_id INTEGER, status TEXT, type TEXT)'); + } + + protected function tearDown(): void { + unlink($this->databaseFile); + } + + public function testLinksTheGroupOwnerThatHasAWordPressLogin(): void { + $this->createWordPressUser('ana'); + $this->createGroup('ana', ['ana'], 'ana'); + + $result = $this->link(false); + + $this->assertSame(['ana'], $result['linked']); + $this->assertSame(['ana' => 'ana'], $this->linkedLogins); + } + + public function testDoesNotLinkAGroupMemberEvenWithAWordPressLogin(): void { + $this->createWordPressUser('ana'); + $this->createWordPressUser('bruno'); + $this->createGroup('ana', ['ana', 'bruno'], 'ana'); + + $result = $this->link(false); + + $this->assertSame(['bruno'], $result['skipped']); + $this->assertArrayNotHasKey('bruno', $this->linkedLogins); + } + + public function testDoesNotLinkAGroupOwnerThatIsNotItsSubAdmin(): void { + $this->createWordPressUser('ana'); + $this->createGroup('ana', ['ana'], null); + + $result = $this->link(false); + + $this->assertSame([], $result['linked']); + $this->assertSame([], $this->linkedLogins); + } + + public function testDoesNotLinkAGroupOwnerWithoutAWordPressLogin(): void { + $this->createWordPressUser('bruno', 'ana'); + $this->createGroup('ana', ['ana'], 'ana'); + + $result = $this->link(false); + + $this->assertSame(['ana'], $result['skipped']); + $this->assertSame([], $this->linkedLogins); + } + + public function testDryRunListsWithoutLinking(): void { + $this->createWordPressUser('ana'); + $this->createGroup('ana', ['ana'], 'ana'); + + $result = $this->link(true); + + $this->assertSame(['ana'], $result['linked']); + $this->assertSame([], $this->linkedLogins); + } + + private function createWordPressUser(string $login, ?string $email = null): void { + $statement = $this->database->prepare('INSERT INTO wp_users (user_login, user_email, user_pass, display_name) VALUES (:login, :email, :hash, :login)'); + $statement->execute(['login' => $login, 'email' => $email ?? $login . '@example.org', 'hash' => 'hash']); + } + + private function createGroup(string $groupId, array $memberIds, ?string $subAdminId): void { + $members = array_map(fn (string $uid) => $this->user($uid), $memberIds); + $group = $this->createMock(IGroup::class); + $group->method('getGID')->willReturn($groupId); + $group->method('getUsers')->willReturn($members); + $this->groups[] = $group; + if ($subAdminId !== null) { + $this->subAdmins[] = [$subAdminId, $groupId]; + } + } + + private function user(string $uid): IUser { + if (!isset($this->users[$uid])) { + $user = $this->createMock(IUser::class); + $user->method('getUID')->willReturn($uid); + $this->users[$uid] = $user; + } + return $this->users[$uid]; + } + + private function link(bool $dryRun): array { + $config = $this->createMock(IConfig::class); + $config->method('getSystemValue')->willReturnMap([ + ['wordpress_dsn', '', 'sqlite:' . $this->databaseFile], + ['wordpress_queries', [], []], + ]); + $config->method('setUserValue')->willReturnCallback(function (string $uid, string $appName, string $key, string $value): void { + if ($appName === 'wordpress_login_backend' && $key === 'wordpress_login') { + $this->linkedLogins[$uid] = $value; + } + }); + $groupManager = $this->createMock(IGroupManager::class); + $groupManager->method('search')->with('')->willReturn($this->groups); + $userManager = $this->createMock(IUserManager::class); + $userManager->method('get')->willReturnCallback(fn (string $uid) => $this->users[$uid] ?? null); + $subAdmin = $this->createMock(ISubAdmin::class); + $subAdmin->method('isSubAdminOfGroup')->willReturnCallback( + fn (IUser $user, IGroup $group) => in_array([$user->getUID(), $group->getGID()], $this->subAdmins, true), + ); + + return (new UserLinker($config, $groupManager, $subAdmin, $userManager, new WordPressUsers($config)))->link($dryRun); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index f362aa5..6c90379 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -14,6 +14,10 @@ if (!interface_exists(IConfig::class)) { interface IConfig { public function getSystemValue($key, $default = ''); + + public function getUserValue($userId, $appName, $key, $default = ''); + + public function setUserValue($userId, $appName, $key, $value, $preCondition = null); } } } @@ -26,6 +30,55 @@ public function getUID(); } } +namespace OCP { + if (!interface_exists(IGroup::class)) { + interface IGroup { + public function getGID(); + + public function getUsers(); + } + } + + if (!interface_exists(IGroupManager::class)) { + interface IGroupManager { + public function search(string $search, ?int $limit = null, ?int $offset = 0); + } + } + + if (!interface_exists(IUserManager::class)) { + interface IUserManager { + public function get($uid); + } + } +} + +namespace OCP\Group { + use OCP\IGroup; + use OCP\IUser; + + if (!interface_exists(ISubAdmin::class)) { + interface ISubAdmin { + public function isSubAdminOfGroup(IUser $user, IGroup $group): bool; + } + } +} + +namespace OCP\AppFramework { + if (!class_exists(App::class)) { + class App { + public function __construct(string $appName, array $urlParams = []) { + } + } + } +} + +namespace OCP\AppFramework\Bootstrap { + if (!interface_exists(IBootstrap::class)) { + interface IBootstrap { + } + } +} + namespace OCP\EventDispatcher { if (!class_exists(Event::class)) { class Event {