MAke work when auth disabled in env

This commit is contained in:
Georges-Antoine Assi
2023-08-17 23:06:22 -04:00
parent 1a3beb1891
commit 0c3bdfe354
6 changed files with 38 additions and 8 deletions

View File

@@ -12,6 +12,7 @@ from models.user import User, Role
from utils.cache import cache
from utils.auth import authenticate_user, get_password_hash
from utils.oauth import protected_route
from config import ROMM_AUTH_ENABLED
router = APIRouter()
@@ -86,7 +87,7 @@ def users(request: Request) -> list[UserSchema]:
@protected_route(router.get, "/users/me", ["me.read"])
@requires(["me.read"])
def current_user(request: Request) -> UserSchema:
def current_user(request: Request) -> UserSchema | None:
return request.user
@@ -110,6 +111,12 @@ def get_user(request: Request, user_id: int) -> UserSchema:
def create_user(
request: Request, username: str, password: str, role: str
) -> UserSchema:
if not ROMM_AUTH_ENABLED:
raise HTTPException(
status_code=400,
detail="Cannot create user: ROMM_AUTH_ENABLED is set to False",
)
user = User(
username=username,
hashed_password=get_password_hash(password),
@@ -138,6 +145,12 @@ class UserUpdateForm:
def update_user(
request: Request, user_id: int, form_data: Annotated[UserUpdateForm, Depends()]
) -> UserSchema:
if not ROMM_AUTH_ENABLED:
raise HTTPException(
status_code=400,
detail="Cannot update user: ROMM_AUTH_ENABLED is set to False",
)
user = dbh.get_user(user_id)
if not user:
raise HTTPException(status_code=404, detail="User not found")
@@ -182,6 +195,12 @@ def update_user(
@protected_route(router.delete, "/users/{user_id}", ["users.write"])
def delete_user(request: Request, user_id: int):
if not ROMM_AUTH_ENABLED:
raise HTTPException(
status_code=400,
detail="Cannot delete user: ROMM_AUTH_ENABLED is set to False",
)
user = dbh.get_user(user_id)
if not user:
raise HTTPException(status_code=404, detail="User not found")

View File

@@ -9,7 +9,7 @@ from fastapi_pagination import add_pagination
from starlette.middleware.authentication import AuthenticationMiddleware
from starlette.middleware.sessions import SessionMiddleware
from config import DEV_PORT, DEV_HOST, ROMM_AUTH_SECRET_KEY
from config import DEV_PORT, DEV_HOST, ROMM_AUTH_SECRET_KEY, ROMM_AUTH_ENABLED
from endpoints import search, platform, rom, identity, oauth, scan # noqa
from utils.socket import socket_app
from utils.auth import (
@@ -64,7 +64,7 @@ app.mount("/ws", socket_app)
# Endpoint to set the CSRF token in cache
@app.get("/heartbeat")
def heartbeat():
return {"status": "ok"}
return {"ROMM_AUTH_ENABLED": ROMM_AUTH_ENABLED}
@app.on_event("startup")

View File

@@ -4,14 +4,20 @@ import { useTheme } from "vuetify";
import axios from "axios";
import cookie from "js-cookie";
import { themes } from "@/styles/themes.js";
import storeAuth from "@/stores/auth.js";
// Props
const auth = storeAuth();
onBeforeMount(async () => {
// Set CSRF token for all requests
await axios.get("/api/heartbeat");
const { data } = await axios.get("/api/heartbeat");
auth.setEnabled(data.ROMM_AUTH_ENABLED ?? false);
axios.defaults.headers.common["x-csrftoken"] = cookie.get("csrftoken");
});
useTheme().global.name.value = themes[localStorage.getItem("theme")] || themes[0];
useTheme().global.name.value =
themes[localStorage.getItem("theme")] || themes[0];
</script>
<template>

View File

@@ -1,12 +1,14 @@
<script setup>
import { ref, inject } from "vue";
import storePlatforms from "@/stores/platforms";
import storeAuth from "@/stores/auth";
import DrawerHeader from "@/components/Drawer/Header.vue";
import PlatformListItem from "@/components/Platform/PlatformListItem.vue";
import LogoutBtn from "@/components/Drawer/LogoutBtn.vue";
import RailFooter from "@/components/Drawer/RailFooter.vue";
// Props
const platforms = storePlatforms();
const auth = storeAuth();
const drawer = ref(undefined);
const open = ref(["Platforms", "Library", "Settings"]);
const rail = ref(localStorage.getItem("rail") == "true");
@@ -127,7 +129,7 @@ emitter.on("toggleDrawerRail", () => {
<template v-slot:append>
<v-divider class="border-opacity-25" :thickness="1" />
<logout-btn :rail="rail" />
<rail-footer v-if="auth.enabled" :rail="rail" />
</template>
</v-navigation-drawer>
</template>

View File

@@ -1,11 +1,14 @@
import { defineStore } from "pinia";
export default defineStore("auth", {
state: () => ({ user: null }),
state: () => ({ enabled: false, user: null }),
actions: {
setUser(user) {
this.user = user;
},
setEnabled(enabled) {
this.enabled = enabled;
},
},
});