1
0
Fork 0
mirror of https://github.com/Kozea/Radicale.git synced 2025-06-26 16:45:52 +00:00
Radicale/radicale/rights/from_file.py

95 lines
2.7 KiB
Python
Raw Normal View History

2012-08-11 00:56:45 +02:00
# -*- coding: utf-8 -*-
#
# This file is part of Radicale Server - Calendar Server
2013-04-26 01:28:03 +02:00
# Copyright © 2012-2013 Guillaume Ayoub
2012-08-11 00:56:45 +02:00
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Radicale. If not, see <http://www.gnu.org/licenses/>.
"""
File-based rights.
The owner is implied to have all rights on their collections.
Rights are read from a file whose name is specified in the config (section
"right", key "file").
2012-08-11 00:56:45 +02:00
Example:
# This means user1 may read, user2 may write, user3 has full access
[/user0/calendar]
user1: r
user2: w
user3: rw
2012-08-11 00:56:45 +02:00
# user0 can read /user1/cal
[/user1/cal]
user0: r
2012-08-11 00:56:45 +02:00
# If a collection /a/b is shared and other users than the owner are supposed to
# find the collection in a propfind request, an additional line for /a has to
# be in the defintions. E.g.:
2012-08-11 00:56:45 +02:00
[/user0]
user1: r
2012-08-11 00:56:45 +02:00
"""
from radicale import config, log
from radicale.rights import owner_only
# Manage Python2/3 different modules
# pylint: disable=F0401
try:
2013-04-26 01:28:03 +02:00
from configparser import (
RawConfigParser as ConfigParser, NoSectionError, NoOptionError)
except ImportError:
2013-04-26 01:28:03 +02:00
from ConfigParser import (
RawConfigParser as ConfigParser, NoSectionError, NoOptionError)
# pylint: enable=F0401
2012-08-11 00:56:45 +02:00
FILENAME = config.get("rights", "file")
if FILENAME:
log.LOGGER.debug("Reading rights from file %s" % FILENAME)
RIGHTS = ConfigParser()
RIGHTS.read(FILENAME)
else:
log.LOGGER.error("No file name configured for rights type 'from_file'")
RIGHTS = None
2012-08-11 00:56:45 +02:00
def read_authorized(user, collection):
"""Check if the user is allowed to read the collection."""
2013-02-01 00:16:31 +01:00
if user is None:
return False
elif owner_only.read_authorized(user, collection):
return True
else:
try:
return "r" in RIGHTS.get(collection.url.rstrip("/") or "/", user)
except (NoSectionError, NoOptionError):
return False
2012-08-11 00:56:45 +02:00
2013-04-26 01:28:03 +02:00
2012-08-11 00:56:45 +02:00
def write_authorized(user, collection):
"""Check if the user is allowed to write the collection."""
2013-02-01 00:16:31 +01:00
if user is None:
return False
elif owner_only.read_authorized(user, collection):
return True
else:
try:
return "w" in RIGHTS.get(collection.url.rstrip("/") or "/", user)
except (NoSectionError, NoOptionError):
return False