Dead simple user list.
[zzz-spline-users.git] / spline / plugins / users / controllers / accounts.py
index ba2fa26..9add733 100644 (file)
@@ -2,6 +2,7 @@ import logging
 from openid.consumer.consumer import Consumer
 from openid.extensions.sreg import SRegRequest, SRegResponse
 from openid.store.filestore import FileOpenIDStore
+from openid.yadis.discover import DiscoveryFailure
 from sqlalchemy.orm.exc import NoResultFound
 
 from pylons import config, request, response, session, tmpl_context as c, url
@@ -19,17 +20,36 @@ class AccountsController(BaseController):
 
     openid_store = FileOpenIDStore('/var/tmp')
 
-    def index(self):
-        # Return a rendered template
-        #   return render('/template.mako')
-        # or, Return a response
-        return str(request.headers) + request.environ.get('scheme', '')
+    def _bail(self, reason):
+        # Used for bailing on a login attempt; reshows the login page
+        c.error = reason
+        c.attempted_openid = request.params.get('openid_identifier', '')
+        return render('/users/login.mako')
+
+
+    def login(self):
+        c.error = None
+        c.attempted_openid = None
+        return render('/users/login.mako')
 
     def login_begin(self):
         """Step one of logging in with OpenID; we redirect to the provider"""
 
         cons = Consumer(session=session, store=self.openid_store)
-        auth_request = cons.begin(request.params['openid'])
+
+        try:
+            openid_url = request.params['openid_identifier']
+        except KeyError:
+            return self._bail("Gotta enter an OpenID to log in.")
+
+        try:
+            auth_request = cons.begin(openid_url)
+        except DiscoveryFailure:
+            return self._bail(
+                "Can't connect to '{0}'.  You sure it's an OpenID?"
+                .format(openid_url)
+            )
+
         sreg_req = SRegRequest(optional=['nickname', 'email', 'dob', 'gender',
                                          'country', 'language', 'timezone'])
         auth_request.addExtension(sreg_req)
@@ -69,8 +89,10 @@ class AccountsController(BaseController):
             # Create db records
             user = model.User(name=username)
             meta.Session.add(user)
+
             openid = model.OpenID(openid=res.identity_url)
             user.openids.append(openid)
+
             meta.Session.commit()
 
         # Remember who's logged in, and we're good to go