diff --git a/components/cache.py b/components/cache.py index 608d635..323b7b4 100644 --- a/components/cache.py +++ b/components/cache.py @@ -4,22 +4,21 @@ from components.utils import ensure_list @validate_call -def buster(object_id: UUID | list[UUID]): - object_ids = ensure_list(object_id) - for object_id in object_ids: - object_id = str(object_id) +def buster(bust_uuid: UUID | list[UUID]): + bust_uuids = ensure_list(bust_uuid) + for bust_uuid in bust_uuids: + bust_uuid = str(bust_uuid) - for user_id in IN_MEMORY_DB["OBJECTS_CACHE"]: - cached_keys = list(IN_MEMORY_DB["OBJECTS_CACHE"][user_id].keys()) - if object_id in cached_keys: - mapping_name = IN_MEMORY_DB["OBJECTS_CACHE"][user_id][object_id].name - if object_id in IN_MEMORY_DB["OBJECTS_CACHE"][user_id]: - del IN_MEMORY_DB["OBJECTS_CACHE"][user_id][object_id] + for user_id in IN_MEMORY_DB["CACHE"]["MODELS"]: + cached_keys = list(IN_MEMORY_DB["CACHE"]["MODELS"][user_id].keys()) + if bust_uuid in cached_keys: + if bust_uuid in IN_MEMORY_DB["CACHE"]["MODELS"][user_id]: + del IN_MEMORY_DB["CACHE"]["MODELS"][user_id][bust_uuid] - for user_id in IN_MEMORY_DB["FORM_OPTIONS_CACHE"]: - for option in IN_MEMORY_DB["FORM_OPTIONS_CACHE"][user_id].copy(): + for user_id in IN_MEMORY_DB["CACHE"]["FORMS"]: + for option in IN_MEMORY_DB["CACHE"]["FORMS"][user_id].copy(): if any( - d["value"] == object_id - for d in IN_MEMORY_DB["FORM_OPTIONS_CACHE"][user_id][option] + d["value"] == bust_uuid + for d in IN_MEMORY_DB["CACHE"]["FORMS"][user_id][option] ): - del IN_MEMORY_DB["FORM_OPTIONS_CACHE"][user_id][option] + del IN_MEMORY_DB["CACHE"]["FORMS"][user_id][option] diff --git a/components/objects.py b/components/objects.py index 6842ef8..51836c6 100644 --- a/components/objects.py +++ b/components/objects.py @@ -23,10 +23,10 @@ async def get( if current_app and session.get("id"): user_id = session["id"] else: - user_id = "anonymous" + user_id = "99999999-9999-9999-9999-999999999999" - if not user_id in IN_MEMORY_DB["OBJECTS_CACHE"]: - IN_MEMORY_DB["OBJECTS_CACHE"][user_id] = dict() + if not user_id in IN_MEMORY_DB["CACHE"]["MODELS"]: + IN_MEMORY_DB["CACHE"]["MODELS"][user_id] = dict() async with TinyDB(**db_params) as db: found_objects = db.table(object_type).search(Query().id.one_of(get_objects)) @@ -45,35 +45,37 @@ async def get( for k, v in o_parsed.details.model_dump(mode="json").items(): if k == "assigned_domain": - if not v in IN_MEMORY_DB["OBJECTS_CACHE"][user_id]: - IN_MEMORY_DB["OBJECTS_CACHE"][user_id][v] = await get( + if not v in IN_MEMORY_DB["CACHE"]["MODELS"][user_id]: + IN_MEMORY_DB["CACHE"]["MODELS"][user_id][v] = await get( object_type="domains", object_id=v, permission_validation=False, ) - o_parsed.details.assigned_domain = IN_MEMORY_DB["OBJECTS_CACHE"][ + o_parsed.details.assigned_domain = IN_MEMORY_DB["CACHE"]["MODELS"][ user_id ][v] elif k in ["assigned_arc_keypair", "assigned_dkim_keypair"] and v: - if not v in IN_MEMORY_DB["OBJECTS_CACHE"][user_id]: - IN_MEMORY_DB["OBJECTS_CACHE"][user_id][v] = await get( + if not v in IN_MEMORY_DB["CACHE"]["MODELS"][user_id]: + IN_MEMORY_DB["CACHE"]["MODELS"][user_id][v] = await get( object_type="keypairs", object_id=v, permission_validation=False, ) - setattr(o_parsed.details, k, IN_MEMORY_DB["OBJECTS_CACHE"][user_id][v]) + setattr( + o_parsed.details, k, IN_MEMORY_DB["CACHE"]["MODELS"][user_id][v] + ) elif k == "assigned_emailusers" and v: o_parsed.details.assigned_emailusers = [] for u in ensure_list(v): - if not u in IN_MEMORY_DB["OBJECTS_CACHE"][user_id]: - IN_MEMORY_DB["OBJECTS_CACHE"][user_id][u] = await get( + if not u in IN_MEMORY_DB["CACHE"]["MODELS"][user_id]: + IN_MEMORY_DB["CACHE"]["MODELS"][user_id][u] = await get( object_type="emailusers", object_id=u, permission_validation=False, ) o_parsed.details.assigned_emailusers.append( - IN_MEMORY_DB["OBJECTS_CACHE"][user_id][u] + IN_MEMORY_DB["CACHE"]["MODELS"][user_id][u] ) object_data.append(o_parsed) @@ -340,7 +342,7 @@ async def create( db.table(object_type).insert(insert_data) try: - del IN_MEMORY_DB["FORM_OPTIONS_CACHE"][session.get("id")][object_type] + del IN_MEMORY_DB["CACHE"]["FORMS"][session.get("id")][object_type] finally: return insert_data["id"] diff --git a/components/smtpd/__init__.py b/components/smtpd/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/components/users.py b/components/users.py index 7d9acc5..4a728af 100644 --- a/components/users.py +++ b/components/users.py @@ -48,9 +48,9 @@ async def create(data: dict): insert_data = create_user.model_dump(mode="json") db.table("users").insert(insert_data) - for user_id in IN_MEMORY_DB["FORM_OPTIONS_CACHE"].copy(): - if "users" in IN_MEMORY_DB["FORM_OPTIONS_CACHE"][user_id]: - del IN_MEMORY_DB["FORM_OPTIONS_CACHE"][user_id]["users"] + for user_id in IN_MEMORY_DB["CACHE"]["FORMS"].copy(): + if "users" in IN_MEMORY_DB["CACHE"]["FORMS"][user_id]: + del IN_MEMORY_DB["CACHE"]["FORMS"][user_id]["users"] return insert_data["id"] @@ -58,12 +58,26 @@ async def create(data: dict): @validate_call async def get(user_id: UUID, join_credentials: bool = True): db_params = evaluate_db_params() + system_id = "00000000-0000-0000-0000-000000000000" + + if not IN_MEMORY_DB["CACHE"]["MODELS"].get(system_id): + IN_MEMORY_DB["CACHE"]["MODELS"][system_id] = dict() async with TinyDB(**db_params) as db: - user = User.model_validate(db.table("users").get(Query().id == str(user_id))) + if not str(user_id) in IN_MEMORY_DB["CACHE"]["MODELS"][system_id]: + print( + User.model_validate(db.table("users").get(Query().id == str(user_id))) + ) + IN_MEMORY_DB["CACHE"]["MODELS"][system_id][ + str(user_id) + ] = User.model_validate(db.table("users").get(Query().id == str(user_id))) + + user = IN_MEMORY_DB["CACHE"]["MODELS"][system_id][str(user_id)].copy() + credentials = db.table("credentials").search( (Query().id.one_of(user.credentials)) ) + if join_credentials: user.credentials = _create_credentials_mapping(credentials) diff --git a/components/web/__init__.py b/components/web/__init__.py index c54017d..65ea44f 100644 --- a/components/web/__init__.py +++ b/components/web/__init__.py @@ -32,8 +32,10 @@ app.config["SERVER_NAME"] = defaults.HOSTNAME app.config["MOD_REQ_LIMIT"] = 10 IN_MEMORY_DB["SESSION_VALIDATED"] = dict() IN_MEMORY_DB["WS_CONNECTIONS"] = dict() -IN_MEMORY_DB["FORM_OPTIONS_CACHE"] = dict() -IN_MEMORY_DB["OBJECTS_CACHE"] = dict() +IN_MEMORY_DB["CACHE"] = { + "MODELS": dict(), + "FORMS": dict(), +} IN_MEMORY_DB["APP_LOGS_FULL_PULL"] = dict() IN_MEMORY_DB["PROMOTE_USERS"] = set() IN_MEMORY_DB["TOKENS"] = { diff --git a/components/web/blueprints/auth.py b/components/web/blueprints/auth.py index 02ef80d..effa01f 100644 --- a/components/web/blueprints/auth.py +++ b/components/web/blueprints/auth.py @@ -13,6 +13,7 @@ from components.web.utils import * from secrets import token_urlsafe from components.utils import expire_key from components.utils.datetimes import utc_now_as_str +from components.logs import logger from components.users import ( get as get_user, what_id, @@ -505,6 +506,7 @@ async def register_webauthn(): ) except Exception as e: + logger.error(e) return trigger_notification( level="error", response_code=409, @@ -601,6 +603,7 @@ async def auth_login_verify(): ) except Exception as e: + logger.error(e) return trigger_notification( level="error", response_code=409, diff --git a/components/web/templates/system/includes/users/groups.html b/components/web/templates/system/includes/users/groups.html index e87ff60..4c8f8ac 100644 --- a/components/web/templates/system/includes/users/groups.html +++ b/components/web/templates/system/includes/users/groups.html @@ -13,7 +13,7 @@