EnablePlugin.php 2.8 KB

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