From 48248df04de4af6113473939c2e5356ba4c686e5 Mon Sep 17 00:00:00 2001 From: "a.e." <49438478+I-Info@users.noreply.github.com> Date: Wed, 1 Jan 2025 17:19:57 +0800 Subject: [PATCH] feat: permission manage UI for org (#3503) --- .../global/support/user/team/org/type.d.ts | 1 + .../support/permission/org/orgSchema.ts | 12 +- .../components/PermissionManage/index.tsx | 104 +++++++++++++++++- 3 files changed, 110 insertions(+), 7 deletions(-) diff --git a/packages/global/support/user/team/org/type.d.ts b/packages/global/support/user/team/org/type.d.ts index 92fbf512b611..d183169d64e6 100644 --- a/packages/global/support/user/team/org/type.d.ts +++ b/packages/global/support/user/team/org/type.d.ts @@ -20,4 +20,5 @@ type OrgMemberSchemaType = { type OrgType = Omit & { avatar: string; members: OrgMemberSchemaType[]; + permission: TeamPermission; }; diff --git a/packages/service/support/permission/org/orgSchema.ts b/packages/service/support/permission/org/orgSchema.ts index a883c02b746d..6d6bc2bb155e 100644 --- a/packages/service/support/permission/org/orgSchema.ts +++ b/packages/service/support/permission/org/orgSchema.ts @@ -45,12 +45,12 @@ OrgSchema.virtual('members', { localField: '_id', foreignField: 'orgId' }); -OrgSchema.virtual('permission', { - ref: ResourcePermissionCollectionName, - localField: '_id', - foreignField: 'orgId', - justOne: true -}); +// OrgSchema.virtual('permission', { +// ref: ResourcePermissionCollectionName, +// localField: '_id', +// foreignField: 'orgId', +// justOne: true +// }); try { OrgSchema.index({ diff --git a/projects/app/src/pages/account/team/components/PermissionManage/index.tsx b/projects/app/src/pages/account/team/components/PermissionManage/index.tsx index d7159b5027b5..60407debd80f 100644 --- a/projects/app/src/pages/account/team/components/PermissionManage/index.tsx +++ b/projects/app/src/pages/account/team/components/PermissionManage/index.tsx @@ -28,6 +28,7 @@ import { } from '@fastgpt/global/support/permission/user/constant'; import { TeamPermission } from '@fastgpt/global/support/permission/user/controller'; import { useCreation } from 'ahooks'; +import { getOrgList } from '@/web/support/user/team/org/api'; function PermissionManage() { const { t } = useTranslation(); @@ -37,6 +38,23 @@ function PermissionManage() { (v) => v ); + const { + data: orgs = [], + loading: isLoadingOrgs, + refresh: refetchOrgs + } = useRequest2(getOrgList, { + manual: false, + refreshDeps: [userInfo?.team?.teamId] + }); + + const filteredOrgs = useCreation( + () => + orgs.filter( + (org) => org.path !== '' && org.name.toLowerCase().includes(searchKey.toLowerCase()) + ), + [orgs, searchKey] + ); + const { runAsync: refetchClbs, data: clbs = [] } = useRequest2(getTeamClbs, { manual: false, refreshDeps: [userInfo?.team?.teamId] @@ -67,15 +85,18 @@ function PermissionManage() { refetchGroups(); refetchMembers(); refetchClbs(); + refetchOrgs(); } }); const { runAsync: onAddPermission, loading: addLoading } = useRequest2( async ({ + orgId, groupId, memberId, per }: { + orgId?: string; groupId?: string; memberId?: string; per: 'write' | 'manage'; @@ -100,6 +121,26 @@ function PermissionManage() { } } } + if (orgId) { + const org = orgs.find((org) => String(org._id) === orgId); + if (org) { + const permission = new TeamPermission({ per: org.permission.value }); + switch (per) { + case 'write': + permission.addPer(TeamWritePermissionVal); + return onUpdateMemberPermission({ + orgId: org._id, + permission: permission.value + }); + case 'manage': + permission.addPer(TeamManagePermissionVal); + return onUpdateMemberPermission({ + orgId: org._id, + permission: permission.value + }); + } + } + } if (memberId) { const member = filteredMembers?.find((member) => String(member.tmbId) === memberId); if (member) { @@ -125,10 +166,12 @@ function PermissionManage() { const { runAsync: onRemovePermission, loading: removeLoading } = useRequest2( async ({ + orgId, groupId, memberId, per }: { + orgId?: string; groupId?: string; memberId?: string; per: 'write' | 'manage'; @@ -153,6 +196,26 @@ function PermissionManage() { } } } + if (orgId) { + const org = orgs.find((org) => String(org._id) === orgId); + if (org) { + const permission = new TeamPermission({ per: org.permission.value }); + switch (per) { + case 'write': + permission.removePer(TeamWritePermissionVal); + return onUpdateMemberPermission({ + orgId: org._id, + permission: permission.value + }); + case 'manage': + permission.removePer(TeamManagePermissionVal); + return onUpdateMemberPermission({ + orgId: org._id, + permission: permission.value + }); + } + } + } if (memberId) { const member = members?.find((member) => String(member.tmbId) === memberId); if (member) { @@ -239,9 +302,48 @@ function PermissionManage() { ))} - {filteredGroups?.length > 0 && filteredMembers?.length > 0 && ( + {filteredGroups?.length > 0 && filteredOrgs?.length > 0 && ( + + )} + + {filteredOrgs?.map((org) => ( + + + + + + + + e.target.checked + ? onAddPermission({ orgId: org._id, per: 'write' }) + : onRemovePermission({ orgId: org._id, per: 'write' }) + } + /> + + + + + + e.target.checked + ? onAddPermission({ orgId: org._id, per: 'manage' }) + : onRemovePermission({ orgId: org._id, per: 'manage' }) + } + /> + + + + ))} + + {filteredOrgs?.length > 0 && filteredMembers?.length > 0 && ( )} + {filteredMembers?.map((member) => (