perf: logs, auth root as super admin, etc (#2615)

* chore: usePagination hook type

* feat: chat log show outlinkuid or tmb avatar and name

* fix: ts error for pagination

* feat: auth root
This commit is contained in:
Finley Ge
2024-09-09 10:05:18 +08:00
committed by GitHub
parent 91ec895fd2
commit a1ae08f62b
19 changed files with 127 additions and 66 deletions

View File

@@ -38,11 +38,13 @@ export const authPluginByTmbId = async ({
export const authAppByTmbId = async ({
tmbId,
appId,
per
per,
isRoot
}: {
tmbId: string;
appId: string;
per: PermissionValueType;
isRoot?: boolean;
}): Promise<{
app: AppDetailType;
}> => {
@@ -55,6 +57,14 @@ export const authAppByTmbId = async ({
return Promise.reject(AppErrEnum.unExist);
}
if (isRoot) {
return {
...app,
defaultPermission: app.defaultPermission,
permission: new AppPermission({ isOwner: true })
};
}
if (String(app.teamId) !== teamId) {
return Promise.reject(AppErrEnum.unAuthApp);
}
@@ -136,7 +146,8 @@ export const authApp = async ({
const { app } = await authAppByTmbId({
tmbId,
appId,
per
per,
isRoot: result.isRoot
});
return {

View File

@@ -78,13 +78,18 @@ export const delResourcePermission = ({
/* 下面代码等迁移 */
/* create token */
export function createJWT(user: { _id?: string; team?: { teamId?: string; tmbId: string } }) {
export function createJWT(user: {
_id?: string;
team?: { teamId?: string; tmbId: string };
isRoot?: boolean;
}) {
const key = process.env.TOKEN_KEY as string;
const token = jwt.sign(
{
userId: String(user._id),
teamId: String(user.team?.teamId),
tmbId: String(user.team?.tmbId),
isRoot: user.isRoot,
exp: Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 7
},
key
@@ -98,6 +103,7 @@ export function authJWT(token: string) {
userId: string;
teamId: string;
tmbId: string;
isRoot: boolean;
}>((resolve, reject) => {
const key = process.env.TOKEN_KEY as string;
@@ -110,7 +116,8 @@ export function authJWT(token: string) {
resolve({
userId: decoded.userId,
teamId: decoded.teamId || '',
tmbId: decoded.tmbId
tmbId: decoded.tmbId,
isRoot: decoded.isRoot
});
});
});
@@ -183,7 +190,7 @@ export async function parseHeaderCert({
const { cookie, token, rootkey, authorization } = (req.headers || {}) as ReqHeaderAuthType;
const { uid, teamId, tmbId, appId, openApiKey, authType } = await (async () => {
const { uid, teamId, tmbId, appId, openApiKey, authType, isRoot } = await (async () => {
if (authApiKey && authorization) {
// apikey from authorization
const authResponse = await parseAuthorization(authorization);
@@ -205,7 +212,8 @@ export async function parseHeaderCert({
tmbId: res.tmbId,
appId: '',
openApiKey: '',
authType: AuthUserTypeEnum.token
authType: AuthUserTypeEnum.token,
isRoot: res.isRoot
};
}
if (authRoot && rootkey) {
@@ -217,7 +225,8 @@ export async function parseHeaderCert({
tmbId: '',
appId: '',
openApiKey: '',
authType: AuthUserTypeEnum.root
authType: AuthUserTypeEnum.root,
isRoot: true
};
}
@@ -234,7 +243,8 @@ export async function parseHeaderCert({
tmbId: String(tmbId),
appId,
authType,
apikey: openApiKey
apikey: openApiKey,
isRoot: !!isRoot
};
}

View File

@@ -24,11 +24,13 @@ import { ParentIdType } from '@fastgpt/global/common/parentFolder/type';
export const authDatasetByTmbId = async ({
tmbId,
datasetId,
per
per,
isRoot = false
}: {
tmbId: string;
datasetId: string;
per: PermissionValueType;
isRoot?: boolean;
}): Promise<{
dataset: DatasetSchemaType & {
permission: DatasetPermission;
@@ -44,6 +46,15 @@ export const authDatasetByTmbId = async ({
return Promise.reject(DatasetErrEnum.unExist);
}
if (isRoot) {
return {
...dataset,
permission: new DatasetPermission({
isOwner: true
})
};
}
if (String(dataset.teamId) !== teamId) {
return Promise.reject(DatasetErrEnum.unAuthDataset);
}
@@ -131,7 +142,8 @@ export const authDataset = async ({
const { dataset } = await authDatasetByTmbId({
tmbId,
datasetId,
per
per,
isRoot: result.isRoot
});
return {
@@ -144,15 +156,17 @@ export const authDataset = async ({
export async function authDatasetCollection({
collectionId,
per = NullPermission,
isRoot = false,
...props
}: AuthModeType & {
collectionId: string;
isRoot?: boolean;
}): Promise<
AuthResponseType<DatasetPermission> & {
collection: CollectionWithDatasetType;
}
> {
const { teamId, tmbId } = await parseHeaderCert(props);
const { teamId, tmbId, isRoot: isRootFromHeader } = await parseHeaderCert(props);
const collection = await getCollectionWithDataset(collectionId);
if (!collection) {
@@ -162,7 +176,8 @@ export async function authDatasetCollection({
const { dataset } = await authDatasetByTmbId({
tmbId,
datasetId: collection.datasetId._id,
per
per,
isRoot: isRootFromHeader || isRoot
});
return {
@@ -184,7 +199,7 @@ export async function authDatasetFile({
file: DatasetFileSchema;
}
> {
const { teamId, tmbId } = await parseHeaderCert(props);
const { teamId, tmbId, isRoot } = await parseHeaderCert(props);
const [file, collection] = await Promise.all([
getFileById({ bucketName: BucketNameEnum.dataset, fileId }),
@@ -206,7 +221,8 @@ export async function authDatasetFile({
const { permission } = await authDatasetCollection({
...props,
collectionId: collection._id,
per
per,
isRoot
});
return {

View File

@@ -4,6 +4,7 @@ import { getTmbInfoByTmbId } from '../../user/team/controller';
import { TeamErrEnum } from '@fastgpt/global/common/error/code/team';
import { AuthModeType, AuthResponseType } from '../type';
import { NullPermission } from '@fastgpt/global/support/permission/constant';
import { TeamPermission } from '@fastgpt/global/support/permission/user/controller';
/* auth user role */
export async function authUserPer(props: AuthModeType): Promise<
@@ -14,6 +15,15 @@ export async function authUserPer(props: AuthModeType): Promise<
const result = await parseHeaderCert(props);
const tmb = await getTmbInfoByTmbId({ tmbId: result.tmbId });
if (result.isRoot) {
return {
...result,
permission: new TeamPermission({
isOwner: true
}),
tmb
};
}
if (!tmb.permission.checkPer(props.per ?? NullPermission)) {
return Promise.reject(TeamErrEnum.unAuthTeam);
}

View File

@@ -16,7 +16,7 @@ type PagingData<T> = {
total?: number;
};
export function usePagination<T = any>({
export function usePagination<ResT = any>({
api,
pageSize = 10,
params = {},
@@ -25,7 +25,7 @@ export function usePagination<T = any>({
onChange,
elementRef
}: {
api: (data: any) => any;
api: (data: any) => Promise<PagingData<ResT>>;
pageSize?: number;
params?: Record<string, any>;
defaultRequest?: boolean;
@@ -41,7 +41,7 @@ export function usePagination<T = any>({
const [total, setTotal] = useState(0);
const totalRef = useRef(total);
totalRef.current = total;
const [data, setData] = useState<T[]>([]);
const [data, setData] = useState<ResT[]>([]);
const dataLengthRef = useRef(data.length);
dataLengthRef.current = data.length;
const maxPage = useMemo(() => Math.ceil(total / pageSize) || 1, [pageSize, total]);
@@ -49,7 +49,7 @@ export function usePagination<T = any>({
const { mutate, isLoading } = useMutation({
mutationFn: async (num: number = pageNum) => {
try {
const res: PagingData<T> = await api({
const res: PagingData<ResT> = await api({
pageNum: num,
pageSize,
...params
@@ -107,7 +107,7 @@ export function usePagination<T = any>({
onKeyDown={(e) => {
// @ts-ignore
const val = +e.target.value;
if (val && e.keyCode === 13) {
if (val && e.key === 'Enter') {
if (val === pageNum) return;
if (val >= maxPage) {
mutate(maxPage);

View File

@@ -72,6 +72,7 @@
"logs_empty": "还没有日志噢~",
"logs_message_total": "消息总数",
"logs_title": "标题",
"logs_chat_user": "使用者",
"mark_count": "标注答案数量",
"module": {
"Confirm Sync": "将会更新至最新的模板配置,不存在模板中的字段将会被删除(包括所有自定义字段),建议您先复制一份节点,再更新原来节点的版本。",