delete.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2008 Nicolas Kandel
  3. # Copyright © 2008 Pascal Halter
  4. # Copyright © 2008-2017 Guillaume Ayoub
  5. # Copyright © 2017-2020 Unrud <unrud@outlook.com>
  6. # Copyright © 2024-2025 Peter Bieringer <pb@bieringer.de>
  7. #
  8. # This library is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  20. import xml.etree.ElementTree as ET
  21. from http import client
  22. from typing import Optional
  23. from radicale import httputils, storage, types, xmlutils
  24. from radicale.app.base import Access, ApplicationBase
  25. from radicale.hook import HookNotificationItem, HookNotificationItemTypes
  26. from radicale.log import logger
  27. def xml_delete(base_prefix: str, path: str, collection: storage.BaseCollection,
  28. item_href: Optional[str] = None) -> ET.Element:
  29. """Read and answer DELETE requests.
  30. Read rfc4918-9.6 for info.
  31. """
  32. collection.delete(item_href)
  33. multistatus = ET.Element(xmlutils.make_clark("D:multistatus"))
  34. response = ET.Element(xmlutils.make_clark("D:response"))
  35. multistatus.append(response)
  36. href_element = ET.Element(xmlutils.make_clark("D:href"))
  37. href_element.text = xmlutils.make_href(base_prefix, path)
  38. response.append(href_element)
  39. status = ET.Element(xmlutils.make_clark("D:status"))
  40. status.text = xmlutils.make_response(200)
  41. response.append(status)
  42. return multistatus
  43. class ApplicationPartDelete(ApplicationBase):
  44. def do_DELETE(self, environ: types.WSGIEnviron, base_prefix: str,
  45. path: str, user: str, remote_host: str, remote_useragent: str) -> types.WSGIResponse:
  46. """Manage DELETE request."""
  47. access = Access(self._rights, user, path)
  48. if not access.check("w"):
  49. return httputils.NOT_ALLOWED
  50. with self._storage.acquire_lock("w", user, path=path, request="DELETE"):
  51. item = next(iter(self._storage.discover(path)), None)
  52. if not item:
  53. return httputils.NOT_FOUND
  54. if not access.check("w", item):
  55. return httputils.NOT_ALLOWED
  56. if_match = environ.get("HTTP_IF_MATCH", "*")
  57. if if_match not in ("*", item.etag):
  58. # ETag precondition not verified, do not delete item
  59. return httputils.PRECONDITION_FAILED
  60. hook_notification_item_list = []
  61. if isinstance(item, storage.BaseCollection):
  62. if self._permit_delete_collection:
  63. if access.check("d", item):
  64. logger.info("delete of collection is permitted by config/option [rights] permit_delete_collection but explicit forbidden by permission 'd': %s", path)
  65. return httputils.NOT_ALLOWED
  66. else:
  67. if not access.check("D", item):
  68. logger.info("delete of collection is prevented by config/option [rights] permit_delete_collection and not explicit allowed by permission 'D': %s", path)
  69. return httputils.NOT_ALLOWED
  70. for i in item.get_all():
  71. hook_notification_item_list.append(
  72. HookNotificationItem(
  73. notification_item_type=HookNotificationItemTypes.DELETE,
  74. path=access.path,
  75. content=i.uid,
  76. uid=i.uid,
  77. old_content=i.serialize(), # type: ignore
  78. new_content=None
  79. )
  80. )
  81. xml_answer = xml_delete(base_prefix, path, item)
  82. else:
  83. assert item.collection is not None
  84. assert item.href is not None
  85. hook_notification_item_list.append(
  86. HookNotificationItem(
  87. notification_item_type=HookNotificationItemTypes.DELETE,
  88. path=access.path,
  89. content=item.uid,
  90. uid=item.uid,
  91. old_content=item.serialize(), # type: ignore
  92. new_content=None,
  93. )
  94. )
  95. xml_answer = xml_delete(
  96. base_prefix, path, item.collection, item.href)
  97. for notification_item in hook_notification_item_list:
  98. self._hook.notify(notification_item)
  99. headers = {"Content-Type": "text/xml; charset=%s" % self._encoding}
  100. return client.OK, headers, self._xml_response(xml_answer), None