Get rid of previous permission providers
Signed-off-by: Émile Ré <nemile.re@gmail.com>
This commit is contained in:
@@ -1,5 +0,0 @@
|
||||
import { createContext } from "react";
|
||||
|
||||
export type PermissionsContextType = Record<string, boolean>;
|
||||
|
||||
export const PermissionsContext = createContext<PermissionsContextType>({});
|
||||
@@ -1,72 +0,0 @@
|
||||
import { useEffect, type PropsWithChildren } from "react";
|
||||
import { PermissionsContext } from "./NewPermissionsContext";
|
||||
import {
|
||||
usePreloadedQuery,
|
||||
useQueryLoader,
|
||||
type PreloadedQuery,
|
||||
} from "react-relay";
|
||||
import type { GraphQLTaggedNode, OperationType } from "relay-runtime";
|
||||
import { useOrganizationId } from "/hooks/useOrganizationId";
|
||||
|
||||
type PermissionsQueryType = OperationType & {
|
||||
response: { viewer: Record<string, boolean> };
|
||||
};
|
||||
|
||||
type PermissionsProviderLoaderProps = PropsWithChildren<{
|
||||
query: GraphQLTaggedNode;
|
||||
}>;
|
||||
|
||||
export function PermissionsProviderLoader<T extends PermissionsQueryType>(
|
||||
props: PermissionsProviderLoaderProps,
|
||||
) {
|
||||
const { children, query } = props;
|
||||
|
||||
const organizationId = useOrganizationId();
|
||||
const [queryRef, loadQuery] = useQueryLoader<T>(query);
|
||||
|
||||
useEffect(() => {
|
||||
loadQuery({
|
||||
organizationId,
|
||||
});
|
||||
}, [loadQuery, organizationId]);
|
||||
|
||||
if (!queryRef) {
|
||||
// While permissions are loading, consider there are no permissions
|
||||
return <PermissionsContext value={{}}>{children}</PermissionsContext>;
|
||||
}
|
||||
|
||||
return (
|
||||
<PermissionsProvider query={query} queryRef={queryRef}>
|
||||
{children}
|
||||
</PermissionsProvider>
|
||||
);
|
||||
}
|
||||
|
||||
type PermissionsProviderProps<T extends PermissionsQueryType> =
|
||||
PermissionsProviderLoaderProps & {
|
||||
queryRef: PreloadedQuery<T>;
|
||||
};
|
||||
|
||||
function PermissionsProvider<T extends PermissionsQueryType>(
|
||||
props: PermissionsProviderProps<T>,
|
||||
) {
|
||||
const { children, query, queryRef } = props;
|
||||
|
||||
const { viewer: permissions } = usePreloadedQuery<T>(query, queryRef);
|
||||
|
||||
const handler = {
|
||||
get(target: Record<string, boolean>, prop: string) {
|
||||
if (!(prop in target)) {
|
||||
throw new Error(`Field ${prop} is not defined in IAM viewer node`);
|
||||
}
|
||||
|
||||
return Reflect.get(target, prop);
|
||||
},
|
||||
};
|
||||
|
||||
return (
|
||||
<PermissionsContext value={new Proxy(permissions, handler)}>
|
||||
{children}
|
||||
</PermissionsContext>
|
||||
);
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
import { createContext } from "react";
|
||||
import { Role } from "@probo/helpers";
|
||||
|
||||
export type PermissionsResponse = {
|
||||
permissions: Record<string, Record<string, boolean>>;
|
||||
role: Role;
|
||||
};
|
||||
|
||||
type PermissionsContextType = {
|
||||
isAuthorized: (entity: string, action: string) => boolean;
|
||||
} & PermissionsResponse;
|
||||
|
||||
export const PermissionsContext = createContext<PermissionsContextType>({
|
||||
permissions: {},
|
||||
role: Role.VIEWER,
|
||||
isAuthorized: () => false,
|
||||
});
|
||||
@@ -1,41 +0,0 @@
|
||||
import { type PropsWithChildren } from "react";
|
||||
import { PermissionsContext } from "./PermissionsContext";
|
||||
import { Role } from "@probo/helpers";
|
||||
|
||||
export function PermissionsProvider(props: PropsWithChildren) {
|
||||
const { children } = props;
|
||||
|
||||
// const organizationId = useOrganizationId();
|
||||
|
||||
// const { data } = useSuspenseQuery<PermissionsResponse>({
|
||||
// queryKey: ["permissions", organizationId],
|
||||
// queryFn: async () => {
|
||||
// const response = await fetch(`/authz/${organizationId}/permissions`, { credentials: "include" });
|
||||
// if (!response.ok) {
|
||||
// throw new Error("Failed to fetch permissions");
|
||||
// }
|
||||
// return response.json() as Promise<PermissionsResponse>;
|
||||
// },
|
||||
// });
|
||||
|
||||
// @ts-expect-error wip refactor
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const isAuthorized = (entity: string, action: string) => {
|
||||
// return data.permissions[entity]?.[action] ?? false;
|
||||
return true;
|
||||
};
|
||||
|
||||
return (
|
||||
<PermissionsContext
|
||||
value={{ permissions: {}, role: Role.OWNER, isAuthorized }}
|
||||
>
|
||||
{children}
|
||||
</PermissionsContext>
|
||||
);
|
||||
|
||||
// return (
|
||||
// <PermissionsContext value={{ ...data, isAuthorized }}>
|
||||
// {children}
|
||||
// </PermissionsContext>
|
||||
// );
|
||||
}
|
||||
Reference in New Issue
Block a user