ValidatePhoneNumber.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Repair\NC21;
  8. use OCP\Accounts\IAccountManager;
  9. use OCP\IConfig;
  10. use OCP\IUser;
  11. use OCP\IUserManager;
  12. use OCP\Migration\IOutput;
  13. use OCP\Migration\IRepairStep;
  14. class ValidatePhoneNumber implements IRepairStep {
  15. /** @var IConfig */
  16. protected $config;
  17. /** @var IUserManager */
  18. protected $userManager;
  19. /** @var IAccountManager */
  20. private $accountManager;
  21. public function __construct(IUserManager $userManager,
  22. IAccountManager $accountManager,
  23. IConfig $config) {
  24. $this->config = $config;
  25. $this->userManager = $userManager;
  26. $this->accountManager = $accountManager;
  27. }
  28. public function getName(): string {
  29. return 'Validate the phone number and store it in a known format for search';
  30. }
  31. public function run(IOutput $output): void {
  32. if ($this->config->getSystemValueString('default_phone_region', '') === '') {
  33. $output->warning('Can not validate phone numbers without `default_phone_region` being set in the config file');
  34. return;
  35. }
  36. $numUpdated = 0;
  37. $numRemoved = 0;
  38. $this->userManager->callForSeenUsers(function (IUser $user) use (&$numUpdated, &$numRemoved) {
  39. $account = $this->accountManager->getAccount($user);
  40. $property = $account->getProperty(IAccountManager::PROPERTY_PHONE);
  41. if ($property->getValue() !== '') {
  42. $this->accountManager->updateAccount($account);
  43. $updatedAccount = $this->accountManager->getAccount($user);
  44. $updatedProperty = $updatedAccount->getProperty(IAccountManager::PROPERTY_PHONE);
  45. if ($property->getValue() !== $updatedProperty->getValue()) {
  46. if ($updatedProperty->getValue() === '') {
  47. $numRemoved++;
  48. } else {
  49. $numUpdated++;
  50. }
  51. }
  52. }
  53. });
  54. if ($numRemoved > 0 || $numUpdated > 0) {
  55. $output->info('Updated ' . $numUpdated . ' entries and cleaned ' . $numRemoved . ' invalid phone numbers');
  56. }
  57. }
  58. }