Group access constraints

* adds new decorators for all three important groups
* reorganize some default group settings for setup_data.py
This commit is contained in:
mipel 2021-07-09 08:18:37 +02:00
parent 50d98b2a83
commit 39c1d9eb12
4 changed files with 68 additions and 8 deletions

View File

@ -13,6 +13,8 @@ from django.shortcuts import redirect
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from konova.settings import DEFAULT_GROUP, ETS_GROUP, ZB_GROUP
def staff_required(function):
"""
@ -42,3 +44,63 @@ def superuser_required(function):
messages.info(request, _("You need to be administrator to perform this action!"))
return redirect(request.META.get("HTTP_REFERER", reverse("home")))
return wrap
def default_group_required(function):
"""
A decorator for functions which shall only be usable for users of specific groups.
Group identifiers can be found in konova/settings.py
"""
@wraps(function)
def wrap(request, *args, **kwargs):
user = request.user
has_group = user.groups.filter(
name=DEFAULT_GROUP
).exists()
if has_group:
return function(request, *args, **kwargs)
else:
messages.info(request, _("You need to be part of another user group."))
return redirect(request.META.get("HTTP_REFERER", reverse("home")))
return wrap
def registration_office_group_required(function):
"""
A decorator for functions which shall only be usable for users of specific groups.
Group identifiers can be found in konova/settings.py
"""
@wraps(function)
def wrap(request, *args, **kwargs):
user = request.user
has_group = user.groups.filter(
name=ZB_GROUP
).exists()
if has_group:
return function(request, *args, **kwargs)
else:
messages.info(request, _("You need to be part of another user group."))
return redirect(request.META.get("HTTP_REFERER", reverse("home")))
return wrap
def conservation_office_group_required(function):
"""
A decorator for functions which shall only be usable for users of specific groups.
Group identifiers can be found in konova/settings.py
"""
@wraps(function)
def wrap(request, *args, **kwargs):
user = request.user
has_group = user.groups.filter(
name=ETS_GROUP
).exists()
if has_group:
return function(request, *args, **kwargs)
else:
messages.info(request, _("You need to be part of another user group."))
return redirect(request.META.get("HTTP_REFERER", reverse("home")))
return wrap

View File

@ -27,13 +27,13 @@ TEST_ORGANISATION_DATA = [
GROUPS_DATA = [
{
"name": DEFAULT_GROUP,
"name": _(DEFAULT_GROUP),
},
{
"name": ZB_GROUP,
"name": _(ZB_GROUP),
},
{
"name": ETS_GROUP,
"name": _(ETS_GROUP),
},
]

View File

@ -11,8 +11,6 @@ from django.contrib.auth.models import User
from django.contrib.gis.db.models import MultiPolygonField
from django.db import models
from konova.enums import ServerMessageImportance
class BaseResource(models.Model):
"""

View File

@ -52,9 +52,9 @@ DEFAULT_LON = 7.00
DEFAULT_ZOOM = 8.0
# GROUPS
DEFAULT_GROUP = _("Default")
ZB_GROUP = _("Registration office")
ETS_GROUP = _("Conservation office")
DEFAULT_GROUP = "Default"
ZB_GROUP = "Registration office"
ETS_GROUP = "Conservation office"
# ServerMessageImportance bootstrap resolver
SVI_BOOTSTRAP_CLS_MAP = {