EnablePlugin.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\CalDAV\BirthdayCalendar;
  7. use OCA\DAV\CalDAV\BirthdayService;
  8. use OCA\DAV\CalDAV\CalendarHome;
  9. use OCP\IConfig;
  10. use OCP\IUser;
  11. use Sabre\DAV\Server;
  12. use Sabre\DAV\ServerPlugin;
  13. use Sabre\HTTP\RequestInterface;
  14. use Sabre\HTTP\ResponseInterface;
  15. /**
  16. * Class EnablePlugin
  17. * allows users to re-enable the birthday calendar via CalDAV
  18. *
  19. * @package OCA\DAV\CalDAV\BirthdayCalendar
  20. */
  21. class EnablePlugin extends ServerPlugin {
  22. public const NS_Nextcloud = 'http://nextcloud.com/ns';
  23. /**
  24. * @var IConfig
  25. */
  26. protected $config;
  27. /**
  28. * @var BirthdayService
  29. */
  30. protected $birthdayService;
  31. /**
  32. * @var Server
  33. */
  34. protected $server;
  35. /** @var IUser */
  36. private $user;
  37. /**
  38. * PublishPlugin constructor.
  39. *
  40. * @param IConfig $config
  41. * @param BirthdayService $birthdayService
  42. * @param IUser $user
  43. */
  44. public function __construct(IConfig $config, BirthdayService $birthdayService, IUser $user) {
  45. $this->config = $config;
  46. $this->birthdayService = $birthdayService;
  47. $this->user = $user;
  48. }
  49. /**
  50. * This method should return a list of server-features.
  51. *
  52. * This is for example 'versioning' and is added to the DAV: header
  53. * in an OPTIONS response.
  54. *
  55. * @return string[]
  56. */
  57. public function getFeatures() {
  58. return ['nc-enable-birthday-calendar'];
  59. }
  60. /**
  61. * Returns a plugin name.
  62. *
  63. * Using this name other plugins will be able to access other plugins
  64. * using Sabre\DAV\Server::getPlugin
  65. *
  66. * @return string
  67. */
  68. public function getPluginName() {
  69. return 'nc-enable-birthday-calendar';
  70. }
  71. /**
  72. * This initializes the plugin.
  73. *
  74. * This function is called by Sabre\DAV\Server, after
  75. * addPlugin is called.
  76. *
  77. * This method should set up the required event subscriptions.
  78. *
  79. * @param Server $server
  80. */
  81. public function initialize(Server $server) {
  82. $this->server = $server;
  83. $this->server->on('method:POST', [$this, 'httpPost']);
  84. }
  85. /**
  86. * We intercept this to handle POST requests on calendar homes.
  87. *
  88. * @param RequestInterface $request
  89. * @param ResponseInterface $response
  90. *
  91. * @return bool|void
  92. */
  93. public function httpPost(RequestInterface $request, ResponseInterface $response) {
  94. $node = $this->server->tree->getNodeForPath($this->server->getRequestUri());
  95. if (!($node instanceof CalendarHome)) {
  96. return;
  97. }
  98. $requestBody = $request->getBodyAsString();
  99. $this->server->xml->parse($requestBody, $request->getUrl(), $documentType);
  100. if ($documentType !== '{'.self::NS_Nextcloud.'}enable-birthday-calendar') {
  101. return;
  102. }
  103. $owner = substr($node->getOwner(), 17);
  104. if($owner !== $this->user->getUID()) {
  105. $this->server->httpResponse->setStatus(403);
  106. return false;
  107. }
  108. $this->config->setUserValue($this->user->getUID(), 'dav', 'generateBirthdayCalendar', 'yes');
  109. $this->birthdayService->syncUser($this->user->getUID());
  110. $this->server->httpResponse->setStatus(204);
  111. return false;
  112. }
  113. }