2021-09-08 22:01:10 +00:00
|
|
|
import logging
|
2022-04-14 12:33:53 +00:00
|
|
|
import typing
|
2022-04-11 05:23:55 +00:00
|
|
|
from enum import Enum
|
|
|
|
from peewee import (
|
|
|
|
ForeignKeyField,
|
|
|
|
CharField,
|
|
|
|
IntegerField,
|
|
|
|
DoesNotExist,
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
from app.classes.models.base_model import BaseModel
|
|
|
|
from app.classes.models.users import Users, ApiKeys, helper_users
|
2022-04-12 23:22:57 +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-10-09 08:45:40 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# User_Crafty Class
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-04-11 05:23:55 +00:00
|
|
|
class User_Crafty(BaseModel):
|
2022-03-23 02:50:12 +00:00
|
|
|
user_id = ForeignKeyField(Users, backref="users_crafty")
|
2021-09-08 22:01:10 +00:00
|
|
|
permissions = CharField(default="00000000")
|
|
|
|
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:
|
2022-03-23 02:50:12 +00:00
|
|
|
table_name = "user_crafty"
|
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
|
|
|
# Crafty Permissions Class
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
class Enum_Permissions_Crafty(Enum):
|
|
|
|
Server_Creation = 0
|
|
|
|
User_Config = 1
|
|
|
|
Roles_Config = 2
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
class Permissions_Crafty:
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Crafty Permissions Methods
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_permissions_list():
|
2022-04-14 12:33:53 +00:00
|
|
|
permissions_list: typing.List[Enum_Permissions_Crafty] = []
|
2021-09-08 22:01:10 +00:00
|
|
|
for member in Enum_Permissions_Crafty.__members__.items():
|
|
|
|
permissions_list.append(member[1])
|
|
|
|
return permissions_list
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_permissions(permissions_mask):
|
2022-04-14 12:33:53 +00:00
|
|
|
permissions_list: typing.List[Enum_Permissions_Crafty] = []
|
2021-09-08 22:01:10 +00:00
|
|
|
for member in Enum_Permissions_Crafty.__members__.items():
|
2022-04-11 05:23:55 +00:00
|
|
|
if Permissions_Crafty.has_permission(permissions_mask, member[1]):
|
2021-09-08 22:01:10 +00:00
|
|
|
permissions_list.append(member[1])
|
|
|
|
return permissions_list
|
|
|
|
|
|
|
|
@staticmethod
|
2022-04-14 12:33:53 +00:00
|
|
|
def has_permission(
|
|
|
|
permission_mask: typing.Mapping[int, str],
|
|
|
|
permission_tested: Enum_Permissions_Crafty,
|
|
|
|
):
|
2021-09-08 22:01:10 +00:00
|
|
|
result = False
|
2022-03-23 02:50:12 +00:00
|
|
|
if permission_mask[permission_tested.value] == "1":
|
2021-09-08 22:01:10 +00:00
|
|
|
result = True
|
|
|
|
return result
|
|
|
|
|
|
|
|
@staticmethod
|
2022-03-23 02:50:12 +00:00
|
|
|
def set_permission(
|
|
|
|
permission_mask, permission_tested: Enum_Permissions_Crafty, value
|
|
|
|
):
|
2021-09-08 22:01:10 +00:00
|
|
|
l = list(permission_mask)
|
|
|
|
l[permission_tested.value] = str(value)
|
2022-03-23 02:50:12 +00:00
|
|
|
permission_mask = "".join(l)
|
2021-09-08 22:01:10 +00:00
|
|
|
return permission_mask
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_permission(permission_mask, permission_tested: Enum_Permissions_Crafty):
|
|
|
|
return permission_mask[permission_tested.value]
|
2021-10-09 08:45:40 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_crafty_permissions_mask(user_id):
|
2022-03-23 02:50:12 +00:00
|
|
|
permissions_mask = ""
|
2022-04-11 05:23:55 +00:00
|
|
|
user_crafty = Permissions_Crafty.get_User_Crafty(user_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
permissions_mask = user_crafty.permissions
|
|
|
|
return permissions_mask
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_all_permission_quantity_list():
|
|
|
|
quantity_list = {
|
|
|
|
Enum_Permissions_Crafty.Server_Creation.name: -1,
|
|
|
|
Enum_Permissions_Crafty.User_Config.name: -1,
|
|
|
|
Enum_Permissions_Crafty.Roles_Config.name: -1,
|
|
|
|
}
|
|
|
|
return quantity_list
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_permission_quantity_list(user_id):
|
2022-04-11 05:23:55 +00:00
|
|
|
user_crafty = Permissions_Crafty.get_User_Crafty(user_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
quantity_list = {
|
2022-03-23 06:06:13 +00:00
|
|
|
Enum_Permissions_Crafty.Server_Creation.name: user_crafty.limit_server_creation, # pylint: disable=line-too-long
|
2021-09-08 22:01:10 +00:00
|
|
|
Enum_Permissions_Crafty.User_Config.name: user_crafty.limit_user_creation,
|
|
|
|
Enum_Permissions_Crafty.Roles_Config.name: user_crafty.limit_role_creation,
|
|
|
|
}
|
|
|
|
return quantity_list
|
2021-10-09 08:45:40 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# User_Crafty Methods
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_User_Crafty(user_id):
|
|
|
|
try:
|
2022-03-23 02:50:12 +00:00
|
|
|
user_crafty = (
|
|
|
|
User_Crafty.select().where(User_Crafty.user_id == user_id).get()
|
|
|
|
)
|
2022-01-26 01:45:30 +00:00
|
|
|
except DoesNotExist:
|
2022-03-23 02:50:12 +00:00
|
|
|
user_crafty = User_Crafty.insert(
|
|
|
|
{
|
|
|
|
User_Crafty.user_id: user_id,
|
|
|
|
User_Crafty.permissions: "000",
|
|
|
|
User_Crafty.limit_server_creation: 0,
|
|
|
|
User_Crafty.limit_user_creation: 0,
|
|
|
|
User_Crafty.limit_role_creation: 0,
|
|
|
|
User_Crafty.created_server: 0,
|
|
|
|
User_Crafty.created_user: 0,
|
|
|
|
User_Crafty.created_role: 0,
|
|
|
|
}
|
|
|
|
).execute()
|
2022-04-11 05:23:55 +00:00
|
|
|
user_crafty = Permissions_Crafty.get_User_Crafty(user_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
return user_crafty
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def add_user_crafty(user_id, uc_permissions):
|
2022-03-23 02:50:12 +00:00
|
|
|
user_crafty = User_Crafty.insert(
|
|
|
|
{User_Crafty.user_id: user_id, User_Crafty.permissions: uc_permissions}
|
|
|
|
).execute()
|
2021-09-08 22:01:10 +00:00
|
|
|
return user_crafty
|
2021-10-09 08:45:40 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
2022-03-23 02:50:12 +00:00
|
|
|
def add_or_update_user(
|
|
|
|
user_id,
|
|
|
|
permissions_mask,
|
|
|
|
limit_server_creation,
|
|
|
|
limit_user_creation,
|
|
|
|
limit_role_creation,
|
|
|
|
):
|
2021-09-08 22:01:10 +00:00
|
|
|
try:
|
2022-03-23 02:50:12 +00:00
|
|
|
user_crafty = (
|
|
|
|
User_Crafty.select().where(User_Crafty.user_id == user_id).get()
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
user_crafty.permissions = permissions_mask
|
|
|
|
user_crafty.limit_server_creation = limit_server_creation
|
|
|
|
user_crafty.limit_user_creation = limit_user_creation
|
|
|
|
user_crafty.limit_role_creation = limit_role_creation
|
|
|
|
User_Crafty.save(user_crafty)
|
|
|
|
except:
|
2022-03-23 02:50:12 +00:00
|
|
|
User_Crafty.insert(
|
|
|
|
{
|
|
|
|
User_Crafty.user_id: user_id,
|
|
|
|
User_Crafty.permissions: permissions_mask,
|
|
|
|
User_Crafty.limit_server_creation: limit_server_creation,
|
|
|
|
User_Crafty.limit_user_creation: limit_user_creation,
|
|
|
|
User_Crafty.limit_role_creation: limit_role_creation,
|
|
|
|
}
|
|
|
|
).execute()
|
2021-10-09 08:45:40 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_created_quantity_list(user_id):
|
2022-04-11 05:23:55 +00:00
|
|
|
user_crafty = Permissions_Crafty.get_User_Crafty(user_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
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):
|
2022-04-11 05:23:55 +00:00
|
|
|
quantity_list = Permissions_Crafty.get_permission_quantity_list(user_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
return quantity_list[permission]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def can_add_in_crafty(user_id, permission):
|
2022-04-11 05:23:55 +00:00
|
|
|
user_crafty = Permissions_Crafty.get_User_Crafty(user_id)
|
|
|
|
can = Permissions_Crafty.has_permission(user_crafty.permissions, permission)
|
|
|
|
limit_list = Permissions_Crafty.get_permission_quantity_list(user_id)
|
|
|
|
quantity_list = Permissions_Crafty.get_created_quantity_list(user_id)
|
2022-03-23 02:50:12 +00:00
|
|
|
return can and (
|
|
|
|
(quantity_list[permission.name] < limit_list[permission.name])
|
|
|
|
or limit_list[permission.name] == -1
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def add_server_creation(user_id):
|
2022-04-11 05:23:55 +00:00
|
|
|
user_crafty = Permissions_Crafty.get_User_Crafty(user_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
user_crafty.created_server += 1
|
|
|
|
User_Crafty.save(user_crafty)
|
2021-10-09 08:45:40 +00:00
|
|
|
return user_crafty.created_server
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-01-15 00:23:50 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_api_key_permissions_list(key: ApiKeys):
|
2022-04-11 05:23:55 +00:00
|
|
|
user = helper_users.get_user(key.user_id)
|
2022-04-10 19:39:31 +00:00
|
|
|
if user["superuser"] and key.superuser:
|
2022-04-11 05:23:55 +00:00
|
|
|
return Permissions_Crafty.get_permissions_list()
|
2022-01-15 00:23:50 +00:00
|
|
|
else:
|
2022-04-10 19:39:31 +00:00
|
|
|
if user["superuser"]:
|
|
|
|
user_permissions_mask = "111"
|
|
|
|
else:
|
2022-04-11 05:23:55 +00:00
|
|
|
user_permissions_mask = Permissions_Crafty.get_crafty_permissions_mask(
|
2022-04-10 19:39:31 +00:00
|
|
|
user["user_id"]
|
|
|
|
)
|
2022-01-15 00:23:50 +00:00
|
|
|
key_permissions_mask: str = key.crafty_permissions
|
2022-04-12 23:22:57 +00:00
|
|
|
permissions_mask = PermissionHelper.combine_masks(
|
2022-03-23 02:50:12 +00:00
|
|
|
user_permissions_mask, key_permissions_mask
|
|
|
|
)
|
2022-04-11 05:23:55 +00:00
|
|
|
permissions_list = Permissions_Crafty.get_permissions(permissions_mask)
|
2022-01-15 00:23:50 +00:00
|
|
|
return permissions_list
|