2021-09-08 22:01:10 +00:00
|
|
|
import logging
|
2022-05-18 10:04:38 +00:00
|
|
|
import typing as t
|
2022-04-11 05:23:55 +00:00
|
|
|
from enum import Enum
|
|
|
|
from peewee import (
|
|
|
|
ForeignKeyField,
|
|
|
|
CharField,
|
|
|
|
CompositeKey,
|
|
|
|
JOIN,
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
from app.classes.models.base_model import BaseModel
|
2021-09-08 22:01:10 +00:00
|
|
|
from app.classes.models.servers import Servers
|
|
|
|
from app.classes.models.roles import Roles
|
2022-04-14 02:10:25 +00:00
|
|
|
from app.classes.models.users import UserRoles, HelperUsers, ApiKeys, Users
|
2022-04-11 05:23:55 +00:00
|
|
|
from app.classes.shared.permission_helper import PermissionHelper
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-03-08 04:40:44 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2023-02-15 23:01:21 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Role Servers Class
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-04-14 02:10:25 +00:00
|
|
|
class RoleServers(BaseModel):
|
2022-03-23 02:50:12 +00:00
|
|
|
role_id = ForeignKeyField(Roles, backref="role_server")
|
|
|
|
server_id = ForeignKeyField(Servers, backref="role_server")
|
2021-09-08 22:01:10 +00:00
|
|
|
permissions = CharField(default="00000000")
|
|
|
|
|
|
|
|
class Meta:
|
2022-03-23 02:50:12 +00:00
|
|
|
table_name = "role_servers"
|
|
|
|
primary_key = CompositeKey("role_id", "server_id")
|
2021-09-08 22:01:10 +00:00
|
|
|
|
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 Class
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-04-14 02:10:25 +00:00
|
|
|
class EnumPermissionsServer(Enum):
|
|
|
|
COMMANDS = 0
|
|
|
|
TERMINAL = 1
|
|
|
|
LOGS = 2
|
|
|
|
SCHEDULE = 3
|
|
|
|
BACKUP = 4
|
|
|
|
FILES = 5
|
|
|
|
CONFIG = 6
|
|
|
|
PLAYERS = 7
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2021-09-29 19:40:56 +00:00
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
class PermissionsServers:
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_or_create(role_id, server, permissions_mask):
|
2022-04-14 02:10:25 +00:00
|
|
|
return RoleServers.get_or_create(
|
2022-03-23 02:50:12 +00:00
|
|
|
role_id=role_id, server_id=server, permissions=permissions_mask
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_permissions_list():
|
2022-05-17 22:55:05 +00:00
|
|
|
return list(EnumPermissionsServer.__members__.values())
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_permissions(permissions_mask):
|
2022-05-16 15:01:15 +00:00
|
|
|
return [
|
|
|
|
permission
|
2022-05-17 22:55:05 +00:00
|
|
|
for permission in EnumPermissionsServer.__members__.values()
|
2022-05-16 15:01:15 +00:00
|
|
|
if PermissionsServers.has_permission(permissions_mask, permission)
|
|
|
|
]
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-04-14 02:10:25 +00:00
|
|
|
def has_permission(permission_mask, permission_tested: EnumPermissionsServer):
|
2022-03-23 02:50:12 +00:00
|
|
|
return permission_mask[permission_tested.value] == "1"
|
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
|
|
|
):
|
2021-09-08 22:01:10 +00:00
|
|
|
list_perms = list(permission_mask)
|
|
|
|
list_perms[permission_tested.value] = str(value)
|
2022-03-23 02:50:12 +00:00
|
|
|
permission_mask = "".join(list_perms)
|
2021-09-08 22:01:10 +00:00
|
|
|
return permission_mask
|
|
|
|
|
|
|
|
@staticmethod
|
2022-04-14 02:10:25 +00:00
|
|
|
def get_permission(permission_mask, permission_tested: EnumPermissionsServer):
|
2021-09-08 22:01:10 +00:00
|
|
|
return permission_mask[permission_tested.value]
|
|
|
|
|
2022-01-15 00:23:50 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_token_permissions(permissions_mask, api_permissions_mask):
|
2022-05-17 22:55:05 +00:00
|
|
|
return [
|
|
|
|
permission
|
|
|
|
for permission in EnumPermissionsServer.__members__.values()
|
2022-04-11 05:23:55 +00:00
|
|
|
if PermissionHelper.both_have_perm(
|
2022-05-17 22:55:05 +00:00
|
|
|
permissions_mask, api_permissions_mask, permission
|
|
|
|
)
|
|
|
|
]
|
2022-01-15 00:23:50 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Role_Servers Methods
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
2022-05-07 21:07:55 +00:00
|
|
|
def get_role_servers_from_role_id(roleid: t.Union[str, int]):
|
2022-04-14 02:10:25 +00:00
|
|
|
return RoleServers.select().where(RoleServers.role_id == roleid)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-05-07 21:07:55 +00:00
|
|
|
def get_servers_from_role(role_id: t.Union[str, int]):
|
2022-03-23 02:50:12 +00:00
|
|
|
return (
|
2022-04-14 02:10:25 +00:00
|
|
|
RoleServers.select()
|
2022-03-23 02:50:12 +00:00
|
|
|
.join(Servers, JOIN.INNER)
|
2022-04-14 02:10:25 +00:00
|
|
|
.where(RoleServers.role_id == role_id)
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-05-07 21:07:55 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_server_ids_from_role(role_id: t.Union[str, int]) -> t.List[int]:
|
|
|
|
# FIXME: somehow retrieve only the server ids, not the whole servers
|
|
|
|
return [
|
|
|
|
role_servers.server_id.server_id
|
|
|
|
for role_servers in (
|
|
|
|
RoleServers.select(RoleServers.server_id).where(
|
|
|
|
RoleServers.role_id == role_id
|
|
|
|
)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
2021-09-25 22:17:06 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_roles_from_server(server_id):
|
2022-03-23 02:50:12 +00:00
|
|
|
return (
|
2022-04-14 02:10:25 +00:00
|
|
|
RoleServers.select()
|
2022-03-23 02:50:12 +00:00
|
|
|
.join(Roles, JOIN.INNER)
|
2022-04-14 02:10:25 +00:00
|
|
|
.where(RoleServers.server_id == server_id)
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2021-09-25 22:17:06 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def add_role_server(server_id, role_id, rs_permissions="00000000"):
|
2022-04-14 02:10:25 +00:00
|
|
|
servers = RoleServers.insert(
|
2022-03-23 02:50:12 +00:00
|
|
|
{
|
2022-04-14 02:10:25 +00:00
|
|
|
RoleServers.server_id: server_id,
|
|
|
|
RoleServers.role_id: role_id,
|
|
|
|
RoleServers.permissions: rs_permissions,
|
2022-03-23 02:50:12 +00:00
|
|
|
}
|
|
|
|
).execute()
|
2021-09-08 22:01:10 +00:00
|
|
|
return servers
|
2021-09-29 19:40:56 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_permissions_mask(role_id, server_id):
|
2022-03-23 02:50:12 +00:00
|
|
|
permissions_mask = ""
|
|
|
|
role_server = (
|
2022-04-14 02:10:25 +00:00
|
|
|
RoleServers.select()
|
|
|
|
.where(RoleServers.role_id == role_id)
|
|
|
|
.where(RoleServers.server_id == server_id)
|
2022-03-23 02:50:12 +00:00
|
|
|
.get()
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
permissions_mask = role_server.permissions
|
|
|
|
return permissions_mask
|
|
|
|
|
2021-11-29 21:22:46 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_server_roles(server_id):
|
|
|
|
role_list = []
|
2022-04-14 02:10:25 +00:00
|
|
|
roles = RoleServers.select().where(RoleServers.server_id == server_id).execute()
|
2021-11-29 21:22:46 +00:00
|
|
|
for role in roles:
|
|
|
|
role_list.append(role.role_id)
|
|
|
|
return role_list
|
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_role_permissions_list(role_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
role_server = RoleServers.get_or_none(RoleServers.role_id == role_id)
|
2022-05-16 15:01:15 +00:00
|
|
|
permissions_mask = (
|
|
|
|
"00000000" if role_server is None else role_server.permissions
|
|
|
|
)
|
2022-04-14 02:10:25 +00:00
|
|
|
permissions_list = PermissionsServers.get_permissions(permissions_mask)
|
2021-09-08 22:01:10 +00:00
|
|
|
return permissions_list
|
|
|
|
|
2022-05-17 22:09:58 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_role_permissions_dict(role_id):
|
|
|
|
permissions_dict: t.Dict[str, t.List[EnumPermissionsServer]] = {}
|
|
|
|
role_servers = RoleServers.select(
|
|
|
|
RoleServers.server_id, RoleServers.permissions
|
|
|
|
).where(RoleServers.role_id == role_id)
|
|
|
|
for role_server in role_servers:
|
|
|
|
permissions_dict[
|
|
|
|
role_server.server_id_id
|
|
|
|
] = PermissionsServers.get_permissions(role_server.permissions)
|
|
|
|
return permissions_dict
|
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def update_role_permission(role_id, server_id, permissions_mask):
|
2022-05-16 15:01:15 +00:00
|
|
|
RoleServers.update(permissions=permissions_mask).where(
|
|
|
|
RoleServers.role_id == role_id, RoleServers.server_id == server_id
|
|
|
|
).execute()
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-05-09 23:08:49 +00:00
|
|
|
def delete_roles_permissions(
|
|
|
|
role_id: t.Union[str, int], removed_servers: t.Sequence[t.Union[str, int]]
|
|
|
|
):
|
2022-03-23 02:50:12 +00:00
|
|
|
return (
|
2022-04-14 02:10:25 +00:00
|
|
|
RoleServers.delete()
|
|
|
|
.where(RoleServers.role_id == role_id)
|
|
|
|
.where(RoleServers.server_id.in_(removed_servers))
|
2022-03-23 02:50:12 +00:00
|
|
|
.execute()
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2021-09-25 21:59:01 +00:00
|
|
|
@staticmethod
|
|
|
|
def remove_roles_of_server(server_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
return RoleServers.delete().where(RoleServers.server_id == server_id).execute()
|
2021-09-25 21:59:01 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
2022-01-15 00:23:50 +00:00
|
|
|
def get_user_id_permissions_mask(user_id, server_id: str):
|
2022-04-14 02:10:25 +00:00
|
|
|
user = HelperUsers.get_user_model(user_id)
|
|
|
|
return PermissionsServers.get_user_permissions_mask(user, server_id)
|
2022-01-15 00:23:50 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_user_permissions_mask(user: Users, server_id: str):
|
|
|
|
if user.superuser:
|
2022-05-23 20:03:50 +00:00
|
|
|
permissions_mask = "1" * len(EnumPermissionsServer)
|
2022-01-15 00:23:50 +00:00
|
|
|
else:
|
2022-04-14 02:10:25 +00:00
|
|
|
roles_list = HelperUsers.get_user_roles_id(user.user_id)
|
2022-03-23 02:50:12 +00:00
|
|
|
role_server = (
|
2022-04-14 02:10:25 +00:00
|
|
|
RoleServers.select()
|
|
|
|
.where(RoleServers.role_id.in_(roles_list))
|
|
|
|
.where(RoleServers.server_id == server_id)
|
2022-03-23 02:50:12 +00:00
|
|
|
.execute()
|
|
|
|
)
|
2022-02-26 23:00:54 +00:00
|
|
|
try:
|
|
|
|
permissions_mask = role_server[0].permissions
|
|
|
|
except IndexError:
|
2022-05-23 20:03:50 +00:00
|
|
|
permissions_mask = "0" * len(EnumPermissionsServer)
|
2022-01-15 00:23:50 +00:00
|
|
|
return permissions_mask
|
|
|
|
|
2022-01-20 18:37:45 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_server_user_list(server_id):
|
|
|
|
final_users = []
|
2022-04-14 02:10:25 +00:00
|
|
|
server_roles = RoleServers.select().where(RoleServers.server_id == server_id)
|
2022-05-15 22:02:17 +00:00
|
|
|
super_users = Users.select(Users.user_id).where(
|
2022-04-02 16:54:30 +00:00
|
|
|
Users.superuser == True # pylint: disable=singleton-comparison
|
|
|
|
)
|
2022-01-20 18:37:45 +00:00
|
|
|
for role in server_roles:
|
2022-05-15 22:02:17 +00:00
|
|
|
users = UserRoles.select(UserRoles.user_id).where(
|
|
|
|
UserRoles.role_id == role.role_id
|
|
|
|
)
|
2022-01-20 18:37:45 +00:00
|
|
|
for user in users:
|
2022-05-15 22:02:17 +00:00
|
|
|
if user.user_id_id not in final_users:
|
|
|
|
final_users.append(user.user_id_id)
|
2022-01-20 18:37:45 +00:00
|
|
|
for suser in super_users:
|
|
|
|
if suser.user_id not in final_users:
|
|
|
|
final_users.append(suser.user_id)
|
|
|
|
return final_users
|
|
|
|
|
2022-01-15 00:23:50 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_user_id_permissions_list(user_id, server_id: str):
|
2022-04-14 02:10:25 +00:00
|
|
|
user = HelperUsers.get_user_model(user_id)
|
|
|
|
return PermissionsServers.get_user_permissions_list(user, server_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-01-15 00:23:50 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_user_permissions_list(user: Users, server_id: str):
|
|
|
|
if user.superuser:
|
2022-04-14 02:10:25 +00:00
|
|
|
permissions_list = PermissionsServers.get_permissions_list()
|
2021-09-08 22:01:10 +00:00
|
|
|
else:
|
2022-04-14 02:10:25 +00:00
|
|
|
permissions_mask = PermissionsServers.get_user_permissions_mask(
|
2022-03-23 02:50:12 +00:00
|
|
|
user, server_id
|
|
|
|
)
|
2022-04-14 02:10:25 +00:00
|
|
|
permissions_list = PermissionsServers.get_permissions(permissions_mask)
|
2021-09-08 22:01:10 +00:00
|
|
|
return permissions_list
|
2021-09-29 19:40:56 +00:00
|
|
|
|
2022-01-15 00:23:50 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_api_key_id_permissions_list(key_id, server_id: str):
|
|
|
|
key = ApiKeys.get(ApiKeys.token_id == key_id)
|
2022-04-14 02:10:25 +00:00
|
|
|
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
|
|
|
user = HelperUsers.get_user(key.user_id)
|
2022-04-10 19:39:31 +00:00
|
|
|
if user["superuser"] and key.superuser:
|
2022-04-14 02:10:25 +00:00
|
|
|
return PermissionsServers.get_permissions_list()
|
2022-06-14 12:40:57 +00:00
|
|
|
roles_list = HelperUsers.get_user_roles_id(user["user_id"])
|
|
|
|
role_server = (
|
|
|
|
RoleServers.select()
|
|
|
|
.where(RoleServers.role_id.in_(roles_list))
|
|
|
|
.where(RoleServers.server_id == server_id)
|
|
|
|
.execute()
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
user_permissions_mask = role_server[0].permissions
|
|
|
|
except:
|
|
|
|
if user["superuser"]:
|
|
|
|
user_permissions_mask = "11111111"
|
|
|
|
else:
|
|
|
|
user_permissions_mask = "00000000"
|
|
|
|
key_permissions_mask = key.server_permissions
|
|
|
|
permissions_mask = PermissionHelper.combine_masks(
|
|
|
|
user_permissions_mask, key_permissions_mask
|
|
|
|
)
|
|
|
|
permissions_list = PermissionsServers.get_permissions(permissions_mask)
|
|
|
|
return permissions_list
|