2021-09-08 22:01:10 +00:00
|
|
|
import logging
|
2022-05-07 21:07:55 +00:00
|
|
|
import typing as t
|
2022-01-15 00:23:50 +00:00
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
from app.classes.models.users import HelperUsers
|
2022-03-23 02:50:12 +00:00
|
|
|
from app.classes.models.crafty_permissions import (
|
2022-04-14 02:10:25 +00:00
|
|
|
PermissionsCrafty,
|
|
|
|
EnumPermissionsCrafty,
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
class UsersController:
|
2022-05-24 21:29:31 +00:00
|
|
|
class ApiPermissionDict(t.TypedDict):
|
|
|
|
name: str
|
|
|
|
quantity: int
|
|
|
|
enabled: bool
|
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def __init__(self, helper, users_helper, authentication):
|
|
|
|
self.helper = helper
|
|
|
|
self.users_helper = users_helper
|
|
|
|
self.authentication = authentication
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2022-04-14 12:33:53 +00:00
|
|
|
_permissions_props = {
|
|
|
|
"name": {
|
|
|
|
"type": "string",
|
|
|
|
"enum": [
|
|
|
|
permission.name
|
2022-05-05 00:32:09 +00:00
|
|
|
for permission in PermissionsCrafty.get_permissions_list()
|
2022-04-14 12:33:53 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
"quantity": {"type": "number", "minimum": 0},
|
|
|
|
"enabled": {"type": "boolean"},
|
|
|
|
}
|
2022-05-07 21:07:55 +00:00
|
|
|
self.user_jsonschema_props: t.Final = {
|
2022-04-14 12:33:53 +00:00
|
|
|
"username": {
|
|
|
|
"type": "string",
|
|
|
|
"maxLength": 20,
|
|
|
|
"minLength": 4,
|
|
|
|
"pattern": "^[a-z0-9_]+$",
|
2022-05-05 00:32:09 +00:00
|
|
|
"examples": ["admin"],
|
|
|
|
"title": "Username",
|
|
|
|
},
|
|
|
|
"password": {
|
|
|
|
"type": "string",
|
|
|
|
"maxLength": 20,
|
|
|
|
"minLength": 4,
|
|
|
|
"examples": ["crafty"],
|
|
|
|
"title": "Password",
|
|
|
|
},
|
|
|
|
"email": {
|
|
|
|
"type": "string",
|
|
|
|
"format": "email",
|
|
|
|
"examples": ["default@example.com"],
|
|
|
|
"title": "E-Mail",
|
|
|
|
},
|
|
|
|
"enabled": {
|
|
|
|
"type": "boolean",
|
|
|
|
"examples": [True],
|
|
|
|
"title": "Enabled",
|
2022-04-14 12:33:53 +00:00
|
|
|
},
|
|
|
|
"lang": {
|
|
|
|
"type": "string",
|
|
|
|
"maxLength": 10,
|
|
|
|
"minLength": 2,
|
2022-05-05 00:32:09 +00:00
|
|
|
"examples": ["en"],
|
|
|
|
"title": "Language",
|
|
|
|
},
|
|
|
|
"superuser": {
|
|
|
|
"type": "boolean",
|
|
|
|
"examples": [False],
|
|
|
|
"title": "Superuser",
|
2022-04-14 12:33:53 +00:00
|
|
|
},
|
|
|
|
"permissions": {
|
|
|
|
"type": "array",
|
|
|
|
"items": {
|
|
|
|
"type": "object",
|
|
|
|
"properties": _permissions_props,
|
|
|
|
"required": ["name", "quantity", "enabled"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"roles": {
|
|
|
|
"type": "array",
|
2022-05-09 23:08:49 +00:00
|
|
|
"items": {
|
|
|
|
"type": "string",
|
|
|
|
"minLength": 1,
|
|
|
|
},
|
2022-04-14 12:33:53 +00:00
|
|
|
},
|
|
|
|
"hints": {"type": "boolean"},
|
|
|
|
}
|
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Users Methods
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_all_users():
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperUsers.get_all_users()
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-14 12:33:53 +00:00
|
|
|
@staticmethod
|
2022-05-07 21:07:55 +00:00
|
|
|
def get_all_user_ids() -> t.List[int]:
|
2022-05-05 00:32:09 +00:00
|
|
|
return HelperUsers.get_all_user_ids()
|
2022-04-14 12:33:53 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_id_by_name(username):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperUsers.get_user_id_by_name(username)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2021-09-29 22:05:07 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_user_lang_by_id(user_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperUsers.get_user_lang_by_id(user_id)
|
2021-09-29 22:05:07 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_user_by_id(user_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperUsers.get_user(user_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-02-26 23:00:54 +00:00
|
|
|
@staticmethod
|
|
|
|
def update_server_order(user_id, user_server_order):
|
2022-04-14 02:10:25 +00:00
|
|
|
HelperUsers.update_server_order(user_id, user_server_order)
|
2022-02-26 23:00:54 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_server_order(user_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperUsers.get_server_order(user_id)
|
2022-02-26 23:00:54 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def user_query(user_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperUsers.user_query(user_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-01-14 01:42:53 +00:00
|
|
|
@staticmethod
|
|
|
|
def set_support_path(user_id, support_path):
|
2022-04-14 02:10:25 +00:00
|
|
|
HelperUsers.set_support_path(user_id, support_path)
|
2022-01-14 01:42:53 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def update_user(self, user_id: str, user_data=None, user_crafty_data=None):
|
2022-01-15 00:23:50 +00:00
|
|
|
if user_crafty_data is None:
|
|
|
|
user_crafty_data = {}
|
|
|
|
if user_data is None:
|
|
|
|
user_data = {}
|
2022-04-14 02:10:25 +00:00
|
|
|
base_data = HelperUsers.get_user(user_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
up_data = {}
|
|
|
|
added_roles = set()
|
|
|
|
removed_roles = set()
|
|
|
|
for key in user_data:
|
|
|
|
if key == "user_id":
|
|
|
|
continue
|
2022-06-14 12:40:57 +00:00
|
|
|
if key == "roles":
|
2022-05-07 21:07:55 +00:00
|
|
|
added_roles = set(user_data["roles"]).difference(
|
|
|
|
set(base_data["roles"])
|
|
|
|
)
|
|
|
|
removed_roles = set(base_data["roles"]).difference(
|
|
|
|
set(user_data["roles"])
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
elif key == "password":
|
2022-03-23 02:50:12 +00:00
|
|
|
if user_data["password"] is not None and user_data["password"] != "":
|
2022-04-11 05:23:55 +00:00
|
|
|
up_data["password"] = self.helper.encode_pass(user_data["password"])
|
2022-05-16 19:39:10 +00:00
|
|
|
elif key == "lang":
|
|
|
|
up_data["lang"] = user_data["lang"]
|
|
|
|
elif key == "hints":
|
|
|
|
up_data["hints"] = user_data["hints"]
|
2021-09-08 22:01:10 +00:00
|
|
|
elif base_data[key] != user_data[key]:
|
|
|
|
up_data[key] = user_data[key]
|
2022-04-11 05:23:55 +00:00
|
|
|
up_data["last_update"] = self.helper.get_time_as_string()
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.debug(f"user: {user_data} +role:{added_roles} -role:{removed_roles}")
|
2021-09-08 22:01:10 +00:00
|
|
|
for role in added_roles:
|
2022-04-14 02:10:25 +00:00
|
|
|
HelperUsers.get_or_create(user_id=user_id, role_id=role)
|
2022-04-10 23:01:53 +00:00
|
|
|
permissions_mask = user_crafty_data.get("permissions_mask", "000")
|
|
|
|
|
|
|
|
if "server_quantity" in user_crafty_data:
|
2022-05-05 00:32:09 +00:00
|
|
|
limit_server_creation = user_crafty_data["server_quantity"].get(
|
|
|
|
EnumPermissionsCrafty.SERVER_CREATION.name, 0
|
|
|
|
)
|
|
|
|
|
|
|
|
limit_user_creation = user_crafty_data["server_quantity"].get(
|
|
|
|
EnumPermissionsCrafty.USER_CONFIG.name, 0
|
|
|
|
)
|
|
|
|
limit_role_creation = user_crafty_data["server_quantity"].get(
|
|
|
|
EnumPermissionsCrafty.ROLES_CONFIG.name, 0
|
|
|
|
)
|
2022-04-10 23:01:53 +00:00
|
|
|
else:
|
|
|
|
limit_server_creation = 0
|
|
|
|
limit_user_creation = 0
|
|
|
|
limit_role_creation = 0
|
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
PermissionsCrafty.add_or_update_user(
|
2022-04-10 23:01:53 +00:00
|
|
|
user_id,
|
|
|
|
permissions_mask,
|
|
|
|
limit_server_creation,
|
|
|
|
limit_user_creation,
|
|
|
|
limit_role_creation,
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
self.users_helper.delete_user_roles(user_id, removed_roles)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
self.users_helper.update_user(user_id, up_data)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-05-07 21:07:55 +00:00
|
|
|
def raw_update_user(self, user_id: int, up_data: t.Optional[t.Dict[str, t.Any]]):
|
2022-04-14 12:33:53 +00:00
|
|
|
"""Directly passes the data to the model helper.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (int): The id of the user to update.
|
2022-05-07 21:07:55 +00:00
|
|
|
up_data (t.Optional[t.Dict[str, t.Any]]): Update data.
|
2022-04-14 12:33:53 +00:00
|
|
|
"""
|
|
|
|
self.users_helper.update_user(user_id, up_data)
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
def add_user(
|
2022-04-11 05:23:55 +00:00
|
|
|
self,
|
2022-03-23 02:50:12 +00:00
|
|
|
username,
|
|
|
|
password,
|
|
|
|
email="default@example.com",
|
|
|
|
enabled: bool = True,
|
|
|
|
superuser: bool = False,
|
|
|
|
):
|
2022-04-11 05:23:55 +00:00
|
|
|
return self.users_helper.add_user(
|
2022-03-23 02:50:12 +00:00
|
|
|
username,
|
|
|
|
password=password,
|
|
|
|
email=email,
|
|
|
|
enabled=enabled,
|
|
|
|
superuser=superuser,
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def add_rawpass_user(
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
email="default@example.com",
|
|
|
|
enabled: bool = True,
|
|
|
|
superuser: bool = False,
|
|
|
|
):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperUsers.add_rawpass_user(
|
2022-03-23 02:50:12 +00:00
|
|
|
username,
|
|
|
|
password=password,
|
|
|
|
email=email,
|
|
|
|
enabled=enabled,
|
|
|
|
superuser=superuser,
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def remove_user(self, user_id):
|
|
|
|
return self.users_helper.remove_user(user_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def user_id_exists(user_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperUsers.user_id_exists(user_id)
|
2022-03-13 13:26:38 +00:00
|
|
|
|
2022-03-13 12:29:26 +00:00
|
|
|
@staticmethod
|
|
|
|
def set_prepare(user_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperUsers.set_prepare(user_id)
|
2022-03-13 13:26:38 +00:00
|
|
|
|
2022-03-13 12:29:26 +00:00
|
|
|
@staticmethod
|
|
|
|
def stop_prepare(user_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperUsers.stop_prepare(user_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def get_user_id_by_api_token(self, token: str) -> str:
|
|
|
|
token_data = self.authentication.check_no_iat(token)
|
2022-03-23 02:50:12 +00:00
|
|
|
return token_data["user_id"]
|
2022-01-15 00:23:50 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def get_user_by_api_token(self, token: str):
|
2022-04-14 12:33:53 +00:00
|
|
|
_, _, user = self.authentication.check_err(token)
|
2022-01-15 00:23:50 +00:00
|
|
|
return user
|
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def get_api_key_by_token(self, token: str):
|
|
|
|
key, _, _ = self.authentication.check(token)
|
2022-04-10 19:39:31 +00:00
|
|
|
return key
|
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# User Roles Methods
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_user_roles_id(user_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperUsers.get_user_roles_id(user_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_user_roles_names(user_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperUsers.get_user_roles_names(user_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def add_role_to_user(self, user_id, role_id):
|
|
|
|
return self.users_helper.add_role_to_user(user_id, role_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def add_user_roles(self, user):
|
|
|
|
return self.users_helper.add_user_roles(user)
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def user_role_query(user_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperUsers.user_role_query(user_id)
|
2022-01-15 00:23:50 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-01-15 00:23:50 +00:00
|
|
|
# Api Keys Methods
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-01-15 00:23:50 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_user_api_keys(user_id: str):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperUsers.get_user_api_keys(user_id)
|
2022-01-15 00:23:50 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_user_api_key(key_id: str):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperUsers.get_user_api_key(key_id)
|
2022-01-15 00:23:50 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
def add_user_api_key(
|
2022-04-11 05:23:55 +00:00
|
|
|
self,
|
2022-03-23 02:50:12 +00:00
|
|
|
name: str,
|
|
|
|
user_id: str,
|
|
|
|
superuser: bool = False,
|
2022-05-07 21:07:55 +00:00
|
|
|
server_permissions_mask: t.Optional[str] = None,
|
|
|
|
crafty_permissions_mask: t.Optional[str] = None,
|
2022-03-23 02:50:12 +00:00
|
|
|
):
|
2022-04-11 05:23:55 +00:00
|
|
|
return self.users_helper.add_user_api_key(
|
2022-03-23 02:50:12 +00:00
|
|
|
name, user_id, superuser, server_permissions_mask, crafty_permissions_mask
|
|
|
|
)
|
2022-01-15 00:23:50 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def delete_user_api_keys(self, user_id: str):
|
|
|
|
return self.users_helper.delete_user_api_keys(user_id)
|
2022-01-15 00:23:50 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def delete_user_api_key(self, key_id: str):
|
|
|
|
return self.users_helper.delete_user_api_key(key_id)
|