|
| 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 | +} |
0 commit comments