8000 Fix nested group URLs in the admin console by georgepstaylor · Pull Request #52389 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions js/apps/admin-ui/src/PageNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { useRealm } from "./context/realm-context/RealmContext";
import { useServerInfo } from "./context/server-info/ServerInfoProvider";
import type { Environment } from "./environment-types";
import { toPage } from "./page/routes";
import { normalizeNavRoutePath } from "./page-nav-utils";
import { routes } from "./routes";
import { resolveDisplayName } from "./util";
import useIsFeatureEnabled, { Feature } from "./utils/useIsFeatureEnabled";
Expand All @@ -31,9 +32,9 @@ const LeftNav = ({ title, path, id }: LeftNavProps) => {
const { hasAccess } = useAccess();
const { realm } = useRealm();
const encodedRealm = encodeURIComponent(realm);
const navPath = id || path;
const route = routes.find(
(route) =>
route.path.replace(/\/:.+?(\?|(?:(?!\/).)*|$)/g, "") === (id || path),
(route) => normalizeNavRoutePath(route.path) === navPath,
);

const accessAllowed =
Expand Down
13 changes: 13 additions & 0 deletions js/apps/admin-ui/src/__tests__/page-nav-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { describe, expect, it } from "vitest";
import { normalizeNavRoutePath } from "../page-nav-utils";

describe("normalizeNavRoutePath", () => {
it("matches splat routes to their base nav path", () => {
expect(normalizeNavRoutePath("/:realm/groups/*")).toBe("/groups");
});

it("matches parameterized routes without splats", () => {
expect(normalizeNavRoutePath("/:realm/clients")).toBe("/clients");
expect(normalizeNavRoutePath("/:realm/groups/:id")).toBe("/groups");
});
});
24 changes: 24 additions & 0 deletions js/apps/admin-ui/src/groups/__tests__/Groups.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { describe, expect, it } from "vitest";
import { toGroups } from "../routes/Groups";

describe("toGroups", () => {
it("preserves hierarchy separators in realm group paths", () => {
expect(
toGroups({ realm: "master", id: "parent-id/child-id" }).pathname,
).toBe("/master/groups/parent-id/child-id");
});

it("preserves hierarchy separators in organization group paths", () => {
expect(
toGroups({
realm: "master",
orgId: "organization-id",
id: "parent-id/child-id",
}).pathname,
).toBe("/master/organizations/organization-id/groups/parent-id/child-id");
});
Comment thread
edewit marked this conversation as resolved.
Resolved

it("generates the group root path without an id", () => {
expect(toGroups({ realm: "master" }).pathname).toBe("/master/groups");
});
});
4 changes: 2 additions & 2 deletions js/apps/admin-ui/src/groups/routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AppRouteObject } from "../routes";
import { GroupsRoute, GroupsWithIdRoute } from "./routes/Groups";
import { GroupsRoute } from "./routes/Groups";

const routes: AppRouteObject[] = [GroupsRoute, GroupsWithIdRoute];
const routes: AppRouteObject[] = [GroupsRoute];

export default routes;
19 changes: 2 additions & 17 deletions js/apps/admin-ui/src/groups/routes/Groups.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,10 @@ export const OrgGroupsRoute: AppRouteObject = {
},
};

export const GroupsWithIdRoute: AppRouteObject = {
...GroupsRoute,
path: "/:realm/groups/:id",
};

export const OrgGroupsWithIdRoute: AppRouteObject = {
...OrgGroupsRoute,
path: "/:realm/organizations/:orgId/groups/:id",
};

export const toGroups = (params: GroupsParams): Partial<Path> => {
const routes = {
orgGroups: params.id ? OrgGroupsWithIdRoute.path : OrgGroupsRoute.path,
realmGroups: params.id ? GroupsWithIdRoute.path : GroupsRoute.path,
};

const path = params.orgId ? routes.orgGroups : routes.realmGroups;
const path = params.orgId ? OrgGroupsRoute.path : GroupsRoute.path;
Comment thread
edewit marked this conversation as resolved.
Resolved

return {
pathname: generatePath(path, params),
pathname: generatePath(path, { ...params, "*": params.id }),
Comment thread
edewit marked this conversation as resolved.
Resolved
Comment thread
edewit marked this conversation as resolved.
Resolved
};
Comment thread
edewit marked this conversation as resolved.
Resolved
};
2 changes: 2 additions & 0 deletions js/apps/admin-ui/src/page-nav-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const normalizeNavRoutePath = (routePath: string): string =>
routePath.replace(/\/:.+?(\?|(?:(?!\/).)*|$)/g, "").replace(/\/\*$/, "");
Loading
0