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 IAuthAnyMaintainerAnyAdminRoutesProps {
|
2021-06-04 13:00:14 +00:00
|
|
|
children: JSX.Element;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-26 14:24:16 +00:00
|
|
|
* Checks if a user is any maintainer or any admin when routing
|
2021-06-04 13:00:14 +00:00
|
|
|
*/
|
2021-10-26 14:24:16 +00:00
|
|
|
const AuthAnyMaintainerAnyAdminRoutes = ({
|
2021-10-22 15:34:45 +00:00
|
|
|
children,
|
2022-08-08 18:39:08 +00:00
|
|
|
}: IAuthAnyMaintainerAnyAdminRoutesProps) => {
|
2022-01-27 22:10:12 +00:00
|
|
|
const handlePageError = useErrorHandler();
|
|
|
|
|
const {
|
|
|
|
|
currentUser,
|
|
|
|
|
isGlobalAdmin,
|
|
|
|
|
isGlobalMaintainer,
|
|
|
|
|
isAnyTeamAdmin,
|
|
|
|
|
isAnyTeamMaintainer,
|
|
|
|
|
} = useContext(AppContext);
|
|
|
|
|
|
|
|
|
|
if (!currentUser) {
|
2021-06-04 13:00:14 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
2022-01-27 22:10:12 +00:00
|
|
|
!isGlobalAdmin &&
|
|
|
|
|
!isGlobalMaintainer &&
|
|
|
|
|
!isAnyTeamAdmin &&
|
|
|
|
|
!isAnyTeamMaintainer
|
2021-06-04 13:00:14 +00:00
|
|
|
) {
|
2022-01-27 22:10:12 +00:00
|
|
|
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 AuthAnyMaintainerAnyAdminRoutes;
|