separate authorisation function from decorator

This commit is contained in:
Stefano Brentegani 2014-07-29 20:55:25 +02:00
parent aea940cb3f
commit 581e43a3b0
2 changed files with 6 additions and 11 deletions

View File

@ -4,14 +4,12 @@ from helpdesk import settings as helpdesk_settings
if helpdesk_settings.HELPDESK_CUSTOM_STAFF_FILTER_CALLBACK:
# apply a custom user validation condition
helpdesk_staff_member_required = user_passes_test(helpdesk_settings.HELPDESK_CUSTOM_STAFF_FILTER_CALLBACK)
is_helpdesk_staff = helpdesk_settings.HELPDESK_CUSTOM_STAFF_FILTER_CALLBACK
elif helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE:
# treat 'normal' users like 'staff'
helpdesk_staff_member_required = user_passes_test(lambda u: u.is_authenticated() and u.is_active)
is_helpdesk_staff = lambda u: u.is_authenticated() and u.is_active
else:
try:
from django.contrib.admin.views.decorators import staff_member_required as helpdesk_staff_member_required
except ImportError:
helpdesk_staff_member_required = user_passes_test(lambda u: u.is_authenticated() and u.is_active and u.is_staff)
is_helpdesk_staff = lambda u: u.is_authenticated() and u.is_active and u.is_staff
helpdesk_staff_member_required = user_passes_test(is_helpdesk_staff)
helpdesk_superuser_required = user_passes_test(lambda u: u.is_authenticated() and u.is_active and u.is_superuser)

View File

@ -39,7 +39,6 @@ class KBDisabledTestCase(TestCase):
class StaffUserTestCaseMixin(object):
HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE = False
HELPDESK_CUSTOM_STAFF_FILTER_CALLBACK = None
expected_login_template = 'admin/login.html'
def setUp(self):
self.old_settings = settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE, settings.HELPDESK_CUSTOM_STAFF_FILTER_CALLBACK
@ -62,13 +61,12 @@ class StaffUserTestCaseMixin(object):
def test_anonymous_user(self):
"""Access to the dashboard always requires a login"""
response = self.client.get(reverse('helpdesk_dashboard'), follow=True)
self.assertTemplateUsed(response, self.expected_login_template)
self.assertTemplateUsed(response, 'helpdesk/registration/login.html')
class NonStaffUsersAllowedTestCase(StaffUserTestCaseMixin, TestCase):
HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE = True
HELPDESK_CUSTOM_STAFF_FILTER_CALLBACK = None
expected_login_template = 'helpdesk/registration/login.html'
def test_non_staff_allowed(self):
"""If HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE is True,
@ -100,7 +98,6 @@ class StaffUsersOnlyTestCase(StaffUserTestCaseMixin, TestCase):
class CustomStaffUserTestCase(StaffUserTestCaseMixin, TestCase):
HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE = False
expected_login_template = 'helpdesk/registration/login.html'
@staticmethod
def HELPDESK_CUSTOM_STAFF_FILTER_CALLBACK(user):
@ -122,4 +119,4 @@ class CustomStaffUserTestCase(StaffUserTestCaseMixin, TestCase):
self.client.login(username=user.username, password='frog')
response = self.client.get(reverse('helpdesk_dashboard'), follow=True)
self.assertTemplateUsed(response, self.expected_login_template)
self.assertTemplateUsed(response, 'helpdesk/registration/login.html')