2018-11-03 23:40:20 +01:00
|
|
|
from typing import Dict, List, Union, cast
|
2017-08-23 20:51:06 +02:00
|
|
|
|
2018-11-01 17:30:18 +01:00
|
|
|
from asgiref.sync import async_to_sync
|
2018-08-31 15:33:41 +02:00
|
|
|
from django.apps import apps
|
|
|
|
from django.conf import settings
|
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
|
2018-08-31 15:33:41 +02:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2017-08-23 20:51:06 +02:00
|
|
|
from django.db.models import Model
|
2015-01-22 22:50:19 +01:00
|
|
|
|
2018-07-09 23:22:26 +02:00
|
|
|
from .cache import element_cache
|
2015-01-22 22:50:19 +01:00
|
|
|
|
|
|
|
|
2018-08-31 15:33:41 +02:00
|
|
|
GROUP_DEFAULT_PK = 1 # This is the hard coded pk for the default group.
|
2018-10-09 13:44:38 +02:00
|
|
|
GROUP_ADMIN_PK = 2 # This is the hard coded pk for the admin group.
|
2018-08-31 15:33:41 +02:00
|
|
|
|
2018-11-03 23:40:20 +01:00
|
|
|
# Hard coded collection string for users and groups
|
2019-01-06 16:22:33 +01:00
|
|
|
group_collection_string = "users/group"
|
|
|
|
user_collection_string = "users/user"
|
2018-11-03 23:40:20 +01:00
|
|
|
|
2018-08-31 15:33:41 +02:00
|
|
|
|
|
|
|
def get_group_model() -> Model:
|
|
|
|
"""
|
|
|
|
Return the Group model that is active in this project.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return apps.get_model(settings.AUTH_GROUP_MODEL, require_ready=False)
|
|
|
|
except ValueError:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"AUTH_GROUP_MODEL must be of the form 'app_label.model_name'"
|
|
|
|
)
|
2018-08-31 15:33:41 +02:00
|
|
|
except LookupError:
|
|
|
|
raise ImproperlyConfigured(
|
2019-01-06 16:22:33 +01:00
|
|
|
"AUTH_GROUP_MODEL refers to model '%s' that has not been installed"
|
|
|
|
% settings.AUTH_GROUP_MODEL
|
2018-08-31 15:33:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-11-03 23:40:20 +01:00
|
|
|
def has_perm(user_id: int, 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
|
|
|
|
2018-11-03 23:40:20 +01:00
|
|
|
user_id 0 means anonymous user.
|
2016-12-17 09:30:20 +01:00
|
|
|
"""
|
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
|
2018-11-03 23:40:20 +01:00
|
|
|
user_id = user_to_user_id(user_id)
|
|
|
|
return async_to_sync(async_has_perm)(user_id, perm)
|
2018-11-01 17:30:18 +01:00
|
|
|
|
|
|
|
|
2018-11-03 23:40:20 +01:00
|
|
|
async def async_has_perm(user_id: int, perm: str) -> bool:
|
2018-11-01 17:30:18 +01:00
|
|
|
"""
|
|
|
|
Checks that user has a specific permission.
|
|
|
|
|
2018-11-03 23:40:20 +01:00
|
|
|
user_id 0 means anonymous user.
|
2018-11-01 17:30:18 +01:00
|
|
|
"""
|
2018-11-03 23:40:20 +01:00
|
|
|
if not user_id and not await async_anonymous_is_enabled():
|
2016-12-17 09:30:20 +01:00
|
|
|
has_perm = False
|
2018-11-03 23:40:20 +01:00
|
|
|
elif not user_id:
|
2018-08-31 15:33:41 +02:00
|
|
|
# Use the permissions from the default group.
|
2019-01-06 16:22:33 +01:00
|
|
|
default_group = await element_cache.get_element_full_data(
|
|
|
|
group_collection_string, GROUP_DEFAULT_PK
|
|
|
|
)
|
2018-11-01 17:30:18 +01:00
|
|
|
if default_group is None:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise RuntimeError("Default Group does not exist.")
|
|
|
|
has_perm = perm in default_group["permissions"]
|
2016-12-17 09:30:20 +01:00
|
|
|
else:
|
2019-01-06 16:22:33 +01:00
|
|
|
user_data = await element_cache.get_element_full_data(
|
|
|
|
user_collection_string, user_id
|
|
|
|
)
|
2018-11-03 23:40:20 +01:00
|
|
|
if user_data is None:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise RuntimeError("User with id {} does not exist.".format(user_id))
|
|
|
|
if GROUP_ADMIN_PK in user_data["groups_id"]:
|
2018-11-03 23:40:20 +01:00
|
|
|
# User in admin group (pk 2) grants all permissions.
|
|
|
|
has_perm = True
|
2016-12-17 09:30:20 +01:00
|
|
|
else:
|
2018-11-03 23:40:20 +01:00
|
|
|
# Get all groups of the user and then see, if one group has the required
|
|
|
|
# permission. If the user has no groups, then use the default group.
|
2019-01-06 16:22:33 +01:00
|
|
|
group_ids = user_data["groups_id"] or [GROUP_DEFAULT_PK]
|
2018-11-03 23:40:20 +01:00
|
|
|
for group_id in group_ids:
|
2019-01-06 16:22:33 +01:00
|
|
|
group = await element_cache.get_element_full_data(
|
|
|
|
group_collection_string, group_id
|
|
|
|
)
|
2018-11-03 23:40:20 +01:00
|
|
|
if group is None:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise RuntimeError(
|
|
|
|
"User is in non existing group with id {}.".format(group_id)
|
|
|
|
)
|
2018-11-03 23:40:20 +01:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
if perm in group["permissions"]:
|
2018-11-03 23:40:20 +01:00
|
|
|
has_perm = True
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
has_perm = False
|
2016-12-17 09:30:20 +01:00
|
|
|
return has_perm
|
2017-01-15 13:33:54 +01:00
|
|
|
|
|
|
|
|
2018-11-03 23:40:20 +01:00
|
|
|
def in_some_groups(user_id: int, groups: List[int]) -> bool:
|
2018-08-31 15:33:41 +02:00
|
|
|
"""
|
|
|
|
Checks that user is in at least one given group. Groups can be given as a list
|
2018-10-09 13:44:38 +02:00
|
|
|
of ids or group instances. If the user is in the admin group (pk = 2) the result
|
|
|
|
is always true.
|
2018-08-31 15:33:41 +02:00
|
|
|
|
2018-11-03 23:40:20 +01:00
|
|
|
user_id 0 means anonymous user.
|
2018-08-31 15:33:41 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
if len(groups) == 0:
|
|
|
|
return False # early end here, if no groups are given.
|
|
|
|
|
|
|
|
# Convert user to right type
|
|
|
|
# TODO: Remove this and make use, that user has always the right type
|
2018-11-03 23:40:20 +01:00
|
|
|
user_id = user_to_user_id(user_id)
|
|
|
|
return async_to_sync(async_in_some_groups)(user_id, groups)
|
2018-11-01 17:30:18 +01:00
|
|
|
|
|
|
|
|
2018-11-03 23:40:20 +01:00
|
|
|
async def async_in_some_groups(user_id: int, groups: List[int]) -> bool:
|
2018-11-01 17:30:18 +01:00
|
|
|
"""
|
|
|
|
Checks that user is in at least one given group. Groups can be given as a list
|
|
|
|
of ids. If the user is in the admin group (pk = 2) the result
|
|
|
|
is always true.
|
|
|
|
|
2018-11-03 23:40:20 +01:00
|
|
|
user_id 0 means anonymous user.
|
2018-11-01 17:30:18 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
if not len(groups):
|
|
|
|
return False # early end here, if no groups are given.
|
|
|
|
|
2018-11-03 23:40:20 +01:00
|
|
|
if not user_id and not await async_anonymous_is_enabled():
|
2018-08-31 15:33:41 +02:00
|
|
|
in_some_groups = False
|
2018-11-03 23:40:20 +01:00
|
|
|
elif not user_id:
|
2018-08-31 15:33:41 +02:00
|
|
|
# Use the permissions from the default group.
|
|
|
|
in_some_groups = GROUP_DEFAULT_PK in groups
|
|
|
|
else:
|
2019-01-06 16:22:33 +01:00
|
|
|
user_data = await element_cache.get_element_full_data(
|
|
|
|
user_collection_string, user_id
|
|
|
|
)
|
2018-11-03 23:40:20 +01:00
|
|
|
if user_data is None:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise RuntimeError("User with id {} does not exist.".format(user_id))
|
|
|
|
if GROUP_ADMIN_PK in user_data["groups_id"]:
|
2018-11-03 23:40:20 +01:00
|
|
|
# User in admin group (pk 2) grants all permissions.
|
|
|
|
in_some_groups = True
|
2018-08-31 15:33:41 +02:00
|
|
|
else:
|
2018-11-03 23:40:20 +01:00
|
|
|
# Get all groups of the user and then see, if one group has the required
|
|
|
|
# permission. If the user has no groups, then use the default group.
|
2019-01-06 16:22:33 +01:00
|
|
|
group_ids = user_data["groups_id"] or [GROUP_DEFAULT_PK]
|
2018-11-03 23:40:20 +01:00
|
|
|
for group_id in group_ids:
|
|
|
|
if group_id in groups:
|
|
|
|
in_some_groups = True
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
in_some_groups = False
|
2018-08-31 15:33:41 +02:00
|
|
|
return in_some_groups
|
|
|
|
|
|
|
|
|
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
|
2019-01-06 16:22:33 +01:00
|
|
|
|
|
|
|
return config["general_system_enable_anonymous"]
|
2017-01-26 15:34:24 +01:00
|
|
|
|
|
|
|
|
2018-07-09 23:22:26 +02:00
|
|
|
async def async_anonymous_is_enabled() -> bool:
|
|
|
|
"""
|
|
|
|
Like anonymous_is_enabled but async.
|
|
|
|
"""
|
|
|
|
from ..core.config import config
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2018-07-09 23:22:26 +02:00
|
|
|
if config.key_to_id is None:
|
|
|
|
await config.build_key_to_id()
|
|
|
|
config.key_to_id = cast(Dict[str, int], config.key_to_id)
|
2019-01-06 16:22:33 +01:00
|
|
|
element = await element_cache.get_element_full_data(
|
|
|
|
config.get_collection_string(),
|
|
|
|
config.key_to_id["general_system_enable_anonymous"],
|
|
|
|
)
|
|
|
|
return False if element is None else element["value"]
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
|
2018-11-03 23:40:20 +01:00
|
|
|
AnyUser = Union[Model, int, AnonymousUser, None]
|
2017-08-23 20:51:06 +02:00
|
|
|
|
|
|
|
|
2018-11-03 23:40:20 +01:00
|
|
|
def user_to_user_id(user: AnyUser) -> int:
|
2017-01-26 15:34:24 +01:00
|
|
|
"""
|
2018-11-03 23:40:20 +01:00
|
|
|
Takes an object, that represents a user returns its user_id.
|
|
|
|
|
|
|
|
user_id 0 means anonymous user.
|
2017-01-26 15:34:24 +01:00
|
|
|
|
|
|
|
User can be
|
2017-01-26 21:15:35 +01:00
|
|
|
* an user object,
|
|
|
|
* 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:
|
2018-11-03 23:40:20 +01:00
|
|
|
user_id = 0
|
2017-01-26 15:34:24 +01:00
|
|
|
elif isinstance(user, int):
|
2018-11-03 23:40:20 +01:00
|
|
|
# Nothing to do
|
|
|
|
user_id = user
|
2017-01-26 15:34:24 +01:00
|
|
|
elif isinstance(user, AnonymousUser):
|
2018-11-03 23:40:20 +01:00
|
|
|
user_id = 0
|
2017-01-26 21:15:35 +01:00
|
|
|
elif isinstance(user, User):
|
2018-11-03 23:40:20 +01:00
|
|
|
user_id = user.pk
|
2017-01-26 15:34:24 +01:00
|
|
|
else:
|
|
|
|
raise TypeError(
|
2019-01-06 16:22:33 +01:00
|
|
|
"Unsupported type for user. User {} has type {}.".format(user, type(user))
|
|
|
|
)
|
2018-11-03 23:40:20 +01:00
|
|
|
return user_id
|