Add user-scoped Hermes profile access

This commit is contained in:
ekko
2026-05-23 18:44:53 +08:00
committed by ekko
parent 56e7716302
commit 3f6a25d8f1
54 changed files with 2656 additions and 592 deletions
+80
View File
@@ -3,6 +3,7 @@ import { request } from './client'
export interface AuthStatus {
hasPasswordLogin: boolean
username: string | null
hasUsers?: boolean
}
export async function fetchAuthStatus(): Promise<AuthStatus> {
@@ -27,6 +28,21 @@ export async function loginWithPassword(username: string, password: string): Pro
return data.token
}
export interface CurrentUser {
id: number
username: string
role: UserRole
status: UserStatus
created_at: number
updated_at: number
last_login_at: number | null
}
export async function fetchCurrentUser(): Promise<CurrentUser> {
const res = await request<{ user: CurrentUser }>('/api/auth/me')
return res.user
}
export async function setupPassword(username: string, password: string): Promise<void> {
return request('/api/auth/setup', {
method: 'POST',
@@ -54,6 +70,70 @@ export async function removePassword(): Promise<void> {
})
}
export type UserRole = 'super_admin' | 'admin'
export type UserStatus = 'active' | 'disabled'
export interface ManagedUser {
id: number
username: string
role: UserRole
status: UserStatus
profiles: string[]
default_profile: string | null
created_at: number
updated_at: number
last_login_at: number | null
}
export interface ManagedUsersResponse {
users: ManagedUser[]
profiles: string[]
}
export async function fetchManagedUsers(): Promise<ManagedUsersResponse> {
return request<ManagedUsersResponse>('/api/auth/users')
}
export async function createManagedUser(input: {
username: string
password: string
role: UserRole
status: UserStatus
profiles: string[]
defaultProfile?: string | null
}): Promise<ManagedUsersResponse> {
const res = await request<{ users: ManagedUser[] }>('/api/auth/users', {
method: 'POST',
body: JSON.stringify(input),
})
const current = await fetchManagedUsers()
return { ...current, users: res.users }
}
export async function updateManagedUser(id: number, input: {
username?: string
password?: string
role?: UserRole
status?: UserStatus
profiles?: string[]
defaultProfile?: string | null
}): Promise<ManagedUsersResponse> {
const res = await request<{ users: ManagedUser[] }>(`/api/auth/users/${id}`, {
method: 'PUT',
body: JSON.stringify(input),
})
const current = await fetchManagedUsers()
return { ...current, users: res.users }
}
export async function deleteManagedUser(id: number): Promise<ManagedUsersResponse> {
const res = await request<{ users: ManagedUser[] }>(`/api/auth/users/${id}`, {
method: 'DELETE',
})
const current = await fetchManagedUsers()
return { ...current, users: res.users }
}
export interface LockedIp {
ip: string
type: 'password' | 'token'