Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRDCDH-2099 PBAC - Display Submission Request Action buttons based on permissions #569

Merged
merged 13 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/config/AuthPermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,21 @@ export const PERMISSION_MAP = {
submission_request: {
view: NO_CONDITIONS,
create: NO_CONDITIONS,
submit: NO_CONDITIONS,
submit: (user, application) => {
Alejandro-Vega marked this conversation as resolved.
Show resolved Hide resolved
const isFormOwner = application?.applicant?.applicantID === user?._id;
const hasPermissionKey = user?.permissions?.includes("submission_request:submit");
const submitStatuses: ApplicationStatus[] = ["In Progress", "Inquired"];

// Check for implicit permission as well as for the permission key
if (!isFormOwner && !hasPermissionKey) {
return false;
}
if (!submitStatuses?.includes(application?.status)) {
return false;
}

return true;
},
review: NO_CONDITIONS,
},
dashboard: {
Expand Down
22 changes: 5 additions & 17 deletions src/content/questionnaire/FormView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ const FormView: FC<Props> = ({ section }: Props) => {
? `/submission/${data?.["_id"]}/${sectionKeys[sectionIndex + 1]}`
: null;
const isSectionD = activeSection === "D";
const isFormOwner = data?.applicant?.applicantID === user?._id;
const formContentRef = useRef(null);
const lastSectionRef = useRef(null);
const hasReopenedFormRef = useRef(false);
Expand Down Expand Up @@ -538,13 +539,10 @@ const FormView: FC<Props> = ({ section }: Props) => {
};

const handleSubmitForm = () => {
if (
!hasPermission(user, "submission_request", "submit", data) ||
(data?.status !== "In Progress" &&
(data?.status !== "Inquired" || user?.role !== "Federal Lead"))
) {
if (!hasPermission(user, "submission_request", "submit", data)) {
Logger.error("Invalid request to submit Submission Request form.", {
userRole: user?.role,
isFormOwner,
hasPermission,
Alejandro-Vega marked this conversation as resolved.
Show resolved Hide resolved
submissionStatus: data?.status,
});
return;
Expand Down Expand Up @@ -627,15 +625,6 @@ const FormView: FC<Props> = ({ section }: Props) => {
};
});

useEffect(() => {
const formLoaded = status === FormStatus.LOADED && authStatus === AuthStatus.LOADED && data;
const invalidFormAuth = formMode === "Unauthorized" || authStatus === AuthStatus.ERROR || !user;

if (formLoaded && invalidFormAuth) {
navigate("/");
}
}, [formMode, navigate, status, authStatus, user, data]);

useEffect(() => {
const isComplete = isAllSectionsComplete();
setAllSectionsComplete(isComplete);
Expand Down Expand Up @@ -730,8 +719,7 @@ const FormView: FC<Props> = ({ section }: Props) => {
)}

{activeSection === "REVIEW" &&
hasPermission(user, "submission_request", "submit") &&
["In Progress", "Inquired"].includes(data?.status) && (
hasPermission(user, "submission_request", "submit", data) && (
<StyledExtendedLoadingButton
id="submission-form-submit-button"
variant="contained"
Expand Down
13 changes: 9 additions & 4 deletions src/hooks/useFormMode.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect, useState } from "react";
import { Status as AuthStatus, useAuthContext } from "../components/Contexts/AuthContext";
import { Status as FormStatus, useFormContext } from "../components/Contexts/FormContext";
import { FormMode, FormModes, getFormMode } from "../utils";
import { FormMode, FormModes, getFormMode, Logger } from "../utils";

const useFormMode = () => {
const { user, status: authStatus } = useAuthContext();
Expand All @@ -15,10 +15,15 @@ const useFormMode = () => {
}

const updatedFormMode: FormMode = getFormMode(user, data);
if (updatedFormMode === FormModes.UNAUTHORIZED) {
Logger.error("useFormMode: User is unauthorized to view this Submission Request.", {
user,
data,
});
}

setFormMode(updatedFormMode);
setReadOnlyInputs(
updatedFormMode === FormModes.VIEW_ONLY || updatedFormMode === FormModes.REVIEW
);
setReadOnlyInputs(updatedFormMode !== FormModes.EDIT);
}, [user, data]);

return { formMode, readOnlyInputs };
Expand Down
8 changes: 6 additions & 2 deletions src/utils/formModeUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,14 @@ describe("getFormMode tests based on provided requirements", () => {
expect(utils.getFormMode(null, null)).toBe(utils.FormModes.UNAUTHORIZED);
});

it("should set Unauthorized form if user does not have the required permissions", () => {
it("should set Unauthorized form if user does not have the required permissions and is not submission owner", () => {
const user: User = { ...baseUser, role: undefined, permissions: [] };
const submission: Application = {
...baseSubmission,
applicant: { ...baseSubmission.applicant, applicantID: "some-other-user" },
};

expect(utils.getFormMode(user, baseSubmission)).toBe(utils.FormModes.UNAUTHORIZED);
expect(utils.getFormMode(user, submission)).toBe(utils.FormModes.UNAUTHORIZED);
});

it("should set 'View Only' if form status is unknown or not defined", () => {
Expand Down
14 changes: 9 additions & 5 deletions src/utils/formModeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ export const getFormMode = (user: User, data: Application): FormMode => {
if (!data) {
return FormModes.UNAUTHORIZED;
}
if (
!hasPermission(user, "submission_request", "view") &&
user?._id !== data.applicant?.applicantID
) {
const isFormOwner = user?._id === data.applicant?.applicantID;
if (!hasPermission(user, "submission_request", "view") && !isFormOwner) {
return FormModes.UNAUTHORIZED;
}

if (
!isFormOwner &&
!hasPermission(user, "submission_request", "view") &&
!hasPermission(user, "submission_request", "create") &&
!hasPermission(user, "submission_request", "review")
Expand All @@ -45,7 +44,12 @@ export const getFormMode = (user: User, data: Application): FormMode => {
return FormModes.REVIEW;
}

if (hasPermission(user, "submission_request", "create") && EditStatuses.includes(data?.status)) {
// User is only allowed to edit their own Submission Request
if (
isFormOwner &&
hasPermission(user, "submission_request", "create") &&
EditStatuses.includes(data?.status)
) {
return FormModes.EDIT;
}

Expand Down
Loading