access.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2011-2012 Guillaume Ayoub
  5. #
  6. # This library is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. Radicale access module.
  20. Manage access to collections.
  21. """
  22. from radicale import auth, rights, log
  23. AUTH = None
  24. RIGHTS = None
  25. def load():
  26. """Load authentication and rights modules."""
  27. global AUTH, RIGHTS
  28. AUTH = auth.load()
  29. RIGHTS = rights.load()
  30. def is_authenticated(user, password):
  31. """Check if the user is authenticated."""
  32. if AUTH is None:
  33. return True
  34. return AUTH.is_authenticated(user, password) if user else False
  35. def read_authorized(user, collection):
  36. """Check if the user is allowed to read the collection."""
  37. if RIGHTS is None:
  38. return True
  39. user_authorized = RIGHTS.read_authorized(user, collection)
  40. log.LOGGER.debug(
  41. "Read %s %s -- %i" % (user, collection.owner, user_authorized))
  42. return user_authorized
  43. def write_authorized(user, collection):
  44. """Check if the user is allowed to write the collection."""
  45. if RIGHTS is None:
  46. return True
  47. user_authorized = RIGHTS.write_authorized(user, collection)
  48. log.LOGGER.debug(
  49. "Write %s %s -- %i" % (user, collection.owner, user_authorized))
  50. return user_authorized