EnablePlugin.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * @copyright 2017, Georg Ehrke <oc.list@georgehrke.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  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
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\DAV\CalDAV\BirthdayCalendar;
  26. use OCA\DAV\CalDAV\BirthdayService;
  27. use OCA\DAV\CalDAV\CalendarHome;
  28. use OCP\IConfig;
  29. use OCP\IUser;
  30. use Sabre\DAV\Server;
  31. use Sabre\DAV\ServerPlugin;
  32. use Sabre\HTTP\RequestInterface;
  33. use Sabre\HTTP\ResponseInterface;
  34. /**
  35. * Class EnablePlugin
  36. * allows users to re-enable the birthday calendar via CalDAV
  37. *
  38. * @package OCA\DAV\CalDAV\BirthdayCalendar
  39. */
  40. class EnablePlugin extends ServerPlugin {
  41. public const NS_Nextcloud = 'http://nextcloud.com/ns';
  42. /**
  43. * @var IConfig
  44. */
  45. protected $config;
  46. /**
  47. * @var BirthdayService
  48. */
  49. protected $birthdayService;
  50. /**
  51. * @var Server
  52. */
  53. protected $server;
  54. /** @var IUser */
  55. private $user;
  56. /**
  57. * PublishPlugin constructor.
  58. *
  59. * @param IConfig $config
  60. * @param BirthdayService $birthdayService
  61. * @param IUser $user
  62. */
  63. public function __construct(IConfig $config, BirthdayService $birthdayService, IUser $user) {
  64. $this->config = $config;
  65. $this->birthdayService = $birthdayService;
  66. $this->user = $user;
  67. }
  68. /**
  69. * This method should return a list of server-features.
  70. *
  71. * This is for example 'versioning' and is added to the DAV: header
  72. * in an OPTIONS response.
  73. *
  74. * @return string[]
  75. */
  76. public function getFeatures() {
  77. return ['nc-enable-birthday-calendar'];
  78. }
  79. /**
  80. * Returns a plugin name.
  81. *
  82. * Using this name other plugins will be able to access other plugins
  83. * using Sabre\DAV\Server::getPlugin
  84. *
  85. * @return string
  86. */
  87. public function getPluginName() {
  88. return 'nc-enable-birthday-calendar';
  89. }
  90. /**
  91. * This initializes the plugin.
  92. *
  93. * This function is called by Sabre\DAV\Server, after
  94. * addPlugin is called.
  95. *
  96. * This method should set up the required event subscriptions.
  97. *
  98. * @param Server $server
  99. */
  100. public function initialize(Server $server) {
  101. $this->server = $server;
  102. $this->server->on('method:POST', [$this, 'httpPost']);
  103. }
  104. /**
  105. * We intercept this to handle POST requests on calendar homes.
  106. *
  107. * @param RequestInterface $request
  108. * @param ResponseInterface $response
  109. *
  110. * @return bool|void
  111. */
  112. public function httpPost(RequestInterface $request, ResponseInterface $response) {
  113. $node = $this->server->tree->getNodeForPath($this->server->getRequestUri());
  114. if (!($node instanceof CalendarHome)) {
  115. return;
  116. }
  117. $requestBody = $request->getBodyAsString();
  118. $this->server->xml->parse($requestBody, $request->getUrl(), $documentType);
  119. if ($documentType !== '{'.self::NS_Nextcloud.'}enable-birthday-calendar') {
  120. return;
  121. }
  122. $owner = substr($node->getOwner(), 17);
  123. if($owner !== $this->user->getUID()) {
  124. $this->server->httpResponse->setStatus(403);
  125. return false;
  126. }
  127. $this->config->setUserValue($this->user->getUID(), 'dav', 'generateBirthdayCalendar', 'yes');
  128. $this->birthdayService->syncUser($this->user->getUID());
  129. $this->server->httpResponse->setStatus(204);
  130. return false;
  131. }
  132. }