from_file.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 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. File-based rights.
  20. The owner is 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. from radicale import config, log
  39. from radicale.rights import owner_only
  40. # Manage Python2/3 different modules
  41. # pylint: disable=F0401
  42. try:
  43. from configparser import RawConfigParser as ConfigParser, NoSectionError, NoOptionError
  44. except ImportError:
  45. from ConfigParser import RawConfigParser as ConfigParser, NoSectionError, NoOptionError
  46. # pylint: enable=F0401
  47. FILENAME = config.get("rights", "file")
  48. if FILENAME:
  49. log.LOGGER.debug("Reading rights from file %s" % FILENAME)
  50. RIGHTS = ConfigParser()
  51. RIGHTS.read(FILENAME)
  52. else:
  53. log.LOGGER.error("No file name configured for rights type 'from_file'")
  54. RIGHTS = None
  55. def read_authorized(user, collection):
  56. """Check if the user is allowed to read the collection."""
  57. if user is None:
  58. return False
  59. elif owner_only.read_authorized(user, collection):
  60. return True
  61. else:
  62. try:
  63. return "r" in RIGHTS.get(collection.url.rstrip("/") or "/", user)
  64. except (NoSectionError, NoOptionError):
  65. return False
  66. def write_authorized(user, collection):
  67. """Check if the user is allowed to write the collection."""
  68. if user is None:
  69. return False
  70. elif owner_only.read_authorized(user, collection):
  71. return True
  72. else:
  73. try:
  74. return "w" in RIGHTS.get(collection.url.rstrip("/") or "/", user)
  75. except (NoSectionError, NoOptionError):
  76. return False