2022-01-27 22:10:12 +00:00
|
|
|
import React, { useContext } from "react";
|
|
|
|
|
import { useErrorHandler } from "react-error-boundary";
|
|
|
|
|
import { AppContext } from "context/app";
|
2021-06-04 13:00:14 +00:00
|
|
|
|
2021-10-26 14:24:16 +00:00
|
|
|
interface IAuthAnyAdminRoutesProps {
|
2021-06-04 13:00:14 +00:00
|
|
|
children: JSX.Element;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-08-08 18:39:08 +00:00
|
|
|
* Checks if a user is any global or team admin when routing
|
2021-06-04 13:00:14 +00:00
|
|
|
*/
|
2022-08-08 18:39:08 +00:00
|
|
|
const AuthAnyAdminRoutes = ({ children }: IAuthAnyAdminRoutesProps) => {
|
2022-01-27 22:10:12 +00:00
|
|
|
const handlePageError = useErrorHandler();
|
|
|
|
|
const { currentUser, isGlobalAdmin, isAnyTeamAdmin } = useContext(AppContext);
|
2021-06-04 13:00:14 +00:00
|
|
|
|
2022-01-27 22:10:12 +00:00
|
|
|
if (!currentUser) {
|
2021-06-04 13:00:14 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-27 22:10:12 +00:00
|
|
|
if (!isGlobalAdmin && !isAnyTeamAdmin) {
|
|
|
|
|
handlePageError({ status: 403 });
|
2021-06-04 13:00:14 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
2022-01-27 22:10:12 +00:00
|
|
|
|
2021-06-04 13:00:14 +00:00
|
|
|
return <>{children}</>;
|
|
|
|
|
};
|
|
|
|
|
|
2021-10-26 14:24:16 +00:00
|
|
|
export default AuthAnyAdminRoutes;
|