mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-14 16:40:30 +00:00
feat(infisical-pg): new org role routes completed
This commit is contained in:
@@ -1,2 +1,14 @@
|
||||
export { useCreateRole, useDeleteRole, useUpdateRole } from "./mutation";
|
||||
export { useGetRoles, useGetUserOrgPermissions,useGetUserProjectPermissions } from "./queries";
|
||||
export {
|
||||
useCreateOrgRole,
|
||||
useCreateRole,
|
||||
useDeleteOrgRole,
|
||||
useDeleteRole,
|
||||
useUpdateOrgRole,
|
||||
useUpdateRole
|
||||
} from "./mutation";
|
||||
export {
|
||||
useGetOrgRoles,
|
||||
useGetRoles,
|
||||
useGetUserOrgPermissions,
|
||||
useGetUserProjectPermissions
|
||||
} from "./queries";
|
||||
|
||||
@@ -1,9 +1,17 @@
|
||||
import { packRules } from "@casl/ability/extra";
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
import { apiRequest } from "@app/config/request";
|
||||
|
||||
import { roleQueryKeys } from "./queries";
|
||||
import { TCreateRoleDTO, TDeleteRoleDTO, TUpdateRoleDTO } from "./types";
|
||||
import {
|
||||
TCreateOrgRoleDTO,
|
||||
TCreateRoleDTO,
|
||||
TDeleteOrgRoleDTO,
|
||||
TDeleteRoleDTO,
|
||||
TUpdateOrgRoleDTO,
|
||||
TUpdateRoleDTO
|
||||
} from "./types";
|
||||
|
||||
export const useCreateRole = <T extends string | undefined>() => {
|
||||
const queryClient = useQueryClient();
|
||||
@@ -40,3 +48,47 @@ export const useDeleteRole = () => {
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const useCreateOrgRole = () => {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: ({ orgId, permissions, ...dto }: TCreateOrgRoleDTO) =>
|
||||
apiRequest.post(`/api/ee/v1/organization/${orgId}/roles`, {
|
||||
...dto,
|
||||
permissions: permissions.length ? packRules(permissions) : []
|
||||
}),
|
||||
onSuccess: (_, { orgId }) => {
|
||||
queryClient.invalidateQueries(roleQueryKeys.getOrgRoles(orgId));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const useUpdateOrgRole = () => {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: ({ id, orgId, permissions, ...dto }: TUpdateOrgRoleDTO) =>
|
||||
apiRequest.patch(`/api/ee/v1/organization/${orgId}/roles/${id}`, {
|
||||
...dto,
|
||||
permissions: permissions?.length ? packRules(permissions) : undefined
|
||||
}),
|
||||
onSuccess: (_, { orgId }) => {
|
||||
queryClient.invalidateQueries(roleQueryKeys.getOrgRoles(orgId));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const useDeleteOrgRole = () => {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: ({ orgId, id }: TDeleteOrgRoleDTO) =>
|
||||
apiRequest.delete(`/api/ee/v1/organization/${orgId}/roles/${id}`, {
|
||||
data: { orgId }
|
||||
}),
|
||||
onSuccess: (_, { orgId }) => {
|
||||
queryClient.invalidateQueries(roleQueryKeys.getOrgRoles(orgId));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@@ -13,6 +13,8 @@ import {
|
||||
TGetRolesDTO,
|
||||
TGetUserOrgPermissionsDTO,
|
||||
TGetUserProjectPermissionDTO,
|
||||
TOrgRole,
|
||||
TPermission,
|
||||
TRole
|
||||
} from "./types";
|
||||
|
||||
@@ -36,6 +38,7 @@ const conditionsMatcher = buildMongoQueryMatcher({ $glob }, { glob });
|
||||
|
||||
export const roleQueryKeys = {
|
||||
getRoles: ({ orgId, workspaceId }: TGetRolesDTO) => ["roles", { orgId, workspaceId }] as const,
|
||||
getOrgRoles: (orgId: string) => ["org-roles", { orgId }] as const,
|
||||
getUserOrgPermissions: ({ orgId }: TGetUserOrgPermissionsDTO) =>
|
||||
["user-permissions", { orgId }] as const,
|
||||
getUserProjectPermissions: ({ workspaceId }: TGetUserProjectPermissionDTO) =>
|
||||
@@ -63,6 +66,23 @@ export const useGetRoles = ({ orgId, workspaceId }: TGetRolesDTO) =>
|
||||
enabled: Boolean(orgId)
|
||||
});
|
||||
|
||||
const getOrgRoles = async (orgId: string) => {
|
||||
const { data } = await apiRequest.get<{
|
||||
data: { roles: Array<Omit<TOrgRole, "permissions"> & { permissions: unknown }> };
|
||||
}>(`/api/ee/v1/organization/${orgId}/roles`);
|
||||
return data.data.roles.map(({ permissions, ...el }) => ({
|
||||
...el,
|
||||
permissions: unpackRules(permissions as PackRule<TPermission>[])
|
||||
}));
|
||||
};
|
||||
|
||||
export const useGetOrgRoles = (orgId: string, enable = true) =>
|
||||
useQuery({
|
||||
queryKey: roleQueryKeys.getOrgRoles(orgId),
|
||||
queryFn: () => getOrgRoles(orgId),
|
||||
enabled: Boolean(orgId) && enable
|
||||
});
|
||||
|
||||
const getUserOrgPermissions = async ({ orgId }: TGetUserOrgPermissionsDTO) => {
|
||||
if (orgId === "") return { permissions: [], membership: null };
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ export type TGetRolesDTO = {
|
||||
workspaceId?: string;
|
||||
};
|
||||
|
||||
// @depreciated
|
||||
export type TRole<T extends string | undefined> = {
|
||||
id: string;
|
||||
organization: string;
|
||||
@@ -15,6 +16,17 @@ export type TRole<T extends string | undefined> = {
|
||||
updatedAt: string;
|
||||
};
|
||||
|
||||
export type TOrgRole = {
|
||||
slug: string;
|
||||
name: string;
|
||||
orgId: string;
|
||||
id: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
description?: string;
|
||||
permissions: TPermission[];
|
||||
};
|
||||
|
||||
export type TPermission = {
|
||||
conditions?: Record<string, any>;
|
||||
action: string;
|
||||
@@ -55,3 +67,21 @@ export type TGetUserOrgPermissionsDTO = {
|
||||
export type TGetUserProjectPermissionDTO = {
|
||||
workspaceId: string;
|
||||
};
|
||||
|
||||
export type TCreateOrgRoleDTO = {
|
||||
orgId: string;
|
||||
name: string;
|
||||
description?: string;
|
||||
slug: string;
|
||||
permissions: TPermission[];
|
||||
};
|
||||
|
||||
export type TUpdateOrgRoleDTO = {
|
||||
orgId: string;
|
||||
id: string;
|
||||
} & Partial<Omit<TCreateOrgRoleDTO, "orgId">>;
|
||||
|
||||
export type TDeleteOrgRoleDTO = {
|
||||
orgId: string;
|
||||
id: string;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user