Fix server quotas. Removed un-used perms columns

This commit is contained in:
Andrew 2022-08-20 13:00:20 -04:00
parent 245d64ca45
commit cafa720bba
11 changed files with 117 additions and 59 deletions

View File

@ -51,6 +51,7 @@ class ServersController(metaclass=Singleton):
server_log_file: str,
server_stop: str,
server_type: str,
created_by: int,
server_port: int = 25565,
server_host: str = "127.0.0.1",
) -> int:
@ -85,6 +86,7 @@ class ServersController(metaclass=Singleton):
server_log_file,
server_stop,
server_type,
created_by,
server_port,
server_host,
)

View File

@ -9,6 +9,7 @@ from peewee import (
)
from app.classes.models.base_model import BaseModel
from app.classes.models.servers import HelperServers
from app.classes.models.users import Users, ApiKeys, HelperUsers
from app.classes.shared.permission_helper import PermissionHelper
@ -23,9 +24,6 @@ class UserCrafty(BaseModel):
limit_server_creation = IntegerField(default=-1)
limit_user_creation = IntegerField(default=0)
limit_role_creation = IntegerField(default=0)
created_server = IntegerField(default=0)
created_user = IntegerField(default=0)
created_role = IntegerField(default=0)
class Meta:
table_name = "user_crafty"
@ -107,9 +105,6 @@ class PermissionsCrafty:
UserCrafty.limit_server_creation: 0,
UserCrafty.limit_user_creation: 0,
UserCrafty.limit_role_creation: 0,
UserCrafty.created_server: 0,
UserCrafty.created_user: 0,
UserCrafty.created_role: 0,
}
).execute()
user_crafty = PermissionsCrafty.get_user_crafty(user_id)
@ -159,11 +154,16 @@ class PermissionsCrafty:
@staticmethod
def get_created_quantity_list(user_id):
user_crafty = PermissionsCrafty.get_user_crafty(user_id)
quantity_list = {
EnumPermissionsCrafty.SERVER_CREATION.name: user_crafty.created_server,
EnumPermissionsCrafty.USER_CONFIG.name: user_crafty.created_user,
EnumPermissionsCrafty.ROLES_CONFIG.name: user_crafty.created_role,
EnumPermissionsCrafty.SERVER_CREATION.name: HelperServers.get_total_owned_servers(
user_id
),
EnumPermissionsCrafty.USER_CONFIG.name: HelperUsers.get_managed_users(
user_id
).count(),
EnumPermissionsCrafty.ROLES_CONFIG.name: HelperUsers.get_managed_roles(
user_id
).count(),
}
return quantity_list
@ -183,31 +183,6 @@ class PermissionsCrafty:
or limit_list[permission.name] == -1
)
@staticmethod
def add_server_creation(user_id: int):
"""Increase the "Server Creation" counter for this user
Args:
user_id (int): The modifiable user's ID
"""
UserCrafty.update(created_server=UserCrafty.created_server + 1).where(
UserCrafty.user_id == user_id
).execute()
@staticmethod
def add_user_creation(user_id):
user_crafty = PermissionsCrafty.get_user_crafty(user_id)
user_crafty.created_user += 1
UserCrafty.save(user_crafty)
return user_crafty.created_user
@staticmethod
def add_role_creation(user_id):
user_crafty = PermissionsCrafty.get_user_crafty(user_id)
user_crafty.created_role += 1
UserCrafty.save(user_crafty)
return user_crafty.created_role
@staticmethod
def get_api_key_permissions_list(key: ApiKeys):
user = HelperUsers.get_user(key.user_id)

View File

@ -38,6 +38,7 @@ class Servers(BaseModel):
logs_delete_after = IntegerField(default=0)
type = CharField(default="minecraft-java")
show_status = BooleanField(default=1)
created_by = IntegerField(default=-100)
class Meta:
table_name = "servers"
@ -64,6 +65,7 @@ class HelperServers:
server_log_file: str,
server_stop: str,
server_type: str,
created_by: int,
server_port: int = 25565,
server_host: str = "127.0.0.1",
) -> int:
@ -105,6 +107,7 @@ class HelperServers:
Servers.stop_command: server_stop,
Servers.backup_path: backup_path,
Servers.type: server_type,
Servers.created_by: created_by,
}
).execute()
@ -112,6 +115,10 @@ class HelperServers:
def get_server_obj(server_id):
return Servers.get_by_id(server_id)
@staticmethod
def get_total_owned_servers(user_id):
return Servers.select().where(Servers.created_by == user_id).count()
@staticmethod
def get_server_type_by_id(server_id):
server_type = Servers.select().where(Servers.server_id == server_id).get()

View File

