2022-11-03 17:19:44 +02:00
|
|
|
from mailu import models, utils
|
2017-09-24 12:00:39 +02:00
|
|
|
from mailu.ui import ui, access, forms
|
2018-10-18 15:57:43 +02:00
|
|
|
from flask import current_app as app
|
2016-03-19 21:37:48 +02:00
|
|
|
|
|
|
|
import flask
|
2016-08-13 20:51:54 +02:00
|
|
|
import flask_login
|
2017-02-02 23:29:33 +02:00
|
|
|
import wtforms
|
2016-03-22 20:47:15 +02:00
|
|
|
import wtforms_components
|
2016-03-19 21:37:48 +02:00
|
|
|
|
2017-09-24 12:00:39 +02:00
|
|
|
@ui.route('/user/list/<domain_name>', methods=['GET'])
|
2016-08-29 19:24:39 +02:00
|
|
|
@access.domain_admin(models.Domain, 'domain_name')
|
2016-03-19 21:37:48 +02:00
|
|
|
def user_list(domain_name):
|
2016-08-29 19:24:39 +02:00
|
|
|
domain = models.Domain.query.get(domain_name) or flask.abort(404)
|
2016-03-19 21:37:48 +02:00
|
|
|
return flask.render_template('user/list.html', domain=domain)
|
|
|
|
|
|
|
|
|
2017-09-24 12:00:39 +02:00
|
|
|
@ui.route('/user/create/<domain_name>', methods=['GET', 'POST'])
|
2016-08-29 19:24:39 +02:00
|
|
|
@access.domain_admin(models.Domain, 'domain_name')
|
2016-03-19 21:37:48 +02:00
|
|
|
def user_create(domain_name):
|
2016-08-29 19:24:39 +02:00
|
|
|
domain = models.Domain.query.get(domain_name) or flask.abort(404)
|
2019-01-05 14:45:55 +02:00
|
|
|
if not domain.max_users == -1 and len(domain.users) >= domain.max_users:
|
2016-03-19 21:37:48 +02:00
|
|
|
flask.flash('Too many users for domain %s' % domain, 'error')
|
2016-03-20 16:36:56 +02:00
|
|
|
return flask.redirect(
|
|
|
|
flask.url_for('.user_list', domain_name=domain.name))
|
2016-03-22 20:47:15 +02:00
|
|
|
form = forms.UserForm()
|
2019-01-09 13:03:47 +02:00
|
|
|
form.pw.validators = [wtforms.validators.DataRequired()]
|
2017-02-14 22:37:51 +02:00
|
|
|
if domain.max_quota_bytes:
|
|
|
|
form.quota_bytes.validators = [
|
|
|
|
wtforms.validators.NumberRange(max=domain.max_quota_bytes)]
|
2016-03-19 21:37:48 +02:00
|
|
|
if form.validate_on_submit():
|
2022-11-03 17:19:44 +02:00
|
|
|
if msg := utils.isBadOrPwned(form):
|
|
|
|
flask.flash(msg, "error")
|
2022-10-31 20:41:40 +02:00
|
|
|
return flask.render_template('user/create.html',
|
|
|
|
domain=domain, form=form)
|
2016-05-01 20:04:40 +02:00
|
|
|
if domain.has_email(form.localpart.data):
|
2016-05-01 21:12:08 +02:00
|
|
|
flask.flash('Email is already used', 'error')
|
2016-03-19 21:37:48 +02:00
|
|
|
else:
|
2016-05-01 21:12:08 +02:00
|
|
|
user = models.User(domain=domain)
|
|
|
|
form.populate_obj(user)
|
2016-03-19 21:37:48 +02:00
|
|
|
user.set_password(form.pw.data)
|
2018-10-18 15:57:43 +02:00
|
|
|
models.db.session.add(user)
|
|
|
|
models.db.session.commit()
|
2017-11-10 12:56:43 +02:00
|
|
|
user.send_welcome()
|
2016-03-19 21:37:48 +02:00
|
|
|
flask.flash('User %s created' % user)
|
|
|
|
return flask.redirect(
|
2016-03-20 16:36:56 +02:00
|
|
|
flask.url_for('.user_list', domain_name=domain.name))
|
2016-03-19 21:37:48 +02:00
|
|
|
return flask.render_template('user/create.html',
|
|
|
|
domain=domain, form=form)
|
|
|
|
|
|
|
|
|
2019-01-04 19:01:46 +02:00
|
|
|
@ui.route('/user/edit/<path:user_email>', methods=['GET', 'POST'])
|
2016-08-29 19:24:39 +02:00
|
|
|
@access.domain_admin(models.User, 'user_email')
|
2016-03-19 21:37:48 +02:00
|
|
|
def user_edit(user_email):
|
2016-08-29 19:24:39 +02:00
|
|
|
user = models.User.query.get(user_email) or flask.abort(404)
|
2017-02-02 23:29:33 +02:00
|
|
|
# Handle the case where user quota is more than allowed
|
|
|
|
max_quota_bytes = user.domain.max_quota_bytes
|
|
|
|
if max_quota_bytes and user.quota_bytes > max_quota_bytes:
|
|
|
|
max_quota_bytes = user.quota_bytes
|
|
|
|
# Create the form
|
2016-03-22 20:47:15 +02:00
|
|
|
form = forms.UserForm(obj=user)
|
|
|
|
wtforms_components.read_only(form.localpart)
|
2017-01-25 01:05:03 +02:00
|
|
|
form.localpart.validators = []
|
2017-02-14 22:37:51 +02:00
|
|
|
if max_quota_bytes:
|
|
|
|
form.quota_bytes.validators = [
|
|
|
|
wtforms.validators.NumberRange(max=max_quota_bytes)]
|
2016-03-19 21:37:48 +02:00
|
|
|
if form.validate_on_submit():
|
2022-11-03 17:19:44 +02:00
|
|
|
if msg := utils.isBadOrPwned(form):
|
|
|
|
flask.flash(msg, "error")
|
2022-10-31 20:41:40 +02:00
|
|
|
return flask.render_template('user/edit.html', form=form, user=user,
|
|
|
|
domain=user.domain, max_quota_bytes=max_quota_bytes)
|
2016-05-01 21:12:08 +02:00
|
|
|
form.populate_obj(user)
|
2016-03-22 22:15:57 +02:00
|
|
|
if form.pw.data:
|
|
|
|
user.set_password(form.pw.data)
|
2018-10-18 15:57:43 +02:00
|
|
|
models.db.session.commit()
|
2016-03-19 21:37:48 +02:00
|
|
|
flask.flash('User %s updated' % user)
|
|
|
|
return flask.redirect(
|
2016-03-20 16:36:56 +02:00
|
|
|
flask.url_for('.user_list', domain_name=user.domain.name))
|
2017-02-02 23:29:33 +02:00
|
|
|
return flask.render_template('user/edit.html', form=form, user=user,
|
|
|
|
domain=user.domain, max_quota_bytes=max_quota_bytes)
|
2016-03-19 21:37:48 +02:00
|
|
|
|
|
|
|
|
2019-01-04 19:01:46 +02:00
|
|
|
@ui.route('/user/delete/<path:user_email>', methods=['GET', 'POST'])
|
2016-08-29 19:24:39 +02:00
|
|
|
@access.domain_admin(models.User, 'user_email')
|
2016-08-29 19:35:09 +02:00
|
|
|
@access.confirmation_required("delete {user_email}")
|
2016-03-19 21:37:48 +02:00
|
|
|
def user_delete(user_email):
|
2016-08-29 19:24:39 +02:00
|
|
|
user = models.User.query.get(user_email) or flask.abort(404)
|
2016-11-12 16:47:48 +02:00
|
|
|
domain = user.domain
|
2018-10-18 15:57:43 +02:00
|
|
|
models.db.session.delete(user)
|
|
|
|
models.db.session.commit()
|
2016-03-19 21:37:48 +02:00
|
|
|
flask.flash('User %s deleted' % user)
|
2016-03-20 16:36:56 +02:00
|
|
|
return flask.redirect(
|
2016-11-12 16:47:48 +02:00
|
|
|
flask.url_for('.user_list', domain_name=domain.name))
|
2016-03-19 21:37:48 +02:00
|
|
|
|
|
|
|
|
2017-09-24 12:00:39 +02:00
|
|
|
@ui.route('/user/settings', methods=['GET', 'POST'], defaults={'user_email': None})
|
2019-01-04 19:01:46 +02:00
|
|
|
@ui.route('/user/usersettings/<path:user_email>', methods=['GET', 'POST'])
|
2016-08-29 19:24:39 +02:00
|
|
|
@access.owner(models.User, 'user_email')
|
2016-03-19 21:37:48 +02:00
|
|
|
def user_settings(user_email):
|
2016-10-02 10:14:53 +02:00
|
|
|
user_email_or_current = user_email or flask_login.current_user.email
|
|
|
|
user = models.User.query.get(user_email_or_current) or flask.abort(404)
|
2016-03-20 12:14:27 +02:00
|
|
|
form = forms.UserSettingsForm(obj=user)
|
2018-12-28 22:03:57 +02:00
|
|
|
if isinstance(form.forward_destination.data,str):
|
|
|
|
data = form.forward_destination.data.replace(" ","").split(",")
|
|
|
|
else:
|
|
|
|
data = form.forward_destination.data
|
|
|
|
form.forward_destination.data = ", ".join(data)
|
2016-03-20 12:09:06 +02:00
|
|
|
if form.validate_on_submit():
|
2018-12-28 22:03:57 +02:00
|
|
|
form.forward_destination.data = form.forward_destination.data.replace(" ","").split(",")
|
2016-05-01 21:12:08 +02:00
|
|
|
form.populate_obj(user)
|
2018-10-18 15:57:43 +02:00
|
|
|
models.db.session.commit()
|
2018-12-28 22:03:57 +02:00
|
|
|
form.forward_destination.data = ", ".join(form.forward_destination.data)
|
2016-03-20 12:09:06 +02:00
|
|
|
flask.flash('Settings updated for %s' % user)
|
|
|
|
if user_email:
|
|
|
|
return flask.redirect(
|
2016-03-20 16:36:56 +02:00
|
|
|
flask.url_for('.user_list', domain_name=user.domain.name))
|
2016-03-20 12:09:06 +02:00
|
|
|
return flask.render_template('user/settings.html', form=form, user=user)
|
2016-03-19 21:37:48 +02:00
|
|
|
|
|
|
|
|
2017-09-24 12:00:39 +02:00
|
|
|
@ui.route('/user/password', methods=['GET', 'POST'], defaults={'user_email': None})
|
2019-01-04 19:01:46 +02:00
|
|
|
@ui.route('/user/password/<path:user_email>', methods=['GET', 'POST'])
|
2016-08-29 19:24:39 +02:00
|
|
|
@access.owner(models.User, 'user_email')
|
2016-03-19 21:37:48 +02:00
|
|
|
def user_password(user_email):
|
2016-10-02 10:14:53 +02:00
|
|
|
user_email_or_current = user_email or flask_login.current_user.email
|
|
|
|
user = models.User.query.get(user_email_or_current) or flask.abort(404)
|
2016-03-19 21:37:48 +02:00
|
|
|
form = forms.UserPasswordForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
if form.pw.data != form.pw2.data:
|
|
|
|
flask.flash('Passwords do not match', 'error')
|
|
|
|
else:
|
2022-11-03 17:19:44 +02:00
|
|
|
if msg := utils.isBadOrPwned(form):
|
|
|
|
flask.flash(msg, "error")
|
2022-10-31 20:41:40 +02:00
|
|
|
return flask.render_template('user/password.html', form=form, user=user)
|
2021-02-22 21:43:52 +02:00
|
|
|
flask.session.regenerate()
|
2016-03-19 21:37:48 +02:00
|
|
|
user.set_password(form.pw.data)
|
2018-10-18 15:57:43 +02:00
|
|
|
models.db.session.commit()
|
2016-03-19 21:37:48 +02:00
|
|
|
flask.flash('Password updated for %s' % user)
|
|
|
|
if user_email:
|
2016-03-20 16:36:56 +02:00
|
|
|
return flask.redirect(flask.url_for('.user_list',
|
|
|
|
domain_name=user.domain.name))
|
2016-03-19 21:37:48 +02:00
|
|
|
return flask.render_template('user/password.html', form=form, user=user)
|
|
|
|
|
|
|
|
|
2017-09-24 12:00:39 +02:00
|
|
|
@ui.route('/user/reply', methods=['GET', 'POST'], defaults={'user_email': None})
|
2019-01-04 19:01:46 +02:00
|
|
|
@ui.route('/user/reply/<path:user_email>', methods=['GET', 'POST'])
|
2016-08-29 19:24:39 +02:00
|
|
|
@access.owner(models.User, 'user_email')
|
2016-03-20 12:00:01 +02:00
|
|
|
def user_reply(user_email):
|
2016-10-02 10:14:53 +02:00
|
|
|
user_email_or_current = user_email or flask_login.current_user.email
|
|
|
|
user = models.User.query.get(user_email_or_current) or flask.abort(404)
|
2016-03-20 12:14:27 +02:00
|
|
|
form = forms.UserReplyForm(obj=user)
|
2016-03-20 12:00:01 +02:00
|
|
|
if form.validate_on_submit():
|
2016-05-01 21:12:08 +02:00
|
|
|
form.populate_obj(user)
|
2018-10-18 15:57:43 +02:00
|
|
|
models.db.session.commit()
|
2016-03-20 12:00:01 +02:00
|
|
|
flask.flash('Auto-reply message updated for %s' % user)
|
|
|
|
if user_email:
|
|
|
|
return flask.redirect(
|
2016-03-20 16:36:56 +02:00
|
|
|
flask.url_for('.user_list', domain_name=user.domain.name))
|
2016-03-20 12:00:01 +02:00
|
|
|
return flask.render_template('user/reply.html', form=form, user=user)
|
2017-12-03 13:01:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
@ui.route('/user/signup', methods=['GET', 'POST'])
|
|
|
|
@ui.route('/user/signup/<domain_name>', methods=['GET', 'POST'])
|
|
|
|
def user_signup(domain_name=None):
|
|
|
|
available_domains = {
|
|
|
|
domain.name: domain
|
|
|
|
for domain in models.Domain.query.filter_by(signup_enabled=True).all()
|
2019-01-05 14:45:55 +02:00
|
|
|
if domain.max_users == -1 or len(domain.users) < domain.max_users
|
2017-12-03 13:01:25 +02:00
|
|
|
}
|
|
|
|
if not available_domains:
|
|
|
|
flask.flash('No domain available for registration')
|
|
|
|
if not domain_name:
|
|
|
|
return flask.render_template('user/signup_domain.html',
|
|
|
|
available_domains=available_domains)
|
|
|
|
domain = available_domains.get(domain_name) or flask.abort(404)
|
2017-12-03 18:53:33 +02:00
|
|
|
quota_bytes = domain.max_quota_bytes or app.config['DEFAULT_QUOTA']
|
2018-11-07 09:58:49 +02:00
|
|
|
if app.config['RECAPTCHA_PUBLIC_KEY'] == "" or app.config['RECAPTCHA_PRIVATE_KEY'] == "":
|
|
|
|
form = forms.UserSignupForm()
|
|
|
|
else:
|
|
|
|
form = forms.UserSignupFormCaptcha()
|
|
|
|
|
2017-12-03 13:01:25 +02:00
|
|
|
if form.validate_on_submit():
|
2022-08-27 18:09:52 +02:00
|
|
|
if domain.has_email(form.localpart.data) or models.Alias.resolve(form.localpart.data, domain_name):
|
2017-12-03 13:01:25 +02:00
|
|
|
flask.flash('Email is already used', 'error')
|
|
|
|
else:
|
2022-11-03 17:19:44 +02:00
|
|
|
if msg := utils.isBadOrPwned(form):
|
|
|
|
flask.flash(msg, "error")
|
2022-10-31 20:41:40 +02:00
|
|
|
return flask.render_template('user/signup.html', domain=domain, form=form)
|
2021-02-22 21:43:52 +02:00
|
|
|
flask.session.regenerate()
|
2017-12-03 13:01:25 +02:00
|
|
|
user = models.User(domain=domain)
|
|
|
|
form.populate_obj(user)
|
|
|
|
user.set_password(form.pw.data)
|
|
|
|
user.quota_bytes = quota_bytes
|
2018-10-18 15:57:43 +02:00
|
|
|
models.db.session.add(user)
|
|
|
|
models.db.session.commit()
|
2017-12-03 13:01:25 +02:00
|
|
|
user.send_welcome()
|
|
|
|
flask.flash('Successfully signed up %s' % user)
|
|
|
|
return flask.redirect(flask.url_for('.index'))
|
|
|
|
return flask.render_template('user/signup.html', domain=domain, form=form)
|