diff --git a/accessibility/cbv_decorators.py b/accessibility/cbv_decorators.py new file mode 100644 index 000000000..5499cadd8 --- /dev/null +++ b/accessibility/cbv_decorators.py @@ -0,0 +1,55 @@ +""" +employee/decorators.py +""" + +from django.http import HttpResponse +from django.contrib import messages +from django.utils.translation import gettext_lazy as _ +from django.shortcuts import redirect +from accessibility.methods import check_is_accessible +from base.decorators import decorator_with_arguments +from horilla.horilla_middlewares import _thread_locals + +@decorator_with_arguments +def enter_if_accessible(function, feature, perm=None, method=None): + """ + accessible check decorator for cbv + """ + + + def check_accessible(self, *args, **kwargs): + """ + Check accessible + """ + path = "/" + request = getattr(_thread_locals,"request") + if not getattr(self,"request",None): + self.request = request + referrer = request.META.get("HTTP_REFERER") + if referrer and request.path not in referrer: + path = request.META["HTTP_REFERER"] + accessible = False + cache_key = request.session.session_key + "accessibility_filter" + employee = getattr(request.user, "employee_get") + if employee: + accessible = check_is_accessible(feature, cache_key, employee) + has_perm = True + if perm: + has_perm = request.user.has_perm(perm) + + if accessible or has_perm or method(request): + return function(self, *args, **kwargs) + key = "HTTP_HX_REQUEST" + keys = request.META.keys() + messages.info(request, _("You dont have access to the feature")) + if key in keys: + return HttpResponse( + f""" + + """ + ) + return redirect(path) + + return check_accessible diff --git a/accessibility/methods.py b/accessibility/methods.py index ef8c11fd5..b72c666a7 100644 --- a/accessibility/methods.py +++ b/accessibility/methods.py @@ -18,6 +18,8 @@ def check_is_accessible(feature, cache_key, employee): accessibility = DefaultAccessibility.objects.filter(feature=feature).first() + if accessibility and accessibility.exclude_all: + return False if not feature or not accessibility: return True diff --git a/accessibility/models.py b/accessibility/models.py index 69a674bfa..10fcee153 100644 --- a/accessibility/models.py +++ b/accessibility/models.py @@ -15,3 +15,4 @@ class DefaultAccessibility(HorillaModel): feature = models.CharField(max_length=100, choices=ACCESSBILITY_FEATURE) filter = models.JSONField() + exclude_all = models.BooleanField(default=False) diff --git a/accessibility/templates/accessibility/accessibility.html b/accessibility/templates/accessibility/accessibility.html index 7c2950fb8..7e7254cf6 100644 --- a/accessibility/templates/accessibility/accessibility.html +++ b/accessibility/templates/accessibility/accessibility.html @@ -75,9 +75,44 @@
+
+
+ +
+
+
+ +
+
+
+
+
+ {% trans "Only those normal users/employees with any category mentioned in the form can access the" %} `{{display}}` {% trans "feature" %} +
+ {% trans "If skip all the category fields in the form, then all normal users/employees can access the feature" %} +
{{accessibility_filter.form.structured}} +