Adding Crafty Permissions

This commit is contained in:
Silversthorn 2021-08-22 11:57:02 +02:00
parent 540cdf6149
commit 331f3e7b12
4 changed files with 101 additions and 25 deletions

View File

@ -12,7 +12,7 @@ from distutils import dir_util
from app.classes.shared.helpers import helper from app.classes.shared.helpers import helper
from app.classes.shared.console import console from app.classes.shared.console import console
from app.classes.shared.models import db_helper, permissions from app.classes.shared.models import db_helper, server_permissions
from app.classes.shared.server import Server from app.classes.shared.server import Server
from app.classes.minecraft.server_props import ServerProps from app.classes.minecraft.server_props import ServerProps
@ -112,7 +112,7 @@ class Controller:
@staticmethod @staticmethod
def list_defined_permissions(): def list_defined_permissions():
permissions_list = permissions.get_permissions_list() permissions_list = server_permissions.get_permissions_list()
return permissions_list return permissions_list
@staticmethod @staticmethod

View File

@ -141,6 +141,14 @@ class Role_Servers(Model):
primary_key = CompositeKey('role_id', 'server_id') primary_key = CompositeKey('role_id', 'server_id')
database = database database = database
class User_Crafty(Model):
user_id = ForeignKeyField(Users, backref='users_crafty')
permissions = CharField(default="00000000")
limit_server_creation = IntegerField(default=-1)
class Meta:
table_name = 'user_crafty'
database = database
class Server_Stats(Model): class Server_Stats(Model):
stats_id = AutoField() stats_id = AutoField()
@ -408,7 +416,7 @@ class db_shortcuts:
latest = Server_Stats.select().where(Server_Stats.server_id == s.get('server_id')).order_by( latest = Server_Stats.select().where(Server_Stats.server_id == s.get('server_id')).order_by(
Server_Stats.created.desc()).limit(1) Server_Stats.created.desc()).limit(1)
user_permissions = db_helper.get_user_permissions_list(user_id, s.get('server_id')) user_permissions = db_helper.get_user_permissions_list(user_id, s.get('server_id'))
if Enum_Permissions.Commands in user_permissions: if Enum_Permissions_Server.Commands in user_permissions:
user_command_permission = True user_command_permission = True
else: else:
user_command_permission = False user_command_permission = False
@ -443,7 +451,7 @@ class db_shortcuts:
permissions_mask = '' permissions_mask = ''
role_server = Role_Servers.select().where(Role_Servers.role_id == role_id).execute() role_server = Role_Servers.select().where(Role_Servers.role_id == role_id).execute()
permissions_mask = role_server[0].permissions permissions_mask = role_server[0].permissions
permissions_list = permissions.get_permissions(permissions_mask) permissions_list = server_permissions.get_permissions(permissions_mask)
return permissions_list return permissions_list
@staticmethod @staticmethod
@ -453,12 +461,12 @@ class db_shortcuts:
user = db_helper.get_user(user_id) user = db_helper.get_user(user_id)
if user['superuser'] == True: if user['superuser'] == True:
permissions_list = permissions.get_permissions_list() permissions_list = server_permissions.get_permissions_list()
else: else:
roles_list = db_helper.get_user_roles_id(user_id) roles_list = db_helper.get_user_roles_id(user_id)
role_server = Role_Servers.select().where(Role_Servers.role_id.in_(roles_list)).where(Role_Servers.server_id == int(server_id)).execute() role_server = Role_Servers.select().where(Role_Servers.role_id.in_(roles_list)).where(Role_Servers.server_id == int(server_id)).execute()
permissions_mask = role_server[0].permissions permissions_mask = role_server[0].permissions
permissions_list = permissions.get_permissions(permissions_mask) permissions_list = server_permissions.get_permissions(permissions_mask)
return permissions_list return permissions_list
@staticmethod @staticmethod
@ -989,7 +997,7 @@ class db_shortcuts:
b = Backups.create(**conf) b = Backups.create(**conf)
logger.debug("Creating new backup record.") logger.debug("Creating new backup record.")
class Enum_Permissions(Enum): class Enum_Permissions_Server(Enum):
Commands = 0 Commands = 0
Terminal = 1 Terminal = 1
Logs = 2 Logs = 2
@ -1004,36 +1012,77 @@ class Permissions_Servers:
@staticmethod @staticmethod
def get_permissions_list(): def get_permissions_list():
permissions_list = [] permissions_list = []
for member in Enum_Permissions.__members__.items(): for member in Enum_Permissions_Server.__members__.items():
permissions_list.append(member[1]) permissions_list.append(member[1])
return permissions_list return permissions_list
@staticmethod @staticmethod
def get_permissions(permissions_mask): def get_permissions(permissions_mask):
permissions_list = [] permissions_list = []
for member in Enum_Permissions.__members__.items(): for member in Enum_Permissions_Server.__members__.items():
if permissions.has_permission(permissions_mask, member[1]): if server_permissions.has_permission(permissions_mask, member[1]):
permissions_list.append(member[1]) permissions_list.append(member[1])
return permissions_list return permissions_list
@staticmethod @staticmethod
def has_permission(permission_mask, permission_tested): def has_permission(permission_mask, permission_tested: Enum_Permissions_Server):
result = False result = False
if permission_mask[permission_tested.value] == '1': if permission_mask[permission_tested.value] == '1':
result = True result = True
return result return result
@staticmethod @staticmethod
def set_permission(permission_mask, permission_tested, value): def set_permission(permission_mask, permission_tested: Enum_Permissions_Server, value):
l = list(permission_mask) l = list(permission_mask)
l[permission_tested.value] = str(value) l[permission_tested.value] = str(value)
permission_mask = ''.join(l) permission_mask = ''.join(l)
return permission_mask return permission_mask
@staticmethod @staticmethod
def get_permission(permission_mask, permission_tested): def get_permission(permission_mask, permission_tested: Enum_Permissions_Server):
return permission_mask[permission_tested.value] return permission_mask[permission_tested.value]
class Enum_Permissions_Crafty(Enum):
Server_Creation = 0
User_Config = 1
Roles_Config = 2
class Permissions_Crafty:
@staticmethod
def get_permissions_list():
permissions_list = []
for member in Enum_Permissions_Crafty.__members__.items():
permissions_list.append(member[1])
return permissions_list
@staticmethod
def get_permissions(permissions_mask):
permissions_list = []
for member in Enum_Permissions_Crafty.__members__.items():
if server_permissions.has_permission(permissions_mask, member[1]):
permissions_list.append(member[1])
return permissions_list
@staticmethod
def has_permission(permission_mask, permission_tested: Enum_Permissions_Crafty):
result = False
if permission_mask[permission_tested.value] == '1':
result = True
return result
@staticmethod
def set_permission(permission_mask, permission_tested: Enum_Permissions_Crafty, value):
l = list(permission_mask)
l[permission_tested.value] = str(value)
permission_mask = ''.join(l)
return permission_mask
@staticmethod
def get_permission(permission_mask, permission_tested: Enum_Permissions_Crafty):
return permission_mask[permission_tested.value]
installer = db_builder() installer = db_builder()
db_helper = db_shortcuts() db_helper = db_shortcuts()
permissions = Permissions_Servers() server_permissions = Permissions_Servers()
crafty_permissions = Permissions_Crafty()

