From adb982114a9798054bdab1511158640e207dd140 Mon Sep 17 00:00:00 2001 From: Col Crunch Date: Wed, 1 Aug 2018 22:38:54 -0400 Subject: [PATCH 1/3] Actually use srp.add_srpfleetmain permission Also adds a new @permissions_required decorator. --- allianceauth/authentication/decorators.py | 31 +++++++++++++++++++ .../srp/templates/srp/management.html | 2 ++ allianceauth/srp/views.py | 2 +- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/allianceauth/authentication/decorators.py b/allianceauth/authentication/decorators.py index 60f938f0..88742fd7 100644 --- a/allianceauth/authentication/decorators.py +++ b/allianceauth/authentication/decorators.py @@ -1,4 +1,6 @@ from django.conf.urls import include +from django.contrib.auth.decorators import user_passes_test +from django.core.exceptions import PermissionDenied from functools import wraps from django.shortcuts import redirect from django.contrib import messages @@ -35,3 +37,32 @@ def main_character_required(view_func): messages.error(request, _('A main character is required to perform that action. Add one below.')) return redirect('authentication:dashboard') return login_required(_wrapped_view) + + +def permissions_required(perm, login_url=None, raise_exception=False): + """ + Decorator for views that checks whether a user has a particular permission + enabled, redirecting to the log-in page if necessary. + If the raise_exception parameter is given the PermissionDenied exception + is raised. + + This decorator is identical to the django permission_required except it + allows for passing a tuple/list of perms that will return true if any one + of them is present. + """ + def check_perms(user): + if isinstance(perm, str): + perms = (perm,) + else: + perms = perm + # First check if the user has the permission (even anon users) + for perm_ in perms: + perm_ = (perm_,) + if user.has_perms(perm_): + return True + # In case the 403 handler should be called raise the exception + if raise_exception: + raise PermissionDenied + # As the last resort, show the login form + return False + return user_passes_test(check_perms, login_url=login_url) diff --git a/allianceauth/srp/templates/srp/management.html b/allianceauth/srp/templates/srp/management.html index 8a402c4b..863e97ac 100644 --- a/allianceauth/srp/templates/srp/management.html +++ b/allianceauth/srp/templates/srp/management.html @@ -17,6 +17,8 @@ {% trans "View All" %} + {% endif %} + {% if perms.srp.add_srpfleetmain or perms.auth.srp_management %} {% trans "Add SRP Fleet" %} diff --git a/allianceauth/srp/views.py b/allianceauth/srp/views.py index 6f400318..f1a704f6 100755 --- a/allianceauth/srp/views.py +++ b/allianceauth/srp/views.py @@ -59,7 +59,7 @@ def srp_fleet_view(request, fleet_id): @login_required -@permission_required('auth.srp_management') +@permissions_required(('auth.srp_management', 'srp.add_srpfleetmain')) def srp_fleet_add_view(request): logger.debug("srp_fleet_add_view called by user %s" % request.user) completed = False From c7860f8e5c326ffac483f42601d5f392dbbd2f66 Mon Sep 17 00:00:00 2001 From: Col Crunch Date: Wed, 1 Aug 2018 22:50:44 -0400 Subject: [PATCH 2/3] oops --- allianceauth/srp/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/allianceauth/srp/views.py b/allianceauth/srp/views.py index f1a704f6..40604d72 100755 --- a/allianceauth/srp/views.py +++ b/allianceauth/srp/views.py @@ -3,13 +3,13 @@ import uuid from django.contrib import messages from django.contrib.auth.decorators import login_required -from django.contrib.auth.decorators import permission_required from django.contrib.humanize.templatetags.humanize import intcomma from django.http import JsonResponse, Http404 from django.shortcuts import render, redirect, get_object_or_404 from django.utils import timezone from django.utils.translation import ugettext_lazy as _ from django.db.models import Sum +from allianceauth.authentication.decorators import permissions_required from allianceauth.eveonline.providers import provider from allianceauth.notifications import notify from .form import SrpFleetMainForm From e49e04034c8fba9e6064ea27a0f471401b39b837 Mon Sep 17 00:00:00 2001 From: Col Crunch Date: Wed, 1 Aug 2018 23:00:31 -0400 Subject: [PATCH 3/3] Imports are hard Note to self: Read before commit --- allianceauth/srp/views.py | 1 + 1 file changed, 1 insertion(+) diff --git a/allianceauth/srp/views.py b/allianceauth/srp/views.py index 40604d72..c66e42f4 100755 --- a/allianceauth/srp/views.py +++ b/allianceauth/srp/views.py @@ -3,6 +3,7 @@ import uuid from django.contrib import messages from django.contrib.auth.decorators import login_required +from django.contrib.auth.decorators import permission_required from django.contrib.humanize.templatetags.humanize import intcomma from django.http import JsonResponse, Http404 from django.shortcuts import render, redirect, get_object_or_404