129 lines
3.5 KiB
Python
129 lines
3.5 KiB
Python
from flask import Blueprint, request, redirect, flash
|
|
from auth import require_level
|
|
from config_utils import load_core, save_core, verify_core_hash, apply_msg
|
|
import sanitize
|
|
|
|
bp = Blueprint('action_apply_host_overrides', __name__)
|
|
|
|
VIEW = '/view/view_host_overrides'
|
|
|
|
|
|
def _row_index():
|
|
try:
|
|
return int(request.form.get('row_index', ''))
|
|
except (ValueError, TypeError):
|
|
return None
|
|
|
|
|
|
def _hash_ok():
|
|
if not verify_core_hash(request.form.get('config_hash', '')):
|
|
flash('Configuration was modified by another session. Please refresh and try again.', 'error')
|
|
return False
|
|
return True
|
|
|
|
|
|
@bp.route('/action/add_host_override', methods=['POST'])
|
|
@require_level('administrator')
|
|
def add_host_override():
|
|
description = sanitize.text(request.form.get('description', ''))
|
|
host = sanitize.hostname(request.form.get('host', ''))
|
|
ip = sanitize.ip(request.form.get('ip', ''))
|
|
|
|
if not host or not ip:
|
|
flash('Hostname and IP address are required.', 'error')
|
|
return redirect(VIEW)
|
|
|
|
if not _hash_ok():
|
|
return redirect(VIEW)
|
|
|
|
core = load_core()
|
|
core.setdefault('host_overrides', []).append({
|
|
'description': description,
|
|
'host': host,
|
|
'ip': ip,
|
|
'enabled': True,
|
|
})
|
|
save_core(core)
|
|
|
|
flash(apply_msg(), 'success')
|
|
return redirect(VIEW)
|
|
|
|
|
|
@bp.route('/action/toggle_host_override', methods=['POST'])
|
|
@require_level('administrator')
|
|
def toggle_host_override():
|
|
idx = _row_index()
|
|
if idx is None:
|
|
flash('Invalid request.', 'error')
|
|
return redirect(VIEW)
|
|
|
|
if not _hash_ok():
|
|
return redirect(VIEW)
|
|
|
|
core = load_core()
|
|
items = core.get('host_overrides', [])
|
|
if idx < 0 or idx >= len(items):
|
|
flash('Entry not found.', 'error')
|
|
return redirect(VIEW)
|
|
|
|
items[idx]['enabled'] = not items[idx].get('enabled', True)
|
|
save_core(core)
|
|
|
|
flash(apply_msg(), 'success')
|
|
return redirect(VIEW)
|
|
|
|
|
|
@bp.route('/action/edit_host_override', methods=['POST'])
|
|
@require_level('administrator')
|
|
def edit_host_override():
|
|
idx = _row_index()
|
|
if idx is None:
|
|
flash('Invalid request.', 'error')
|
|
return redirect(VIEW)
|
|
|
|
description = sanitize.text(request.form.get('description', ''))
|
|
host = sanitize.hostname(request.form.get('host', ''))
|
|
ip = sanitize.ip(request.form.get('ip', ''))
|
|
|
|
if not host or not ip:
|
|
flash('Hostname and IP address are required.', 'error')
|
|
return redirect(VIEW)
|
|
|
|
if not _hash_ok():
|
|
return redirect(VIEW)
|
|
|
|
core = load_core()
|
|
items = core.get('host_overrides', [])
|
|
if idx < 0 or idx >= len(items):
|
|
flash('Entry not found.', 'error')
|
|
return redirect(VIEW)
|
|
|
|
items[idx].update({'description': description, 'host': host, 'ip': ip})
|
|
save_core(core)
|
|
|
|
flash(apply_msg(), 'success')
|
|
return redirect(VIEW)
|
|
|
|
|
|
@bp.route('/action/delete_host_override', methods=['POST'])
|
|
@require_level('administrator')
|
|
def delete_host_override():
|
|
idx = _row_index()
|
|
if idx is None:
|
|
flash('Invalid request.', 'error')
|
|
return redirect(VIEW)
|
|
|
|
if not _hash_ok():
|
|
return redirect(VIEW)
|
|
|
|
core = load_core()
|
|
items = core.get('host_overrides', [])
|
|
if idx < 0 or idx >= len(items):
|
|
flash('Entry not found.', 'error')
|
|
return redirect(VIEW)
|
|
|
|
removed = items.pop(idx)
|
|
save_core(core)
|
|
|
|
flash(apply_msg(), 'success')
|
|
return redirect(VIEW)
|