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

46 lines
1.6 KiB
Python
Raw Normal View History

from werkzeug.utils import redirect
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()
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":
fields.append(form.submitAdmin)
2021-10-27 10:22:36 +02:00
if str(app.config["WEBMAIL"]).upper() != "NONE":
fields.append(form.submitWebmail)
fields = tuple(fields)
2021-09-24 10:07:52 +02:00
if form.validate_on_submit():
if form.submitAdmin.data:
destination = app.config['WEB_ADMIN']
elif form.submitWebmail.data:
destination = app.config['WEB_WEBMAIL']
2021-09-24 10:07:52 +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)
return flask.redirect(destination)
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, fields=fields)
@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'))