SyncBirthdayCalendar.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Command;
  8. use OCA\DAV\CalDAV\BirthdayService;
  9. use OCP\IConfig;
  10. use OCP\IUser;
  11. use OCP\IUserManager;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Helper\ProgressBar;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. class SyncBirthdayCalendar extends Command {
  18. public function __construct(
  19. private IUserManager $userManager,
  20. private IConfig $config,
  21. private BirthdayService $birthdayService,
  22. ) {
  23. parent::__construct();
  24. }
  25. protected function configure(): void {
  26. $this
  27. ->setName('dav:sync-birthday-calendar')
  28. ->setDescription('Synchronizes the birthday calendar')
  29. ->addArgument('user',
  30. InputArgument::OPTIONAL,
  31. 'User for whom the birthday calendar will be synchronized');
  32. }
  33. protected function execute(InputInterface $input, OutputInterface $output): int {
  34. $this->verifyEnabled();
  35. $user = $input->getArgument('user');
  36. if (!is_null($user)) {
  37. if (!$this->userManager->userExists($user)) {
  38. throw new \InvalidArgumentException("User <$user> in unknown.");
  39. }
  40. // re-enable the birthday calendar in case it's called directly with a user name
  41. $isEnabled = $this->config->getUserValue($user, 'dav', 'generateBirthdayCalendar', 'yes');
  42. if ($isEnabled !== 'yes') {
  43. $this->config->setUserValue($user, 'dav', 'generateBirthdayCalendar', 'yes');
  44. $output->writeln("Re-enabling birthday calendar for $user");
  45. }
  46. $output->writeln("Start birthday calendar sync for $user");
  47. $this->birthdayService->syncUser($user);
  48. return self::SUCCESS;
  49. }
  50. $output->writeln('Start birthday calendar sync for all users ...');
  51. $p = new ProgressBar($output);
  52. $p->start();
  53. $this->userManager->callForSeenUsers(function ($user) use ($p): void {
  54. $p->advance();
  55. $userId = $user->getUID();
  56. $isEnabled = $this->config->getUserValue($userId, 'dav', 'generateBirthdayCalendar', 'yes');
  57. if ($isEnabled !== 'yes') {
  58. return;
  59. }
  60. /** @var IUser $user */
  61. $this->birthdayService->syncUser($user->getUID());
  62. });
  63. $p->finish();
  64. $output->writeln('');
  65. return self::SUCCESS;
  66. }
  67. protected function verifyEnabled(): void {
  68. $isEnabled = $this->config->getAppValue('dav', 'generateBirthdayCalendar', 'yes');
  69. if ($isEnabled !== 'yes') {
  70. throw new \InvalidArgumentException('Birthday calendars are disabled');
  71. }
  72. }
  73. }