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-09-02 17:08:50 +02:00
|
|
|
endpoint = flask.request.args.get('next', 'ui.index')
|
2021-09-24 10:07:52 +02:00
|
|
|
|
2021-10-27 10:22:36 +02:00
|
|
|
form.target.choices = []
|
|
|
|
if str(app.config["ADMIN"]).upper() != "FALSE":
|
|
|
|
form.target.choices += [("Admin", "Admin")]
|
|
|
|
if str(app.config["WEBMAIL"]).upper() != "NONE":
|
|
|
|
form.target.choices += [("Webmail", "Webmail")]
|
|
|
|
if endpoint == "ui.webmail":
|
|
|
|
form.target.choices.reverse()
|
2021-09-24 10:07:52 +02:00
|
|
|
|
2021-09-02 13:36:42 +02:00
|
|
|
if form.validate_on_submit():
|
2021-09-24 15:29:28 +02:00
|
|
|
if str(form.target.data) == 'Admin':
|
2021-10-27 11:11:22 +02:00
|
|
|
endpoint = 'ui.user_settings'
|
2021-10-27 13:24:10 +02:00
|
|
|
destination = app.config['WEB_ADMIN']
|
2021-09-24 15:29:28 +02:00
|
|
|
elif str(form.target.data) == 'Webmail':
|
2021-09-24 10:07:52 +02:00
|
|
|
endpoint = 'ui.webmail'
|
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-09-02 17:08:50 +02:00
|
|
|
return flask.render_template('login.html', form=form, endpoint=endpoint)
|
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'))
|