mirror of
https://github.com/labring/FastGPT.git
synced 2025-08-06 07:28:56 +00:00
Permission (#1687)
Co-authored-by: Archer <545436317@qq.com> Co-authored-by: Finley Ge <32237950+FinleyGe@users.noreply.github.com>
This commit is contained in:
@@ -4,13 +4,15 @@ import { connectToDatabase } from '@/service/mongo';
|
||||
import { MongoOpenApi } from '@fastgpt/service/support/openapi/schema';
|
||||
import { customAlphabet } from 'nanoid';
|
||||
import type { EditApiKeyProps } from '@/global/support/openapi/api';
|
||||
import { authUserNotVisitor } from '@fastgpt/service/support/permission/auth/user';
|
||||
import { authUserPer } from '@fastgpt/service/support/permission/user/auth';
|
||||
import { WritePermissionVal } from '@fastgpt/global/support/permission/constant';
|
||||
import { getNanoid } from '@fastgpt/global/common/string/tools';
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
try {
|
||||
await connectToDatabase();
|
||||
const { appId, name, limit } = req.body as EditApiKeyProps;
|
||||
const { teamId, tmbId } = await authUserNotVisitor({ req, authToken: true });
|
||||
const { teamId, tmbId } = await authUserPer({ req, authToken: true, per: WritePermissionVal });
|
||||
|
||||
const count = await MongoOpenApi.find({ tmbId, appId }).countDocuments();
|
||||
|
||||
@@ -18,11 +20,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
throw new Error('最多 10 组 API 秘钥');
|
||||
}
|
||||
|
||||
const nanoid = customAlphabet(
|
||||
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890',
|
||||
Math.floor(Math.random() * 14) + 52
|
||||
);
|
||||
const apiKey = `${global.systemEnv?.openapiPrefix || 'fastgpt'}-${nanoid()}`;
|
||||
const nanoid = getNanoid(Math.floor(Math.random() * 14) + 52);
|
||||
const apiKey = `${global.systemEnv?.openapiPrefix || 'fastgpt'}-${nanoid}`;
|
||||
|
||||
await MongoOpenApi.create({
|
||||
teamId,
|
||||
|
@@ -3,8 +3,9 @@ import { jsonRes } from '@fastgpt/service/common/response';
|
||||
import { connectToDatabase } from '@/service/mongo';
|
||||
import { MongoOpenApi } from '@fastgpt/service/support/openapi/schema';
|
||||
import type { GetApiKeyProps } from '@/global/support/openapi/api';
|
||||
import { authUserNotVisitor } from '@fastgpt/service/support/permission/auth/user';
|
||||
import { authApp } from '@fastgpt/service/support/permission/auth/app';
|
||||
import { authUserPer } from '@fastgpt/service/support/permission/user/auth';
|
||||
import { authApp } from '@fastgpt/service/support/permission/app/auth';
|
||||
import { ManagePermissionVal } from '@fastgpt/global/support/permission/constant';
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
try {
|
||||
@@ -12,11 +13,15 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
const { appId } = req.query as GetApiKeyProps;
|
||||
|
||||
if (appId) {
|
||||
const { tmbId, teamOwner } = await authApp({ req, authToken: true, appId, per: 'w' });
|
||||
await authApp({
|
||||
req,
|
||||
authToken: true,
|
||||
appId,
|
||||
per: ManagePermissionVal
|
||||
});
|
||||
|
||||
const findResponse = await MongoOpenApi.find({
|
||||
appId,
|
||||
...(!teamOwner && { tmbId })
|
||||
appId
|
||||
}).sort({ _id: -1 });
|
||||
|
||||
return jsonRes(res, {
|
||||
@@ -24,16 +29,16 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
});
|
||||
}
|
||||
|
||||
const {
|
||||
teamId,
|
||||
tmbId,
|
||||
isOwner: teamOwner
|
||||
} = await authUserNotVisitor({ req, authToken: true });
|
||||
const { teamId, tmbId, permission } = await authUserPer({
|
||||
req,
|
||||
authToken: true,
|
||||
per: ManagePermissionVal
|
||||
});
|
||||
|
||||
const findResponse = await MongoOpenApi.find({
|
||||
appId,
|
||||
teamId,
|
||||
...(!teamOwner && { tmbId })
|
||||
...(!permission.isOwner && { tmbId })
|
||||
}).sort({ _id: -1 });
|
||||
|
||||
return jsonRes(res, {
|
||||
|
Reference in New Issue
Block a user