allow admin role api keys (#2124)

* allow admin role api keys

* bump to rerun deployment

* types needs explicit export now for APIKey

* remove api_key.role, use User.role instead

* fix formatting

* formatting

* formatting

---------

Co-authored-by: Richard Kuo <rkuo@rkuo.com>
This commit is contained in:
rkuo-danswer
2024-08-13 14:00:57 -07:00
committed by GitHub
parent 5dda047999
commit f15d6d2b59
8 changed files with 68 additions and 9 deletions

View File

@@ -1,10 +1,15 @@
import { Form, Formik } from "formik";
import { PopupSpec } from "@/components/admin/connectors/Popup";
import { TextFormField } from "@/components/admin/connectors/Field";
import {
BooleanFormField,
TextFormField,
} from "@/components/admin/connectors/Field";
import { createApiKey, updateApiKey } from "./lib";
import { Modal } from "@/components/Modal";
import { XIcon } from "@/components/icons/icons";
import { Button, Divider, Text } from "@tremor/react";
import { UserRole } from "@/lib/types";
import { APIKey } from "./types";
interface DanswerApiKeyFormProps {
onClose: () => void;
@@ -42,14 +47,25 @@ export const DanswerApiKeyForm = ({
<Formik
initialValues={{
name: apiKey?.api_key_name || "",
is_admin: apiKey?.api_key_role == "admin" ?? false,
}}
onSubmit={async (values, formikHelpers) => {
formikHelpers.setSubmitting(true);
// Map the boolean to a UserRole string
const role: UserRole = values.is_admin ? "admin" : "basic";
// Prepare the payload with the UserRole
const payload = {
...values,
role, // Assign the role directly as a UserRole type
};
let response;
if (isUpdate) {
response = await updateApiKey(apiKey.api_key_id, values);
response = await updateApiKey(apiKey.api_key_id, payload);
} else {
response = await createApiKey(values);
response = await createApiKey(payload);
}
formikHelpers.setSubmitting(false);
if (response.ok) {
@@ -88,6 +104,15 @@ export const DanswerApiKeyForm = ({
autoCompleteDisabled={true}
/>
<BooleanFormField
small
noPadding
alignTop
name="is_admin"
label="Is Admin?"
subtext="If set, this API key will have access to admin level server API's."
/>
<div className="flex">
<Button
type="submit"

View File

@@ -26,6 +26,7 @@ import { Modal } from "@/components/Modal";
import { Spinner } from "@/components/Spinner";
import { deleteApiKey, regenerateApiKey } from "./lib";
import { DanswerApiKeyForm } from "./DanswerApiKeyForm";
import { APIKey } from "./types";
const API_KEY_TEXT = `
API Keys allow you to access Danswer APIs programmatically. Click the button below to generate a new API Key.
@@ -173,6 +174,7 @@ function Main() {
<TableRow>
<TableHeaderCell>Name</TableHeaderCell>
<TableHeaderCell>API Key</TableHeaderCell>
<TableHeaderCell>Role</TableHeaderCell>
<TableHeaderCell>Regenerate</TableHeaderCell>
<TableHeaderCell>Delete</TableHeaderCell>
</TableRow>
@@ -201,6 +203,9 @@ function Main() {
<TableCell className="max-w-64">
{apiKey.api_key_display}
</TableCell>
<TableCell className="max-w-64">
{apiKey.api_key_role.toUpperCase()}
</TableCell>
<TableCell>
<div
className={`

View File

@@ -1,11 +1,15 @@
interface APIKey {
import { UserRole } from "@/lib/types";
export interface APIKey {
api_key_id: number;
api_key_display: string;
api_key: string | null;
api_key_name: string | null;
api_key_role: UserRole;
user_id: string;
}
interface APIKeyArgs {
export interface APIKeyArgs {
name?: string;
role: UserRole;
}

View File

@@ -14,13 +14,15 @@ export enum UserStatus {
deactivated = "deactivated",
}
export type UserRole = "basic" | "admin";
export interface User {
id: string;
email: string;
is_active: string;
is_superuser: string;
is_verified: string;
role: "basic" | "admin";
role: UserRole;
preferences: UserPreferences;
status: UserStatus;
current_token_created_at?: Date;