From b92844ba80c70c3da043df32e05b22990b837601 Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Fri, 18 Aug 2023 10:00:15 -0400 Subject: [PATCH] Microfixes + get edit working --- backend/endpoints/identity.py | 9 +++++---- backend/main.py | 18 ++++++++---------- backend/utils/auth.py | 10 ++++++++-- .../src/components/Dialog/User/CreateUser.vue | 16 ++++++++++++++-- .../src/components/Dialog/User/EditUser.vue | 15 ++++++++++++--- frontend/src/services/api.js | 4 ++-- 6 files changed, 49 insertions(+), 23 deletions(-) diff --git a/backend/endpoints/identity.py b/backend/endpoints/identity.py index 0628aa67d..ce4a96745 100644 --- a/backend/endpoints/identity.py +++ b/backend/endpoints/identity.py @@ -1,4 +1,3 @@ -import os import secrets import base64 import binascii @@ -137,7 +136,7 @@ class UserUpdateForm: self.disabled = disabled -@protected_route(router.patch, "/users/{user_id}", ["users.write"]) +@protected_route(router.put, "/users/{user_id}", ["users.write"]) @requires(["users.write"]) def update_user( request: Request, user_id: int, form_data: Annotated[UserUpdateForm, Depends()] @@ -154,7 +153,7 @@ def update_user( cleaned_data = {} - if form_data.username: + if form_data.username != user.username: existing_user = dbh.get_user_by_username(form_data.username.lower()) if existing_user: raise HTTPException( @@ -181,7 +180,9 @@ def update_user( dbh.update_user(user_id, cleaned_data) # Log out the current user if username or password changed - if cleaned_data.get("username") or cleaned_data.get("hashed_password"): + if request.user.id == user_id and ( + cleaned_data.get("username") or cleaned_data.get("hashed_password") + ): session_id = request.session.get("session_id") if session_id: cache.delete(f"romm:{session_id}") diff --git a/backend/main.py b/backend/main.py index 5db0a6a71..007ce161a 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1,4 +1,3 @@ -import os import uvicorn import alembic.config import re @@ -30,6 +29,14 @@ app.add_middleware( allow_headers=["*"], ) +if ROMM_AUTH_ENABLED and "pytest" not in sys.modules: + # CSRF protection (except endpoints listed in exempt_urls) + app.add_middleware( + CustomCSRFMiddleware, + secret=ROMM_AUTH_SECRET_KEY, + exempt_urls=[re.compile(r"^/token.*"), re.compile(r"^/ws")], + ) + # Handles both basic and oauth authentication app.add_middleware( AuthenticationMiddleware, @@ -44,15 +51,6 @@ app.add_middleware( https_only=False, ) -if "pytest" not in sys.modules and os.environ.get("ROMM_AUTH_ENABLED"): - # CSRF protection (except endpoints listed in exempt_urls) - app.add_middleware( - CustomCSRFMiddleware, - secret=ROMM_AUTH_SECRET_KEY, - exempt_urls=[re.compile(r"^/token.*"), re.compile(r"^/ws")], - ) - - app.include_router(oauth.router) app.include_router(identity.router) app.include_router(platform.router) diff --git a/backend/utils/auth.py b/backend/utils/auth.py index eae1765fd..6c40af9ec 100644 --- a/backend/utils/auth.py +++ b/backend/utils/auth.py @@ -56,15 +56,21 @@ async def get_current_active_user_from_session(conn: HTTPConnection): if not username: return None - # Key exists therefore user is authenticated + # Key exists therefore user is probably authenticated user = dbh.get_user_by_username(username) if user is None: + cache.delete(f"romm:{session_id}") + conn.session["session_id"] = None + raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, + status_code=status.HTTP_403_FORBIDDEN, detail="User not found", ) if user.disabled: + cache.delete(f"romm:{session_id}") + conn.session["session_id"] = None + raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail="Inactive user" ) diff --git a/frontend/src/components/Dialog/User/CreateUser.vue b/frontend/src/components/Dialog/User/CreateUser.vue index d060088b2..d3f868be8 100644 --- a/frontend/src/components/Dialog/User/CreateUser.vue +++ b/frontend/src/components/Dialog/User/CreateUser.vue @@ -16,7 +16,15 @@ emitter.on("showCreateUserDialog", () => { }); async function createUser() { - await createUserApi(user.value); + await createUserApi(user.value).catch(({ response, message }) => { + emitter.emit("snackbarShow", { + msg: `Unable to create user: ${ + response?.data?.detail || response?.statusText || message + }`, + icon: "mdi-close-circle", + color: "red", + }); + }); show.value = false; emitter.emit("refreshView"); } @@ -85,7 +93,11 @@ async function createUser() { Cancel - + Create diff --git a/frontend/src/components/Dialog/User/EditUser.vue b/frontend/src/components/Dialog/User/EditUser.vue index 52888e8a5..fa778e365 100644 --- a/frontend/src/components/Dialog/User/EditUser.vue +++ b/frontend/src/components/Dialog/User/EditUser.vue @@ -1,6 +1,8 @@