Allow multiple access for secured routes (#946)

This commit is contained in:
Jon Koops 2021-08-09 22:20:49 +02:00 committed by GitHub
parent 45e07266eb
commit ed33242b34
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 4 deletions

View file

@ -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 />;
}; };

View file

@ -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 (

View file

@ -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 = (

View file

@ -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;
}; };