2023-05-29 10:24:21 +00:00
|
|
|
import flask
|
|
|
|
import flask_login
|
|
|
|
|
|
|
|
from . import db
|
2024-08-14 09:25:07 +00:00
|
|
|
from . import ipsets
|
2023-05-29 10:24:21 +00:00
|
|
|
from . import system
|
|
|
|
|
2023-12-04 08:46:37 +00:00
|
|
|
blueprint = flask.Blueprint('rules', __name__)
|
2023-05-29 10:24:21 +00:00
|
|
|
|
|
|
|
@blueprint.route('/', methods=('GET', 'POST'))
|
|
|
|
@flask_login.login_required
|
|
|
|
def index():
|
2023-07-12 12:18:31 +00:00
|
|
|
if not flask_login.current_user.is_admin:
|
|
|
|
return flask.Response('forbidden', status=403, mimetype='text/plain')
|
2023-05-29 10:24:21 +00:00
|
|
|
|
2023-07-12 12:18:31 +00:00
|
|
|
if flask.request.method == 'POST':
|
|
|
|
with db.locked():
|
|
|
|
rules = db.read('rules')
|
|
|
|
form = flask.request.form
|
|
|
|
oldrules = {rule['name']: rule for rule in rules}
|
|
|
|
rules = []
|
|
|
|
for index, name in sorted(
|
|
|
|
zip(form.getlist('index'), form.getlist('name')), key=lambda e: int(e[0] or 0)):
|
|
|
|
if index and name:
|
|
|
|
rules.append(oldrules.get(name, {'name': name}))
|
|
|
|
db.write('rules', rules)
|
|
|
|
system.run(system.save_config)
|
2023-05-29 10:24:21 +00:00
|
|
|
|
2023-07-12 12:18:31 +00:00
|
|
|
return flask.render_template('rules/index.html', rules=db.load('rules'))
|
2023-05-29 10:24:21 +00:00
|
|
|
|
|
|
|
@blueprint.route('/edit/<int:index>', methods=('GET', 'POST'))
|
|
|
|
@flask_login.login_required
|
|
|
|
def edit(index):
|
|
|
|
try:
|
|
|
|
if not flask_login.current_user.is_admin:
|
|
|
|
return flask.Response('forbidden', status=403, mimetype='text/plain')
|
|
|
|
|
|
|
|
if flask.request.method == 'POST':
|
|
|
|
with db.locked():
|
|
|
|
form = flask.request.form
|
|
|
|
rules = db.read('rules')
|
|
|
|
rules[index]['name'] = form.get('name')
|
2023-06-26 09:49:26 +00:00
|
|
|
rules[index]['text'] = form.get('text').replace('\r\n', '\n')
|
2023-05-29 10:24:21 +00:00
|
|
|
rules[index]['managers'] = [m for m in form.getlist('manager') if m]
|
|
|
|
db.write('rules', rules)
|
|
|
|
system.run(system.save_config)
|
|
|
|
|
2023-07-24 13:45:45 +00:00
|
|
|
with db.locked():
|
2024-08-14 09:25:07 +00:00
|
|
|
return flask.render_template('rules/edit.html', index=index, rule=db.load('rules')[index], ipsets=ipsets.read())
|
2023-05-29 10:24:21 +00:00
|
|
|
except IndexError as e:
|
|
|
|
return flask.Response(f'invalid rule: {index}', status=400, mimetype='text/plain')
|
|
|
|
|
|
|
|
def can_toggle(user, rule):
|
|
|
|
return user.is_admin or not user.groups.isdisjoint(rule.get('managers', ()))
|
|
|
|
|
2024-05-29 09:10:31 +00:00
|
|
|
@blueprint.route('/manage', methods=('GET', 'POST'))
|
2023-05-29 10:24:21 +00:00
|
|
|
@flask_login.login_required
|
|
|
|
def manage():
|
2024-05-29 09:10:31 +00:00
|
|
|
with db.locked():
|
|
|
|
rules = db.read('rules')
|
|
|
|
allowed = set(rule['name'] for rule in rules if can_toggle(flask_login.current_user, rule))
|
|
|
|
if flask.request.method == 'POST':
|
|
|
|
# check that all posted rules are allowed for this user
|
|
|
|
posted = set(flask.request.form.getlist('rule'))
|
|
|
|
if posted - allowed:
|
2023-05-29 10:24:21 +00:00
|
|
|
return flask.Response('forbidden', status=403, mimetype='text/plain')
|
2024-05-29 09:10:31 +00:00
|
|
|
|
|
|
|
# set status for posted rules
|
|
|
|
enabled = set(flask.request.form.getlist('enabled'))
|
|
|
|
for rule in rules:
|
|
|
|
if rule['name'] in posted:
|
|
|
|
rule['enabled'] = (rule['name'] in enabled)
|
2023-05-29 10:24:21 +00:00
|
|
|
db.write('rules', rules)
|
2024-05-29 09:10:31 +00:00
|
|
|
system.run(system.save_config)
|
|
|
|
return flask.redirect(flask.url_for('rules.manage'))
|
|
|
|
return flask.render_template('rules/manage.html', rules=[rule for rule in rules if rule['name'] in allowed])
|