mirror of
https://github.com/langgenius/dify.git
synced 2026-08-28 20:21:58 +08:00
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: fatelei <fatelei@gmail.com> Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: 盐粒 Yanli <yanli@dify.ai> Co-authored-by: Charles Yao <chongbinyao33@gmail.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com> Co-authored-by: yunlu.wen <yunlu.wen@dify.ai> Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com> Co-authored-by: Jingyi <jingyi.qi@dify.ai> Co-authored-by: yyh <yuanyouhuilyz@gmail.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: hjlarry <hjlarry@163.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Xiyuan Chen <52963600+GareArc@users.noreply.github.com> Co-authored-by: gigglewang <gigglewang@dify.ai> Co-authored-by: chariri <w@chariri.moe> Co-authored-by: Evan <2869018789@qq.com> Co-authored-by: zyssyz123 <916125788@qq.com>
119 lines
3.5 KiB
TypeScript
119 lines
3.5 KiB
TypeScript
import type {
|
|
CopyWorkspaceRoleRequest,
|
|
CreateRoleRequest,
|
|
GetMembersOfRoleRequest,
|
|
GetMembersOfRoleResponse,
|
|
Role,
|
|
RoleListRequest,
|
|
RoleListResponse,
|
|
UpdateRolesRequest,
|
|
} from '@/models/access-control'
|
|
import type { CommonResponse } from '@/models/common'
|
|
import { useInfiniteQuery, useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
|
import { del, get, post, put } from '../base'
|
|
import { commonQueryKeys } from '../use-common'
|
|
|
|
const NAME_SPACE = 'rbac-role-management'
|
|
|
|
export const useWorkspaceRoleList = (params: RoleListRequest) => {
|
|
const { page = 1, ...queryParams } = params
|
|
|
|
return useInfiniteQuery({
|
|
queryKey: [NAME_SPACE, 'workspace-role-list', queryParams],
|
|
queryFn: ({ pageParam }) => get<RoleListResponse>('/workspaces/current/rbac/roles', {
|
|
params: {
|
|
...queryParams,
|
|
page: pageParam,
|
|
},
|
|
}),
|
|
initialPageParam: page,
|
|
getNextPageParam: (lastPage) => {
|
|
const { current_page, total_pages } = lastPage.pagination
|
|
|
|
if (current_page < total_pages)
|
|
return current_page + 1
|
|
|
|
return undefined
|
|
},
|
|
})
|
|
}
|
|
|
|
export const useCreateWorkspaceRole = () => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationKey: [NAME_SPACE, 'create-workspace-role'],
|
|
mutationFn: (data: CreateRoleRequest) =>
|
|
post<Role>('/workspaces/current/rbac/roles', {
|
|
body: { ...data },
|
|
}),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: [NAME_SPACE, 'workspace-role-list'] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export const useUpdateWorkspaceRole = () => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationKey: [NAME_SPACE, 'update-workspace-role'],
|
|
mutationFn: (data: UpdateRolesRequest) =>
|
|
put<Role>(`/workspaces/current/rbac/roles/${data.id}`, {
|
|
body: { ...data },
|
|
}),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: [NAME_SPACE, 'workspace-role-list'] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export const useDeleteWorkspaceRole = () => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationKey: [NAME_SPACE, 'delete-workspace-role'],
|
|
mutationFn: (id: string) =>
|
|
del<CommonResponse>(`/workspaces/current/rbac/roles/${id}`),
|
|
onSuccess: () => Promise.all([
|
|
queryClient.invalidateQueries({ queryKey: [NAME_SPACE, 'workspace-role-list'] }),
|
|
queryClient.invalidateQueries({ queryKey: commonQueryKeys.members }),
|
|
]),
|
|
})
|
|
}
|
|
|
|
export const useCopyWorkspaceRole = () => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationKey: [NAME_SPACE, 'copy-workspace-role'],
|
|
mutationFn: ({ roleId, copy_member }: CopyWorkspaceRoleRequest) =>
|
|
post<Role>(`/workspaces/current/rbac/roles/${roleId}/copy`, {
|
|
body: { copy_member },
|
|
}),
|
|
onSuccess: (_role, variables) => {
|
|
const invalidations = [
|
|
queryClient.invalidateQueries({ queryKey: [NAME_SPACE, 'workspace-role-list'] }),
|
|
]
|
|
|
|
if (variables.copy_member)
|
|
invalidations.push(queryClient.invalidateQueries({ queryKey: commonQueryKeys.members }))
|
|
|
|
return Promise.all(invalidations)
|
|
},
|
|
})
|
|
}
|
|
|
|
export const useGetMembersOfRole = (params: GetMembersOfRoleRequest) => {
|
|
const { roleId, ...paginationParams } = params
|
|
return useQuery({
|
|
queryKey: [NAME_SPACE, 'members-of-role', roleId, paginationParams],
|
|
queryFn: () => get<GetMembersOfRoleResponse>(`/workspaces/current/rbac/roles/${roleId}/members`, {
|
|
params: {
|
|
...paginationParams,
|
|
},
|
|
}),
|
|
enabled: !!roleId,
|
|
})
|
|
}
|