4.6.7-alpha commit (#743)

Co-authored-by: Archer <545436317@qq.com>
Co-authored-by: heheer <71265218+newfish-cmyk@users.noreply.github.com>
This commit is contained in:
Archer
2024-01-19 11:17:28 +08:00
committed by GitHub
parent 8ee7407c4c
commit c031e6dcc9
324 changed files with 8509 additions and 4757 deletions

View File

@@ -6,7 +6,7 @@ import { TeamMemberRoleEnum } from '@fastgpt/global/support/user/team/constant';
import { parseHeaderCert } from '../controller';
import { PermissionTypeEnum } from '@fastgpt/global/support/permission/constant';
import { AppErrEnum } from '@fastgpt/global/common/error/code/app';
import { getTeamInfoByTmbId } from '../../user/team/controller';
import { getTmbInfoByTmbId } from '../../user/team/controller';
// 模型使用权校验
export async function authApp({
@@ -24,7 +24,7 @@ export async function authApp({
> {
const result = await parseHeaderCert(props);
const { teamId, tmbId } = result;
const { role } = await getTeamInfoByTmbId({ tmbId });
const { role } = await getTmbInfoByTmbId({ tmbId });
const { app, isOwner, canWrite } = await (async () => {
// get app

View File

@@ -13,8 +13,9 @@ import {
} from '@fastgpt/global/core/dataset/type';
import { getFileById } from '../../../common/file/gridfs/controller';
import { BucketNameEnum } from '@fastgpt/global/common/file/constants';
import { getTeamInfoByTmbId } from '../../user/team/controller';
import { getTmbInfoByTmbId } from '../../user/team/controller';
import { CommonErrEnum } from '@fastgpt/global/common/error/code/common';
import { MongoDatasetCollection } from '../../../core/dataset/collection/schema';
export async function authDatasetByTmbId({
teamId,
@@ -27,7 +28,7 @@ export async function authDatasetByTmbId({
datasetId: string;
per: AuthModeType['per'];
}) {
const { role } = await getTeamInfoByTmbId({ tmbId });
const { role } = await getTmbInfoByTmbId({ tmbId });
const { dataset, isOwner, canWrite } = await (async () => {
const dataset = await MongoDataset.findOne({ _id: datasetId, teamId }).lean();
@@ -107,7 +108,7 @@ export async function authDatasetCollection({
}
> {
const { userId, teamId, tmbId } = await parseHeaderCert(props);
const { role } = await getTeamInfoByTmbId({ tmbId });
const { role } = await getTmbInfoByTmbId({ tmbId });
const { collection, isOwner, canWrite } = await (async () => {
const collection = await getCollectionWithDataset(collectionId);
@@ -163,47 +164,40 @@ export async function authDatasetFile({
}
> {
const { userId, teamId, tmbId } = await parseHeaderCert(props);
const { role } = await getTeamInfoByTmbId({ tmbId });
const file = await getFileById({ bucketName: BucketNameEnum.dataset, fileId });
const [file, collection] = await Promise.all([
getFileById({ bucketName: BucketNameEnum.dataset, fileId }),
MongoDatasetCollection.findOne({
teamId,
fileId
})
]);
if (!file) {
return Promise.reject(CommonErrEnum.fileNotFound);
}
if (file.metadata.teamId !== teamId) {
if (!collection) {
return Promise.reject(DatasetErrEnum.unAuthDatasetFile);
}
const { dataset } = await authDataset({
...props,
datasetId: file.metadata.datasetId,
per
});
const isOwner =
role !== TeamMemberRoleEnum.visitor &&
(String(dataset.tmbId) === tmbId || role === TeamMemberRoleEnum.owner);
// file role = collection role
try {
const { isOwner, canWrite } = await authDatasetCollection({
...props,
collectionId: collection._id,
per
});
const canWrite =
isOwner ||
(role !== TeamMemberRoleEnum.visitor && dataset.permission === PermissionTypeEnum.public);
if (per === 'r' && !isOwner && dataset.permission !== PermissionTypeEnum.public) {
return {
userId,
teamId,
tmbId,
file,
isOwner,
canWrite
};
} catch (error) {
return Promise.reject(DatasetErrEnum.unAuthDatasetFile);
}
if (per === 'w' && !canWrite) {
return Promise.reject(DatasetErrEnum.unAuthDatasetFile);
}
if (per === 'owner' && !isOwner) {
return Promise.reject(DatasetErrEnum.unAuthDatasetFile);
}
return {
userId,
teamId,
tmbId,
file,
isOwner,
canWrite
};
}

View File

@@ -2,7 +2,7 @@ import { AuthResponseType } from '@fastgpt/global/support/permission/type';
import { AuthModeType } from '../type';
import { OpenApiSchema } from '@fastgpt/global/support/openapi/type';
import { parseHeaderCert } from '../controller';
import { getTeamInfoByTmbId } from '../../user/team/controller';
import { getTmbInfoByTmbId } from '../../user/team/controller';
import { MongoOpenApi } from '../../openapi/schema';
import { OpenApiErrEnum } from '@fastgpt/global/common/error/code/openapi';
import { TeamMemberRoleEnum } from '@fastgpt/global/support/user/team/constant';
@@ -21,7 +21,7 @@ export async function authOpenApiKeyCrud({
const result = await parseHeaderCert(props);
const { tmbId, teamId } = result;
const { role } = await getTeamInfoByTmbId({ tmbId });
const { role } = await getTmbInfoByTmbId({ tmbId });
const { openapi, isOwner, canWrite } = await (async () => {
const openapi = await MongoOpenApi.findOne({ _id: id, teamId });

View File

@@ -9,7 +9,7 @@ import { MongoApp } from '../../../core/app/schema';
import { OutLinkErrEnum } from '@fastgpt/global/common/error/code/outLink';
import { PermissionTypeEnum } from '@fastgpt/global/support/permission/constant';
import { AppErrEnum } from '@fastgpt/global/common/error/code/app';
import { getTeamInfoByTmbId } from '../../user/team/controller';
import { getTmbInfoByTmbId } from '../../user/team/controller';
/* crud outlink permission */
export async function authOutLinkCrud({
@@ -27,7 +27,7 @@ export async function authOutLinkCrud({
const result = await parseHeaderCert(props);
const { tmbId, teamId } = result;
const { role } = await getTeamInfoByTmbId({ tmbId });
const { role } = await getTmbInfoByTmbId({ tmbId });
const { app, outLink, isOwner, canWrite } = await (async () => {
const outLink = await MongoOutLink.findOne({ _id: outLinkId, teamId });

View File

@@ -1,7 +1,7 @@
import { AuthResponseType } from '@fastgpt/global/support/permission/type';
import { AuthModeType } from '../type';
import { parseHeaderCert } from '../controller';
import { getTeamInfoByTmbId } from '../../user/team/controller';
import { getTmbInfoByTmbId } from '../../user/team/controller';
import { TeamMemberRoleEnum } from '@fastgpt/global/support/user/team/constant';
import { MongoPlugin } from '../../../core/plugin/schema';
import { PluginErrEnum } from '@fastgpt/global/common/error/code/plugin';
@@ -23,7 +23,7 @@ export async function authPluginCrud({
const result = await parseHeaderCert(props);
const { tmbId, teamId } = result;
const { role } = await getTeamInfoByTmbId({ tmbId });
const { role } = await getTmbInfoByTmbId({ tmbId });
const { plugin, isOwner, canWrite } = await (async () => {
const plugin = await MongoPlugin.findOne({ _id: id, teamId });
@@ -73,7 +73,7 @@ export async function authPluginCanUse({
}
if (source === PluginSourceEnum.personal) {
const { role } = await getTeamInfoByTmbId({ tmbId });
const { role } = await getTmbInfoByTmbId({ tmbId });
const plugin = await MongoPlugin.findOne({ _id: pluginId, teamId });
if (!plugin) {
return Promise.reject(PluginErrEnum.unExist);

View File

@@ -3,7 +3,7 @@ import { AuthModeType } from '../type';
import { TeamItemType } from '@fastgpt/global/support/user/team/type';
import { TeamMemberRoleEnum } from '@fastgpt/global/support/user/team/constant';
import { parseHeaderCert } from '../controller';
import { getTeamInfoByTmbId } from '../../user/team/controller';
import { getTmbInfoByTmbId } from '../../user/team/controller';
import { UserErrEnum } from '../../../../global/common/error/code/user';
export async function authUserNotVisitor(props: AuthModeType): Promise<
@@ -13,7 +13,7 @@ export async function authUserNotVisitor(props: AuthModeType): Promise<
}
> {
const { userId, teamId, tmbId } = await parseHeaderCert(props);
const team = await getTeamInfoByTmbId({ tmbId });
const team = await getTmbInfoByTmbId({ tmbId });
if (team.role === TeamMemberRoleEnum.visitor) {
return Promise.reject(UserErrEnum.binVisitor);
@@ -38,7 +38,7 @@ export async function authUserRole(props: AuthModeType): Promise<
}
> {
const result = await parseHeaderCert(props);
const { role: userRole, canWrite } = await getTeamInfoByTmbId({ tmbId: result.tmbId });
const { role: userRole, canWrite } = await getTmbInfoByTmbId({ tmbId: result.tmbId });
return {
...result,

View File

@@ -14,7 +14,7 @@ export const checkDatasetLimit = async ({
const usedSize = await getVectorCountByTeamId(teamId);
if (usedSize + insertLen >= maxSize) {
return Promise.reject(`数据库容量已满,无法继续添加。可以在账号页面进行扩容。`);
return Promise.reject(`数据库容量不足,无法继续添加。可以在账号页面进行扩容。`);
}
return;
};

View File

@@ -1,6 +1,6 @@
import { UserType } from '@fastgpt/global/support/user/type';
import { MongoUser } from './schema';
import { getTeamInfoByTmbId, getUserDefaultTeam } from './team/controller';
import { getTmbInfoByTmbId, getUserDefaultTeam } from './team/controller';
import { ERROR_ENUM } from '@fastgpt/global/common/error/errorCode';
import { UserErrEnum } from '@fastgpt/global/common/error/code/user';
@@ -21,16 +21,16 @@ export async function getUserDetail({
tmbId?: string;
userId?: string;
}): Promise<UserType> {
const team = await (async () => {
const tmb = await (async () => {
if (tmbId) {
return getTeamInfoByTmbId({ tmbId });
return getTmbInfoByTmbId({ tmbId });
}
if (userId) {
return getUserDefaultTeam({ userId });
}
return Promise.reject(ERROR_ENUM.unAuthorization);
})();
const user = await MongoUser.findById(team.userId);
const user = await MongoUser.findById(tmb.userId);
if (!user) {
return Promise.reject(ERROR_ENUM.unAuthorization);
@@ -44,7 +44,7 @@ export async function getUserDetail({
timezone: user.timezone,
promotionRate: user.promotionRate,
openaiAccount: user.openaiAccount,
team
team: tmb
};
}

View File

@@ -56,9 +56,18 @@ const UserSchema = new Schema({
timezone: {
type: String,
default: 'Asia/Shanghai'
},
lastLoginTmbId: {
type: Schema.Types.ObjectId
}
});
try {
UserSchema.index({ createTime: -1 });
} catch (error) {
console.log(error);
}
export const MongoUser: Model<UserModelSchema> =
models[userCollectionName] || model(userCollectionName, UserSchema);
MongoUser.syncIndexes();

View File

@@ -8,7 +8,7 @@ import {
import { MongoTeamMember } from './teamMemberSchema';
import { MongoTeam } from './teamSchema';
async function getTeam(match: Record<string, any>): Promise<TeamItemType> {
async function getTeamMember(match: Record<string, any>): Promise<TeamItemType> {
const tmb = (await MongoTeamMember.findOne(match).populate('teamId')) as TeamMemberWithTeamSchema;
if (!tmb) {
@@ -31,11 +31,11 @@ async function getTeam(match: Record<string, any>): Promise<TeamItemType> {
};
}
export async function getTeamInfoByTmbId({ tmbId }: { tmbId: string }) {
export async function getTmbInfoByTmbId({ tmbId }: { tmbId: string }) {
if (!tmbId) {
return Promise.reject('tmbId or userId is required');
}
return getTeam({
return getTeamMember({
_id: new Types.ObjectId(tmbId),
status: notLeaveStatus
});
@@ -45,7 +45,7 @@ export async function getUserDefaultTeam({ userId }: { userId: string }) {
if (!userId) {
return Promise.reject('tmbId or userId is required');
}
return getTeam({
return getTeamMember({
userId: new Types.ObjectId(userId),
defaultTeam: true
});

View File

@@ -24,11 +24,11 @@ const TeamSchema = new Schema({
},
balance: {
type: Number,
default: 2 * PRICE_SCALE
default: 0
},
maxSize: {
type: Number,
default: 5
default: 3
},
limit: {
lastExportDatasetTime: {
@@ -41,7 +41,7 @@ const TeamSchema = new Schema({
});
try {
TeamSchema.index({ lastDatasetBillTime: -1 });
// TeamSchema.index({ createTime: -1 });
} catch (error) {
console.log(error);
}

View File

@@ -25,15 +25,13 @@ export const createTrainingBill = async ({
{
moduleName: 'wallet.moduleName.index',
model: vectorModel,
inputTokens: 0,
outputTokens: 0,
charsLength: 0,
amount: 0
},
{
moduleName: 'wallet.moduleName.qa',
model: agentModel,
inputTokens: 0,
outputTokens: 0,
charsLength: 0,
amount: 0
}
],

View File

@@ -52,10 +52,8 @@ const BillSchema = new Schema({
});
try {
BillSchema.index({ teamId: 1 });
BillSchema.index({ tmbId: 1 });
BillSchema.index({ tmbId: 1, time: 1 });
BillSchema.index({ time: 1 }, { expireAfterSeconds: 90 * 24 * 60 * 60 });
BillSchema.index({ teamId: 1, tmbId: 1, time: -1 });
BillSchema.index({ time: 1 }, { expireAfterSeconds: 180 * 24 * 60 * 60 });
} catch (error) {
console.log(error);
}