52 lines
1.8 KiB
Python
52 lines
1.8 KiB
Python
import copy
|
|
from pathlib import Path
|
|
from flask import Blueprint, request, redirect, flash
|
|
from auth import require_level
|
|
from config_utils import CONFIGS_DIR, load_config, record_group, diff_fields
|
|
|
|
_PAGE = Path(__file__).parent.name
|
|
|
|
bp = Blueprint(_PAGE, __name__)
|
|
|
|
RADIUS_SECRET_FILE = Path(CONFIGS_DIR) / '.radius-secret'
|
|
|
|
_VALID_MAC_FORMATS = {
|
|
'aabbccddeeff', 'aa-bb-cc-dd-ee-ff', 'aa:bb:cc:dd:ee:ff',
|
|
'AABBCCDDEEFF', 'AA-BB-CC-DD-EE-FF', 'AA:BB:CC:DD:EE:FF',
|
|
}
|
|
|
|
|
|
@bp.route('/action/radius/regenerate', methods=['POST'])
|
|
@require_level('administrator')
|
|
def regenerate():
|
|
try:
|
|
RADIUS_SECRET_FILE.unlink(missing_ok=True)
|
|
except OSError as ex:
|
|
flash(f'Could not delete .radius-secret: {ex}', 'error')
|
|
return redirect(f'/{_PAGE}')
|
|
flash('Secret deleted. A new secret will be generated when the pending command is applied.', 'success')
|
|
return redirect(f'/{_PAGE}')
|
|
|
|
|
|
@bp.route('/action/radius/options_save', methods=['POST'])
|
|
@require_level('administrator')
|
|
def options_save():
|
|
mac_format = request.form.get('mac_format', 'aabbccddeeff')
|
|
apply_to = request.form.get('apply_to', 'all')
|
|
logging = 'logging' in request.form
|
|
|
|
if mac_format not in _VALID_MAC_FORMATS:
|
|
flash('Invalid MAC format.', 'error')
|
|
return redirect(f'/{_PAGE}')
|
|
if apply_to not in ('all', 'wireless'):
|
|
flash('Invalid apply_to value.', 'error')
|
|
return redirect(f'/{_PAGE}')
|
|
|
|
cfg = load_config()
|
|
before = copy.deepcopy(cfg.get('radius_options', {}))
|
|
after = {'mac_format': mac_format, 'apply_to': apply_to, 'logging': logging}
|
|
cfg['radius_options'] = after
|
|
|
|
changes = diff_fields(before, after)
|
|
flash(record_group(cfg, 'radius_options', 'setting', 'radius_options', changes, 'core apply'), 'success')
|
|
return redirect(f'/{_PAGE}')
|