Improve server permission related functions

This commit is contained in:
luukas 2022-05-16 18:01:15 +03:00
parent cd87a63f1e
commit d83b964267

View File

@ -51,18 +51,17 @@ class PermissionsServers:
@staticmethod
def get_permissions_list():
permissions_list = []
for member in EnumPermissionsServer.__members__.items():
permissions_list.append(member[1])
return permissions_list
return [
permission for _, permission in EnumPermissionsServer.__members__.items()
]
@staticmethod
def get_permissions(permissions_mask):
permissions_list = []
for member in EnumPermissionsServer.__members__.items():
if PermissionsServers.has_permission(permissions_mask, member[1]):
permissions_list.append(member[1])
return permissions_list
return [
permission
for _, permission in EnumPermissionsServer.__members__.items()
if PermissionsServers.has_permission(permissions_mask, permission)
]
@staticmethod
def has_permission(permission_mask, permission_tested: EnumPermissionsServer):
@ -147,23 +146,18 @@ class PermissionsServers:
@staticmethod
def get_role_permissions_list(role_id):
permissions_mask = "00000000"
role_server = RoleServers.get_or_none(RoleServers.role_id == role_id)
if role_server is not None:
permissions_mask = role_server.permissions
permissions_mask = (
"00000000" if role_server is None else role_server.permissions
)
permissions_list = PermissionsServers.get_permissions(permissions_mask)
return permissions_list
@staticmethod
def update_role_permission(role_id, server_id, permissions_mask):
role_server = (
RoleServers.select()
.where(RoleServers.role_id == role_id)
.where(RoleServers.server_id == server_id)
.get()
)
role_server.permissions = permissions_mask
RoleServers.save(role_server)
RoleServers.update(permissions=permissions_mask).where(
RoleServers.role_id == role_id, RoleServers.server_id == server_id
).execute()
@staticmethod
def delete_roles_permissions(role_id, removed_servers=None):