__init__.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2012-2017 Guillaume Ayoub
  3. # Copyright © 2017-2018 Unrud <unrud@outlook.com>
  4. #
  5. # This library is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  17. """
  18. The rights module used to determine if a user can read and/or write
  19. collections and entries.
  20. Permissions:
  21. - R: read collections (excluding address books and calendars)
  22. - r: read address book and calendar collections
  23. - i: subset of **r** that only allows direct access via HTTP method GET
  24. (CalDAV/CardDAV is susceptible to expensive search requests)
  25. - W: write collections (excluding address books and calendars)
  26. - w: write address book and calendar collections
  27. Take a look at the class ``BaseRights`` if you want to implement your own.
  28. """
  29. from typing import Sequence, Set
  30. from radicale import config, utils
  31. INTERNAL_TYPES: Sequence[str] = ("authenticated", "owner_write", "owner_only",
  32. "from_file")
  33. def load(configuration: "config.Configuration") -> "BaseRights":
  34. """Load the rights module chosen in configuration."""
  35. return utils.load_plugin(INTERNAL_TYPES, "rights", "Rights", BaseRights,
  36. configuration)
  37. def intersect(a: str, b: str) -> str:
  38. """Intersect two lists of rights.
  39. Returns all rights that are both in ``a`` and ``b``.
  40. """
  41. return "".join(set(a).intersection(set(b)))
  42. class BaseRights:
  43. _user_groups: Set[str] = set([])
  44. def __init__(self, configuration: "config.Configuration") -> None:
  45. """Initialize BaseRights.
  46. ``configuration`` see ``radicale.config`` module.
  47. The ``configuration`` must not change during the lifetime of
  48. this object, it is kept as an internal reference.
  49. """
  50. self.configuration = configuration
  51. def authorization(self, user: str, path: str) -> str:
  52. """Get granted rights of ``user`` for the collection ``path``.
  53. If ``user`` is empty, check for anonymous rights.
  54. ``path`` is sanitized.
  55. Returns granted rights (e.g. ``"RW"``).
  56. """
  57. raise NotImplementedError