SyncBirthdayCalendar.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  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, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\DAV\Command;
  26. use OCA\DAV\CalDAV\BirthdayService;
  27. use OCP\IConfig;
  28. use OCP\IUser;
  29. use OCP\IUserManager;
  30. use Symfony\Component\Console\Command\Command;
  31. use Symfony\Component\Console\Helper\ProgressBar;
  32. use Symfony\Component\Console\Input\InputArgument;
  33. use Symfony\Component\Console\Input\InputInterface;
  34. use Symfony\Component\Console\Output\OutputInterface;
  35. class SyncBirthdayCalendar extends Command {
  36. public function __construct(
  37. private IUserManager $userManager,
  38. private IConfig $config,
  39. private BirthdayService $birthdayService,
  40. ) {
  41. parent::__construct();
  42. }
  43. protected function configure(): void {
  44. $this
  45. ->setName('dav:sync-birthday-calendar')
  46. ->setDescription('Synchronizes the birthday calendar')
  47. ->addArgument('user',
  48. InputArgument::OPTIONAL,
  49. 'User for whom the birthday calendar will be synchronized');
  50. }
  51. protected function execute(InputInterface $input, OutputInterface $output): int {
  52. $this->verifyEnabled();
  53. $user = $input->getArgument('user');
  54. if (!is_null($user)) {
  55. if (!$this->userManager->userExists($user)) {
  56. throw new \InvalidArgumentException("User <$user> in unknown.");
  57. }
  58. // re-enable the birthday calendar in case it's called directly with a user name
  59. $isEnabled = $this->config->getUserValue($user, 'dav', 'generateBirthdayCalendar', 'yes');
  60. if ($isEnabled !== 'yes') {
  61. $this->config->setUserValue($user, 'dav', 'generateBirthdayCalendar', 'yes');
  62. $output->writeln("Re-enabling birthday calendar for $user");
  63. }
  64. $output->writeln("Start birthday calendar sync for $user");
  65. $this->birthdayService->syncUser($user);
  66. return self::SUCCESS;
  67. }
  68. $output->writeln("Start birthday calendar sync for all users ...");
  69. $p = new ProgressBar($output);
  70. $p->start();
  71. $this->userManager->callForSeenUsers(function ($user) use ($p) {
  72. $p->advance();
  73. $userId = $user->getUID();
  74. $isEnabled = $this->config->getUserValue($userId, 'dav', 'generateBirthdayCalendar', 'yes');
  75. if ($isEnabled !== 'yes') {
  76. return;
  77. }
  78. /** @var IUser $user */
  79. $this->birthdayService->syncUser($user->getUID());
  80. });
  81. $p->finish();
  82. $output->writeln('');
  83. return self::SUCCESS;
  84. }
  85. protected function verifyEnabled(): void {
  86. $isEnabled = $this->config->getAppValue('dav', 'generateBirthdayCalendar', 'yes');
  87. if ($isEnabled !== 'yes') {
  88. throw new \InvalidArgumentException('Birthday calendars are disabled');
  89. }
  90. }
  91. }