Repository: wagtail/wagtail

Issue:
Login screen should redirect somewhere appropriate if visited while already logged in
Rather confusing, visiting the login screen while already logged in presents the user with a login form again. It should redirect them to the dashboard, I'd suggest.



Discussion:
Fixed by #38

Reopening as #38 was reverted by #42 

@kaedroho what's the status of this? 

@kaedroho ping!


Resolving patch:
diff --git a/runtests.py b/runtests.py
--- a/runtests.py
+++ b/runtests.py
@@ -103,6 +103,7 @@
         WAGTAILSEARCH_BACKENDS=WAGTAILSEARCH_BACKENDS,
         WAGTAIL_SITE_NAME='Test Site',
         LOGIN_REDIRECT_URL='wagtailadmin_home',
+        LOGIN_URL='wagtailadmin_login',
     )
 
 
diff --git a/wagtail/wagtailadmin/urls.py b/wagtail/wagtailadmin/urls.py
--- a/wagtail/wagtailadmin/urls.py
+++ b/wagtail/wagtailadmin/urls.py
@@ -5,15 +5,8 @@
 from wagtail.wagtailadmin.views import account, chooser, home, pages, tags, userbar
 from wagtail.wagtailadmin import hooks
 
-urlpatterns = [
-    url(
-        r'^login/$', 'django.contrib.auth.views.login', {
-            'template_name': 'wagtailadmin/login.html',
-            'authentication_form': LoginForm,
-            'extra_context': {'show_password_reset': getattr(settings, 'WAGTAIL_PASSWORD_MANAGEMENT_ENABLED', True)},
-        }, name='wagtailadmin_login'
-    ),
 
+urlpatterns = [
     # Password reset
     url(
         r'^password_reset/$', 'django.contrib.auth.views.password_reset', {
@@ -81,6 +74,7 @@
 
     url(r'^tag-autocomplete/$', tags.autocomplete, name='wagtailadmin_tag_autocomplete'),
 
+    url(r'^login/$', account.login, name='wagtailadmin_login'),
     url(r'^account/$', account.account, name='wagtailadmin_account'),
     url(r'^account/change_password/$', account.change_password, name='wagtailadmin_account_change_password'),
     url(r'^logout/$', account.logout, name='wagtailadmin_logout'),
@@ -90,6 +84,13 @@
 ]
 
 
+# This is here to make sure that 'django.contrib.auth.views.login' is reversed correctly
+# It must be placed after 'wagtailadmin_login' to prevent this from being used
+urlpatterns += [
+    url(r'^login/$', 'django.contrib.auth.views.login'),
+]
+
+
 # Import additional urlpatterns from any apps that define a register_admin_urls hook
 for fn in hooks.get_hooks('register_admin_urls'):
     urls = fn()
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
@@ -3,8 +3,13 @@
 from django.contrib import messages
 from django.contrib.auth.forms import SetPasswordForm
 from django.contrib.auth.decorators import permission_required
-from django.contrib.auth.views import logout as auth_logout
+from django.contrib.auth.views import logout as auth_logout, login as auth_login
 from django.utils.translation import ugettext as _ 
+from django.views.decorators.debug import sensitive_post_parameters
+from django.views.decorators.cache import never_cache
+
+from wagtail.wagtailadmin import forms
+
 
 @permission_required('wagtailadmin.access_admin')
 def account(request):
@@ -37,6 +42,21 @@ def change_password(request):
     })
 
 
+@sensitive_post_parameters()
+@never_cache
+def login(request):
+    if request.user.is_authenticated():
+        return redirect('wagtailadmin_home')
+    else:
+        return auth_login(request,
+            template_name='wagtailadmin/login.html',
+            authentication_form=forms.LoginForm,
+            extra_context={
+                'show_password_reset': getattr(settings, 'WAGTAIL_PASSWORD_MANAGEMENT_ENABLED', True),
+            },
+        )
+
+
 def logout(request):
     response = auth_logout(request, next_page = 'wagtailadmin_login')
 


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
@@ -50,7 +50,6 @@ def test_login_view_post(self):
         self.assertTrue('_auth_user_id' in self.client.session)
         self.assertEqual(self.client.session['_auth_user_id'], User.objects.get(username='test').id)
 
-    @unittest.expectedFailure # See: https://github.com/torchbox/wagtail/issues/25
     def test_already_logged_in_redirect(self):
         """
         This tests that a user who is already logged in is automatically
@@ -68,7 +67,7 @@ def test_logout(self):
         """
         This tests that the user can logout
         """
-        # Get logout page page
+        # Get logout page
         response = self.client.get(reverse('wagtailadmin_logout'))
 
         # Check that the user was redirected to the login page
@@ -78,6 +77,40 @@ def test_logout(self):
         # Check that the user was logged out
         self.assertFalse('_auth_user_id' in self.client.session)
 
+    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'))
+
+        # Check that the user was redirected to the login page and that next was set correctly
+        self.assertEqual(response.status_code, 302)
+        self.assertURLEqual(response.url, reverse('wagtailadmin_login') + '?next=' + reverse('wagtailadmin_home'))
+
+    def test_not_logged_in_redirect_default_settings(self):
+        """
+        This does the same as the above test but checks that it
+        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'))
+
+        # Check that the user was redirected to the login page and that next was set correctly
+        # Note: The user will be redirected to 'django.contrib.auth.views.login' but
+        # this must be the same URL as 'wagtailadmin_login'
+        self.assertEqual(response.status_code, 302)
+        self.assertURLEqual(response.url, reverse('wagtailadmin_login') + '?next=' + reverse('wagtailadmin_home'))
+
 
 class TestAccountSection(TestCase, WagtailTestUtils):
     """