1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# -*- coding: utf-8 -*-
from flask import Blueprint
from flask import current_app, redirect, request, g, flash, url_for
from flask_login import current_user
from uuid import uuid4
from werkzeug.exceptions import Forbidden
from accounts.utils import templated
from accounts.forms import AdminCreateAccountForm, AdminDisableAccountForm
bp = Blueprint('admin', __name__)
@bp.before_request
def restrict_bp_to_admins():
if not current_user.is_authenticated:
return current_app.login_manager.unauthorized()
if current_user.uid not in current_app.config.get('ADMIN_USERS', []):
raise Forbidden('Du bist kein Admin.')
@bp.route('/')
@templated('admin/index.html')
def index():
return {}
@bp.route('/create_account', methods=['GET', 'POST'])
@templated('admin/create_account.html')
def create_account():
form = AdminCreateAccountForm()
if form.validate_on_submit():
current_app.mail_backend.send(form.mail.data, 'mail/register.txt',
username=form.username.data)
flash('Mail versandt.', 'success')
return redirect(url_for('admin.index'))
return {'form': form}
@bp.route('/view_blacklist')
@bp.route('/view_blacklist/<start>')
@templated('admin/view_blacklist.html')
def view_blacklist(start=''):
entries = current_app.username_blacklist
if start:
entries = [e for e in entries if e.startswith(start)]
next_letters = set(e[len(start)] for e in entries if len(e) > len(start))
return {
'entries': entries,
'start': start,
'next_letters': next_letters,
}
@bp.route('/disable_account', methods=['GET', 'POST'])
@templated('admin/disable_account.html')
def disable_account():
form = AdminDisableAccountForm()
if 'uid' in request.args:
form = AdminDisableAccountForm(username=request.args['uid'])
if form.validate_on_submit():
random_pw = str(uuid4())
form.user.change_password(random_pw)
for service in current_app.all_services:
form.user.reset_password(service.id)
oldmail = form.user.mail
mail = current_app.config['DISABLED_ACCOUNT_MAILADDRESS_TEMPLATE'] % form.user.uid
form.user.change_email(mail)
current_app.user_backend.update(form.user, as_admin=True)
flash('Passwort auf ein zufälliges und Mailadresse auf %s '
'gesetzt.' % mail, 'success')
current_app.mail_backend.send(
current_app.config['MAIL_REGISTER_NOTIFY'],
'mail/disable_notify.txt',
username=form.user.uid, mail=oldmail, admin=current_user.uid)
return redirect(url_for('admin.index'))
return {'form': form}
|