__init__.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # This file is part of Radicale Server - Calendar 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 a collection
  22. - r: read an address book or calendar entry
  23. - W: write a collection
  24. - w: read an address book or calendar entry
  25. Take a look at the class ``BaseRights`` if you want to implement your own.
  26. """
  27. from importlib import import_module
  28. from radicale.log import logger
  29. INTERNAL_TYPES = ("authenticated", "owner_write", "owner_only", "from_file")
  30. def load(configuration):
  31. """Load the rights manager chosen in configuration."""
  32. rights_type = configuration.get("rights", "type")
  33. if rights_type in INTERNAL_TYPES:
  34. module = "radicale.rights.%s" % rights_type
  35. else:
  36. module = rights_type
  37. try:
  38. class_ = import_module(module).Rights
  39. except Exception as e:
  40. raise RuntimeError("Failed to load rights module %r: %s" %
  41. (module, e)) from e
  42. logger.info("Rights type is %r", rights_type)
  43. return class_(configuration)
  44. def intersect_permissions(a, b="RrWw"):
  45. return "".join(set(a).intersection(set(b)))
  46. class BaseRights:
  47. def __init__(self, configuration):
  48. """Initialize BaseRights.
  49. ``configuration`` see ``radicale.config`` module.
  50. The ``configuration`` must not change during the lifetime of
  51. this object, it is kept as an internal reference.
  52. """
  53. self.configuration = configuration
  54. def authorized(self, user, path, permissions):
  55. """Check if the user is allowed to read or write the collection.
  56. If ``user`` is empty, check for anonymous rights.
  57. ``path`` is sanitized.
  58. ``permissions`` can include "R", "r", "W", "w"
  59. Returns granted rights.
  60. """
  61. raise NotImplementedError