19f50baae4
* adds group checker decorator for adding a message informing about missing group privileges
125 lines
3.9 KiB
Python
125 lines
3.9 KiB
Python
"""
|
|
Author: Michel Peltriaux
|
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
|
Created on: 16.11.20
|
|
|
|
"""
|
|
|
|
from functools import wraps
|
|
|
|
from django.contrib import messages
|
|
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):
|
|
"""
|
|
A decorator for functions which shall only be usable for staff members of the system
|
|
"""
|
|
@wraps(function)
|
|
def wrap(request, *args, **kwargs):
|
|
user = request.user
|
|
if user.is_staff:
|
|
return function(request, *args, **kwargs)
|
|
else:
|
|
messages.info(request, _("You need to be staff to perform this action!"))
|
|
return redirect(request.META.get("HTTP_REFERER", reverse("home")))
|
|
return wrap
|
|
|
|
|
|
def superuser_required(function):
|
|
"""
|
|
A decorator for functions which shall only be usable for superusers of the system
|
|
"""
|
|
@wraps(function)
|
|
def wrap(request, *args, **kwargs):
|
|
user = request.user
|
|
if user.is_superuser:
|
|
return function(request, *args, **kwargs)
|
|
else:
|
|
messages.info(request, _("You need to be administrator to perform this action!"))
|
|
return redirect(request.META.get("HTTP_REFERER", reverse("home")))
|
|
return wrap
|
|
|
|
|
|
def any_group_check(function):
|
|
"""
|
|
Checks for any group membership. Adds a message in case of having none.
|
|
|
|
"""
|
|
@wraps(function)
|
|
def wrap(request, *args, **kwargs):
|
|
user = request.user
|
|
# Inform user about missing group privileges!
|
|
groups = user.groups.all()
|
|
if not groups:
|
|
messages.info(
|
|
request,
|
|
_("+++ Attention: You are not part of any group. You won't be able to create, edit or do anything. Please contact an administrator. +++")
|
|
)
|
|
return function(request, *args, **kwargs)
|
|
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 |