update api key manage
This commit is contained in:
@@ -102,6 +102,27 @@ class ApiKeyController extends AbstractController
|
||||
])->withStatus(400);
|
||||
}
|
||||
|
||||
// 同用户内 Key 名称唯一
|
||||
$name_exists = ApiKey::query()
|
||||
->where('user_id', $user->id)
|
||||
->where('name', $name)
|
||||
->exists();
|
||||
if ($name_exists) {
|
||||
return $this->response->json([
|
||||
'code' => 400,
|
||||
'message' => '已存在同名的 API Key,请使用不同的名称',
|
||||
])->withStatus(400);
|
||||
}
|
||||
|
||||
// 每用户最多 10 个 Key
|
||||
$key_count = ApiKey::query()->where('user_id', $user->id)->count();
|
||||
if ($key_count >= 10) {
|
||||
return $this->response->json([
|
||||
'code' => 400,
|
||||
'message' => '每个用户最多创建 10 个 API Key',
|
||||
])->withStatus(400);
|
||||
}
|
||||
|
||||
// 校验过期时间格式
|
||||
if ($expires_at !== null && $expires_at !== '') {
|
||||
try {
|
||||
@@ -241,4 +262,91 @@ class ApiKeyController extends AbstractController
|
||||
'message' => '删除成功',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 启用/禁用自己的 API Key
|
||||
*/
|
||||
#[OA\Patch(
|
||||
path: '/me/api-keys/{id}/toggle',
|
||||
summary: '启用/禁用自己的 API Key',
|
||||
description: '用户切换自己 API Key 的启用状态',
|
||||
security: [['bearerAuth' => []]],
|
||||
tags: ['API Keys'],
|
||||
parameters: [
|
||||
new OA\Parameter(name: 'id', in: 'path', required: true, description: 'API Key ID', schema: new OA\Schema(type: 'integer')),
|
||||
],
|
||||
requestBody: new OA\RequestBody(
|
||||
required: true,
|
||||
content: new OA\JsonContent(
|
||||
required: ['enabled'],
|
||||
properties: [
|
||||
new OA\Property(property: 'enabled', type: 'boolean', description: '是否启用'),
|
||||
]
|
||||
)
|
||||
),
|
||||
responses: [
|
||||
new OA\Response(
|
||||
response: 200,
|
||||
description: '状态更新成功',
|
||||
content: new OA\JsonContent(properties: [
|
||||
new OA\Property(property: 'code', type: 'integer', example: 0),
|
||||
new OA\Property(property: 'message', type: 'string', example: '状态更新成功'),
|
||||
new OA\Property(property: 'data', properties: [
|
||||
new OA\Property(property: 'id', type: 'integer'),
|
||||
new OA\Property(property: 'name', type: 'string'),
|
||||
new OA\Property(property: 'key_prefix', type: 'string'),
|
||||
new OA\Property(property: 'enabled', type: 'boolean'),
|
||||
new OA\Property(property: 'last_used_at', type: 'string', format: 'date-time', nullable: true),
|
||||
new OA\Property(property: 'expires_at', type: 'string', format: 'date-time', nullable: true),
|
||||
new OA\Property(property: 'created_at', type: 'string', format: 'date-time'),
|
||||
], type: 'object'),
|
||||
])
|
||||
),
|
||||
new OA\Response(response: 400, description: '参数错误', content: new OA\JsonContent(ref: '#/components/schemas/ErrorResponse')),
|
||||
new OA\Response(response: 401, description: '未认证', content: new OA\JsonContent(ref: '#/components/schemas/ErrorResponse')),
|
||||
new OA\Response(response: 404, description: 'API Key 不存在', content: new OA\JsonContent(ref: '#/components/schemas/ErrorResponse')),
|
||||
]
|
||||
)]
|
||||
#[RequestMapping(path: "{id}/toggle", methods: "PATCH")]
|
||||
#[Middleware(AuthMiddleware::class)]
|
||||
public function toggle(int $id, AuthManager $auth): \Psr\Http\Message\ResponseInterface|array
|
||||
{
|
||||
$user = $auth->guard('jwt')->user();
|
||||
|
||||
if (!$user instanceof User) {
|
||||
return $this->response->json([
|
||||
'code' => 401,
|
||||
'message' => '未授权',
|
||||
])->withStatus(401);
|
||||
}
|
||||
|
||||
$api_key = ApiKey::query()
|
||||
->where('id', $id)
|
||||
->where('user_id', $user->id)
|
||||
->first();
|
||||
|
||||
if (!$api_key) {
|
||||
return $this->response->json([
|
||||
'code' => 404,
|
||||
'message' => 'API Key 不存在',
|
||||
])->withStatus(404);
|
||||
}
|
||||
|
||||
$enabled = filter_var($this->request->input('enabled'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
|
||||
if ($enabled === null) {
|
||||
return $this->response->json([
|
||||
'code' => 400,
|
||||
'message' => 'enabled 参数不能为空或格式不正确',
|
||||
])->withStatus(400);
|
||||
}
|
||||
|
||||
$api_key->enabled = $enabled;
|
||||
$api_key->save();
|
||||
|
||||
return [
|
||||
'code' => 0,
|
||||
'message' => '状态更新成功',
|
||||
'data' => $api_key,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user