Skip to content

Commit

Permalink
Added company profile edit permissions validation
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaspalma committed Sep 14, 2023
1 parent 8cd872c commit 1d05cf2
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/AppRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ import {
import { CookieConsent } from "./cookieConsent";
import {
EditOfferController,
EditOfferControllerContext,
EditOfferControllerContext
} from "./components/Offers/Edit/EditOfferForm";
import EditOfferPage from "./pages/EditOfferPage";
import PrivacyPolicyPage from "./pages/PrivacyPolicyPage";
import TermsAndConditionsPage from "./pages/TermsAndConditionsPage";
import ChangeLogPage from "./pages/ChangeLogPage";
import EditCompanyProfilePage from "./pages/EditCompanyProfilePage";
import { EditCompanyController } from "./components/Company/Edit/EditCompanyProfileForm";
import { EditCompanyController, EditCompanyControllerContext } from "./components/Company/Edit/EditCompanyProfileForm";

/**
*
Expand Down Expand Up @@ -197,7 +197,8 @@ const AppRouter = () => (
unauthorizedRedirectPath="/"
unauthorizedRedirectMessage="You are not allowed to edit this company"
authorize={(user) => !!(user?.company || user?.isAdmin)}
// controller={EditCompanyController}
context={EditCompanyControllerContext}
controller={EditCompanyController}
>
<PageLayout
key="/company/:id/edit"
Expand Down
60 changes: 56 additions & 4 deletions src/components/Company/Edit/EditCompanyProfileForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import useOffer from "../../../hooks/useOffer";
import { Controller, useForm } from "react-hook-form";
import useCompany from "../../../hooks/useCompany";
import useSession from "../../../hooks/useSession";
import { useParams } from "react-router-dom/cjs/react-router-dom.min";
import { Redirect, useLocation, useParams } from "react-router-dom/cjs/react-router-dom.min";
import { yupResolver } from "@hookform/resolvers/yup";
import EditCompanySchema from "./EditCompanySchema";
import MultiOptionTextField from "../../utils/form/MultiOptionTextField";
Expand All @@ -36,17 +36,65 @@ const useStyles = makeStyles((theme) => ({
}));

export const EditCompanyController = () => {
const { id } = useParams();
const { company, error: companyError, loading: loadingCompany } = useCompany(id);
const { data: user, isValidating } = useSession();
let canEditRaceControl = false;

const shouldRevalidateEditingPermissions = useCallback(() => {
return user?.isAdmin || user?.company?._id === id;
}, [company, user]);

const [canEdit, setCanEdit] = useState(shouldRevalidateEditingPermissions());

useEffect(() => {
setCanEdit(shouldRevalidateEditingPermissions());
if (!loadingCompany && !isValidating) {
canEditRaceControl = true;
}
}, [shouldRevalidateEditingPermissions, loadingCompany, company, user]);

const location = useLocation();
const redirectProps = {
to: {
pathname: "/",
state: {
from: location,
message: "You are not authorized to edit this company.",
},
},
};

return {
controllerOptions: {
initialValue: {
canEdit,
company,
redirectProps,
loadingCompany,
companyError,
isValidating,
canEditRaceControl,
}
},
};
};

const EditCompanyProfileForm = ({ title }) => {
const isMobile = useMobile();
const formCardClasses = useOfferFormStyles(isMobile)();

const classes = useStyles();
const {
company,
loadingCompany,
companyError,
canEdit,
redirectProps,
isValidating,
canEditRaceControl,
} = useContext(EditCompanyControllerContext);

const { id } = useParams();
const { company } = useCompany(id);
const classes = useStyles();

const { control } = useForm({
mode: "all",
Expand Down Expand Up @@ -74,6 +122,10 @@ const EditCompanyProfileForm = ({ title }) => {

const Content = isMobile ? DialogContent : CardContent;

if (companyError || (!loadingCompany && !isValidating && !canEdit && canEditRaceControl)) {
return <Redirect {...redirectProps} />;
}

return <>
<div className={formCardClasses.formCard}>
<CardHeader title={title} />
Expand Down
8 changes: 7 additions & 1 deletion src/components/Offers/Edit/EditOfferForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const EditOfferController = () => {
const { id } = useParams();
const { offer, error: errorOffer, loading: loadingOffer } = useOffer(id);
const { data: user, isValidating } = useSession();
let canEditRaceControl = false;

// This portion of code is used to remove race conditions between useState of canEdit and useEffect
// If the value of useState is false by default, this condition will be wrongly verified, resulting in unwanted redirects
Expand All @@ -63,6 +64,9 @@ export const EditOfferController = () => {

useEffect(() => {
setCanEdit(shouldRevalidateEditingPermissions());
if (!loadingOffer && !isValidating) {
canEditRaceControl = true;
}
}, [shouldRevalidateEditingPermissions, loadingOffer, offer, user]);

const location = useLocation();
Expand Down Expand Up @@ -144,6 +148,7 @@ export const EditOfferController = () => {
user,
isValidating,
canEdit,
canEditRaceControl,
},
},
};
Expand All @@ -156,9 +161,10 @@ const EditOfferForm = () => {
redirectProps,
isValidating,
canEdit,
canEditRaceControl,
} = useContext(EditOfferControllerContext);

if (errorOffer || (!loadingOffer && !isValidating && canEdit === false)) {
if (errorOffer || (!loadingOffer && !isValidating && !canEdit && canEditRaceControl)) {
return <Redirect {...redirectProps} />;
}

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useCompany.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default (id) => {
return {
company,
error,
loading: isLoading,
loading: !data,
mutate,
};
};

0 comments on commit 1d05cf2

Please sign in to comment.