From 19442f8166be0564fd3012ce2f073b6b32a9c24d Mon Sep 17 00:00:00 2001 From: Alexander van Ratingen <470642+alvra@users.noreply.github.com> Date: Sat, 12 Oct 2019 00:22:59 +0200 Subject: [PATCH] Allow passing a function itself to the setting ROSETTA_ACCESS_CONTROL_FUNCTION. --- rosetta/access.py | 17 +++++++++++------ rosetta/tests/tests.py | 9 ++++++++- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/rosetta/access.py b/rosetta/access.py index f7f21be..699617c 100644 --- a/rosetta/access.py +++ b/rosetta/access.py @@ -15,13 +15,18 @@ def get_access_control_function(): Return a predicate for determining if a user can access the Rosetta views """ - fn_path = getattr(settings, 'ROSETTA_ACCESS_CONTROL_FUNCTION', None) - if fn_path is None: + access_function = getattr(settings, 'ROSETTA_ACCESS_CONTROL_FUNCTION', None) + if access_function is None: return is_superuser_staff_or_in_translators_group - # Dynamically load a permissions function - perm_module, perm_func = fn_path.rsplit('.', 1) - perm_module = importlib.import_module(perm_module) - return getattr(perm_module, perm_func) + elif isinstance(access_function, str): + # Dynamically load a permissions function + perm_module, perm_func = access_function.rsplit('.', 1) + perm_module = importlib.import_module(perm_module) + return getattr(perm_module, perm_func) + elif callable(access_function): + return access_function + else: + raise TypeError(access_function) # Default access control test diff --git a/rosetta/tests/tests.py b/rosetta/tests/tests.py index 258b8c1..d3c28ab 100644 --- a/rosetta/tests/tests.py +++ b/rosetta/tests/tests.py @@ -543,11 +543,18 @@ class RosettaTestCase(TestCase): response = self.client.get(self.project_file_list_url) self.assertEqual(200, response.status_code) - # Now replace access control, and check we get redirected + # Now replace access control with a function reference, + # and check we get redirected with self.settings(ROSETTA_ACCESS_CONTROL_FUNCTION='rosetta.tests.no_access'): response = self.client.get(self.project_file_list_url) self.assertEqual(302, response.status_code) + # Now replace access control with a function itself, + # and check we get redirected + with self.settings(ROSETTA_ACCESS_CONTROL_FUNCTION=lambda user: False): + response = self.client.get(self.project_file_list_url) + self.assertEqual(302, response.status_code) + def test_26_urlconf_accept_dots_and_underscores(self): resolver_match = resolve('/rosetta/files/all/fr_FR.utf8/0/') self.assertEqual(resolver_match.url_name, 'rosetta-form')