SyncBirthdayCalendar.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\DAV\Command;
  27. use OCA\DAV\CalDAV\BirthdayService;
  28. use OCP\IConfig;
  29. use OCP\IUser;
  30. use OCP\IUserManager;
  31. use Symfony\Component\Console\Command\Command;
  32. use Symfony\Component\Console\Helper\ProgressBar;
  33. use Symfony\Component\Console\Input\InputArgument;
  34. use Symfony\Component\Console\Input\InputInterface;
  35. use Symfony\Component\Console\Output\OutputInterface;
  36. class SyncBirthdayCalendar extends Command {
  37. /** @var BirthdayService */
  38. private $birthdayService;
  39. /** @var IConfig */
  40. private $config;
  41. /** @var IUserManager */
  42. private $userManager;
  43. /**
  44. * @param IUserManager $userManager
  45. * @param IConfig $config
  46. * @param BirthdayService $birthdayService
  47. */
  48. public function __construct(IUserManager $userManager, IConfig $config,
  49. BirthdayService $birthdayService) {
  50. parent::__construct();
  51. $this->birthdayService = $birthdayService;
  52. $this->config = $config;
  53. $this->userManager = $userManager;
  54. }
  55. protected function configure() {
  56. $this
  57. ->setName('dav:sync-birthday-calendar')
  58. ->setDescription('Synchronizes the birthday calendar')
  59. ->addArgument('user',
  60. InputArgument::OPTIONAL,
  61. 'User for whom the birthday calendar will be synchronized');
  62. }
  63. /**
  64. * @param InputInterface $input
  65. * @param OutputInterface $output
  66. */
  67. protected function execute(InputInterface $input, OutputInterface $output) {
  68. $this->verifyEnabled();
  69. $user = $input->getArgument('user');
  70. if (!is_null($user)) {
  71. if (!$this->userManager->userExists($user)) {
  72. throw new \InvalidArgumentException("User <$user> in unknown.");
  73. }
  74. // re-enable the birthday calendar in case it's called directly with a user name
  75. $isEnabled = $this->config->getUserValue($user, 'dav', 'generateBirthdayCalendar', 'yes');
  76. if ($isEnabled !== 'yes') {
  77. $this->config->setUserValue($user, 'dav', 'generateBirthdayCalendar', 'yes');
  78. $output->writeln("Re-enabling birthday calendar for $user");
  79. }
  80. $output->writeln("Start birthday calendar sync for $user");
  81. $this->birthdayService->syncUser($user);
  82. return;
  83. }
  84. $output->writeln("Start birthday calendar sync for all users ...");
  85. $p = new ProgressBar($output);
  86. $p->start();
  87. $this->userManager->callForSeenUsers(function ($user) use ($p) {
  88. $p->advance();
  89. $userId = $user->getUID();
  90. $isEnabled = $this->config->getUserValue($userId, 'dav', 'generateBirthdayCalendar', 'yes');
  91. if ($isEnabled !== 'yes') {
  92. return;
  93. }
  94. /** @var IUser $user */
  95. $this->birthdayService->syncUser($user->getUID());
  96. });
  97. $p->finish();
  98. $output->writeln('');
  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. }