Holger Frey
7 years ago
6 changed files with 118 additions and 22 deletions
@ -0,0 +1,36 @@ |
|||||||
|
{% extends "ordr2:templates/layout.jinja2" %} |
||||||
|
|
||||||
|
{% block subtitle %} Login {% endblock subtitle %} |
||||||
|
|
||||||
|
{% block content %} |
||||||
|
<div class="container"> |
||||||
|
<div class="row"> |
||||||
|
<div class="span6 offset3"> |
||||||
|
<h1>Log in</h1> |
||||||
|
<?php echo $this->session->flashdata('message'); ?> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
<div class="row"> |
||||||
|
<div class="span6 offset3"> |
||||||
|
<form action="{{request.resource_url(request.root, 'account', 'login')}}" method="post" class="form-horizontal"> |
||||||
|
<fieldset class="control-group"> |
||||||
|
<label for="input01" class="control-label">Username</label> |
||||||
|
<div class="controls"> |
||||||
|
<input type="text" name="username" class="span3" size="30"> |
||||||
|
</div> |
||||||
|
</fieldset> |
||||||
|
<fieldset class="control-group"> |
||||||
|
<label for="password" class="control-label">Password</label> |
||||||
|
<div class="controls"> |
||||||
|
<input type="password" name="password" class="span3" size="30"> |
||||||
|
</div> |
||||||
|
</fieldset> |
||||||
|
<fieldset class="form-actions"> |
||||||
|
<input type="hidden" name="csrf_token" value="{{get_csrf_token()}}"> |
||||||
|
<button class="btn primary large" type="submit">Log in</button> |
||||||
|
</fieldset> |
||||||
|
</form> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
{% endblock content %} |
@ -0,0 +1,60 @@ |
|||||||
|
from pyramid.httpexceptions import HTTPFound |
||||||
|
from pyramid.renderers import render |
||||||
|
from pyramid.security import remember, forget |
||||||
|
from pyramid.view import view_config |
||||||
|
|
||||||
|
from ordr2.models import User |
||||||
|
|
||||||
|
# user log in and log out |
||||||
|
|
||||||
|
@view_config( |
||||||
|
context='ordr2:resources.Account', |
||||||
|
name='login', |
||||||
|
permission='login', |
||||||
|
request_method='GET', |
||||||
|
renderer='ordr2:templates/account/login.jinja2') |
||||||
|
def login_form(context, request): |
||||||
|
''' display a login form ''' |
||||||
|
return {} |
||||||
|
|
||||||
|
|
||||||
|
@view_config( |
||||||
|
context='ordr2:resources.Account', |
||||||
|
name='login', |
||||||
|
permission='login', |
||||||
|
request_method='POST') |
||||||
|
def login(context, request): |
||||||
|
''' loging in a user ''' |
||||||
|
username = request.POST.get('username') |
||||||
|
password = request.POST.get('password') |
||||||
|
|
||||||
|
# Form validation is not done for login forms, |
||||||
|
# either the data represents a user or not. |
||||||
|
user = request.dbsession.query(User).filter_by(user_name=username).first() |
||||||
|
if user is not None: |
||||||
|
if user.is_active and user.check_password(password): |
||||||
|
headers = remember(request, user.id) |
||||||
|
return HTTPFound( |
||||||
|
request.resource_path(request.root, 'orders'), |
||||||
|
headers=headers |
||||||
|
) |
||||||
|
|
||||||
|
#request.session.flash(MSG_LOGIN_ERROR.format(url), 'error') |
||||||
|
return HTTPFound(request.resource_path(context, 'login')) |
||||||
|
|
||||||
|
|
||||||
|
@view_config( |
||||||
|
context='ordr2:resources.Account', |
||||||
|
name='logout', |
||||||
|
permission='logout' |
||||||
|
) |
||||||
|
def logout(context, request): |
||||||
|
''' logout of a user ''' |
||||||
|
if request.user: |
||||||
|
pass |
||||||
|
# request.session.flash(MSG_LOGOUT, 'success') |
||||||
|
headers = forget(request) |
||||||
|
return HTTPFound( |
||||||
|
request.resource_path(request.root, 'about'), |
||||||
|
headers=headers |
||||||
|
) |
Reference in new issue