1
0
mirror of https://github.com/Mailu/Mailu.git synced 2024-12-16 10:59:53 +02:00
Mailu/core/admin/mailu/sso/views/base.py

44 lines
1.6 KiB
Python
Raw Normal View History

from mailu import models
from mailu.sso import sso, forms
from mailu.ui import access
from flask import current_app as app
import flask
import flask_login
@sso.route('/login', methods=['GET', 'POST'])
def login():
form = forms.LoginForm()
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
if form.validate_on_submit():
if str(form.target.data) == 'Admin':
2021-09-24 10:07:52 +02:00
endpoint = 'ui.index'
elif str(form.target.data) == 'Webmail':
2021-09-24 10:07:52 +02:00
endpoint = 'ui.webmail'
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)
return flask.redirect(flask.url_for(endpoint))
else:
flask.flash('Wrong e-mail or password', 'error')
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}.')
return flask.render_template('login.html', form=form, endpoint=endpoint)
@sso.route('/logout', methods=['GET'])
@access.authenticated
def logout():
flask_login.logout_user()
flask.session.destroy()
return flask.redirect(flask.url_for('.login'))