Allow multiple access for secured routes (#946)
This commit is contained in:
parent
45e07266eb
commit
ed33242b34
4 changed files with 19 additions and 4 deletions
|
@ -53,7 +53,12 @@ const RealmPathSelector = ({ children }: { children: ReactNode }) => {
|
||||||
type SecuredRouteProps = { route: RouteDef };
|
type SecuredRouteProps = { route: RouteDef };
|
||||||
const SecuredRoute = ({ route }: SecuredRouteProps) => {
|
const SecuredRoute = ({ route }: SecuredRouteProps) => {
|
||||||
const { hasAccess } = useAccess();
|
const { hasAccess } = useAccess();
|
||||||
if (hasAccess(route.access)) return <route.component />;
|
const accessAllowed =
|
||||||
|
route.access instanceof Array
|
||||||
|
? hasAccess(...route.access)
|
||||||
|
: hasAccess(route.access);
|
||||||
|
|
||||||
|
if (accessAllowed) return <route.component />;
|
||||||
|
|
||||||
return <ForbiddenSection />;
|
return <ForbiddenSection />;
|
||||||
};
|
};
|
||||||
|
|
|
@ -39,7 +39,17 @@ export const PageNav: React.FunctionComponent = () => {
|
||||||
const route = routes.find(
|
const route = routes.find(
|
||||||
(route) => route.path.replace(/\/:.+?(\?|(?:(?!\/).)*|$)/g, "") === path
|
(route) => route.path.replace(/\/:.+?(\?|(?:(?!\/).)*|$)/g, "") === path
|
||||||
);
|
);
|
||||||
if (!route || !hasAccess(route.access)) return <></>;
|
|
||||||
|
const accessAllowed =
|
||||||
|
route &&
|
||||||
|
(route.access instanceof Array
|
||||||
|
? hasAccess(...route.access)
|
||||||
|
: hasAccess(route.access));
|
||||||
|
|
||||||
|
if (!accessAllowed) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
//remove "/realm-name" from the start of the path
|
//remove "/realm-name" from the start of the path
|
||||||
const activeItem = history.location.pathname.substr(realm.length + 1);
|
const activeItem = history.location.pathname.substr(realm.length + 1);
|
||||||
return (
|
return (
|
||||||
|
|
|
@ -13,7 +13,7 @@ export const RealmRoleRoute: RouteDef = {
|
||||||
path: "/:realm/roles/:id/:tab?",
|
path: "/:realm/roles/:id/:tab?",
|
||||||
component: RealmRoleTabs,
|
component: RealmRoleTabs,
|
||||||
breadcrumb: (t) => t("roles:roleDetails"),
|
breadcrumb: (t) => t("roles:roleDetails"),
|
||||||
access: "view-realm",
|
access: ["view-realm", "view-users"],
|
||||||
};
|
};
|
||||||
|
|
||||||
export const toRealmRole = (
|
export const toRealmRole = (
|
||||||
|
|
|
@ -21,7 +21,7 @@ export type RouteDef = {
|
||||||
path: string;
|
path: string;
|
||||||
component: ComponentType;
|
component: ComponentType;
|
||||||
breadcrumb?: (t: TFunction) => string | ComponentType<any>;
|
breadcrumb?: (t: TFunction) => string | ComponentType<any>;
|
||||||
access: AccessType;
|
access: AccessType | AccessType[];
|
||||||
matchOptions?: MatchOptions;
|
matchOptions?: MatchOptions;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue