From 377f13ceee927541ab89af1bc2a30a4c8247a25d Mon Sep 17 00:00:00 2001 From: "Richard Kuo (Danswer)" Date: Tue, 12 Nov 2024 09:59:34 -0800 Subject: [PATCH 1/5] in progress PoC --- backend/danswer/auth/schemas.py | 1 + backend/danswer/auth/users.py | 16 +++++++- .../app/admin/api-key/DanswerApiKeyForm.tsx | 41 ++++++++++++------- web/src/lib/types.ts | 2 + 4 files changed, 44 insertions(+), 16 deletions(-) diff --git a/backend/danswer/auth/schemas.py b/backend/danswer/auth/schemas.py index 9c81899a421..ce503afe76e 100644 --- a/backend/danswer/auth/schemas.py +++ b/backend/danswer/auth/schemas.py @@ -15,6 +15,7 @@ class UserRole(str, Enum): for all groups they are a member of """ + LIMITED = "limited" BASIC = "basic" ADMIN = "admin" CURATOR = "curator" diff --git a/backend/danswer/auth/users.py b/backend/danswer/auth/users.py index 73f1ec18484..23805e518fb 100644 --- a/backend/danswer/auth/users.py +++ b/backend/danswer/auth/users.py @@ -652,10 +652,24 @@ async def current_user_with_expired_token( return await double_check_user(user, include_expired=True) +async def current_limited_user( + user: User | None = Depends(optional_user), +) -> User | None: + return double_check_user(user) + + async def current_user( user: User | None = Depends(optional_user), ) -> User | None: - return await double_check_user(user) + user = await double_check_user(user) + if not user: + return None + + if user.role == UserRole.LIMITED: + raise BasicAuthenticationError( + detail="Access denied. User role is LIMITED. BASIC or higher permissions are required.", + ) + return user async def current_curator_or_admin_user( diff --git a/web/src/app/admin/api-key/DanswerApiKeyForm.tsx b/web/src/app/admin/api-key/DanswerApiKeyForm.tsx index 03f15e08038..80bb84d626f 100644 --- a/web/src/app/admin/api-key/DanswerApiKeyForm.tsx +++ b/web/src/app/admin/api-key/DanswerApiKeyForm.tsx @@ -2,6 +2,7 @@ import { Form, Formik } from "formik"; import { PopupSpec } from "@/components/admin/connectors/Popup"; import { BooleanFormField, + SelectorFormField, TextFormField, } from "@/components/admin/connectors/Field"; import { createApiKey, updateApiKey } from "./lib"; @@ -9,7 +10,7 @@ import { Modal } from "@/components/Modal"; import { Button } from "@/components/ui/button"; import { Separator } from "@/components/ui/separator"; import Text from "@/components/ui/text"; -import { UserRole } from "@/lib/types"; +import { USER_ROLE_LABELS, UserRole } from "@/lib/types"; import { APIKey } from "./types"; interface DanswerApiKeyFormProps { @@ -39,20 +40,15 @@ export const DanswerApiKeyForm = ({ { formikHelpers.setSubmitting(true); - // Map the boolean to a UserRole string - const role: UserRole = values.is_admin - ? UserRole.ADMIN - : UserRole.BASIC; - // Prepare the payload with the UserRole const payload = { ...values, - role, // Assign the role directly as a UserRole type + role: values.role as UserRole, // Assign the role directly as a UserRole type }; let response; @@ -98,13 +94,28 @@ export const DanswerApiKeyForm = ({ autoCompleteDisabled={true} /> -