71 lines
1.6 KiB
Python
71 lines
1.6 KiB
Python
import logging
|
|
|
|
from flask import abort, send_file, redirect, request, url_for
|
|
from flask.ext.login import login_user, UserMixin, login_required, logout_user
|
|
from flask.ext.principal import identity_changed, Identity, AnonymousIdentity
|
|
|
|
from data import model
|
|
from app import app, login_manager
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class _LoginWrappedDBUser(UserMixin):
|
|
def __init__(self, db_user):
|
|
self.db_user = db_user
|
|
|
|
def is_active(self):
|
|
return self.db_user.verified
|
|
|
|
def get_id(self):
|
|
return unicode(self.db_user.username)
|
|
|
|
|
|
@login_manager.user_loader
|
|
def load_user(username):
|
|
db_user = model.get_user(username)
|
|
if db_user:
|
|
return _LoginWrappedDBUser(db_user)
|
|
else:
|
|
return None
|
|
|
|
|
|
@app.route('/', methods=['GET'])
|
|
def index():
|
|
return send_file('templates/index.html')
|
|
|
|
|
|
@app.route('/signin', methods=['POST'])
|
|
def signin():
|
|
username = request.form['username']
|
|
password = request.form['password']
|
|
|
|
#TODO Allow email login
|
|
verified = model.verify_user(username, password)
|
|
if verified:
|
|
logger.debug('Successfully signed in as: %s' % username)
|
|
|
|
login_user(_LoginWrappedDBUser(verified))
|
|
|
|
identity_changed.send(app, identity=Identity(verified.username,
|
|
'username'))
|
|
|
|
return redirect(request.args.get('next') or url_for('index'))
|
|
|
|
abort(403)
|
|
|
|
|
|
@app.route('/signin', methods=['GET'])
|
|
def render_signin_page():
|
|
return send_file('templates/signin.html')
|
|
|
|
|
|
@app.route("/signout")
|
|
@login_required
|
|
def logout():
|
|
logout_user()
|
|
|
|
identity_changed.send(app, identity=AnonymousIdentity())
|
|
|
|
return redirect(url_for('index'))
|