mirror of https://github.com/Aidaho12/haproxy-wi
v7.3.2.0: Enhance user parameter handling in various routes
Added decorator `@get_user_params()` to various route handlers in `routes.py`. Adjusted user handling in `config.py` by eliminating checks for 'login' and directly accessing user parameters from Flask's global variable `g`. Also moved user retrieval process from `routes.py` to lower level operations allowing for better abstraction and less redundancy.pull/390/head
parent
9119787aa1
commit
9a193e43f9
|
@ -2,7 +2,7 @@ import os
|
|||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from flask import render_template, request
|
||||
from flask import render_template, request, g
|
||||
|
||||
import app.modules.db.sql as sql
|
||||
import app.modules.db.user as user_sql
|
||||
|
@ -203,6 +203,8 @@ def upload_and_restart(server_ip: str, cfg: str, just_save: str, service: str, *
|
|||
:return: Error message or service title
|
||||
|
||||
"""
|
||||
user_id = g.user_params['user_id']
|
||||
user = user_sql.get_user_id(user_id)
|
||||
file_format = config_common.get_file_format(service)
|
||||
config_path = kwargs.get('config_file_name')
|
||||
config_date = get_date.return_date('config')
|
||||
|
@ -216,28 +218,27 @@ def upload_and_restart(server_ip: str, cfg: str, just_save: str, service: str, *
|
|||
|
||||
common.check_is_conf(config_path)
|
||||
|
||||
login = kwargs.get('login') if kwargs.get('login') else 1
|
||||
tmp_file = f"{sql.get_setting('tmp_config_path')}/{config_date}.{file_format}"
|
||||
|
||||
try:
|
||||
os.system(f"dos2unix -q {cfg}")
|
||||
except OSError as e:
|
||||
roxywi_common.handle_exceptions(e, 'Roxy-WI server', 'There is no dos2unix')
|
||||
roxywi_common.handle_exceptions(e, 'Roxy-WI server', 'There is no dos2unix', login=user.username)
|
||||
|
||||
try:
|
||||
upload(server_ip, tmp_file, cfg)
|
||||
except Exception as e:
|
||||
roxywi_common.handle_exceptions(e, 'Roxy-WI server', 'Cannot upload config', login=login)
|
||||
roxywi_common.handle_exceptions(e, 'Roxy-WI server', 'Cannot upload config', login=user.username)
|
||||
|
||||
try:
|
||||
if just_save != 'test':
|
||||
roxywi_common.logging(server_ip, 'A new config file has been uploaded', login=login, keep_history=1, service=service)
|
||||
roxywi_common.logging(server_ip, 'A new config file has been uploaded', login=user.username, keep_history=1, service=service)
|
||||
except Exception as e:
|
||||
roxywi_common.logging('Roxy-WI server', str(e), roxywi=1)
|
||||
|
||||
# If master then save a version of config in a new way
|
||||
if not kwargs.get('slave') and service != 'waf':
|
||||
_create_config_version(server_id, server_ip, service, config_path, kwargs.get('login'), cfg, kwargs.get('oldcfg'), tmp_file)
|
||||
_create_config_version(server_id, server_ip, service, config_path, user.username, cfg, kwargs.get('oldcfg'), tmp_file)
|
||||
|
||||
try:
|
||||
commands = _generate_command(service, server_id, just_save, config_path, tmp_file, cfg, server_ip)
|
||||
|
@ -251,7 +252,7 @@ def upload_and_restart(server_ip: str, cfg: str, just_save: str, service: str, *
|
|||
|
||||
try:
|
||||
if just_save in ('reload', 'restart'):
|
||||
roxywi_common.logging(server_ip, f'Service has been {just_save}ed', login=login, keep_history=1, service=service)
|
||||
roxywi_common.logging(server_ip, f'Service has been {just_save}ed', login=user.username, keep_history=1, service=service)
|
||||
except Exception as e:
|
||||
roxywi_common.logging('Roxy-WI server', str(e), roxywi=1)
|
||||
|
||||
|
@ -280,11 +281,6 @@ def master_slave_upload_and_restart(server_ip: str, cfg: str, just_save: str, se
|
|||
waf = kwargs.get('waf')
|
||||
server_name = server_sql.get_hostname_by_server_ip(server_ip)
|
||||
|
||||
if kwargs.get('login'):
|
||||
login = kwargs.get('login')
|
||||
else:
|
||||
login = ''
|
||||
|
||||
for master in masters:
|
||||
if master[0] is not None:
|
||||
try:
|
||||
|
@ -296,7 +292,7 @@ def master_slave_upload_and_restart(server_ip: str, cfg: str, just_save: str, se
|
|||
slave_output += f'<br>slave_server:\n error: {e}'
|
||||
try:
|
||||
output = upload_and_restart(
|
||||
server_ip, cfg, just_save, service, waf=waf, config_file_name=config_file_name, oldcfg=old_cfg, login=login
|
||||
server_ip, cfg, just_save, service, waf=waf, config_file_name=config_file_name, oldcfg=old_cfg
|
||||
)
|
||||
except Exception as e:
|
||||
output = f'error: {e}'
|
||||
|
|
|
@ -70,6 +70,7 @@ def add(service):
|
|||
|
||||
|
||||
@bp.route('/haproxy/add', methods=['POST'])
|
||||
@get_user_params()
|
||||
def add_haproxy():
|
||||
"""
|
||||
Add HAProxy sections
|
||||
|
@ -379,6 +380,7 @@ def add_haproxy():
|
|||
|
||||
|
||||
@bp.post('/haproxy/userlist')
|
||||
@get_user_params()
|
||||
def add_userlist():
|
||||
"""
|
||||
Add HAProxy section userlist
|
||||
|
@ -617,6 +619,7 @@ def lets():
|
|||
|
||||
|
||||
@bp.post('/nginx/upstream')
|
||||
@get_user_params()
|
||||
def add_nginx_upstream():
|
||||
roxywi_auth.page_for_admin(level=3)
|
||||
|
||||
|
|
|
@ -141,8 +141,6 @@ def save_config(service, server_ip):
|
|||
oldcfg = request.form.get('oldconfig')
|
||||
save = request.form.get('save')
|
||||
config_file_name = request.form.get('config_file_name')
|
||||
user_id = g.user_params['user_id']
|
||||
user = user_sql.get_user_id(user_id)
|
||||
|
||||
try:
|
||||
cfg = config_mod.return_cfg(service, server_ip, config_file_name)
|
||||
|
@ -160,7 +158,7 @@ def save_config(service, server_ip):
|
|||
stderr = config_mod.upload_and_restart(server_ip, cfg, save, service, oldcfg=oldcfg)
|
||||
else:
|
||||
stderr = config_mod.master_slave_upload_and_restart(server_ip, cfg, save, service, oldcfg=oldcfg,
|
||||
config_file_name=config_file_name, login=user.username)
|
||||
config_file_name=config_file_name)
|
||||
except Exception as e:
|
||||
return f'error: {e}', 200
|
||||
|
||||
|
@ -322,6 +320,7 @@ def haproxy_section_show(server_ip, section):
|
|||
|
||||
|
||||
@bp.route('/section/haproxy/<server_ip>/save', methods=['POST'])
|
||||
@get_user_params()
|
||||
def haproxy_section_save(server_ip):
|
||||
hap_configs_dir = config_common.get_config_dir('haproxy')
|
||||
cfg = config_common.generate_config_path('haproxy', server_ip)
|
||||
|
|
Loading…
Reference in New Issue