Add site configuration flag to allow changing redirection after login
This commit is contained in:
@@ -235,8 +235,8 @@ def get_next_url_for_login_page(request):
|
||||
/account/finish_auth/ view following login, which will take care of auto-enrollment in
|
||||
the specified course.
|
||||
|
||||
Otherwise, we go to the `next` param or to the dashboard if nothing else is
|
||||
specified.
|
||||
Otherwise, we go to the ?next= query param or the configured custom
|
||||
redirection url (the default behaviour is to go to /dashboard).
|
||||
|
||||
If THIRD_PARTY_AUTH_HINT is set, then `tpa_hint=<hint>` is added as a query parameter.
|
||||
|
||||
@@ -250,9 +250,25 @@ def get_next_url_for_login_page(request):
|
||||
request_is_https=request.is_secure(),
|
||||
)
|
||||
if not redirect_to:
|
||||
try:
|
||||
redirect_to = reverse('dashboard')
|
||||
except NoReverseMatch:
|
||||
if settings.ROOT_URLCONF == 'lms.urls':
|
||||
login_redirect_url = configuration_helpers.get_value('DEFAULT_REDIRECT_AFTER_LOGIN')
|
||||
|
||||
if login_redirect_url:
|
||||
try:
|
||||
redirect_to = reverse(login_redirect_url)
|
||||
except NoReverseMatch:
|
||||
log.warning(
|
||||
u'Default redirect after login doesn\'t exist: %(login_redirect_url)r. '
|
||||
u'Check the value set on DEFAULT_REDIRECT_AFTER_LOGIN configuration variable.',
|
||||
{"login_redirect_url": login_redirect_url}
|
||||
)
|
||||
|
||||
# If redirect url isn't set, reverse to dashboard
|
||||
if not redirect_to:
|
||||
# Tries reversing the LMS dashboard if the url doesn't exist
|
||||
redirect_to = reverse('dashboard')
|
||||
|
||||
elif settings.ROOT_URLCONF == 'cms.urls':
|
||||
redirect_to = reverse('home')
|
||||
|
||||
if any(param in request_params for param in POST_AUTH_PARAMS):
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
|
||||
import logging
|
||||
import unittest
|
||||
|
||||
import ddt
|
||||
from django.conf import settings
|
||||
@@ -133,3 +134,23 @@ class TestLoginHelper(TestCase):
|
||||
|
||||
with with_site_configuration_context(configuration=dict(THIRD_PARTY_AUTH_HINT=tpa_hint)):
|
||||
validate_login()
|
||||
|
||||
@unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms')
|
||||
@ddt.data(
|
||||
(None, '/dashboard'),
|
||||
('invalid-url', '/dashboard'),
|
||||
('courses', '/courses'),
|
||||
)
|
||||
@ddt.unpack
|
||||
def test_custom_redirect_url(self, redirect, expected_url):
|
||||
"""
|
||||
Test custom redirect after login
|
||||
"""
|
||||
configuration_values = {"DEFAULT_REDIRECT_AFTER_LOGIN": redirect}
|
||||
req = self.request.get(settings.LOGIN_URL)
|
||||
req.META["HTTP_ACCEPT"] = "text/html"
|
||||
|
||||
with with_site_configuration_context(configuration=configuration_values):
|
||||
next_page = get_next_url_for_login_page(req)
|
||||
|
||||
self.assertEqual(next_page, expected_url)
|
||||
|
||||
Reference in New Issue
Block a user