mirror of
https://github.com/Wei-Shaw/sub2api
synced 2026-12-06 17:32:31 +00:00
- 系统设置 → 注册设置新增「邮箱黑名单(精确匹配)」,弹窗编辑、批量粘贴,上限 20000 条, 名单存于 settings 键 registration_email_blacklist,由专用接口整体读写;运行时读 60s TTL 进程内缓存,保存后本实例立即生效 - 按收件箱身份匹配(复用 NormalizeEmailForAliasDedup):Gmail 点号、googlemail.com 与 +后缀 变体视为同一邮箱 - 邮箱注册、发验证码、第三方登录补建账号、绑定/换绑邮箱命中时返回 EMAIL_BLOCKED; 已验证邮箱的 OAuth 登录只在新建账号时拦截,已有账号是否可用由账号状态决定 - 保存后对新增项查找仍启用的普通用户并弹窗确认批量禁用(跳过管理员、已禁用与已删除用户), 与单个禁用一样在同一事务内吊销 MCP 授权,禁用后清理认证缓存;也可随时对整份名单手动检查
456 lines
12 KiB
TypeScript
456 lines
12 KiB
TypeScript
/**
|
|
* Admin Users API endpoints
|
|
* Handles user management for administrators
|
|
*/
|
|
|
|
import { apiClient } from '../client'
|
|
import { listPlatformIds } from '@/constants/platformCatalog'
|
|
import type { AccountPlatform, AdminUser, UpdateUserRequest, PaginatedResponse, ApiKey } from '@/types'
|
|
|
|
export interface AdminBindAuthIdentityChannelRequest {
|
|
channel: string
|
|
channel_app_id: string
|
|
channel_subject: string
|
|
metadata?: Record<string, unknown> | null
|
|
}
|
|
|
|
export interface AdminBindAuthIdentityRequest {
|
|
provider_type: string
|
|
provider_key: string
|
|
provider_subject: string
|
|
issuer?: string | null
|
|
metadata?: Record<string, unknown> | null
|
|
channel?: AdminBindAuthIdentityChannelRequest
|
|
}
|
|
|
|
export interface AdminBoundAuthIdentityChannel {
|
|
channel: string
|
|
channel_app_id: string
|
|
channel_subject: string
|
|
metadata: Record<string, unknown> | null
|
|
created_at: string
|
|
updated_at: string
|
|
}
|
|
|
|
export interface AdminBoundAuthIdentity {
|
|
user_id: number
|
|
provider_type: string
|
|
provider_key: string
|
|
provider_subject: string
|
|
verified_at?: string | null
|
|
issuer?: string | null
|
|
metadata: Record<string, unknown> | null
|
|
created_at: string
|
|
updated_at: string
|
|
channel?: AdminBoundAuthIdentityChannel | null
|
|
}
|
|
|
|
export interface BatchUpdateUserLimitsRequest {
|
|
user_ids: number[]
|
|
all?: boolean
|
|
concurrency?: number
|
|
rpm_limit?: number
|
|
}
|
|
|
|
export interface BatchUpdateUserLimitsResponse {
|
|
affected: number
|
|
}
|
|
|
|
/**
|
|
* List all users with pagination
|
|
* @param page - Page number (default: 1)
|
|
* @param pageSize - Items per page (default: 20)
|
|
* @param filters - Optional filters (status, role, search, attributes)
|
|
* @param options - Optional request options (signal)
|
|
* @returns Paginated list of users
|
|
*/
|
|
export async function list(
|
|
page: number = 1,
|
|
pageSize: number = 20,
|
|
filters?: {
|
|
status?: 'active' | 'disabled'
|
|
role?: 'admin' | 'user'
|
|
search?: string
|
|
group_name?: string // fuzzy filter by allowed group name
|
|
api_key_group_id?: number // filter users by the group their API keys are bound to
|
|
attributes?: Record<number, string> // attributeId -> value
|
|
include_subscriptions?: boolean
|
|
sort_by?: string
|
|
sort_order?: 'asc' | 'desc'
|
|
},
|
|
options?: {
|
|
signal?: AbortSignal
|
|
}
|
|
): Promise<PaginatedResponse<AdminUser>> {
|
|
// Build params with attribute filters in attr[id]=value format
|
|
const params: Record<string, any> = {
|
|
page,
|
|
page_size: pageSize,
|
|
status: filters?.status,
|
|
role: filters?.role,
|
|
search: filters?.search,
|
|
group_name: filters?.group_name,
|
|
api_key_group_id: filters?.api_key_group_id,
|
|
include_subscriptions: filters?.include_subscriptions,
|
|
sort_by: filters?.sort_by,
|
|
sort_order: filters?.sort_order
|
|
}
|
|
|
|
// Add attribute filters as attr[id]=value
|
|
if (filters?.attributes) {
|
|
for (const [attrId, value] of Object.entries(filters.attributes)) {
|
|
if (value) {
|
|
params[`attr[${attrId}]`] = value
|
|
}
|
|
}
|
|
}
|
|
const { data } = await apiClient.get<PaginatedResponse<AdminUser>>('/admin/users', {
|
|
params,
|
|
signal: options?.signal
|
|
})
|
|
return data
|
|
}
|
|
|
|
/**
|
|
* Get user by ID
|
|
* @param id - User ID
|
|
* @param includeDeleted - Whether to include soft-deleted users
|
|
* @returns User details
|
|
*/
|
|
export async function getById(id: number, includeDeleted = false): Promise<AdminUser> {
|
|
const url = includeDeleted ? `/admin/users/${id}?include_deleted=true` : `/admin/users/${id}`
|
|
const { data } = await apiClient.get<AdminUser>(url)
|
|
return data
|
|
}
|
|
|
|
/**
|
|
* Create new user
|
|
* @param userData - User data (email, password, etc.)
|
|
* @returns Created user
|
|
*/
|
|
export async function create(userData: {
|
|
email: string
|
|
password: string
|
|
username?: string
|
|
notes?: string
|
|
role?: 'admin' | 'user'
|
|
balance?: number
|
|
concurrency?: number
|
|
rpm_limit?: number
|
|
allowed_groups?: number[] | null
|
|
}): Promise<AdminUser> {
|
|
const { data } = await apiClient.post<AdminUser>('/admin/users', userData)
|
|
return data
|
|
}
|
|
|
|
/**
|
|
* Update user
|
|
* @param id - User ID
|
|
* @param updates - Fields to update
|
|
* @returns Updated user
|
|
*/
|
|
export async function update(id: number, updates: UpdateUserRequest): Promise<AdminUser> {
|
|
const { data } = await apiClient.put<AdminUser>(`/admin/users/${id}`, updates)
|
|
return data
|
|
}
|
|
|
|
/**
|
|
* Delete user
|
|
* @param id - User ID
|
|
* @returns Success confirmation
|
|
*/
|
|
export async function deleteUser(id: number): Promise<{ message: string }> {
|
|
const { data } = await apiClient.delete<{ message: string }>(`/admin/users/${id}`)
|
|
return data
|
|
}
|
|
|
|
/**
|
|
* Update user balance
|
|
* @param id - User ID
|
|
* @param balance - New balance
|
|
* @param operation - Operation type ('set', 'add', 'subtract')
|
|
* @param notes - Optional notes for the balance adjustment
|
|
* @returns Updated user
|
|
*/
|
|
export async function updateBalance(
|
|
id: number,
|
|
balance: number,
|
|
operation: 'set' | 'add' | 'subtract' = 'set',
|
|
notes?: string
|
|
): Promise<AdminUser> {
|
|
const { data } = await apiClient.post<AdminUser>(`/admin/users/${id}/balance`, {
|
|
balance,
|
|
operation,
|
|
notes: notes || ''
|
|
})
|
|
return data
|
|
}
|
|
|
|
/**
|
|
* Update user concurrency
|
|
* @param id - User ID
|
|
* @param concurrency - New concurrency limit
|
|
* @returns Updated user
|
|
*/
|
|
export async function updateConcurrency(id: number, concurrency: number): Promise<AdminUser> {
|
|
return update(id, { concurrency })
|
|
}
|
|
|
|
/** Overwrite concurrency and/or RPM limits for multiple users in one request. */
|
|
export async function batchUpdateLimits(
|
|
request: BatchUpdateUserLimitsRequest
|
|
): Promise<BatchUpdateUserLimitsResponse> {
|
|
const { data } = await apiClient.post<BatchUpdateUserLimitsResponse>(
|
|
'/admin/users/batch-limits',
|
|
request
|
|
)
|
|
return data
|
|
}
|
|
|
|
/** Active non-admin user whose inbox identity matches a blacklisted email. */
|
|
export interface BlacklistedEmailUser {
|
|
id: number
|
|
email: string
|
|
username: string
|
|
}
|
|
|
|
/** Find active non-admin users whose email (or an alias of it) is in the given list. */
|
|
export async function findBlacklistedEmailUsers(
|
|
emails: string[]
|
|
): Promise<{ items: BlacklistedEmailUser[]; total: number }> {
|
|
const { data } = await apiClient.post<{ items: BlacklistedEmailUser[]; total: number }>(
|
|
'/admin/users/blacklist-matches',
|
|
{ emails }
|
|
)
|
|
return data
|
|
}
|
|
|
|
/** Disable multiple users; admins and already-disabled users are skipped. */
|
|
export async function batchDisable(userIds: number[]): Promise<{ affected: number }> {
|
|
const { data } = await apiClient.post<{ affected: number }>('/admin/users/batch-disable', {
|
|
user_ids: userIds
|
|
})
|
|
return data
|
|
}
|
|
|
|
/**
|
|
* Toggle user status
|
|
* @param id - User ID
|
|
* @param status - New status
|
|
* @returns Updated user
|
|
*/
|
|
export async function toggleStatus(id: number, status: 'active' | 'disabled'): Promise<AdminUser> {
|
|
return update(id, { status })
|
|
}
|
|
|
|
/**
|
|
* Get user's API keys
|
|
* @param id - User ID
|
|
* @returns List of user's API keys
|
|
*/
|
|
export async function getUserApiKeys(id: number): Promise<PaginatedResponse<ApiKey>> {
|
|
const { data } = await apiClient.get<PaginatedResponse<ApiKey>>(`/admin/users/${id}/api-keys`)
|
|
return data
|
|
}
|
|
|
|
/**
|
|
* Get user's usage statistics
|
|
* @param id - User ID
|
|
* @param period - Time period
|
|
* @returns User usage statistics
|
|
*/
|
|
export async function getUserUsageStats(
|
|
id: number,
|
|
period: string = 'month'
|
|
): Promise<{
|
|
total_requests: number
|
|
total_cost: number
|
|
total_tokens: number
|
|
}> {
|
|
const { data } = await apiClient.get<{
|
|
total_requests: number
|
|
total_cost: number
|
|
total_tokens: number
|
|
}>(`/admin/users/${id}/usage`, {
|
|
params: { period }
|
|
})
|
|
return data
|
|
}
|
|
|
|
/**
|
|
* Balance history item returned from the API
|
|
*/
|
|
export interface BalanceHistoryItem {
|
|
id: number
|
|
code: string
|
|
type: string
|
|
value: number
|
|
status: string
|
|
used_by: number | null
|
|
used_at: string | null
|
|
created_at: string
|
|
group_id: number | null
|
|
validity_days: number
|
|
notes: string
|
|
user?: { id: number; email: string } | null
|
|
group?: { id: number; name: string } | null
|
|
}
|
|
|
|
// Balance history response extends pagination with total_recharged summary
|
|
export interface BalanceHistoryResponse extends PaginatedResponse<BalanceHistoryItem> {
|
|
total_recharged: number
|
|
}
|
|
|
|
/**
|
|
* Get user's balance/concurrency change history
|
|
* @param id - User ID
|
|
* @param page - Page number
|
|
* @param pageSize - Items per page
|
|
* @param type - Optional type filter (balance, affiliate_balance, admin_balance, concurrency, admin_concurrency, subscription)
|
|
* @returns Paginated balance history with total_recharged
|
|
*/
|
|
export async function getUserBalanceHistory(
|
|
id: number,
|
|
page: number = 1,
|
|
pageSize: number = 20,
|
|
type?: string
|
|
): Promise<BalanceHistoryResponse> {
|
|
const params: Record<string, any> = { page, page_size: pageSize }
|
|
if (type) params.type = type
|
|
const { data } = await apiClient.get<BalanceHistoryResponse>(
|
|
`/admin/users/${id}/balance-history`,
|
|
{ params }
|
|
)
|
|
return data
|
|
}
|
|
|
|
/**
|
|
* Replace user's exclusive group
|
|
* @param userId - User ID
|
|
* @param oldGroupId - Current group ID to replace
|
|
* @param newGroupId - New group ID to replace with
|
|
* @returns Number of migrated keys
|
|
*/
|
|
export async function replaceGroup(
|
|
userId: number,
|
|
oldGroupId: number,
|
|
newGroupId: number
|
|
): Promise<{ migrated_keys: number }> {
|
|
const { data } = await apiClient.post<{ migrated_keys: number }>(
|
|
`/admin/users/${userId}/replace-group`,
|
|
{ old_group_id: oldGroupId, new_group_id: newGroupId }
|
|
)
|
|
return data
|
|
}
|
|
|
|
export async function bindUserAuthIdentity(
|
|
userId: number,
|
|
input: AdminBindAuthIdentityRequest
|
|
): Promise<AdminBoundAuthIdentity> {
|
|
const { data } = await apiClient.post<AdminBoundAuthIdentity>(
|
|
`/admin/users/${userId}/auth-identities`,
|
|
input
|
|
)
|
|
return data
|
|
}
|
|
|
|
/**
|
|
* Platform quota types
|
|
*/
|
|
// 与后端 AllowedQuotaPlatforms 同源:平台清单中的全部具体平台。
|
|
export function platformQuotaPlatforms(): PlatformQuotaPlatform[] {
|
|
return listPlatformIds()
|
|
}
|
|
export type PlatformQuotaPlatform = AccountPlatform
|
|
export type PlatformQuotaWindow = 'daily' | 'weekly' | 'monthly'
|
|
|
|
export interface PlatformQuotaItem {
|
|
platform: PlatformQuotaPlatform
|
|
daily_limit_usd: number | null
|
|
weekly_limit_usd: number | null
|
|
monthly_limit_usd: number | null
|
|
daily_usage_usd: number
|
|
weekly_usage_usd: number
|
|
monthly_usage_usd: number
|
|
daily_window_start?: string | null
|
|
weekly_window_start?: string | null
|
|
monthly_window_start?: string | null
|
|
daily_window_resets_at?: string | null
|
|
weekly_window_resets_at?: string | null
|
|
monthly_window_resets_at?: string | null
|
|
}
|
|
|
|
export interface PlatformQuotaUpdateItem {
|
|
platform: PlatformQuotaPlatform
|
|
daily_limit_usd: number | null
|
|
weekly_limit_usd: number | null
|
|
monthly_limit_usd: number | null
|
|
}
|
|
|
|
export interface PlatformQuotasResponse {
|
|
platform_quotas: PlatformQuotaItem[]
|
|
}
|
|
|
|
/**
|
|
* Get user's platform quotas
|
|
*/
|
|
export async function getPlatformQuotas(id: number): Promise<PlatformQuotasResponse> {
|
|
const { data } = await apiClient.get<PlatformQuotasResponse>(
|
|
`/admin/users/${id}/platform-quotas`
|
|
)
|
|
return data
|
|
}
|
|
|
|
/**
|
|
* Replace user's platform quotas (全量替换)
|
|
*/
|
|
export async function updatePlatformQuotas(
|
|
id: number,
|
|
quotas: PlatformQuotaUpdateItem[]
|
|
): Promise<PlatformQuotasResponse> {
|
|
const { data } = await apiClient.put<PlatformQuotasResponse>(
|
|
`/admin/users/${id}/platform-quotas`,
|
|
{ quotas }
|
|
)
|
|
return data
|
|
}
|
|
|
|
/**
|
|
* Reset a single (platform, window) usage immediately
|
|
*/
|
|
export async function resetPlatformQuotaWindow(
|
|
id: number,
|
|
platform: PlatformQuotaPlatform,
|
|
window: PlatformQuotaWindow
|
|
): Promise<PlatformQuotasResponse> {
|
|
const { data } = await apiClient.post<PlatformQuotasResponse>(
|
|
`/admin/users/${id}/platform-quotas/reset`,
|
|
{ platform, window }
|
|
)
|
|
return data
|
|
}
|
|
|
|
export const usersAPI = {
|
|
list,
|
|
getById,
|
|
create,
|
|
update,
|
|
delete: deleteUser,
|
|
updateBalance,
|
|
updateConcurrency,
|
|
batchUpdateLimits,
|
|
findBlacklistedEmailUsers,
|
|
batchDisable,
|
|
toggleStatus,
|
|
getUserApiKeys,
|
|
getUserUsageStats,
|
|
getUserBalanceHistory,
|
|
replaceGroup,
|
|
bindUserAuthIdentity,
|
|
getPlatformQuotas,
|
|
updatePlatformQuotas,
|
|
resetPlatformQuotaWindow,
|
|
}
|
|
|
|
export default usersAPI
|