from_file.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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
  44. except ImportError:
  45. from ConfigParser import RawConfigParser as ConfigParser
  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. return (
  58. owner_only.read_authorized(user, collection) or
  59. "r" in RIGHTS.get(collection.url.rstrip("/") or "/", user))
  60. def write_authorized(user, collection):
  61. """Check if the user is allowed to write the collection."""
  62. return (
  63. owner_only.write_authorized(user, collection) or
  64. "w" in RIGHTS.get(collection.url.rstrip("/") or "/", user))