SyncAccountDataCommand.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Core\Command\User;
  7. use OC\Core\Command\Base;
  8. use OCP\Accounts\IAccountManager;
  9. use OCP\Accounts\PropertyDoesNotExistException;
  10. use OCP\IUser;
  11. use OCP\IUserManager;
  12. use OCP\User\Backend\IGetDisplayNameBackend;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. class SyncAccountDataCommand extends Base {
  17. protected IUserManager $userManager;
  18. protected IAccountManager $accountManager;
  19. public function __construct(
  20. IUserManager $userManager,
  21. IAccountManager $accountManager
  22. ) {
  23. $this->userManager = $userManager;
  24. $this->accountManager = $accountManager;
  25. parent::__construct();
  26. }
  27. protected function configure() {
  28. $this
  29. ->setName('user:sync-account-data')
  30. ->setDescription('sync user backend data to accounts table for configured users')
  31. ->addOption(
  32. 'limit',
  33. 'l',
  34. InputOption::VALUE_OPTIONAL,
  35. 'Number of users to retrieve',
  36. '500'
  37. )->addOption(
  38. 'offset',
  39. 'o',
  40. InputOption::VALUE_OPTIONAL,
  41. 'Offset for retrieving users',
  42. '0'
  43. );
  44. }
  45. protected function execute(InputInterface $input, OutputInterface $output): int {
  46. $users = $this->userManager->searchDisplayName('', (int) $input->getOption('limit'), (int) $input->getOption('offset'));
  47. foreach ($users as $user) {
  48. $this->updateUserAccount($user, $output);
  49. }
  50. return 0;
  51. }
  52. private function updateUserAccount(IUser $user, OutputInterface $output): void {
  53. $changed = false;
  54. $account = $this->accountManager->getAccount($user);
  55. if ($user->getBackend() instanceof IGetDisplayNameBackend) {
  56. try {
  57. $displayNameProperty = $account->getProperty(IAccountManager::PROPERTY_DISPLAYNAME);
  58. } catch (PropertyDoesNotExistException) {
  59. $displayNameProperty = null;
  60. }
  61. if (!$displayNameProperty || $displayNameProperty->getValue() !== $user->getDisplayName()) {
  62. $output->writeln($user->getUID() . ' - updating changed display name');
  63. $account->setProperty(
  64. IAccountManager::PROPERTY_DISPLAYNAME,
  65. $user->getDisplayName(),
  66. $displayNameProperty ? $displayNameProperty->getScope() : IAccountManager::SCOPE_PRIVATE,
  67. $displayNameProperty ? $displayNameProperty->getVerified() : IAccountManager::NOT_VERIFIED,
  68. $displayNameProperty ? $displayNameProperty->getVerificationData() : ''
  69. );
  70. $changed = true;
  71. }
  72. }
  73. if ($changed) {
  74. $this->accountManager->updateAccount($account);
  75. $output->writeln($user->getUID() . ' - account data updated');
  76. } else {
  77. $output->writeln($user->getUID() . ' - nothing to update');
  78. }
  79. }
  80. }