Skip to content

Commit

Permalink
Restrict form actions based on permissions and if form owner
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro-Vega committed Dec 18, 2024
1 parent fb0c6f5 commit ea86277
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
13 changes: 8 additions & 5 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 @@ -539,12 +540,12 @@ 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"))
(!isFormOwner && !hasPermission(user, "submission_request", "submit", data)) ||
!["In Progress", "Inquired"].includes(data?.status)
) {
Logger.error("Invalid request to submit Submission Request form.", {
userRole: user?.role,
isFormOwner,
hasPermission,
submissionStatus: data?.status,
});
return;
Expand Down Expand Up @@ -730,7 +731,9 @@ const FormView: FC<Props> = ({ section }: Props) => {
)}

{activeSection === "REVIEW" &&
hasPermission(user, "submission_request", "submit") &&
// Submission Request owners aren't granted the permission,
// but should be allowed to submit
(isFormOwner || hasPermission(user, "submission_request", "submit", data)) &&
["In Progress", "Inquired"].includes(data?.status) && (
<StyledExtendedLoadingButton
id="submission-form-submit-button"
Expand Down
6 changes: 2 additions & 4 deletions src/utils/formModeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ 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;
}

Expand Down

0 comments on commit ea86277

Please sign in to comment.