Grant srp.add_srpfleetmain access to create SRP request

See merge request allianceauth/allianceauth!1098
This commit is contained in:
Basraah 2018-08-05 02:24:27 +00:00
commit d429c8b59a
3 changed files with 35 additions and 1 deletions

View File

@ -1,4 +1,6 @@
from django.conf.urls import include 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 functools import wraps
from django.shortcuts import redirect from django.shortcuts import redirect
from django.contrib import messages 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.')) messages.error(request, _('A main character is required to perform that action. Add one below.'))
return redirect('authentication:dashboard') return redirect('authentication:dashboard')
return login_required(_wrapped_view) 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)

View File

@ -17,6 +17,8 @@
<a href="{% url 'srp:all' %}" class="btn btn-primary"> <a href="{% url 'srp:all' %}" class="btn btn-primary">
{% trans "View All" %} {% trans "View All" %}
</a> </a>
{% endif %}
{% if perms.srp.add_srpfleetmain or perms.auth.srp_management %}
<a href="{% url 'srp:add' %}" class="btn btn-success"> <a href="{% url 'srp:add' %}" class="btn btn-success">
{% trans "Add SRP Fleet" %} {% trans "Add SRP Fleet" %}
</a> </a>

View File

@ -10,6 +10,7 @@ from django.shortcuts import render, redirect, get_object_or_404
from django.utils import timezone from django.utils import timezone
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.db.models import Sum from django.db.models import Sum
from allianceauth.authentication.decorators import permissions_required
from allianceauth.eveonline.providers import provider from allianceauth.eveonline.providers import provider
from allianceauth.notifications import notify from allianceauth.notifications import notify
from .form import SrpFleetMainForm from .form import SrpFleetMainForm
@ -59,7 +60,7 @@ def srp_fleet_view(request, fleet_id):
@login_required @login_required
@permission_required('auth.srp_management') @permissions_required(('auth.srp_management', 'srp.add_srpfleetmain'))
def srp_fleet_add_view(request): def srp_fleet_add_view(request):
logger.debug("srp_fleet_add_view called by user %s" % request.user) logger.debug("srp_fleet_add_view called by user %s" % request.user)
completed = False completed = False