v7.3.0.0: Refactor check_user_group_for_flask function and improve error handling

Introduced an optional parameter in check_user_group_for_flask function to accept API tokens and return if it is provided. Enhanced error handling in get_all_statuses function in api_funct.py to catch and inform specific exceptions instead of returning a generic error message. Minor cleanups and import optimizations in other modules.
pull/387/head
Aidaho 2024-06-06 08:53:11 +03:00
parent 7d47e94160
commit be262b0e88
5 changed files with 18 additions and 12 deletions

View File

@ -226,21 +226,25 @@ def get_status(server_id, service):
def get_all_statuses():
data = {}
try:
servers = server_sql.select_servers()
token = request.headers.get('token')
login, group_id, role_id = user_sql.get_username_group_id_from_api_token(token)
sock_port = sql.get_setting('haproxy_sock_port')
for _s in servers:
servers = roxywi_common.get_dick_permit(username=login, group_id=group_id, token=token)
except Exception as e:
data = {"error": f"Cannot parameters: {e}"}
return dict(error=data)
try:
servers = roxywi_common.get_dick_permit(username=login, group_id=group_id, token=token)
except Exception as e:
data = {"error": f"Cannot get the server: {e}"}
return dict(error=data)
try:
for s in servers:
cmd = 'echo "show info" |nc %s %s -w 1|grep -e "Ver\|CurrConns\|Maxco\|MB\|Uptime:"' % (s[2], sock_port)
data[s[2]] = {}
out = server_mod.subprocess_execute(cmd)
data[s[2]] = return_dict_from_out(s[1], out[0])
except Exception:
data = {"error": "Cannot find the server"}
except Exception as e:
data = {"error": f"Cannot find the server: {e}"}
return dict(error=data)
return dict(status=data)

View File

@ -59,4 +59,4 @@ def update_keep_alive_restart(server_id: int, service: str, restarted: int) -> N
try:
query.execute()
except Exception as e:
out_error(e)
out_error(e)

View File

@ -1,4 +1,4 @@
from app.modules.db.db_model import mysql_enable, connect, Server, UDPBalancer
from app.modules.db.db_model import UDPBalancer
from app.modules.db.common import out_error

View File

@ -38,7 +38,9 @@ def get_user_group(**kwargs) -> int:
return user_group
def check_user_group_for_flask():
def check_user_group_for_flask(**kwargs) -> bool:
if kwargs.get('api_token') is not None:
return True
user_uuid = request.cookies.get('uuid')
group_id = request.cookies.get('group')
user_id = user_sql.get_user_id_by_uuid(user_uuid)
@ -182,6 +184,7 @@ def keep_action_history(service: str, action: str, server_ip: str, login: str, u
def get_dick_permit(**kwargs):
api_token = kwargs.get('token')
if not kwargs.get('group_id'):
try:
group_id = get_user_group(id=1)
@ -190,7 +193,7 @@ def get_dick_permit(**kwargs):
else:
group_id = kwargs.pop('group_id')
if check_user_group_for_flask():
if check_user_group_for_flask(api_token=api_token):
try:
servers = server_sql.get_dick_permit(group_id, **kwargs)
except Exception as e:

View File

@ -147,4 +147,3 @@ def install_udp():
return service_mod.run_ansible(inv, server_ips, f'udp'), 201
except Exception as e:
return f'{e}'