Development

This commit is contained in:
Matthew Grotke 2026-05-27 22:04:04 -04:00
parent eed1d295dc
commit d9f3bd8289
45 changed files with 635 additions and 666 deletions

View file

@ -1,3 +1,4 @@
from pathlib import Path
import copy
from flask import Blueprint, request, redirect, flash
@ -6,10 +7,9 @@ from config_utils import load_config, save_config_with_snapshot, verify_config_h
import sanitize
import validation as validate
bp = Blueprint('bannedips', __name__)
VIEW = '/view/view_bannedips'
_PAGE = Path(__file__).parent.name
bp = Blueprint(_PAGE, __name__)
def _row_index():
try:
@ -37,15 +37,15 @@ def _parse_ip():
return ip
@bp.route('/action/add_banned_ip', methods=['POST'])
@bp.route('/action/bannedips/addip_add', methods=['POST'])
@require_level('administrator')
def add_banned_ip():
def addip_add():
description = sanitize.text(request.form.get('description', ''))
ip = _parse_ip()
if ip is None:
return redirect(VIEW)
return redirect(f'/{_PAGE}')
if not _hash_ok():
return redirect(VIEW)
return redirect(f'/{_PAGE}')
cfg = load_config()
entry = {'description': description, 'ip': ip, 'enabled': True}
@ -54,7 +54,7 @@ def add_banned_ip():
if errors:
for msg in errors:
flash(msg, 'error')
return redirect(VIEW)
return redirect(f'/{_PAGE}')
flash(save_config_with_snapshot(
cfg,
@ -62,24 +62,24 @@ def add_banned_ip():
before=None, after=entry,
description=f'Added banned IP: {ip}',
), 'success')
return redirect(VIEW)
return redirect(f'/{_PAGE}')
@bp.route('/action/toggle_banned_ip', methods=['POST'])
@bp.route('/action/bannedips/table_toggle', methods=['POST'])
@require_level('administrator')
def toggle_banned_ip():
def table_toggle():
idx = _row_index()
if idx is None:
flash('Invalid request.', 'error')
return redirect(VIEW)
return redirect(f'/{_PAGE}')
if not _hash_ok():
return redirect(VIEW)
return redirect(f'/{_PAGE}')
cfg = load_config()
items = cfg.get('banned_ips', [])
if idx < 0 or idx >= len(items):
flash('Entry not found.', 'error')
return redirect(VIEW)
return redirect(f'/{_PAGE}')
old_enabled = items[idx].get('enabled', True)
items[idx]['enabled'] = not old_enabled
@ -87,7 +87,7 @@ def toggle_banned_ip():
if errors:
for msg in errors:
flash(msg, 'error')
return redirect(VIEW)
return redirect(f'/{_PAGE}')
action = 'Enabled' if not old_enabled else 'Disabled'
flash(save_config_with_snapshot(
@ -96,31 +96,31 @@ def toggle_banned_ip():
before={'enabled': old_enabled}, after={'enabled': not old_enabled},
description=f'{action} banned IP: {items[idx]["ip"]}',
), 'success')
return redirect(VIEW)
return redirect(f'/{_PAGE}')
@bp.route('/action/edit_banned_ip', methods=['POST'])
@bp.route('/action/bannedips/table_edit', methods=['POST'])
@require_level('administrator')
def edit_banned_ip():
def table_edit():
idx = _row_index()
if idx is None:
flash('Invalid request.', 'error')
return redirect(VIEW)
return redirect(f'/{_PAGE}')
description = sanitize.text(request.form.get('description', ''))
ip = _parse_ip()
if ip is None:
return redirect(VIEW)
return redirect(f'/{_PAGE}')
enabled = request.form.get('enabled') == 'on'
if not _hash_ok():
return redirect(VIEW)
return redirect(f'/{_PAGE}')
cfg = load_config()
items = cfg.get('banned_ips', [])
if idx < 0 or idx >= len(items):
flash('Entry not found.', 'error')
return redirect(VIEW)
return redirect(f'/{_PAGE}')
before = copy.deepcopy(items[idx])
items[idx].update({'description': description, 'ip': ip, 'enabled': enabled})
@ -128,7 +128,7 @@ def edit_banned_ip():
if errors:
for msg in errors:
flash(msg, 'error')
return redirect(VIEW)
return redirect(f'/{_PAGE}')
flash(save_config_with_snapshot(
cfg,
@ -136,31 +136,31 @@ def edit_banned_ip():
before=before, after=copy.deepcopy(items[idx]),
description=f'Edited banned IP: {ip}',
), 'success')
return redirect(VIEW)
return redirect(f'/{_PAGE}')
@bp.route('/action/delete_banned_ip', methods=['POST'])
@bp.route('/action/bannedips/table_delete', methods=['POST'])
@require_level('administrator')
def delete_banned_ip():
def table_delete():
idx = _row_index()
if idx is None:
flash('Invalid request.', 'error')
return redirect(VIEW)
return redirect(f'/{_PAGE}')
if not _hash_ok():
return redirect(VIEW)
return redirect(f'/{_PAGE}')
cfg = load_config()
items = cfg.get('banned_ips', [])
if idx < 0 or idx >= len(items):
flash('Entry not found.', 'error')
return redirect(VIEW)
return redirect(f'/{_PAGE}')
removed = items.pop(idx)
errors = validate.validate_config(cfg)
if errors:
for msg in errors:
flash(msg, 'error')
return redirect(VIEW)
return redirect(f'/{_PAGE}')
flash(save_config_with_snapshot(
cfg,
@ -168,4 +168,4 @@ def delete_banned_ip():
before=removed, after=None,
description=f'Deleted banned IP: {removed["ip"]}',
), 'success')
return redirect(VIEW)
return redirect(f'/{_PAGE}')

View file

@ -1,5 +1,4 @@
{
"id": "view_bannedips",
"client_requirement": "client_is_viewer+",
"items": [
{
@ -43,7 +42,7 @@
"row_actions": [
{
"client_requirement": "client_is_administrator+",
"action": "/action/edit_banned_ip",
"action": "/action/bannedips/table_edit",
"method": "inline_edit",
"text": "Edit",
"class": "btn-ghost btn-sm",
@ -65,7 +64,7 @@
},
{
"client_requirement": "client_is_administrator+",
"action": "/action/delete_banned_ip",
"action": "/action/bannedips/table_delete",
"method": "post",
"text": "Delete",
"class": "btn-danger btn-sm"
@ -80,7 +79,7 @@
"items": [
{
"type": "form",
"action": "/action/add_banned_ip",
"action": "/action/bannedips/addip_add",
"method": "post",
"items": [
{
@ -103,7 +102,7 @@
"items": [
{
"type": "button_primary",
"action": "/action/add_banned_ip",
"action": "/action/bannedips/addip_add",
"method": "post",
"text": "Add Banned IP"
},