Files
iam-service/docs/TENANT_API.md
2026-01-30 16:31:53 +08:00

105 lines
1.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 租户管理 API 使用说明
## 通用约定
### 成功响应
所有成功响应使用 `common-telemetry` 的统一包装:
```json
{
"code": 0,
"message": "Success|Created|Accepted",
"data": {},
"trace_id": null
}
```
### 错误响应
错误响应由 `AppError` 统一转换为 JSON
```json
{
"code": 30000,
"message": "Invalid request parameters: ...",
"details": "...",
"trace_id": null
}
```
常见错误码(节选,来自 `common-telemetry`
- 20006缺少认证 Header`Authorization`
- 20003无权限403
- 30000请求参数错误400
- 30002资源不存在404
- 10001数据库错误500
## 认证与租户上下文
- 受保护接口需要:
- `Authorization: Bearer <access_token>`
- 租户上下文优先来自 Token 的 `tenant_id` claim
- 兼容 `X-Tenant-ID`
- 如果同时传 `X-Tenant-ID` 与 Token则必须一致否则返回 403
## 接口列表
### 1) 租户注册(公共)
`POST /tenants/register`
请求体:
```json
{
"name": "Tenant A",
"config": {
"theme": { "primary": "#1d4ed8" },
"password_policy": { "min_len": 12 }
}
}
```
响应:`TenantResponse`
### 2) 获取当前租户信息(需要 tenant:read
`GET /tenants/me`
请求头:
- `Authorization: Bearer <access_token>`
- `X-Tenant-ID: <uuid>`(可选)
### 3) 更新当前租户信息(需要 tenant:write
`PATCH /tenants/me`
请求体:
```json
{
"name": "New Tenant Name",
"config": { "theme": { "primary": "#0ea5e9" } }
}
```
### 4) 变更当前租户状态(需要 tenant:write
`POST /tenants/me/status`
请求体:
```json
{ "status": "active|disabled|suspended" }
```
### 5) 删除当前租户(需要 tenant:write
`DELETE /tenants/me`
说明:当前实现为物理删除。生产建议改为软删除并触发异步数据清理流程。