2021-09-08 22:01:10 +00:00
|
|
|
import logging
|
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
from app.classes.models.roles import HelperRoles
|
|
|
|
from app.classes.models.server_permissions import PermissionsServers
|
2022-04-11 05:23:55 +00:00
|
|
|
from app.classes.shared.helpers import Helpers
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2021-09-29 19:46:11 +00:00
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
class RolesController:
|
2022-04-12 21:58:59 +00:00
|
|
|
def __init__(self, users_helper, roles_helper):
|
2022-04-11 05:23:55 +00:00
|
|
|
self.users_helper = users_helper
|
2022-04-12 21:58:59 +00:00
|
|
|
self.roles_helper = roles_helper
|
2022-04-11 10:08:36 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_all_roles():
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperRoles.get_all_roles()
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_roleid_by_name(role_name):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperRoles.get_roleid_by_name(role_name)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_role(role_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperRoles.get_role(role_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-03-23 02:50:12 +00:00
|
|
|
def update_role(role_id: str, role_data=None, permissions_mask: str = "00000000"):
|
2022-01-15 00:23:50 +00:00
|
|
|
if role_data is None:
|
|
|
|
role_data = {}
|
2022-04-14 02:10:25 +00:00
|
|
|
base_data = RolesController.get_role_with_servers(role_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
up_data = {}
|
|
|
|
added_servers = set()
|
|
|
|
removed_servers = set()
|
|
|
|
for key in role_data:
|
|
|
|
if key == "role_id":
|
|
|
|
continue
|
|
|
|
elif key == "servers":
|
2022-03-23 02:50:12 +00:00
|
|
|
added_servers = role_data["servers"].difference(base_data["servers"])
|
|
|
|
removed_servers = base_data["servers"].difference(role_data["servers"])
|
2021-09-08 22:01:10 +00:00
|
|
|
elif base_data[key] != role_data[key]:
|
|
|
|
up_data[key] = role_data[key]
|
2022-04-11 05:23:55 +00:00
|
|
|
up_data["last_update"] = Helpers.get_time_as_string()
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.debug(
|
|
|
|
f"role: {role_data} +server:{added_servers} -server{removed_servers}"
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
for server in added_servers:
|
2022-04-14 02:10:25 +00:00
|
|
|
PermissionsServers.get_or_create(role_id, server, permissions_mask)
|
2022-03-23 02:50:12 +00:00
|
|
|
for server in base_data["servers"]:
|
2022-04-14 02:10:25 +00:00
|
|
|
PermissionsServers.update_role_permission(role_id, server, permissions_mask)
|
2022-03-23 06:06:13 +00:00
|
|
|
# TODO: This is horribly inefficient and we should be using bulk queries
|
|
|
|
# but im going for functionality at this point
|
2022-04-14 02:10:25 +00:00
|
|
|
PermissionsServers.delete_roles_permissions(role_id, removed_servers)
|
2021-09-08 22:01:10 +00:00
|
|
|
if up_data:
|
2022-04-14 02:10:25 +00:00
|
|
|
HelperRoles.update_role(role_id, up_data)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def add_role(role_name):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperRoles.add_role(role_name)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def remove_role(self, role_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
role_data = RolesController.get_role_with_servers(role_id)
|
|
|
|
PermissionsServers.delete_roles_permissions(role_id, role_data["servers"])
|
2022-04-11 05:23:55 +00:00
|
|
|
self.users_helper.remove_roles_from_role_id(role_id)
|
2022-04-12 21:58:59 +00:00
|
|
|
return self.roles_helper.remove_role(role_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def role_id_exists(role_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperRoles.role_id_exists(role_id)
|
2021-09-29 19:46:11 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_role_with_servers(role_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
role = HelperRoles.get_role(role_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
if role:
|
2022-04-14 02:10:25 +00:00
|
|
|
servers_query = PermissionsServers.get_servers_from_role(role_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
# TODO: this query needs to be narrower
|
|
|
|
servers = set()
|
|
|
|
for s in servers_query:
|
|
|
|
servers.add(s.server_id.server_id)
|
2022-03-23 02:50:12 +00:00
|
|
|
role["servers"] = servers
|
|
|
|
# logger.debug("role: ({}) {}".format(role_id, role))
|
2021-09-08 22:01:10 +00:00
|
|
|
return role
|
|
|
|
else:
|
2022-03-23 02:50:12 +00:00
|
|
|
# logger.debug("role: ({}) {}".format(role_id, {}))
|
2022-01-26 01:45:30 +00:00
|
|
|
return {}
|