2017-08-23 20:51:06 +02:00
|
|
|
from typing import Optional, Union
|
|
|
|
|
2015-02-12 20:57:05 +01:00
|
|
|
from django.contrib.auth import get_user_model
|
2017-01-26 15:34:24 +01:00
|
|
|
from django.contrib.auth.models import AnonymousUser
|
2017-08-23 20:51:06 +02:00
|
|
|
from django.db.models import Model
|
2015-01-22 22:50:19 +01:00
|
|
|
|
2016-12-17 09:30:20 +01:00
|
|
|
from .collection import CollectionElement
|
2015-01-22 22:50:19 +01:00
|
|
|
|
|
|
|
|
2017-08-23 20:51:06 +02:00
|
|
|
def has_perm(user: Optional[CollectionElement], perm: str) -> bool:
|
2016-12-17 09:30:20 +01:00
|
|
|
"""
|
|
|
|
Checks that user has a specific permission.
|
2017-01-15 13:33:54 +01:00
|
|
|
|
2017-01-26 21:15:35 +01:00
|
|
|
User can be a CollectionElement of a user or None.
|
2016-12-17 09:30:20 +01:00
|
|
|
"""
|
2017-01-26 21:15:35 +01:00
|
|
|
group_collection_string = 'users/group' # This is the hard coded collection string for openslides.users.models.Group
|
|
|
|
|
2017-01-26 15:34:24 +01:00
|
|
|
# Convert user to right type
|
2017-08-24 12:26:55 +02:00
|
|
|
# TODO: Remove this and make use, that user has always the right type
|
2017-01-26 15:34:24 +01:00
|
|
|
user = user_to_collection_user(user)
|
|
|
|
if user is None and not anonymous_is_enabled():
|
2016-12-17 09:30:20 +01:00
|
|
|
has_perm = False
|
2017-01-26 15:34:24 +01:00
|
|
|
elif user is None:
|
2017-01-26 21:15:35 +01:00
|
|
|
# Use the permissions from the default group with id 1.
|
|
|
|
default_group = CollectionElement.from_values(group_collection_string, 1)
|
2017-01-26 15:34:24 +01:00
|
|
|
has_perm = perm in default_group.get_full_data()['permissions']
|
2016-12-17 09:30:20 +01:00
|
|
|
else:
|
|
|
|
# Get all groups of the user and then see, if one group has the required
|
|
|
|
# permission. If the user has no groups, then use group 1.
|
|
|
|
group_ids = user.get_full_data()['groups_id'] or [1]
|
|
|
|
for group_id in group_ids:
|
2017-01-26 21:15:35 +01:00
|
|
|
group = CollectionElement.from_values(group_collection_string, group_id)
|
2016-12-17 09:30:20 +01:00
|
|
|
if perm in group.get_full_data()['permissions']:
|
|
|
|
has_perm = True
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
has_perm = False
|
|
|
|
return has_perm
|
2017-01-15 13:33:54 +01:00
|
|
|
|
|
|
|
|
2017-08-23 20:51:06 +02:00
|
|
|
def anonymous_is_enabled() -> bool:
|
2017-01-26 15:34:24 +01:00
|
|
|
"""
|
2017-01-26 21:15:35 +01:00
|
|
|
Returns True if the anonymous user is enabled in the settings.
|
2017-01-26 15:34:24 +01:00
|
|
|
"""
|
2017-08-22 14:17:20 +02:00
|
|
|
from ..core.config import config
|
|
|
|
return config['general_system_enable_anonymous']
|
2017-01-26 15:34:24 +01:00
|
|
|
|
|
|
|
|
2017-08-23 20:51:06 +02:00
|
|
|
AnyUser = Union[Model, CollectionElement, int, AnonymousUser, None]
|
|
|
|
|
|
|
|
|
|
|
|
def user_to_collection_user(user: AnyUser) -> Optional[CollectionElement]:
|
2017-01-26 15:34:24 +01:00
|
|
|
"""
|
2017-01-26 21:15:35 +01:00
|
|
|
Takes an object, that represents a user and converts it to a CollectionElement
|
|
|
|
or to None, if it is an anonymous user.
|
2017-01-26 15:34:24 +01:00
|
|
|
|
|
|
|
User can be
|
2017-01-26 21:15:35 +01:00
|
|
|
* an user object,
|
|
|
|
* a CollectionElement of an user,
|
|
|
|
* an user id or
|
2017-01-26 15:34:24 +01:00
|
|
|
* an anonymous user.
|
|
|
|
|
2017-01-26 21:15:35 +01:00
|
|
|
Raises an TypeError, if the given user object can not be converted.
|
2017-01-26 15:34:24 +01:00
|
|
|
"""
|
2017-01-26 21:15:35 +01:00
|
|
|
User = get_user_model()
|
|
|
|
|
2017-01-26 15:34:24 +01:00
|
|
|
if user is None:
|
|
|
|
# Nothing to do
|
|
|
|
pass
|
2017-01-26 21:15:35 +01:00
|
|
|
elif isinstance(user, CollectionElement) and user.collection_string == User.get_collection_string():
|
2017-01-26 15:34:24 +01:00
|
|
|
# Nothing to do
|
|
|
|
pass
|
|
|
|
elif isinstance(user, CollectionElement):
|
|
|
|
raise TypeError(
|
|
|
|
"Unsupported type for user. Only CollectionElements for users can be"
|
|
|
|
"used. Not {}".format(user.collection_string))
|
|
|
|
elif isinstance(user, int):
|
2017-01-26 21:15:35 +01:00
|
|
|
user = CollectionElement.from_values(User.get_collection_string(), user)
|
2017-01-26 15:34:24 +01:00
|
|
|
elif isinstance(user, AnonymousUser):
|
|
|
|
user = None
|
2017-01-26 21:15:35 +01:00
|
|
|
elif isinstance(user, User):
|
2017-01-26 15:34:24 +01:00
|
|
|
# Converts a user object to a collection element.
|
|
|
|
# from_instance can not be used because the user serializer loads
|
2017-01-26 21:15:35 +01:00
|
|
|
# the group from the db. So each call to from_instance(user) costs
|
2017-01-26 15:34:24 +01:00
|
|
|
# one db query.
|
2017-01-26 21:15:35 +01:00
|
|
|
user = CollectionElement.from_values(User.get_collection_string(), user.id)
|
2017-01-26 15:34:24 +01:00
|
|
|
else:
|
|
|
|
raise TypeError(
|
|
|
|
"Unsupported type for user. User {} has type {}.".format(user, type(user)))
|
|
|
|
return user
|