56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from flask import request, redirect, render_template, url_for, flash
|
|
from flask.ext.login import (LoginManager, login_required,
|
|
login_user, logout_user, UserMixin)
|
|
|
|
from app import app
|
|
from app.libs.forms import FormLogin
|
|
import app.models as modelos
|
|
|
|
# flask-login - Configurações de Autenticação:
|
|
login_manager = LoginManager()
|
|
login_manager.setup_app(app)
|
|
login_manager.login_view = "login"
|
|
|
|
class User(UserMixin):
|
|
def __init__(self, name, id, active=True):
|
|
self.name = name
|
|
self.id = id
|
|
self.active = active
|
|
|
|
def is_active(self):
|
|
return self.active
|
|
|
|
@login_manager.user_loader
|
|
def load_user(id):
|
|
usuario = modelos.Usuario.query.get(int(id))
|
|
return User(usuario.login, usuario.id)
|
|
|
|
login_manager.setup_app(app)
|
|
|
|
@app.route('/login', methods=['GET', 'POST'])
|
|
def login():
|
|
form = FormLogin(request.form)
|
|
if request.method == 'POST' and form.validate():
|
|
_login = request.form['login']
|
|
_senha = request.form['senha']
|
|
|
|
_em_db = modelos.Usuario.query.filter_by(login = _login).first()
|
|
if (_em_db):
|
|
if ( _login == _em_db.login and _senha == _em_db.senha):
|
|
login_user(User(_em_db.login, _em_db.id))
|
|
redirect(request.args.get("next") or url_for("index"))
|
|
else:
|
|
flash(unicode("Senha Invalida", "UTF-8"))
|
|
return render_template("login.html", form=form)
|
|
else:
|
|
flash(unicode("Usuário não existe", "UTF-8"))
|
|
return render_template("login.html", form=form)
|
|
|
|
@app.route('/logout')
|
|
@login_required
|
|
def logout():
|
|
""" Define a remoção da sessão do usuário """
|
|
logout_user()
|
|
return redirect('/') |