2021-10-27 13:24:10 +02:00
|
|
|
from werkzeug.utils import redirect
|
2021-09-02 13:36:42 +02:00
|
|
|
from mailu import models
|
|
|
|
from mailu.sso import sso, forms
|
2021-09-02 17:08:50 +02:00
|
|
|
from mailu.ui import access
|
2021-09-02 13:36:42 +02:00
|
|
|
|
|
|
|
from flask import current_app as app
|
|
|
|
import flask
|
|
|
|
import flask_login
|
|
|
|
|
|
|
|
@sso.route('/login', methods=['GET', 'POST'])
|
|
|
|
def login():
|
|
|
|
form = forms.LoginForm()
|
2021-10-27 23:51:49 +02:00
|
|
|
form.submitAdmin.label.text = form.submitAdmin.label.text + ' Admin'
|
|
|
|
form.submitWebmail.label.text = form.submitWebmail.label.text + ' Webmail'
|
|
|
|
|
|
|
|
fields = []
|
2021-10-27 10:22:36 +02:00
|
|
|
if str(app.config["ADMIN"]).upper() != "FALSE":
|
2021-10-27 23:51:49 +02:00
|
|
|
fields.append(form.submitAdmin)
|
2021-10-27 10:22:36 +02:00
|
|
|
if str(app.config["WEBMAIL"]).upper() != "NONE":
|
2021-10-27 23:51:49 +02:00
|
|
|
fields.append(form.submitWebmail)
|
|
|
|
fields = tuple(fields)
|
2021-09-24 10:07:52 +02:00
|
|
|
|
2021-09-02 13:36:42 +02:00
|
|
|
if form.validate_on_submit():
|
2021-10-27 23:51:49 +02:00
|
|
|
if form.submitAdmin.data:
|
2021-10-27 13:24:10 +02:00
|
|
|
destination = app.config['WEB_ADMIN']
|
2021-10-27 23:51:49 +02:00
|
|
|
elif form.submitWebmail.data:
|
2021-10-27 13:24:10 +02:00
|
|
|
destination = app.config['WEB_WEBMAIL']
|
2021-09-24 10:07:52 +02:00
|
|
|
|
2021-09-02 13:36:42 +02:00
|
|
|
user = models.User.login(form.email.data, form.pw.data)
|
|
|
|
if user:
|
|
|
|
flask.session.regenerate()
|
2021-09-03 13:01:09 +02:00
|
|
|
flask_login.login_user(user)
|
2021-10-27 13:24:10 +02:00
|
|
|
return flask.redirect(destination)
|
2021-09-02 13:36:42 +02:00
|
|
|
else:
|
|
|
|
flask.flash('Wrong e-mail or password', 'error')
|
2021-09-24 15:29:28 +02:00
|
|
|
client_ip = flask.request.headers["X-Real-IP"] if 'X-Real-IP' in flask.request.headers else flask.request.remote_addr
|
|
|
|
flask.current_app.logger.warn(f'Login failed for {str(form.email.data)} from {client_ip}.')
|
2021-10-27 23:51:49 +02:00
|
|
|
return flask.render_template('login.html', form=form, fields=fields)
|
2021-10-25 21:21:38 +02:00
|
|
|
|
|
|
|
@sso.route('/logout', methods=['GET'])
|
|
|
|
@access.authenticated
|
|
|
|
def logout():
|
|
|
|
flask_login.logout_user()
|
|
|
|
flask.session.destroy()
|
2021-10-27 11:11:22 +02:00
|
|
|
return flask.redirect(flask.url_for('.login'))
|