Просмотр исходного кода

Fix #966: redirect user from guest-only view to homepage if they signed in on it.

Rafał Pitoń 7 лет назад
Родитель
Сommit
f6699ee6f7

+ 4 - 0
misago/users/decorators.py

@@ -1,4 +1,6 @@
+from django.conf import settings
 from django.core.exceptions import PermissionDenied
+from django.shortcuts import redirect
 from django.utils.translation import ugettext as _
 
 from misago.core.exceptions import Banned
@@ -20,6 +22,8 @@ def deny_authenticated(f):
 def deny_guests(f):
     def decorator(request, *args, **kwargs):
         if request.user.is_anonymous:
+            if request.GET.get('ref') == 'login':
+                return redirect(settings.LOGIN_REDIRECT_URL)
             raise PermissionDenied(_("You have to sign in to access this page."))
         else:
             return f(request, *args, **kwargs)

+ 1 - 1
misago/users/tests/test_auth_views.py

@@ -28,7 +28,7 @@ class AuthViewsTests(TestCase):
         )
 
         self.assertEqual(response.status_code, 302)
-        self.assertEqual(response['location'], '/redirect/')
+        self.assertEqual(response['location'], '/redirect/?ref=login')
 
         # invalid redirect (redirects to other site)
         response = self.client.post(

+ 6 - 0
misago/users/tests/test_decorators.py

@@ -32,6 +32,12 @@ class DenyGuestsTests(UserTestCase):
         response = self.client.post(reverse('misago:options'))
         self.assertEqual(response.status_code, 403)
 
+    def test_ref_login(self):
+        """deny_guests decorator redirected guest request to homepage if ref=login"""
+        response = self.client.post('{}?ref=login'.format(reverse('misago:options')))
+        self.assertEqual(response.status_code, 302)
+        self.assertEqual(response['location'], reverse('misago:index'))
+
 
 class DenyBannedIPTests(UserTestCase):
     def test_success(self):

+ 5 - 0
misago/users/views/auth.py

@@ -23,6 +23,11 @@ def login(request):
             )
             if is_redirect_safe:
                 redirect_to_path = urlparse(redirect_to).path
+                if '?' not in redirect_to_path:
+                    redirect_to_path = '{}?'.format(redirect_to_path)
+                else:
+                    redirect_to_path = '{}&'.format(redirect_to_path)
+                redirect_to_path = '{}ref=login'.format(redirect_to_path)
                 try:
                     return redirect(redirect_to_path)
                 except NoReverseMatch: