SyncBirthdayCalendar.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. /** @var BirthdayService */
  37. private $birthdayService;
  38. /** @var IConfig */
  39. private $config;
  40. /** @var IUserManager */
  41. private $userManager;
  42. /**
  43. * @param IUserManager $userManager
  44. * @param IConfig $config
  45. * @param BirthdayService $birthdayService
  46. */
  47. public function __construct(IUserManager $userManager, IConfig $config,
  48. BirthdayService $birthdayService) {
  49. parent::__construct();
  50. $this->birthdayService = $birthdayService;
  51. $this->config = $config;
  52. $this->userManager = $userManager;
  53. }
  54. protected function configure() {
  55. $this
  56. ->setName('dav:sync-birthday-calendar')
  57. ->setDescription('Synchronizes the birthday calendar')
  58. ->addArgument('user',
  59. InputArgument::OPTIONAL,
  60. 'User for whom the birthday calendar will be synchronized');
  61. }
  62. /**
  63. * @param InputInterface $input
  64. * @param OutputInterface $output
  65. */
  66. protected function execute(InputInterface $input, OutputInterface $output): int {
  67. $this->verifyEnabled();
  68. $user = $input->getArgument('user');
  69. if (!is_null($user)) {
  70. if (!$this->userManager->userExists($user)) {
  71. throw new \InvalidArgumentException("User <$user> in unknown.");
  72. }
  73. // re-enable the birthday calendar in case it's called directly with a user name
  74. $isEnabled = $this->config->getUserValue($user, 'dav', 'generateBirthdayCalendar', 'yes');
  75. if ($isEnabled !== 'yes') {
  76. $this->config->setUserValue($user, 'dav', 'generateBirthdayCalendar', 'yes');
  77. $output->writeln("Re-enabling birthday calendar for $user");
  78. }
  79. $output->writeln("Start birthday calendar sync for $user");
  80. $this->birthdayService->syncUser($user);
  81. return 0;
  82. }
  83. $output->writeln("Start birthday calendar sync for all users ...");
  84. $p = new ProgressBar($output);
  85. $p->start();
  86. $this->userManager->callForSeenUsers(function ($user) use ($p) {
  87. $p->advance();
  88. $userId = $user->getUID();
  89. $isEnabled = $this->config->getUserValue($userId, 'dav', 'generateBirthdayCalendar', 'yes');
  90. if ($isEnabled !== 'yes') {
  91. return;
  92. }
  93. /** @var IUser $user */
  94. $this->birthdayService->syncUser($user->getUID());
  95. });
  96. $p->finish();
  97. $output->writeln('');
  98. return 0;
  99. }
  100. protected function verifyEnabled() {
  101. $isEnabled = $this->config->getAppValue('dav', 'generateBirthdayCalendar', 'yes');
  102. if ($isEnabled !== 'yes') {
  103. throw new \InvalidArgumentException('Birthday calendars are disabled');
  104. }
  105. }
  106. }