Commit 9aba056f by Matjaz Gregoric

Support wildcards in unaffected paths.

This commit adds ability to use wildcards denoted by the '*' character
in unaffected paths. The wildcard will match a sequence of zero
or more arbitrary characters.

Example: /*/student_view
parent 9965a53c
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
Splash screen - Middleware Splash screen - Middleware
""" """
import logging import logging
import re
from django.shortcuts import redirect from django.shortcuts import redirect
...@@ -28,8 +29,10 @@ class SplashMiddleware(object): ...@@ -28,8 +29,10 @@ class SplashMiddleware(object):
return return
# Some URLs should never be redirected # Some URLs should never be redirected
if request.path_info in config.unaffected_url_paths_list: path_info = request.path_info
return for unaffected_path in config.unaffected_url_paths_list:
if self.path_matches(path_info, unaffected_path):
return
# Some users should never be redirected # Some users should never be redirected
if request.user.username in config.unaffected_usernames_list: if request.user.username in config.unaffected_usernames_list:
...@@ -39,3 +42,21 @@ class SplashMiddleware(object): ...@@ -39,3 +42,21 @@ class SplashMiddleware(object):
if (cookie_value not in config.cookie_allowed_values_list and if (cookie_value not in config.cookie_allowed_values_list and
request.build_absolute_uri() != config.redirect_url): request.build_absolute_uri() != config.redirect_url):
return redirect(config.redirect_url) return redirect(config.redirect_url)
def path_matches(self, path, pattern):
"""
Determine whether `path` matches the `pattern`.
`pattern` may include wildcards (*) which represent a sequence of
zero or more arbitrary characters.
"""
matches = False
if path == pattern:
matches = True
elif '*' in pattern:
pattern = re.escape(pattern).replace('\\*', '.*')
if re.match(pattern + '$', path):
matches = True
return matches
...@@ -26,7 +26,8 @@ class SplashConfig(ConfigurationModel): ...@@ -26,7 +26,8 @@ class SplashConfig(ConfigurationModel):
unaffected_url_paths = models.TextField( unaffected_url_paths = models.TextField(
default='', default='',
blank=True, blank=True,
help_text="Comma-separated list of URL paths (not including the hostname) which should not be redirected" help_text="Comma-separated list of URL paths (not including the hostname) which should not be redirected. "
"Paths may include wildcards denoted by * (example: /*/student_view)"
) )
redirect_url = models.URLField( redirect_url = models.URLField(
default='http://edx.org', default='http://edx.org',
......
...@@ -188,3 +188,43 @@ class SplashMiddlewareTestCase(TestCase): ...@@ -188,3 +188,43 @@ class SplashMiddlewareTestCase(TestCase):
request = self.build_request(url_path='/my/url/') request = self.build_request(url_path='/my/url/')
response = self.splash_middleware.process_request(request) response = self.splash_middleware.process_request(request)
self.assertEquals(response, None) self.assertEquals(response, None)
def test_unaffected_wildcard_path(self):
"""
Unaffected wildcard paths should never be redirected - custom value
"""
SplashConfig(
enabled=True,
unaffected_url_paths='/test1/*, /test2/*/after, /test3/*/before/*/after',
).save()
# These paths match and should NOT redirect.
request = self.build_request(url_path='/test1/')
response = self.splash_middleware.process_request(request)
self.assertEquals(response, None)
request = self.build_request(url_path='/test1/something')
response = self.splash_middleware.process_request(request)
self.assertEquals(response, None)
request = self.build_request(url_path='/test1/something/else')
response = self.splash_middleware.process_request(request)
self.assertEquals(response, None)
request = self.build_request(url_path='/test2/something/after')
response = self.splash_middleware.process_request(request)
self.assertEquals(response, None)
request = self.build_request(url_path='/test3/something/before/something/else/after')
response = self.splash_middleware.process_request(request)
self.assertEquals(response, None)
# These paths don't match and should redirect.
request = self.build_request(url_path='/test2/')
response = self.splash_middleware.process_request(request)
self.assert_redirect(response, 'http://edx.org')
request = self.build_request(url_path='/test2/after')
response = self.splash_middleware.process_request(request)
self.assert_redirect(response, 'http://edx.org')
request = self.build_request(url_path='/test3/before/after')
response = self.splash_middleware.process_request(request)
self.assert_redirect(response, 'http://edx.org')
request = self.build_request(url_path='/test3/something/before/something/after/more')
response = self.splash_middleware.process_request(request)
self.assert_redirect(response, 'http://edx.org')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment