2021-09-08 22:01:10 +00:00
|
|
|
import logging
|
2022-05-26 12:50:20 +00:00
|
|
|
from app.classes.controllers.servers_controller import ServersController
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
from app.classes.models.server_permissions import (
|
2022-04-14 02:10:25 +00:00
|
|
|
PermissionsServers,
|
|
|
|
EnumPermissionsServer,
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-04-14 02:10:25 +00:00
|
|
|
from app.classes.models.users import HelperUsers, ApiKeys
|
|
|
|
from app.classes.models.roles import HelperRoles
|
|
|
|
from app.classes.models.servers import HelperServers
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
class ServerPermsController:
|
2022-01-26 01:45:30 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_server_user_list(server_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
return PermissionsServers.get_server_user_list(server_id)
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2024-04-06 17:42:41 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_permissions(permissions_mask):
|
|
|
|
return PermissionsServers.get_permissions(permissions_mask)
|
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def list_defined_permissions():
|
2022-04-14 02:10:25 +00:00
|
|
|
permissions_list = PermissionsServers.get_permissions_list()
|
2021-09-08 22:01:10 +00:00
|
|
|
return permissions_list
|
2021-09-28 21:48:54 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_mask_permissions(role_id, server_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
permissions_mask = PermissionsServers.get_permissions_mask(role_id, server_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
return permissions_mask
|
2021-09-28 21:48:54 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
2022-05-17 22:09:58 +00:00
|
|
|
def get_role_permissions_dict(role_id):
|
|
|
|
return PermissionsServers.get_role_permissions_dict(role_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2021-09-08 22:20:38 +00:00
|
|
|
@staticmethod
|
|
|
|
def add_role_server(server_id, role_id, rs_permissions="00000000"):
|
2022-04-14 02:10:25 +00:00
|
|
|
return PermissionsServers.add_role_server(server_id, role_id, rs_permissions)
|
2021-09-08 22:20:38 +00:00
|
|
|
|
2021-11-29 21:22:46 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_server_roles(server_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
return PermissionsServers.get_server_roles(server_id)
|
2021-11-29 21:22:46 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def backup_role_swap(old_server_id, new_server_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
role_list = PermissionsServers.get_server_roles(old_server_id)
|
2021-11-29 21:22:46 +00:00
|
|
|
for role in role_list:
|
2022-04-14 02:10:25 +00:00
|
|
|
PermissionsServers.add_role_server(
|
2022-03-23 02:50:12 +00:00
|
|
|
new_server_id,
|
|
|
|
role.role_id,
|
2022-04-14 02:10:25 +00:00
|
|
|
PermissionsServers.get_permissions_mask(
|
2024-03-24 21:55:58 +00:00
|
|
|
int(role.role_id), old_server_id
|
2022-03-23 02:50:12 +00:00
|
|
|
),
|
|
|
|
)
|
2022-04-11 10:14:32 +00:00
|
|
|
# Permissions_Servers.add_role_server(
|
|
|
|
# new_server_id, role.role_id, "00001000"
|
|
|
|
# )
|
2022-03-23 02:50:12 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Servers Permissions Methods
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_permissions_mask(role_id, server_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
return PermissionsServers.get_permissions_mask(role_id, server_id)
|
2021-09-28 21:48:54 +00:00
|
|
|
|
2024-04-06 17:42:41 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_lowest_api_perm_mask(user_server_permissions_mask, api_key_permssions_mask):
|
|
|
|
mask = ""
|
|
|
|
# If this isn't an API key we'll know the request came from basic
|
|
|
|
# authentication and ignore the API key permissions mask.
|
|
|
|
if not api_key_permssions_mask:
|
|
|
|
return user_server_permissions_mask
|
|
|
|
for _index, (user_perm, api_perm) in enumerate(
|
|
|
|
zip(user_server_permissions_mask, api_key_permssions_mask)
|
|
|
|
):
|
|
|
|
if user_perm == "1" and api_perm == "1":
|
|
|
|
mask += "1"
|
|
|
|
else:
|
|
|
|
mask += "0"
|
|
|
|
return mask
|
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
2022-03-23 02:50:12 +00:00
|
|
|
def set_permission(
|
2022-04-14 02:10:25 +00:00
|
|
|
permission_mask, permission_tested: EnumPermissionsServer, value
|
2022-03-23 02:50:12 +00:00
|
|
|
):
|
2022-04-14 02:10:25 +00:00
|
|
|
return PermissionsServers.set_permission(
|
2022-03-23 02:50:12 +00:00
|
|
|
permission_mask, permission_tested, value
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-01-15 00:23:50 +00:00
|
|
|
def get_user_id_permissions_list(user_id: str, server_id: str):
|
2022-04-14 02:10:25 +00:00
|
|
|
return PermissionsServers.get_user_id_permissions_list(user_id, server_id)
|
2022-01-15 00:23:50 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_api_key_id_permissions_list(key_id: str, server_id: str):
|
2022-04-14 02:10:25 +00:00
|
|
|
key = HelperUsers.get_user_api_key(key_id)
|
|
|
|
return PermissionsServers.get_api_key_permissions_list(key, server_id)
|
2022-01-15 00:23:50 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_api_key_permissions_list(key: ApiKeys, server_id: str):
|
2022-04-14 02:10:25 +00:00
|
|
|
return PermissionsServers.get_api_key_permissions_list(key, server_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2024-04-06 17:42:41 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_user_permissions_mask(user_id: str, server_id: str):
|
|
|
|
user = HelperUsers.get_user_model(user_id)
|
|
|
|
return PermissionsServers.get_user_permissions_mask(user, server_id)
|
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_authorized_servers_stats_from_roles(user_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
user_roles = HelperUsers.get_user_roles_id(user_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
roles_list = []
|
|
|
|
role_server = []
|
|
|
|
authorized_servers = []
|
|
|
|
server_data = []
|
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
for user in user_roles:
|
|
|
|
roles_list.append(HelperRoles.get_role(user.role_id))
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
for role in roles_list:
|
|
|
|
role_test = PermissionsServers.get_role_servers_from_role_id(
|
|
|
|
role.get("role_id")
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-04-14 02:10:25 +00:00
|
|
|
for test in role_test:
|
|
|
|
role_server.append(test)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
for server in role_server:
|
|
|
|
authorized_servers.append(
|
|
|
|
HelperServers.get_server_data_by_id(server.server_id)
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
for server in authorized_servers:
|
2022-05-30 20:05:36 +00:00
|
|
|
srv = ServersController().get_server_instance_by_id(server.get("server_id"))
|
2022-05-26 12:50:20 +00:00
|
|
|
latest = srv.stats_helper.get_latest_server_stats()
|
2022-03-23 02:50:12 +00:00
|
|
|
server_data.append(
|
2022-04-14 02:10:25 +00:00
|
|
|
{
|
|
|
|
"server_data": server,
|
2022-05-26 13:43:24 +00:00
|
|
|
"stats": latest,
|
2022-04-14 02:10:25 +00:00
|
|
|
}
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
return server_data
|