ValidatePhoneNumber.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Repair\NC21;
  25. use OC\Accounts\AccountManager;
  26. use OCP\Accounts\IAccountManager;
  27. use OCP\IConfig;
  28. use OCP\IUser;
  29. use OCP\IUserManager;
  30. use OCP\Migration\IOutput;
  31. use OCP\Migration\IRepairStep;
  32. class ValidatePhoneNumber implements IRepairStep {
  33. /** @var IConfig */
  34. protected $config;
  35. /** @var IUserManager */
  36. protected $userManager;
  37. /** @var AccountManager */
  38. private $accountManager;
  39. public function __construct(IUserManager $userManager,
  40. AccountManager $accountManager,
  41. IConfig $config) {
  42. $this->config = $config;
  43. $this->userManager = $userManager;
  44. $this->accountManager = $accountManager;
  45. }
  46. public function getName(): string {
  47. return 'Validate the phone number and store it in a known format for search';
  48. }
  49. private function shouldRun(): bool {
  50. return true;
  51. }
  52. public function run(IOutput $output): void {
  53. if ($this->config->getSystemValueString('default_phone_region', '') === '') {
  54. throw new \Exception('Can not validate phone numbers without `default_phone_region` being set in the config file');
  55. }
  56. $numUpdated = 0;
  57. $numRemoved = 0;
  58. $this->userManager->callForSeenUsers(function (IUser $user) use (&$numUpdated, &$numRemoved) {
  59. $account = $this->accountManager->getUser($user);
  60. if ($account[IAccountManager::PROPERTY_PHONE]['value'] !== '') {
  61. $updated = $this->accountManager->updateUser($user, $account);
  62. if ($account[IAccountManager::PROPERTY_PHONE]['value'] !== $updated[IAccountManager::PROPERTY_PHONE]['value']) {
  63. if ($updated[IAccountManager::PROPERTY_PHONE]['value'] === '') {
  64. $numRemoved++;
  65. } else {
  66. $numUpdated++;
  67. }
  68. }
  69. }
  70. });
  71. if ($numRemoved > 0 || $numUpdated > 0) {
  72. $output->info('Updated ' . $numUpdated . ' entries and cleaned ' . $numRemoved . ' invalid phone numbers');
  73. }
  74. }
  75. }