EnablePlugin.php 2.8 KB

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