feat(rbac): ui components and hooks for rbac

This commit is contained in:
Akhil Mohan
2023-09-08 21:20:29 +05:30
parent 1bf9041ac9
commit 1167b1bc60
15 changed files with 974 additions and 196 deletions
+1
View File
@@ -6,6 +6,7 @@ export * from "./integrationAuth";
export * from "./integrations";
export * from "./keys";
export * from "./organization";
export * from "./roles";
export * from "./secretFolders";
export * from "./secretImports";
export * from "./secrets";
+2
View File
@@ -0,0 +1,2 @@
export { useCreateRole, useDeleteRole,useUpdateRole } from "./mutation";
export { useGetRoles } from "./queries";
+42
View File
@@ -0,0 +1,42 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { apiRequest } from "@app/config/request";
import { roleQueryKeys } from "./queries";
import { TCreateRoleDTO, TDeleteRoleDTO, TUpdateRoleDTO } from "./types";
export const useCreateRole = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (dto: TCreateRoleDTO) => apiRequest.post("/api/v1/roles", dto),
onSuccess: (_, { orgId, workspaceId }) => {
queryClient.invalidateQueries(roleQueryKeys.getRoles({ orgId, workspaceId }));
}
});
};
export const useUpdateRole = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({ id, ...dto }: TUpdateRoleDTO) => apiRequest.patch(`/api/v1/roles/${id}`, dto),
onSuccess: (_, { orgId, workspaceId }) => {
queryClient.invalidateQueries(roleQueryKeys.getRoles({ orgId, workspaceId }));
}
});
};
export const useDeleteRole = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({ orgId, id }: TDeleteRoleDTO) =>
apiRequest.delete(`/api/v1/roles/${id}`, {
data: { orgId }
}),
onSuccess: (_, { orgId, workspaceId }) => {
queryClient.invalidateQueries(roleQueryKeys.getRoles({ orgId, workspaceId }));
}
});
};
+27
View File
@@ -0,0 +1,27 @@
import { useQuery } from "@tanstack/react-query";
import { apiRequest } from "@app/config/request";
import { TGetRolesDTO, TRole } from "./types";
export const roleQueryKeys = {
getRoles: ({ orgId, workspaceId }: TGetRolesDTO) => ["roles", { orgId, workspaceId }] as const
};
const getRoles = async ({ orgId, workspaceId }: TGetRolesDTO) => {
const { data } = await apiRequest.get<{ data: { roles: TRole[] } }>("/api/v1/roles", {
params: {
workspaceId,
orgId
}
});
return data.data.roles;
};
export const useGetRoles = ({ orgId, workspaceId }: TGetRolesDTO) =>
useQuery({
queryKey: roleQueryKeys.getRoles({ orgId, workspaceId }),
queryFn: () => getRoles({ orgId, workspaceId }),
enabled: Boolean(orgId)
});
+43
View File
@@ -0,0 +1,43 @@
export type TGetRolesDTO = {
orgId: string;
workspaceId?: string;
};
export type TRole = {
_id: string;
organization: string;
workspace: string;
name: string;
description: string;
slug: string;
permissions: TPermission[];
createdAt: string;
updatedAt: string;
};
export type TPermission = {
condition?: Record<string, any>;
action: "read" | "edit" | "create" | "delete";
subject: string;
};
export type TCreateRoleDTO = {
orgId: string;
workspaceId?: string;
name: string;
description?: string;
slug: string;
permissions: TPermission[];
};
export type TUpdateRoleDTO = {
orgId: string;
id: string;
workspaceId?: string;
} & Partial<Omit<TCreateRoleDTO, "orgId" | "workspaceId">>;
export type TDeleteRoleDTO = {
orgId: string;
id: string;
workspaceId?: string;
};
+5 -4
View File
@@ -42,9 +42,10 @@ export type OrgUser = {
};
inviteEmail: string;
organization: string;
role: "owner" | "admin" | "member";
role: "owner" | "admin" | "member" | "custom";
status: "invited" | "accepted" | "verified" | "completed";
deniedPermissions: any[];
customRole: string;
};
export type AddUserToWsDTO = {
@@ -76,7 +77,7 @@ export type AddUserToOrgDTO = {
export type CreateAPIKeyRes = {
apiKey: string;
apiKeyData: APIKeyData;
}
};
export type RenameUserDTO = {
newName: string;
@@ -89,7 +90,7 @@ export type APIKeyData = {
lastUsed: string;
createdAt: string;
expiresAt: string;
}
};
export type TokenVersion = {
_id: string;
@@ -99,4 +100,4 @@ export type TokenVersion = {
lastUsed: string;
createdAt: string;
updatedAt: string;
}
};