2021-09-08 22:01:10 +00:00
|
|
|
import logging
|
2022-05-24 21:55:49 +00:00
|
|
|
import typing as t
|
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
|
2022-04-14 02:10:25 +00:00
|
|
|
from app.classes.models.users import Users, ApiKeys, HelperUsers
|
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-14 02:10:25 +00:00
|
|
|
class UserCrafty(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
|
|
|
# **********************************************************************************
|
2022-04-14 02:10:25 +00:00
|
|
|
class EnumPermissionsCrafty(Enum):
|
|
|
|
SERVER_CREATION = 0
|
|
|
|
USER_CONFIG = 1
|
|
|
|
ROLES_CONFIG = 2
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
class PermissionsCrafty:
|
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-05-17 22:55:05 +00:00
|
|
|
return list(EnumPermissionsCrafty.__members__.values())
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_permissions(permissions_mask):
|
2022-05-16 14:52:09 +00:00
|
|
|
return [
|
|
|
|
permission
|
2022-05-17 22:55:05 +00:00
|
|
|
for permission in EnumPermissionsCrafty.__members__.values()
|
2022-05-16 14:52:09 +00:00
|
|
|
if PermissionsCrafty.has_permission(permissions_mask, permission)
|
|
|
|
]
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-04-14 02:10:25 +00:00
|
|
|
def has_permission(permission_mask, permission_tested: EnumPermissionsCrafty):
|
2022-05-16 14:52:09 +00:00
|
|
|
return permission_mask[permission_tested.value] == "1"
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-03-23 02:50:12 +00:00
|
|
|
def set_permission(
|
2022-04-14 02:10:25 +00:00
|
|
|
permission_mask, permission_tested: EnumPermissionsCrafty, value
|
2022-03-23 02:50:12 +00:00
|
|
|
):
|
2022-05-16 14:52:09 +00:00
|
|
|
index = permission_tested.value
|
|
|
|
return permission_mask[:index] + str(value) + permission_mask[index + 1 :]
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-04-14 02:10:25 +00:00
|
|
|
def get_permission(permission_mask, permission_tested: EnumPermissionsCrafty):
|
2021-09-08 22:01:10 +00:00
|
|
|
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-05-16 14:52:09 +00:00
|
|
|
# TODO: only get the permissions of the UserCrafty
|
2022-04-14 02:10:25 +00:00
|
|
|
user_crafty = PermissionsCrafty.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():
|
2022-05-17 22:55:05 +00:00
|
|
|
return {name: -1 for name in EnumPermissionsCrafty.__members__.keys()}
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_permission_quantity_list(user_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
user_crafty = PermissionsCrafty.get_user_crafty(user_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
quantity_list = {
|
2022-04-14 02:10:25 +00:00
|
|
|
EnumPermissionsCrafty.SERVER_CREATION.name: user_crafty.limit_server_creation, # pylint: disable=line-too-long
|
|
|
|
EnumPermissionsCrafty.USER_CONFIG.name: user_crafty.limit_user_creation,
|
|
|
|
EnumPermissionsCrafty.ROLES_CONFIG.name: user_crafty.limit_role_creation,
|
2021-09-08 22:01:10 +00:00
|
|
|
}
|
|
|
|
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
|
2022-04-14 02:10:25 +00:00
|
|
|
def get_user_crafty(user_id):
|
2021-09-08 22:01:10 +00:00
|
|
|
try:
|
2022-05-16 14:52:09 +00:00
|
|
|
user_crafty = UserCrafty.get(UserCrafty.user_id == user_id)
|
2022-01-26 01:45:30 +00:00
|
|
|
except DoesNotExist:
|
2022-05-24 21:55:49 +00:00
|
|
|
UserCrafty.insert(
|
2022-03-23 02:50:12 +00:00
|
|
|
{
|
2022-04-14 02:10:25 +00:00
|
|
|
UserCrafty.user_id: user_id,
|
|
|
|
UserCrafty.permissions: "000",
|
|
|
|
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,
|
2022-03-23 02:50:12 +00:00
|
|
|
}
|
|
|
|
).execute()
|
2022-04-14 02:10:25 +00:00
|
|
|
user_crafty = PermissionsCrafty.get_user_crafty(user_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
return user_crafty
|
|
|
|
|
2022-05-24 21:55:49 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_user_crafty_optional(user_id) -> t.Optional[UserCrafty]:
|
|
|
|
try:
|
|
|
|
return UserCrafty.get(UserCrafty.user_id == user_id)
|
|
|
|
except DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def add_user_crafty(user_id, uc_permissions):
|
2022-04-14 02:10:25 +00:00
|
|
|
user_crafty = UserCrafty.insert(
|
|
|
|
{UserCrafty.user_id: user_id, UserCrafty.permissions: uc_permissions}
|
2022-03-23 02:50:12 +00:00
|
|
|
).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,
|
|
|
|
):
|
2022-05-16 14:15:46 +00:00
|
|
|
# http://docs.peewee-orm.com/en/latest/peewee/querying.html#upsert
|
|
|
|
|
|
|
|
UserCrafty.replace(
|
|
|
|
{
|
|
|
|
UserCrafty.user_id: user_id,
|
|
|
|
UserCrafty.permissions: permissions_mask,
|
|
|
|
UserCrafty.limit_server_creation: limit_server_creation,
|
|
|
|
UserCrafty.limit_user_creation: limit_user_creation,
|
|
|
|
UserCrafty.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-14 02:10:25 +00:00
|
|
|
user_crafty = PermissionsCrafty.get_user_crafty(user_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
quantity_list = {
|
2022-04-14 02:10:25 +00:00
|
|
|
EnumPermissionsCrafty.SERVER_CREATION.name: user_crafty.created_server,
|
|
|
|
EnumPermissionsCrafty.USER_CONFIG.name: user_crafty.created_user,
|
|
|
|
EnumPermissionsCrafty.ROLES_CONFIG.name: user_crafty.created_role,
|
2021-09-08 22:01:10 +00:00
|
|
|
}
|
|
|
|
return quantity_list
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_crafty_limit_value(user_id, permission):
|
2022-04-14 02:10:25 +00:00
|
|
|
quantity_list = PermissionsCrafty.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-14 02:10:25 +00:00
|
|
|
user_crafty = PermissionsCrafty.get_user_crafty(user_id)
|
|
|
|
can = PermissionsCrafty.has_permission(user_crafty.permissions, permission)
|
|
|
|
limit_list = PermissionsCrafty.get_permission_quantity_list(user_id)
|
|
|
|
quantity_list = PermissionsCrafty.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
|
2022-05-20 15:07:12 +00:00
|
|
|
def add_server_creation(user_id: int):
|
2022-05-07 21:07:55 +00:00
|
|
|
"""Increase the "Server Creation" counter for this user
|
2022-05-05 00:32:09 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (int): The modifiable user's ID
|
|
|
|
"""
|
2022-05-16 14:10:57 +00:00
|
|
|
UserCrafty.update(created_server=UserCrafty.created_server + 1).where(
|
|
|
|
UserCrafty.user_id == user_id
|
|
|
|
).execute()
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-05-18 18:33:22 +00:00
|
|
|
@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
|
|
|
|
|
2022-01-15 00:23:50 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_api_key_permissions_list(key: ApiKeys):
|
2022-04-14 02:10:25 +00:00
|
|
|
user = HelperUsers.get_user(key.user_id)
|
2022-04-10 19:39:31 +00:00
|
|
|
if user["superuser"] and key.superuser:
|
2022-04-14 02:10:25 +00:00
|
|
|
return PermissionsCrafty.get_permissions_list()
|
2022-01-15 00:23:50 +00:00
|
|
|
else:
|
2022-04-10 19:39:31 +00:00
|
|
|
if user["superuser"]:
|
2022-05-16 14:52:09 +00:00
|
|
|
# User is superuser but API key isn't
|
2022-04-10 19:39:31 +00:00
|
|
|
user_permissions_mask = "111"
|
|
|
|
else:
|
2022-05-16 14:52:09 +00:00
|
|
|
# Not superuser
|
2022-04-14 02:10:25 +00:00
|
|
|
user_permissions_mask = PermissionsCrafty.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-14 02:10:25 +00:00
|
|
|
permissions_list = PermissionsCrafty.get_permissions(permissions_mask)
|
2022-01-15 00:23:50 +00:00
|
|
|
return permissions_list
|