View File

@ -12,7 +12,7 @@ from tornado import iostream
from app.classes.shared.console import console from app.classes.shared.console import console
from app.classes.shared.models import Users, installer from app.classes.shared.models import Users, installer
from app.classes.web.base_handler import BaseHandler from app.classes.web.base_handler import BaseHandler
from app.classes.shared.models import db_helper, permissions, Servers, Enum_Permissions from app.classes.shared.models import db_helper, server_permissions, Servers, Enum_Permissions_Server
from app.classes.shared.helpers import helper from app.classes.shared.helpers import helper
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -157,14 +157,14 @@ class PanelHandler(BaseHandler):
page_data['get_players'] = lambda: self.controller.stats.get_server_players(server_id) page_data['get_players'] = lambda: self.controller.stats.get_server_players(server_id)
page_data['active_link'] = subpage page_data['active_link'] = subpage
page_data['permissions'] = { page_data['permissions'] = {
'Commands': Enum_Permissions.Commands, 'Commands': Enum_Permissions_Server.Commands,
'Terminal': Enum_Permissions.Terminal, 'Terminal': Enum_Permissions_Server.Terminal,
'Logs': Enum_Permissions.Logs, 'Logs': Enum_Permissions_Server.Logs,
'Schedule': Enum_Permissions.Schedule, 'Schedule': Enum_Permissions_Server.Schedule,
'Backup': Enum_Permissions.Backup, 'Backup': Enum_Permissions_Server.Backup,
'Files': Enum_Permissions.Files, 'Files': Enum_Permissions_Server.Files,
'Config': Enum_Permissions.Config, 'Config': Enum_Permissions_Server.Config,
'Players': Enum_Permissions.Players, 'Players': Enum_Permissions_Server.Players,
} }
page_data['user_permissions'] = self.controller.get_server_permissions_foruser(exec_user_id, server_id) page_data['user_permissions'] = self.controller.get_server_permissions_foruser(exec_user_id, server_id)
@ -754,7 +754,7 @@ class PanelHandler(BaseHandler):
) )
)) ))
if argument: if argument:
permissions_mask = permissions.set_permission(permissions_mask, permission, argument) permissions_mask = server_permissions.set_permission(permissions_mask, permission, argument)
role_data = { role_data = {
"role_name": role_name, "role_name": role_name,
@ -802,7 +802,7 @@ class PanelHandler(BaseHandler):
) )
)) ))
if argument: if argument:
permissions_mask = permissions.set_permission(permissions_mask, permission, argument) permissions_mask = server_permissions.set_permission(permissions_mask, permission, argument)
role_id = db_helper.add_role(role_name) role_id = db_helper.add_role(role_name)
db_helper.update_role(role_id, {"servers": servers}, permissions_mask) db_helper.update_role(role_id, {"servers": servers}, permissions_mask)

View File

@ -0,0 +1,27 @@
# Generated by database migrator
from peewee import *
from app.classes.shared.models import Users
def migrate(migrator, database, **kwargs):
db = database
class User_Crafty(Model):
user_id = ForeignKeyField(Users, backref='users_crafty')
permissions = CharField(default="00000000")
limit_server_creation = IntegerField(default=-1)
class Meta:
table_name = 'user_crafty'
database = db
migrator.create_table(User_Crafty)
"""
Write your migrations here.
"""
def rollback(migrator, database, **kwargs):
migrator.drop_table('user_crafty') # Can be model class OR table name
"""
Write your rollback migrations here.
"""