1
0
mirror of https://github.com/Mailu/Mailu.git synced 2024-12-14 10:53:30 +02:00
Mailu/admin/mailu/views/users.py

154 lines
6.3 KiB
Python
Raw Normal View History

2017-09-17 14:37:10 +02:00
from mailu import app, db, models, forms, access
2016-03-19 21:37:48 +02:00
import flask
2016-08-13 20:51:54 +02:00
import flask_login
import wtforms
import wtforms_components
2016-03-19 21:37:48 +02:00
@app.route('/user/list/<domain_name>', methods=['GET'])
@access.domain_admin(models.Domain, 'domain_name')
2016-03-19 21:37:48 +02:00
def user_list(domain_name):
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)
@app.route('/user/create/<domain_name>', methods=['GET', 'POST'])
@access.domain_admin(models.Domain, 'domain_name')
2016-03-19 21:37:48 +02:00
def user_create(domain_name):
domain = models.Domain.query.get(domain_name) or flask.abort(404)
if domain.max_users 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')
return flask.redirect(
flask.url_for('.user_list', domain_name=domain.name))
form = forms.UserForm()
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():
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)
db.session.add(user)
db.session.commit()
flask.flash('User %s created' % user)
return flask.redirect(
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)
@app.route('/user/edit/<user_email>', methods=['GET', 'POST'])
@access.domain_admin(models.User, 'user_email')
2016-03-19 21:37:48 +02:00
def user_edit(user_email):
user = models.User.query.get(user_email) or flask.abort(404)
# 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
form = forms.UserForm(obj=user)
wtforms_components.read_only(form.localpart)
form.pw.validators = []
form.localpart.validators = []
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():
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)
2016-03-19 21:37:48 +02:00
db.session.commit()
flask.flash('User %s updated' % user)
return flask.redirect(
flask.url_for('.user_list', domain_name=user.domain.name))
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
@app.route('/user/delete/<user_email>', methods=['GET', 'POST'])
@access.domain_admin(models.User, 'user_email')
@access.confirmation_required("delete {user_email}")
2016-03-19 21:37:48 +02:00
def user_delete(user_email):
user = models.User.query.get(user_email) or flask.abort(404)
domain = user.domain
2016-03-19 21:37:48 +02:00
db.session.delete(user)
db.session.commit()
flask.flash('User %s deleted' % user)
return flask.redirect(
flask.url_for('.user_list', domain_name=domain.name))
2016-03-19 21:37:48 +02:00
@app.route('/user/settings', methods=['GET', 'POST'], defaults={'user_email': None})
@app.route('/user/usersettings/<user_email>', methods=['GET', 'POST'])
@access.owner(models.User, 'user_email')
2016-03-19 21:37:48 +02:00
def user_settings(user_email):
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)
2016-03-20 12:09:06 +02:00
if form.validate_on_submit():
2016-05-01 21:12:08 +02:00
form.populate_obj(user)
2016-03-20 12:09:06 +02:00
db.session.commit()
flask.flash('Settings updated for %s' % user)
if user_email:
return flask.redirect(
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
@app.route('/user/password', methods=['GET', 'POST'], defaults={'user_email': None})
@app.route('/user/password/<user_email>', methods=['GET', 'POST'])
@access.owner(models.User, 'user_email')
2016-03-19 21:37:48 +02:00
def user_password(user_email):
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:
user.set_password(form.pw.data)
db.session.commit()
flask.flash('Password updated for %s' % user)
if user_email:
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)
@app.route('/user/forward', methods=['GET', 'POST'], defaults={'user_email': None})
@app.route('/user/forward/<user_email>', methods=['GET', 'POST'])
@access.owner(models.User, 'user_email')
2016-03-19 21:37:48 +02:00
def user_forward(user_email):
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.UserForwardForm(obj=user)
2016-03-19 21:37:48 +02:00
if form.validate_on_submit():
form.populate_obj(user)
2016-03-19 21:37:48 +02:00
db.session.commit()
flask.flash('Forward destination updated for %s' % user)
if user_email:
return flask.redirect(
flask.url_for('.user_list', domain_name=user.domain.name))
return flask.render_template('user/forward.html', form=form, user=user)
2016-03-19 21:37:48 +02:00
@app.route('/user/reply', methods=['GET', 'POST'], defaults={'user_email': None})
@app.route('/user/reply/<user_email>', methods=['GET', 'POST'])
@access.owner(models.User, 'user_email')
def user_reply(user_email):
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)
if form.validate_on_submit():
2016-05-01 21:12:08 +02:00
form.populate_obj(user)
db.session.commit()
flask.flash('Auto-reply message updated for %s' % user)
if user_email:
return flask.redirect(
flask.url_for('.user_list', domain_name=user.domain.name))
return flask.render_template('user/reply.html', form=form, user=user)