from_file.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2012-2013 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. File-based rights.
  20. The owners are implied to have all rights on their collections.
  21. Rights are read from a file whose name is specified in the config (section
  22. "right", key "file").
  23. Example:
  24. # This means user1 may read, user2 may write, user3 has full access
  25. [user0/calendar]
  26. user1: r
  27. user2: w
  28. user3: rw
  29. # user0 can read user1/cal
  30. [user1/cal]
  31. user0: r
  32. # If a collection a/b is shared and other users than the owner are supposed to
  33. # find the collection in a propfind request, an additional line for a has to
  34. # be in the defintions. E.g.:
  35. [user0]
  36. user1: r
  37. """
  38. import os.path
  39. from radicale import config, log
  40. from radicale.rights import owner_only
  41. # Manage Python2/3 different modules
  42. # pylint: disable=F0401
  43. try:
  44. from configparser import (
  45. RawConfigParser as ConfigParser, NoSectionError, NoOptionError)
  46. except ImportError:
  47. from ConfigParser import (
  48. RawConfigParser as ConfigParser, NoSectionError, NoOptionError)
  49. # pylint: enable=F0401
  50. FILENAME = (
  51. os.path.expanduser(config.get("rights", "file")) or
  52. log.LOGGER.error("No file name configured for rights type 'from_file'"))
  53. def _read_rights():
  54. """Update the rights according to the configuration file."""
  55. log.LOGGER.debug("Reading rights from file %s" % FILENAME)
  56. rights = ConfigParser()
  57. if not rights.read(FILENAME):
  58. log.LOGGER.error(
  59. "File '%s' not found for rights management" % FILENAME)
  60. return rights
  61. def read_authorized(user, collection):
  62. """Check if the user is allowed to read the collection."""
  63. if user is None:
  64. return False
  65. elif owner_only.read_authorized(user, collection):
  66. return True
  67. else:
  68. try:
  69. return "r" in _read_rights().get(
  70. collection.url.rstrip("/") or "/", user)
  71. except (NoSectionError, NoOptionError):
  72. return False
  73. def write_authorized(user, collection):
  74. """Check if the user is allowed to write the collection."""
  75. if user is None:
  76. return False
  77. elif owner_only.write_authorized(user, collection):
  78. return True
  79. else:
  80. try:
  81. return "w" in _read_rights().get(
  82. collection.url.rstrip("/") or "/", user)
  83. except (NoSectionError, NoOptionError):
  84. return False