Skip to content

Commit 7dd1332

Browse files
committed
refactor
1 parent 619242c commit 7dd1332

File tree

8 files changed

+35
-28
lines changed

8 files changed

+35
-28
lines changed

src/components/ControlPlane/GitRepositories.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { useHandleResourcePatch } from '../../lib/api/types/crossplane/useHandle
2424
import { ErrorDialog, ErrorDialogHandle } from '../Shared/ErrorMessageBox.tsx';
2525
import type { GitReposResponse } from '../../lib/api/types/flux/listGitRepo';
2626
import { ActionsMenu, type ActionItem } from './ActionsMenu';
27-
import { useAuthMcp } from '../../spaces/mcp/auth/AuthContextMcp.tsx';
27+
import { useHasMcpAdminRights } from '../../spaces/mcp/auth/useHasMcpAdminRights.ts';
2828

2929
export type GitRepoItem = GitReposResponse['items'][0] & {
3030
apiVersion?: string;
@@ -47,7 +47,6 @@ export function GitRepositories() {
4747
readyMessage: string;
4848
revision?: string;
4949
};
50-
const { hasMCPAdminRights } = useAuthMcp();
5150
const openEditPanel = useCallback(
5251
(item: GitRepoItem) => {
5352
const identityKey = `${item.kind}:${item.metadata.namespace ?? ''}:${item.metadata.name}`;
@@ -64,6 +63,7 @@ export function GitRepositories() {
6463
},
6564
[openInAside, handlePatch],
6665
);
66+
const hasMCPAdminRights = useHasMcpAdminRights();
6767

6868
const columns = useMemo<AnalyticalTableColumnDefinition[]>(
6969
() =>

src/components/ControlPlane/Kustomizations.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { useHandleResourcePatch } from '../../lib/api/types/crossplane/useHandle
2424
import { ErrorDialog, ErrorDialogHandle } from '../Shared/ErrorMessageBox.tsx';
2525
import type { KustomizationsResponse } from '../../lib/api/types/flux/listKustomization';
2626
import { ActionsMenu, type ActionItem } from './ActionsMenu';
27-
import { useAuthMcp } from '../../spaces/mcp/auth/AuthContextMcp.tsx';
27+
import { useHasMcpAdminRights } from '../../spaces/mcp/auth/useHasMcpAdminRights.ts';
2828

2929
export type KustomizationItem = KustomizationsResponse['items'][0] & {
3030
apiVersion?: string;
@@ -63,7 +63,7 @@ export function Kustomizations() {
6363
},
6464
[openInAside, handlePatch],
6565
);
66-
const { hasMCPAdminRights } = useAuthMcp();
66+
const hasMCPAdminRights = useHasMcpAdminRights();
6767
const columns = useMemo<AnalyticalTableColumnDefinition[]>(
6868
() =>
6969
[

src/components/ControlPlane/ManagedResources.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import { YamlSidePanel } from '../Yaml/YamlSidePanel.tsx';
3636
import { ErrorDialog, ErrorDialogHandle } from '../Shared/ErrorMessageBox.tsx';
3737
import { APIError } from '../../lib/api/error.ts';
3838
import { useHandleResourcePatch } from '../../lib/api/types/crossplane/useHandleResourcePatch.ts';
39-
import { useAuthMcp } from '../../spaces/mcp/auth/AuthContextMcp.tsx';
39+
import { useHasMcpAdminRights } from '../../spaces/mcp/auth/useHasMcpAdminRights.ts';
4040

4141
interface StatusFilterColumn {
4242
filterValue?: string;
@@ -120,7 +120,7 @@ export function ManagedResources() {
120120
},
121121
[openInAside, handlePatch],
122122
);
123-
const { hasMCPAdminRights } = useAuthMcp();
123+
const hasMCPAdminRights = useHasMcpAdminRights();
124124
const columns = useMemo<AnalyticalTableColumnDefinition[]>(
125125
() =>
126126
[

src/components/ControlPlane/ProvidersConfig.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { useSplitter } from '../Splitter/SplitterContext.tsx';
2424
import { YamlSidePanel } from '../Yaml/YamlSidePanel.tsx';
2525
import { useHandleResourcePatch } from '../../lib/api/types/crossplane/useHandleResourcePatch.ts';
2626
import { ErrorDialog, ErrorDialogHandle } from '../Shared/ErrorMessageBox.tsx';
27-
import { useAuthMcp } from '../../spaces/mcp/auth/AuthContextMcp.tsx';
27+
import { useHasMcpAdminRights } from '../../spaces/mcp/auth/useHasMcpAdminRights.ts';
2828

2929
type Rows = {
3030
parent: string;
@@ -76,7 +76,7 @@ export function ProvidersConfig() {
7676
},
7777
[openInAside, handlePatch],
7878
);
79-
const { hasMCPAdminRights } = useAuthMcp();
79+
const hasMCPAdminRights = useHasMcpAdminRights();
8080
const columns = useMemo<AnalyticalTableColumnDefinition[]>(
8181
() =>
8282
[

src/lib/shared/McpContext.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createContext, ReactNode, useContext } from 'react';
2-
import { ControlPlane as ManagedControlPlaneResource } from '../api/types/crate/controlPlanes.ts';
2+
import { ControlPlane as ManagedControlPlaneResource, RoleBinding } from '../api/types/crate/controlPlanes.ts';
33
import { ApiConfigProvider } from '../../components/Shared/k8s';
44
import { useApiResource } from '../api/useApiResource.ts';
55
import { GetKubeconfig } from '../api/types/crate/getKubeconfig.ts';
@@ -15,6 +15,7 @@ interface Mcp {
1515
secretName?: string;
1616
secretKey?: string;
1717
kubeconfig?: string;
18+
roleBindings?: RoleBinding[];
1819
}
1920

2021
interface Props {
@@ -44,6 +45,7 @@ export const McpContextProvider = ({ children, context }: Props) => {
4445
return <></>;
4546
}
4647
context.kubeconfig = kubeconfig.data;
48+
context.roleBindings = mcp.data?.spec?.authorization?.roleBindings;
4749
return <McpContext.Provider value={context}>{children}</McpContext.Provider>;
4850
};
4951

src/spaces/mcp/auth/AuthContextMcp.tsx

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,17 @@ import { createContext, useState, useEffect, ReactNode, use } from 'react';
22
import { MeResponseSchema } from './auth.schemas';
33
import { AUTH_FLOW_SESSION_KEY } from '../../../common/auth/AuthCallbackHandler.tsx';
44
import { getRedirectSuffix } from '../../../common/auth/getRedirectSuffix.ts';
5-
import { RoleBinding } from '../../../lib/api/types/crate/controlPlanes.ts';
6-
import { useAuthOnboarding } from '../../onboarding/auth/AuthContextOnboarding.tsx';
75

86
interface AuthContextMcpType {
97
isLoading: boolean;
108
isAuthenticated: boolean;
119
error: Error | null;
1210
login: () => void;
13-
hasMCPAdminRights: boolean;
1411
}
1512

1613
const AuthContextMcp = createContext<AuthContextMcpType | null>(null);
1714

18-
export function AuthProviderMcp({ children, mcpUsers = [] }: { children: ReactNode; mcpUsers?: RoleBinding[] }) {
19-
const auth = useAuthOnboarding();
20-
21-
const userEmail = auth.user?.email;
22-
23-
const matchingRoleBinding = mcpUsers.find(
24-
(roleBinding) =>
25-
Array.isArray(roleBinding.subjects) &&
26-
roleBinding.subjects.some((subject) => subject?.name?.includes(userEmail ?? '')),
27-
);
28-
const hasMCPAdminRights = matchingRoleBinding?.role === 'admin';
29-
15+
export function AuthProviderMcp({ children }: { children: ReactNode }) {
3016
const [isAuthenticated, setIsAuthenticated] = useState(false);
3117
const [isLoading, setIsLoading] = useState(true);
3218
const [error, setError] = useState<Error | null>(null);
@@ -73,9 +59,7 @@ export function AuthProviderMcp({ children, mcpUsers = [] }: { children: ReactNo
7359
window.location.replace(`/api/auth/mcp/login?redirectTo=${encodeURIComponent(getRedirectSuffix())}`);
7460
};
7561

76-
return (
77-
<AuthContextMcp value={{ isLoading, isAuthenticated, error, login, hasMCPAdminRights }}>{children}</AuthContextMcp>
78-
);
62+
return <AuthContextMcp value={{ isLoading, isAuthenticated, error, login }}>{children}</AuthContextMcp>;
7963
}
8064

8165
export const useAuthMcp = () => {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { useAuthOnboarding } from '../../onboarding/auth/AuthContextOnboarding.tsx';
2+
import { useMcp } from '../../../lib/shared/McpContext.tsx';
3+
4+
export function useHasMcpAdminRights(): boolean {
5+
const auth = useAuthOnboarding();
6+
const mcp = useMcp();
7+
const userEmail = auth.user?.email;
8+
const mcpUsers = mcp.roleBindings ?? [];
9+
10+
if (!userEmail) {
11+
return false;
12+
}
13+
14+
const matchingRoleBinding = mcpUsers.find(
15+
(roleBinding) =>
16+
Array.isArray(roleBinding.subjects) && roleBinding.subjects.some((subject) => subject?.name?.includes(userEmail)),
17+
);
18+
19+
return matchingRoleBinding?.role === 'admin';
20+
}

src/spaces/mcp/pages/McpPage.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { useApiResource } from '../../../lib/api/useApiResource.ts';
2828
import { YamlViewButton } from '../../../components/Yaml/YamlViewButton.tsx';
2929
import { Landscapers } from '../../../components/ControlPlane/Landscapers.tsx';
3030
import { AuthProviderMcp } from '../auth/AuthContextMcp.tsx';
31+
import { useHasMcpAdminRights } from '../auth/useHasMcpAdminRights.ts';
3132
import { isNotFoundError } from '../../../lib/api/error.ts';
3233
import { NotFoundBanner } from '../../../components/Ui/NotFoundBanner/NotFoundBanner.tsx';
3334
import Graph from '../../../components/Graphs/Graph.tsx';
@@ -93,7 +94,7 @@ export default function McpPage() {
9394
name: controlPlaneName,
9495
}}
9596
>
96-
<AuthProviderMcp mcpUsers={mcp?.spec?.authorization?.roleBindings}>
97+
<AuthProviderMcp>
9798
<WithinManagedControlPlane>
9899
<ObjectPage
99100
mode="IconTabBar"

0 commit comments

Comments
 (0)