Adding Support For Server Creation Quantity Limit

This commit is contained in:
Silversthorn 2021-08-25 00:23:33 +02:00
parent bb1f43bc81
commit 397fbd5268
4 changed files with 55 additions and 4 deletions

View File

@ -147,9 +147,18 @@ class Controller:
@staticmethod
def can_create_server(user_id):
permissions_mask = db_helper.get_crafty_permissions_mask(user_id)
return crafty_permissions.has_permission(permissions_mask, Enum_Permissions_Crafty.Server_Creation)
return db_helper.can_add_in_crafty(user_id, Enum_Permissions_Crafty.Server_Creation)
@staticmethod
def can_add_user(user_id):
#TODO: Complete if we need a User Addition limit
return True
@staticmethod
def can_add_role(user_id):
#TODO: Complete if we need a User Addition limit
return True
@staticmethod
def list_all_crafty_permissions_quantity_limits():
return db_helper.get_all_permission_quantity_list()

View File

@ -135,6 +135,9 @@ class User_Crafty(Model):
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'
@ -468,7 +471,38 @@ class db_shortcuts:
Enum_Permissions_Crafty.Roles_Config.name: user_crafty.limit_role_creation,
}
return quantity_list
@staticmethod
def get_created_quantity_list(user_id):
user_crafty = User_Crafty.select().where(User_Crafty.user_id == user_id).get()
quantity_list = {
Enum_Permissions_Crafty.Server_Creation.name: user_crafty.created_server,
Enum_Permissions_Crafty.User_Config.name: user_crafty.created_user,
Enum_Permissions_Crafty.Roles_Config.name: user_crafty.created_role,
}
return quantity_list
@staticmethod
def get_crafty_limit_value(user_id, permission):
user_crafty = User_Crafty.select().where(User_Crafty.user_id == user_id).get()
quantity_list = get_permission_quantity_list(user_id)
return quantity_list[permission]
@staticmethod
def can_add_in_crafty(user_id, permission):
user_crafty = User_Crafty.select().where(User_Crafty.user_id == user_id).get()
can = crafty_permissions.has_permission(user_crafty.permissions, permission)
limit_list = db_helper.get_permission_quantity_list(user_id)
quantity_list = db_helper.get_created_quantity_list(user_id)
return can and quantity_list[permission.name] < limit_list[permission.name]
@staticmethod
def add_server_creation(user_id):
user_crafty = User_Crafty.select().where(User_Crafty.user_id == user_id).get()
user_crafty.created_server += 1
User_Crafty.save(user_crafty)
return user_crafty.created_server
@staticmethod
def get_latest_hosts_stats():
query = Host_Stats.select().order_by(Host_Stats.id.desc()).get()

View File

@ -69,8 +69,8 @@ class ServerHandler(BaseHandler):
if page == "step1":
if not self.controller.can_create_server(exec_user_id):
self.redirect("/panel/error?error=Unauthorized access: not at server creator")
if not exec_user['superuser'] and not self.controller.can_create_server(exec_user_id):
self.redirect("/panel/error?error=Unauthorized access: not a server creator or server limit reached")
return
page_data['server_types'] = server_jar_obj.get_serverjar_data_sorted()
@ -204,6 +204,8 @@ class ServerHandler(BaseHandler):
role_id = db_helper.add_role("Creator of Server with id={}".format(new_server_id))
db_helper.add_role_server(new_server_id, role_id, "11111111")
db_helper.add_role_to_user(exec_user_id, role_id)
if not exec_user['superuser']:
db_helper.add_server_creation(exec_user_id)
self.controller.stats.record_stats()
self.redirect("/panel/dashboard")

View File

@ -4,6 +4,9 @@ import peewee
def migrate(migrator, database, **kwargs):
migrator.add_columns('user_crafty', limit_user_creation=peewee.IntegerField(default=0))
migrator.add_columns('user_crafty', limit_role_creation=peewee.IntegerField(default=0))
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 migrations here.
"""
@ -13,6 +16,9 @@ def migrate(migrator, database, **kwargs):
def rollback(migrator, database, **kwargs):
migrator.drop_columns('user_crafty', ['limit_user_creation'])
migrator.drop_columns('user_crafty', ['limit_role_creation'])
migrator.drop_columns('user_crafty', ['created_server'])
migrator.drop_columns('user_crafty', ['created_user'])
migrator.drop_columns('user_crafty', ['created_role'])
"""
Write your rollback migrations here.
"""