Skip to content

Commit 0d6d088

Browse files
juliusknorrkesselb
authored andcommitted
feat: Add occ user:sync-account-data for updating oc_accounts information from user backends
This can be useful in cases where the state between user backend and oc_accounts has become inconsistent. Usually the account data is updated once the change on the user backend is detected. Potential leftovers from older bugs (nextcloud/user_saml#582) might though never get updated. This could lead to the contacts menu never showing the correct display name. The contacts menu is read from the system address book, which is only updated from oc_accounts. Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent 11b2762 commit 0d6d088

4 files changed

Lines changed: 108 additions & 0 deletions

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2023 Julius Härrtl <jus@bitgrid.net>
4+
*
5+
* @author Julius Härrtl <jus@bitgrid.net>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
namespace OC\Core\Command\User;
24+
25+
use OC\Core\Command\Base;
26+
use OCP\Accounts\IAccountManager;
27+
use OCP\Accounts\PropertyDoesNotExistException;
28+
use OCP\IUser;
29+
use OCP\IUserManager;
30+
use OCP\User\Backend\IGetDisplayNameBackend;
31+
use Symfony\Component\Console\Input\InputInterface;
32+
use Symfony\Component\Console\Input\InputOption;
33+
use Symfony\Component\Console\Output\OutputInterface;
34+
35+
class SyncAccountDataCommand extends Base {
36+
protected IUserManager $userManager;
37+
protected IAccountManager $accountManager;
38+
39+
public function __construct(
40+
IUserManager $userManager,
41+
IAccountManager $accountManager
42+
) {
43+
$this->userManager = $userManager;
44+
$this->accountManager = $accountManager;
45+
parent::__construct();
46+
}
47+
48+
protected function configure() {
49+
$this
50+
->setName('user:sync-account-data')
51+
->setDescription('sync user backend data to accounts table for configured users')
52+
->addOption(
53+
'limit',
54+
'l',
55+
InputOption::VALUE_OPTIONAL,
56+
'Number of users to retrieve',
57+
'500'
58+
)->addOption(
59+
'offset',
60+
'o',
61+
InputOption::VALUE_OPTIONAL,
62+
'Offset for retrieving users',
63+
'0'
64+
);
65+
}
66+
67+
protected function execute(InputInterface $input, OutputInterface $output): int {
68+
$users = $this->userManager->searchDisplayName('', (int) $input->getOption('limit'), (int) $input->getOption('offset'));
69+
70+
foreach ($users as $user) {
71+
$this->updateUserAccount($user, $output);
72+
}
73+
return 0;
74+
}
75+
76+
private function updateUserAccount(IUser $user, OutputInterface $output): void {
77+
$changed = false;
78+
$account = $this->accountManager->getAccount($user);
79+
if ($user->getBackend() instanceof IGetDisplayNameBackend) {
80+
try {
81+
$displayNameProperty = $account->getProperty(IAccountManager::PROPERTY_DISPLAYNAME);
82+
} catch (PropertyDoesNotExistException) {
83+
$displayNameProperty = null;
84+
}
85+
if (!$displayNameProperty || $displayNameProperty->getValue() !== $user->getDisplayName()) {
86+
$output->writeln($user->getUID() . ' - updating changed display name');
87+
$account->setProperty(
88+
IAccountManager::PROPERTY_DISPLAYNAME,
89+
$user->getDisplayName(),
90+
$displayNameProperty ? $displayNameProperty->getScope() : IAccountManager::SCOPE_PRIVATE,
91+
$displayNameProperty ? $displayNameProperty->getVerified() : IAccountManager::NOT_VERIFIED,
92+
$displayNameProperty ? $displayNameProperty->getVerificationData() : ''
93+
);
94+
$changed = true;
95+
}
96+
}
97+
98+
if ($changed) {
99+
$this->accountManager->updateAccount($account);
100+
$output->writeln($user->getUID() . ' - account data updated');
101+
} else {
102+
$output->writeln($user->getUID() . ' - nothing to update');
103+
}
104+
}
105+
}

core/register_command.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@
192192
$application->add(new OC\Core\Command\User\Setting(\OC::$server->getUserManager(), \OC::$server->getConfig()));
193193
$application->add(new OC\Core\Command\User\ListCommand(\OC::$server->getUserManager(), \OC::$server->getGroupManager()));
194194
$application->add(new OC\Core\Command\User\Info(\OC::$server->getUserManager(), \OC::$server->getGroupManager()));
195+
$application->add(new OC\Core\Command\User\SyncAccountDataCommand(\OC::$server->getUserManager(), \OC::$server->get(\OCP\Accounts\IAccountManager::class)));
195196
$application->add(new OC\Core\Command\User\AddAppPassword(\OC::$server->get(\OCP\IUserManager::class), \OC::$server->get(\OC\Authentication\Token\IProvider::class), \OC::$server->get(\OCP\Security\ISecureRandom::class), \OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class)));
196197

197198
$application->add(new OC\Core\Command\Group\Add(\OC::$server->getGroupManager()));

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,7 @@
10361036
'OC\\Core\\Command\\User\\Report' => $baseDir . '/core/Command/User/Report.php',
10371037
'OC\\Core\\Command\\User\\ResetPassword' => $baseDir . '/core/Command/User/ResetPassword.php',
10381038
'OC\\Core\\Command\\User\\Setting' => $baseDir . '/core/Command/User/Setting.php',
1039+
'OC\\Core\\Command\\User\\SyncAccountDataCommand' => $baseDir . '/core/Command/User/SyncAccountDataCommand.php',
10391040
'OC\\Core\\Controller\\AppPasswordController' => $baseDir . '/core/Controller/AppPasswordController.php',
10401041
'OC\\Core\\Controller\\AutoCompleteController' => $baseDir . '/core/Controller/AutoCompleteController.php',
10411042
'OC\\Core\\Controller\\AvatarController' => $baseDir . '/core/Controller/AvatarController.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
10691069
'OC\\Core\\Command\\User\\Report' => __DIR__ . '/../../..' . '/core/Command/User/Report.php',
10701070
'OC\\Core\\Command\\User\\ResetPassword' => __DIR__ . '/../../..' . '/core/Command/User/ResetPassword.php',
10711071
'OC\\Core\\Command\\User\\Setting' => __DIR__ . '/../../..' . '/core/Command/User/Setting.php',
1072+
'OC\\Core\\Command\\User\\SyncAccountDataCommand' => __DIR__ . '/../../..' . '/core/Command/User/SyncAccountDataCommand.php',
10721073
'OC\\Core\\Controller\\AppPasswordController' => __DIR__ . '/../../..' . '/core/Controller/AppPasswordController.php',
10731074
'OC\\Core\\Controller\\AutoCompleteController' => __DIR__ . '/../../..' . '/core/Controller/AutoCompleteController.php',
10741075
'OC\\Core\\Controller\\AvatarController' => __DIR__ . '/../../..' . '/core/Controller/AvatarController.php',

0 commit comments

Comments
 (0)