Repository: wagtail/wagtail

Issue:
Bug - Redirect loop
There seems to be a bug of redirection loop when a user (created without any admin/editor roles) attempts to log in. I was trying out the demo and apparently the admin interface does not cater for users without any roles.

It would be nice to know if there is a workaround this issue. Thanks!



Discussion:
Thanks for spotting! This should be an easy fix, looking into it now.


Resolving patch:
diff --git a/wagtail/wagtailadmin/views/account.py b/wagtail/wagtailadmin/views/account.py
--- a/wagtail/wagtailadmin/views/account.py
+++ b/wagtail/wagtailadmin/views/account.py
@@ -75,7 +75,7 @@ def notification_preferences(request):
 @sensitive_post_parameters()
 @never_cache
 def login(request):
-    if request.user.is_authenticated():
+    if request.user.is_authenticated() and request.user.has_perm('wagtailadmin.access_admin'):
         return redirect('wagtailadmin_home')
     else:
         return auth_login(request,


Test patch:
diff --git a/wagtail/wagtailadmin/tests/test_account_management.py b/wagtail/wagtailadmin/tests/test_account_management.py
--- a/wagtail/wagtailadmin/tests/test_account_management.py
+++ b/wagtail/wagtailadmin/tests/test_account_management.py
@@ -14,16 +14,10 @@ class TestAuthentication(TestCase, WagtailTestUtils):
     """
     This tests that users can login and logout of the admin interface
     """
-    def setUp(self):
-        self.login()
-
     def test_login_view(self):
         """
         This tests that the login view responds with a login page
         """
-        # Logout so we can test the login view
-        self.client.logout()
-
         # Get login page
         response = self.client.get(reverse('wagtailadmin_login'))
 
@@ -36,8 +30,8 @@ def test_login_view_post(self):
         This posts user credentials to the login view and checks that
         the user was logged in successfully
         """
-        # Logout so we can test the login view
-        self.client.logout()
+        # Create user to log in with
+        user = User.objects.create_superuser(username='test', email='test@email.com', password='password')
 
         # Post credentials to the login page
         post_data = {
@@ -59,16 +53,40 @@ def test_already_logged_in_redirect(self):
         redirected to the admin dashboard if they try to access the login
         page
         """
+        # Login
+        self.login()
+
         # Get login page
         response = self.client.get(reverse('wagtailadmin_login'))
 
         # Check that the user was redirected to the dashboard
         self.assertRedirects(response, reverse('wagtailadmin_home'))
 
+    def test_logged_in_as_non_privileged_user_doesnt_redirect(self):
+        """
+        This tests that if the user is logged in but hasn't got permission
+        to access the admin, they are not redirected to the admin
+
+        This tests issue #431
+        """
+        # Login as unprivileged user
+        User.objects.create(username='unprivileged', password='123')
+        self.client.login(username='unprivileged', password='123')
+
+        # Get login page
+        response = self.client.get(reverse('wagtailadmin_login'))
+
+        # Check that the user recieved a login page and was not redirected
+        self.assertEqual(response.status_code, 200)
+        self.assertTemplateUsed(response, 'wagtailadmin/login.html')
+
     def test_logout(self):
         """
         This tests that the user can logout
         """
+        # Login
+        self.login()
+
         # Get logout page
         response = self.client.get(reverse('wagtailadmin_logout'))
 
@@ -83,9 +101,6 @@ def test_not_logged_in_redirect(self):
         This tests that a not logged in user is redirected to the
         login page
         """
-        # Logout
-        self.client.logout()
-
         # Get dashboard
         response = self.client.get(reverse('wagtailadmin_home'))
 
@@ -98,9 +113,6 @@ def test_not_logged_in_redirect_default_settings(self):
         redirects to the correct place when the user has not set
         the LOGIN_URL setting correctly
         """
-        # Logout
-        self.client.logout()
-
         # Get dashboard with default LOGIN_URL setting
         with self.settings(LOGIN_URL='django.contrib.auth.views.login'):
             response = self.client.get(reverse('wagtailadmin_home'))