Log proper action for delete_session depending on what function it performs: logout, delete_session (user's own) or admin:delete_session.
This commit is contained in:
@@ -517,16 +517,18 @@ def set_session_host(key: str, host: str, *, ctx: SessionContext | None = None)
|
|||||||
update_session(key, host=host, ctx=ctx)
|
update_session(key, host=host, ctx=ctx)
|
||||||
|
|
||||||
|
|
||||||
def delete_session(key: str, *, ctx: SessionContext | None = None) -> None:
|
def delete_session(
|
||||||
|
key: str, *, ctx: SessionContext | None = None, action: str = "delete_session"
|
||||||
|
) -> None:
|
||||||
"""Delete a session.
|
"""Delete a session.
|
||||||
|
|
||||||
The acting user should be logged via ctx.
|
The acting user should be logged via ctx.
|
||||||
For user logout, pass ctx of the user's session.
|
For user logout, pass ctx of the user's session and action="logout".
|
||||||
For admin terminating a session, pass admin's ctx.
|
For admin terminating a session, pass admin's ctx.
|
||||||
"""
|
"""
|
||||||
if key not in _db.sessions:
|
if key not in _db.sessions:
|
||||||
raise ValueError("Session not found")
|
raise ValueError("Session not found")
|
||||||
with _db.transaction("delete_session", ctx):
|
with _db.transaction(action, ctx):
|
||||||
del _db.sessions[key]
|
del _db.sessions[key]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -706,7 +706,7 @@ async def admin_delete_user_session(
|
|||||||
if not target_session or target_session.user_uuid != user_uuid:
|
if not target_session or target_session.user_uuid != user_uuid:
|
||||||
raise HTTPException(status_code=404, detail="Session not found")
|
raise HTTPException(status_code=404, detail="Session not found")
|
||||||
|
|
||||||
db.delete_session(session_id, ctx=ctx)
|
db.delete_session(session_id, ctx=ctx, action="admin:delete_session")
|
||||||
|
|
||||||
# Check if admin terminated their own session
|
# Check if admin terminated their own session
|
||||||
current_terminated = session_id == auth
|
current_terminated = session_id == auth
|
||||||
|
|||||||
@@ -252,7 +252,7 @@ async def api_logout(request: Request, response: Response, auth=AUTH_COOKIE):
|
|||||||
if not ctx:
|
if not ctx:
|
||||||
return {"message": "Already logged out"}
|
return {"message": "Already logged out"}
|
||||||
with suppress(Exception):
|
with suppress(Exception):
|
||||||
db.delete_session(auth, ctx=ctx)
|
db.delete_session(auth, ctx=ctx, action="logout")
|
||||||
session.clear_session_cookie(response)
|
session.clear_session_cookie(response)
|
||||||
return {"message": "Logged out successfully"}
|
return {"message": "Logged out successfully"}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user