1
0

SyncAccountDataCommand.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. use OC\Core\Command\Base;
  25. use OCP\Accounts\IAccountManager;
  26. use OCP\Accounts\PropertyDoesNotExistException;
  27. use OCP\IUser;
  28. use OCP\IUserManager;
  29. use OCP\User\Backend\IGetDisplayNameBackend;
  30. use Symfony\Component\Console\Input\InputInterface;
  31. use Symfony\Component\Console\Input\InputOption;
  32. use Symfony\Component\Console\Output\OutputInterface;
  33. class SyncAccountDataCommand extends Base {
  34. protected IUserManager $userManager;
  35. protected IAccountManager $accountManager;
  36. public function __construct(
  37. IUserManager $userManager,
  38. IAccountManager $accountManager
  39. ) {
  40. $this->userManager = $userManager;
  41. $this->accountManager = $accountManager;
  42. parent::__construct();
  43. }
  44. protected function configure() {
  45. $this
  46. ->setName('user:sync-account-data')
  47. ->setDescription('sync user backend data to accounts table for configured users')
  48. ->addOption(
  49. 'limit',
  50. 'l',
  51. InputOption::VALUE_OPTIONAL,
  52. 'Number of users to retrieve',
  53. '500'
  54. )->addOption(
  55. 'offset',
  56. 'o',
  57. InputOption::VALUE_OPTIONAL,
  58. 'Offset for retrieving users',
  59. '0'
  60. );
  61. }
  62. protected function execute(InputInterface $input, OutputInterface $output): int {
  63. $users = $this->userManager->searchDisplayName('', (int) $input->getOption('limit'), (int) $input->getOption('offset'));
  64. foreach ($users as $user) {
  65. $this->updateUserAccount($user, $output);
  66. }
  67. return 0;
  68. }
  69. private function updateUserAccount(IUser $user, OutputInterface $output): void {
  70. $changed = false;
  71. $account = $this->accountManager->getAccount($user);
  72. if ($user->getBackend() instanceof IGetDisplayNameBackend) {
  73. try {
  74. $displayNameProperty = $account->getProperty(IAccountManager::PROPERTY_DISPLAYNAME);
  75. } catch (PropertyDoesNotExistException) {
  76. $displayNameProperty = null;
  77. }
  78. if (!$displayNameProperty || $displayNameProperty->getValue() !== $user->getDisplayName()) {
  79. $output->writeln($user->getUID() . ' - updating changed display name');
  80. $account->setProperty(
  81. IAccountManager::PROPERTY_DISPLAYNAME,
  82. $user->getDisplayName(),
  83. $displayNameProperty ? $displayNameProperty->getScope() : IAccountManager::SCOPE_PRIVATE,
  84. $displayNameProperty ? $displayNameProperty->getVerified() : IAccountManager::NOT_VERIFIED,
  85. $displayNameProperty ? $displayNameProperty->getVerificationData() : ''
  86. );
  87. $changed = true;
  88. }
  89. }
  90. if ($changed) {
  91. $this->accountManager->updateAccount($account);
  92. $output->writeln($user->getUID() . ' - account data updated');
  93. } else {
  94. $output->writeln($user->getUID() . ' - nothing to update');
  95. }
  96. }
  97. }