ValidatePhoneNumber.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Repair\NC21;
  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 IAccountManager */
  38. private $accountManager;
  39. public function __construct(IUserManager $userManager,
  40. IAccountManager $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. public function run(IOutput $output): void {
  50. if ($this->config->getSystemValueString('default_phone_region', '') === '') {
  51. $output->warning('Can not validate phone numbers without `default_phone_region` being set in the config file');
  52. return;
  53. }
  54. $numUpdated = 0;
  55. $numRemoved = 0;
  56. $this->userManager->callForSeenUsers(function (IUser $user) use (&$numUpdated, &$numRemoved) {
  57. $account = $this->accountManager->getAccount($user);
  58. $property = $account->getProperty(IAccountManager::PROPERTY_PHONE);
  59. if ($property->getValue() !== '') {
  60. $this->accountManager->updateAccount($account);
  61. $updatedAccount = $this->accountManager->getAccount($user);
  62. $updatedProperty = $updatedAccount->getProperty(IAccountManager::PROPERTY_PHONE);
  63. if ($property->getValue() !== $updatedProperty->getValue()) {
  64. if ($updatedProperty->getValue() === '') {
  65. $numRemoved++;
  66. } else {
  67. $numUpdated++;
  68. }
  69. }
  70. }
  71. });
  72. if ($numRemoved > 0 || $numUpdated > 0) {
  73. $output->info('Updated ' . $numUpdated . ' entries and cleaned ' . $numRemoved . ' invalid phone numbers');
  74. }
  75. }
  76. }