Commits

Anonymous committed 3ef2f93

Added basic auth for admin

Comments (0)

Files changed (7)

skypieamc/config/routing.py

 
     # CUSTOM ROUTES HERE
 
+    map.connect('login', '/login', controller='account', action='login')
+    map.connect('logout', '/logout', controller='account', action='logout')
     map.connect('/{controller}/{action}')
     map.connect('/{controller}/{action}/{id}')
     map.connect('root', '/', controller='file', action='list');

skypieamc/controllers/account.py

 
 from pylons import request, response, session, tmpl_context as c
 from pylons.controllers.util import abort, redirect_to
+from pylons.decorators import validate
+from pylons.decorators.rest import restrict
 
+from formencode import Schema
+from formencode.validators import String
 from sqlalchemy import and_, delete, desc
 from webhelpers import paginate
 
 
 log = logging.getLogger(__name__)
 
+class LoginForm(Schema):
+
+    allow_extra_fields = True
+    filter_extra_fields = True
+
+    username = String(
+        not_empty=True,
+        messages={'empty': u'Please enter your username.'}
+    )
+    password = String(
+        not_empty=True,
+        messages={'empty': u'Please enter your password.'}
+    )
+
 class AccountController(BaseController):
 
+    def __before__(self):
+        username = session.get('user')
+        if username:
+            request.environ['REMOTE_USER'] = username
+
+    def login(self):
+        """
+        Render login form
+        """
+        return render('/derived/account/login.mako')
+        
+
+    @restrict('POST')
+    @validate(schema=LoginForm(), form='login')
+    def do_login(self):
+        """
+        Authenticate user
+        """
+        username = self.form_result['username']
+        password = self.form_result['password']
+        if username == 'admin' and \
+           password == 'openthedoor, sesame!':
+            request.environ['REMOTE_USER'] = username
+            session['user'] = username
+            referer = session.pop('referer', None)
+            session.save()
+            redirect_to(referer or 'root')
+        else:
+            session['flash_msg'] = u'Login failed!'
+            session.save()
+            redirect_to('login')
+
+    def logout(self):
+        """
+        Log out an user
+        """
+        request.environ['REMOTE_USER'] = ''
+        session.pop('user', None)
+        session['flash_msg'] = u'You have been logged out.'
+        session.save()
+        redirect_to('root')
+
     def list(self, id=None):
+        if not request.environ.get('REMOTE_USER'):
+            if request.query_string:
+                session['referer'] = \
+                    '{0}?{1]'.format(request.path_info, request.query_string)
+            else:
+                session['referer'] = request.path_info
+            session.save()
+            redirect_to('login')
+
         files = meta.Session.query(model.File).\
             order_by(desc(model.File.uploaded))
         c.paginator = paginate.Page(

skypieamc/public/css/account.css

+form dt {
+  width: 85px;
+}

skypieamc/public/css/main.css

   margin-bottom: 10px;
 }
 
+form dt {
+  float: left;
+  color: #7A2433;
+}
+
+form label {
+  font-size: 108%;
+  font-weight: bold;
+}
+
 /** footer **/
 
 #ft {

skypieamc/public/css/upload.css

 #file-uploader dt {
-  float: left;
   width: 45px;
-  color: #7A2433;
 }
 
 #file-uploader #tags {
 }
 
 #file-uploader label {
-  font-size: 108%;
-  font-weight: bold;
   position: relative;
   top: 2px;
 }

skypieamc/templates/base.mako

       <h1>${h.link_to('SkypieaMC', h.url_for('root'))}</h1>
     </div>
     <div class="yui-u">
-      <%doc>
       <div id="login">
-        ${h.link_to('Login', h.url_for(''))}
-        | ${h.link_to('Sign up', h.url_for(''))}
+        <p>
+          %if session.has_key('user'):
+            Hi, ${h.link_to(session['user'],
+                            h.url_for(controller='account', action='list'))}
+            | ${h.link_to('Logout', h.url_for('logout'))}
+          %else:
+            ${h.link_to('Login', h.url_for('login'))}
+            | ${h.link_to('Sign up', h.url_for(''))}
+          %endif
+        </p>
       </div>
-      </%doc>
     </div>
   </div>
 </%def>

skypieamc/templates/derived/account/login.mako

+<%inherit file="/base.mako" />
+
+<%def name="css()">
+  ${parent.css()}
+  ${h.stylesheet_link(h.url_for('/css/account.css'))}
+</%def>
+
+${h.form(h.url_for(controller='account', action='do_login'), method='post')}
+  <dl>
+    <dt><label for="username">Username:</label></dt>
+    <dd>${h.text('username')}</dd>
+    <dt><label for="password">Password:</label></dt>
+    <dd>${h.password('password')}</dd>
+  </dl>
+  ${h.submit('btn_submit', value='Login')}
+${h.end_form()}
+
+<%def name="sidebar()"></%def>