@ -244,7 +244,7 @@ class Controller:
except:
return {"percent": 0, "total_files": 0}
def create_api_server(self, data: dict):
def create_api_server(self, data: dict, user_id):
server_fs_uuid = Helpers.create_uuid()
new_server_path = os.path.join(self.helper.servers_dir, server_fs_uuid)
backup_path = os.path.join(self.helper.backup_path, server_fs_uuid)
@ -307,7 +307,9 @@ class Controller:
# TODO: Copy files from the zip file to the new server directory
server_file = create_data["jarfile"]
raise Exception("Not yet implemented")
_create_server_properties_if_needed(create_data["server_properties_port"])
_create_server_properties_if_needed(
create_data["server_properties_port"],
)
min_mem = create_data["mem_min"]
max_mem = create_data["mem_max"]
@ -403,6 +405,7 @@ class Controller:
server_log_file=log_location,
server_stop=stop_command,
server_port=monitoring_port,
created_by=user_id,
server_host=monitoring_host,
server_type=monitoring_type,
)
@ -429,6 +432,7 @@ class Controller:
min_mem: int,
max_mem: int,
port: int,
user_id: int,
):
server_id = Helpers.create_uuid()
server_dir = os.path.join(self.helper.servers_dir, server_id)
@ -489,6 +493,7 @@ class Controller:
server_log_file,
server_stop,
port,
user_id,
server_type="minecraft-java",
)
@ -524,6 +529,7 @@ class Controller:
min_mem: int,
max_mem: int,
port: int,
user_id: int,
):
server_id = Helpers.create_uuid()
new_server_dir = os.path.join(self.helper.servers_dir, server_id)
@ -584,6 +590,7 @@ class Controller:
server_log_file,
server_stop,
port,
user_id,
server_type="minecraft-java",
)
return new_id
@ -596,6 +603,7 @@ class Controller:
min_mem: int,
max_mem: int,
port: int,
user_id: int,
):
server_id = Helpers.create_uuid()
new_server_dir = os.path.join(self.helper.servers_dir, server_id)
@ -664,6 +672,7 @@ class Controller:
server_log_file,
server_stop,
port,
user_id,
server_type="minecraft-java",
)
return new_id
@ -673,7 +682,12 @@ class Controller:
# **********************************************************************************
def import_bedrock_server(
self, server_name: str, server_path: str, server_exe: str, port: int
self,
server_name: str,
server_path: str,
server_exe: str,
port: int,
user_id: int,
):
server_id = Helpers.create_uuid()
new_server_dir = os.path.join(self.helper.servers_dir, server_id)
@ -727,6 +741,7 @@ class Controller:
server_log_file,
server_stop,
port,
user_id,
server_type="minecraft-bedrock",
)
if os.name != "nt":
@ -735,7 +750,12 @@ class Controller:
return new_id
def import_bedrock_zip_server(
self, server_name: str, zip_path: str, server_exe: str, port: int
self,
server_name: str,
zip_path: str,
server_exe: str,
port: int,
user_id: int,
):
server_id = Helpers.create_uuid()
new_server_dir = os.path.join(self.helper.servers_dir, server_id)
@ -796,6 +816,7 @@ class Controller:
server_log_file,
server_stop,
port,
user_id,
server_type="minecraft-bedrock",
)
if os.name != "nt":
@ -838,6 +859,7 @@ class Controller:
server_log_file: str,
server_stop: str,
server_port: int,
created_by: int,
server_type: str,
server_host: str = "127.0.0.1",
):
@ -852,6 +874,7 @@ class Controller:
server_log_file,
server_stop,
server_type,
created_by,
server_port,
server_host,
)

View File

@ -394,6 +394,7 @@ class AjaxHandler(BaseHandler):
"1",
"2",
server_data["server_port"],
server_data["created_by"],
)
new_server_id = new_server
new_server = self.controller.servers.get_server_data(new_server)
@ -416,6 +417,7 @@ class AjaxHandler(BaseHandler):
temp_dir,
server_data["executable"],
server_data["server_port"],
server_data["created_by"],
)
new_server_id = new_server
new_server = self.controller.servers.get_server_data(new_server)

View File

@ -869,7 +869,7 @@ class PanelHandler(BaseHandler):
for item in page_data["roles"]:
page_data["assigned_roles"].append(item.role_id)
page_data["managed_roles"] = self.controller.roles.get_managed_roles(
page_data["managed_roles"] = self.controller.users.get_managed_roles(
exec_user["user_id"]
)
@ -2282,7 +2282,6 @@ class PanelHandler(BaseHandler):
server_id=0,
source_ip=self.get_remote_ip(),
)
self.controller.crafty_perms.add_user_creation(exec_user["user_id"])
self.redirect("/panel/panel_config")
elif page == "edit_role":
@ -2359,7 +2358,6 @@ class PanelHandler(BaseHandler):
server_id=0,
source_ip=self.get_remote_ip(),
)
self.controller.crafty_perms.add_role_creation(exec_user["user_id"])
self.redirect("/panel/panel_config")
else:

View File

