← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~huwshimi/maas/login-redirect-1034892 into lp:maas

 

Huw Wilkins has proposed merging lp:~huwshimi/maas/login-redirect-1034892 into lp:maas.

Commit message:
Visiting the login page while authenticated now redirects to the index.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)
Related bugs:
  Bug #1034892 in MAAS: "Login page is unhelpful if already authenticated"
  https://bugs.launchpad.net/maas/+bug/1034892

For more details, see:
https://code.launchpad.net/~huwshimi/maas/login-redirect-1034892/+merge/132120

The login view now checks if the user is authenticated, and if so redirects to the index url. This means we no longer serve the login form to a user who is already logged in.
-- 
https://code.launchpad.net/~huwshimi/maas/login-redirect-1034892/+merge/132120
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~huwshimi/maas/login-redirect-1034892 into lp:maas.
=== modified file 'src/maasserver/tests/test_views_account.py'
--- src/maasserver/tests/test_views_account.py	2012-06-25 13:59:23 +0000
+++ src/maasserver/tests/test_views_account.py	2012-10-30 14:40:39 +0000
@@ -34,3 +34,12 @@
         response = self.client.get('/accounts/login/')
         self.assertTrue(response.context['no_users'])
         self.assertEqual(path, response.context['create_command'])
+    
+    def test_login_redirects_when_authenticated(self):
+        name = factory.getRandomString()
+        password = factory.getRandomString()
+        factory.make_user(name, password)
+        self.client.login(username=name, password=password)
+        response = self.client.get('/accounts/login/', follow=True)
+        self.assertEqual(302, response.redirect_chain[0][1])
+        self.assertEqual('http://testserver/', response.redirect_chain[0][0])

=== modified file 'src/maasserver/views/account.py'
--- src/maasserver/views/account.py	2012-06-25 13:59:23 +0000
+++ src/maasserver/views/account.py	2012-10-30 14:40:39 +0000
@@ -22,6 +22,7 @@
     logout as dj_logout,
     )
 from django.core.urlresolvers import reverse
+from django.http import HttpResponseRedirect
 from maasserver.models import UserProfile
 
 
@@ -30,7 +31,10 @@
         'no_users': UserProfile.objects.all_users().count() == 0,
         'create_command': django_settings.MAAS_CLI,
         }
-    return dj_login(request, extra_context=extra_context)
+    if request.user.is_authenticated():
+        return HttpResponseRedirect(reverse('index'))
+    else:
+        return dj_login(request, extra_context=extra_context)
 
 
 def logout(request):