@ -665,10 +665,9 @@ class ApiServersIndexHandler(BaseApiHandler):
},
)
new_server_id, new_server_uuid = self.controller.create_api_server(data)
# Increase the server creation counter
self.controller.crafty_perms.add_server_creation(user["user_id"])
new_server_id, new_server_uuid = self.controller.create_api_server(
data, user["user_id"]
)
self.controller.servers.stats.record_stats()

View File

@ -84,6 +84,7 @@ class ApiServersServerActionHandler(BaseApiHandler):
new_server_log_file,
server_data.get("stop_command"),
server_data.get("type"),
user_id,
server_data.get("server_port"),
)

View File

@ -265,6 +265,7 @@ class ServerHandler(BaseHandler):
backup_path = os.path.join(self.helper.backup_path, new_server_uuid)
server_port = server_data.get("server_port")
server_type = server_data.get("type")
created_by = exec_user["user_id"]
new_server_id = self.controller.servers.create_server(
new_server_name,
@ -276,6 +277,7 @@ class ServerHandler(BaseHandler):
new_server_log_file,
stop_command,
server_type,
created_by,
server_port,
)
if not exec_user["superuser"]:
@ -292,9 +294,6 @@ class ServerHandler(BaseHandler):
self.controller.users.add_role_to_user(
exec_user["user_id"], role_id
)
self.controller.crafty_perms.add_server_creation(
exec_user["user_id"]
)
self.controller.servers.init_all_servers()
@ -354,6 +353,7 @@ class ServerHandler(BaseHandler):
min_mem,
max_mem,
port,
exec_user["user_id"],
)
self.controller.management.add_to_audit_log(
exec_user["user_id"],
@ -370,7 +370,13 @@ class ServerHandler(BaseHandler):
return
new_server_id = self.controller.import_zip_server(
server_name, zip_path, import_server_jar, min_mem, max_mem, port
server_name,
zip_path,
import_server_jar,
min_mem,
max_mem,
port,
exec_user["user_id"],
)
if new_server_id == "false":
self.redirect(
@ -403,6 +409,7 @@ class ServerHandler(BaseHandler):
min_mem,
max_mem,
port,
exec_user["user_id"],
)
self.controller.management.add_to_audit_log(
exec_user["user_id"],
@ -430,9 +437,6 @@ class ServerHandler(BaseHandler):
self.controller.users.add_role_to_user(
exec_user["user_id"], role_id
)
self.controller.crafty_perms.add_server_creation(
exec_user["user_id"]
)
else:
for role in captured_roles:
@ -485,7 +489,11 @@ class ServerHandler(BaseHandler):
return
new_server_id = self.controller.import_bedrock_server(
server_name, import_server_path, import_server_exe, port
server_name,
import_server_path,
import_server_exe,
port,
exec_user["user_id"],
)
self.controller.management.add_to_audit_log(
exec_user["user_id"],
@ -502,7 +510,11 @@ class ServerHandler(BaseHandler):
return
new_server_id = self.controller.import_bedrock_zip_server(
server_name, zip_path, import_server_exe, port
server_name,
zip_path,
import_server_exe,
port,
exec_user["user_id"],
)
if new_server_id == "false":
self.redirect(
@ -528,7 +540,13 @@ class ServerHandler(BaseHandler):
# TODO: add server type check here and call the correct server
# add functions if not a jar
new_server_id = self.controller.create_jar_server(
server_type, server_version, server_name, min_mem, max_mem, port
server_type,
server_version,
server_name,
min_mem,
max_mem,
port,
exec_user["user_id"],
)
self.controller.management.add_to_audit_log(
exec_user["user_id"],
@ -556,9 +574,6 @@ class ServerHandler(BaseHandler):
self.controller.users.add_role_to_user(
exec_user["user_id"], role_id
)
self.controller.crafty_perms.add_server_creation(
exec_user["user_id"]
)
else:
for role in captured_roles:

View File

@ -0,0 +1,16 @@
# Generated by database migrator
import peewee
def migrate(migrator, database, **kwargs):
migrator.add_columns("servers", created_by=peewee.IntegerField(default=-100))
"""
Write your migrations here.
"""
def rollback(migrator, database, **kwargs):
migrator.drop_columns("servers", ["created_by"])
"""
Write your rollback migrations here.
"""

View File

@ -0,0 +1,20 @@
# Generated by database migrator
import peewee
def migrate(migrator, database, **kwargs):
migrator.drop_columns("user_crafty", ["created_server"])
migrator.drop_columns("user_crafty", ["created_user"])
migrator.drop_columns("user_crafty", ["created_role"])
"""
Write your migrations here.
"""
def rollback(migrator, database, **kwargs):
migrator.add_columns("user_crafty", created_server=peewee.IntegerField(default=0))
migrator.add_columns("user_crafty", created_user=peewee.IntegerField(default=0))
migrator.add_columns("user_crafty", created_role=peewee.IntegerField(default=0))
"""
Write your rollback migrations here.
"""