Skip to content

Commit

Permalink
Add protected routes to compliance report and schedules.
Browse files Browse the repository at this point in the history
  • Loading branch information
areyeslo committed Jan 14, 2025
1 parent 883175a commit 4e6fd1f
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 24 deletions.
29 changes: 17 additions & 12 deletions frontend/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@ import { ApiDocs } from './components/ApiDocs'
import { Login } from './components/Login'
import { NotFound } from './components/NotFound'
import PublicLayout from './layouts/PublicLayout'
import { AddEditNotionalTransfers } from '@/views/NotionalTransfers'
import { AddEditOtherUses } from './views/OtherUses/AddEditOtherUses'
import { AddEditFinalSupplyEquipments } from './views/FinalSupplyEquipments/AddEditFinalSupplyEquipments'
import { AddEditFuelSupplies } from './views/FuelSupplies/AddEditFuelSupplies'
import { AddEditFuelExports } from './views/FuelExports/AddEditFuelExports'
import { AddEditAllocationAgreements } from './views/AllocationAgreements/AddEditAllocationAgreements'
import AddEditNotionalTransfersWithAccess from './views/NotionalTransfers/AddEditNotionalTransfers'
import AddEditOtherUsesWithAccess from './views/OtherUses/AddEditOtherUses'
import AddEditFinalSupplyEquipmentsWithAccess from './views/FinalSupplyEquipments/AddEditFinalSupplyEquipments'
import AddEditFuelSuppliesWithAccess from './views/FuelSupplies/AddEditFuelSupplies'
import AddEditFuelExportsWithAccess from './views/FuelExports/AddEditFuelExports'
import AddEditAllocationAgreementsWithAccess from './views/AllocationAgreements/AddEditAllocationAgreements'
import { logout } from '@/utils/keycloak.js'
import { CompareReports } from '@/views/CompareReports/CompareReports'
import { ComplianceReportViewSelector } from '@/views/ComplianceReports/ComplianceReportViewSelector.jsx'
import { useCurrentUser } from './hooks/useCurrentUser'
import Loading from './components/Loading'
import { Unauthorized } from './components/Unauthorized'

const router = createBrowserRouter([
{
Expand All @@ -47,6 +48,10 @@ const router = createBrowserRouter([
path: ROUTES.LOGIN,
element: <Login />,
handle: { title: 'Login' }
},
{
path: '/unauthorized',
element: <Unauthorized />
}
]
},
Expand Down Expand Up @@ -331,47 +336,47 @@ const router = createBrowserRouter([
},
{
path: ROUTES.REPORTS_ADD_NOTIONAL_TRANSFERS,
element: <AddEditNotionalTransfers />,
element: <AddEditNotionalTransfersWithAccess />,
handle: {
title: 'Notional transfer of eligible renewable fuels',
mode: 'add'
}
},
{
path: ROUTES.REPORTS_ADD_ALLOCATION_AGREEMENTS,
element: <AddEditAllocationAgreements />,
element: <AddEditAllocationAgreementsWithAccess />,
handle: {
title: 'Allocation agreements',
mode: 'add'
}
},
{
path: ROUTES.REPORTS_ADD_OTHER_USE_FUELS,
element: <AddEditOtherUses />,
element: <AddEditOtherUsesWithAccess />,
handle: {
title: 'Fuels for other use',
mode: 'add'
}
},
{
path: ROUTES.REPORTS_ADD_FINAL_SUPPLY_EQUIPMENTS,
element: <AddEditFinalSupplyEquipments />,
element: <AddEditFinalSupplyEquipmentsWithAccess />,
handle: {
title: 'Final supply equipment',
mode: 'add'
}
},
{
path: ROUTES.REPORTS_ADD_SUPPLY_OF_FUEL,
element: <AddEditFuelSupplies />,
element: <AddEditFuelSuppliesWithAccess />,
handle: {
title: 'Supply of fuel',
mode: 'add'
}
},
{
path: ROUTES.REPORTS_ADD_FUEL_EXPORTS,
element: <AddEditFuelExports />,
element: <AddEditFuelExportsWithAccess />,
handle: {
title: 'Export fuels',
mode: 'add'
Expand Down
40 changes: 40 additions & 0 deletions frontend/src/utils/withComplianceReportAcess.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { roles } from '@/constants/roles'
import { ROUTES } from '@/constants/routes'
import { COMPLIANCE_REPORT_STATUSES } from '@/constants/statuses'
import { useGetComplianceReport } from '@/hooks/useComplianceReports.js'
import { useCurrentUser } from '@/hooks/useCurrentUser.js'
import { useEffect } from 'react'
import { useNavigate, useParams } from 'react-router-dom'


const withComplianceReportAccess = (WrappedComponent) => {
function WithComplianceReportAccess(props) {
const { compliancePeriod, complianceReportId } = useParams()
const navigate = useNavigate()
const { data: currentUser, isLoading: isUserLoading } = useCurrentUser()
const { data: reportData, isLoading: isReportLoading } = useGetComplianceReport(currentUser?.organization?.organizationId, complianceReportId)

useEffect(() => {
if (!isUserLoading && !isReportLoading) {
const isGovernmentUser = currentUser?.roles?.some(role => role.name === roles.government)
const reportStatus = reportData?.report?.currentStatus?.status

// Prevent Analysts from accessing Draft reports
if (isGovernmentUser && reportStatus === COMPLIANCE_REPORT_STATUSES.DRAFT) {
navigate(ROUTES.REPORTS, { replace: true })
}
}
}, [isUserLoading, isReportLoading, currentUser, reportData, navigate])

if (isUserLoading || isReportLoading) {
return <div>Loading...</div>
}

return <WrappedComponent {...props} />
}

return WithComplianceReportAccess
}


export default withComplianceReportAccess
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useTranslation } from 'react-i18next'
import { useLocation, useNavigate, useParams } from 'react-router-dom'
import BCBox from '@/components/BCBox'
import { BCGridEditor } from '@/components/BCDataGrid/BCGridEditor'
import withComplianceReportAccess from '@/utils/withComplianceReportAcess'
import {
defaultColDef,
allocationAgreementColDefs,
Expand All @@ -20,7 +21,7 @@ import { v4 as uuid } from 'uuid'
import * as ROUTES from '@/constants/routes/routes.js'
import { DEFAULT_CI_FUEL } from '@/constants/common'

export const AddEditAllocationAgreements = () => {
const AddEditAllocationAgreements = () => {
const [rowData, setRowData] = useState([])
const gridRef = useRef(null)
const [gridApi, setGridApi] = useState()
Expand Down Expand Up @@ -390,3 +391,7 @@ export const AddEditAllocationAgreements = () => {
)
)
}

const AddEditAllocationAgreementsWithAccess = withComplianceReportAccess(AddEditAllocationAgreements)

export default AddEditAllocationAgreementsWithAccess
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useGetComplianceReport } from '@/hooks/useComplianceReports.js'
import { useCurrentUser } from '@/hooks/useCurrentUser.js'
import Loading from '@/components/Loading.jsx'
import { ViewLegacyComplianceReport } from '@/views/ComplianceReports/ViewLegacyComplianceReport.jsx'
import ViewLegacyComplianceReportWithAccess from '@/views/ComplianceReports/ViewLegacyComplianceReport.jsx'
import { useParams } from 'react-router-dom'
import { EditViewComplianceReport } from '@/views/ComplianceReports/EditViewComplianceReport.jsx'
import EditViewComplianceReportWithAccess from '@/views/ComplianceReports/EditViewComplianceReport.jsx'

export const ComplianceReportViewSelector = () => {
const { complianceReportId } = useParams()
Expand All @@ -25,13 +25,13 @@ export const ComplianceReportViewSelector = () => {
}

return reportData.report.legacyId ? (
<ViewLegacyComplianceReport
<ViewLegacyComplianceReportWithAccess
reportData={reportData}
error={error}
isError={isError}
/>
) : (
<EditViewComplianceReport
<EditViewComplianceReportWithAccess
reportData={reportData}
error={error}
isError={isError}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import BCButton from '@/components/BCButton'
import Loading from '@/components/Loading'
import { Role } from '@/components/Role'
import { roles, govRoles } from '@/constants/roles'
import withComplianceReportAccess from '@/utils/withComplianceReportAcess'
import { Fab, Stack, Tooltip } from '@mui/material'
import BCTypography from '@/components/BCTypography'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
Expand All @@ -33,7 +34,7 @@ const iconStyle = {
height: '2rem',
color: colors.white.main
}
export const EditViewComplianceReport = ({ reportData, isError, error }) => {
const EditViewComplianceReport = ({ reportData, isError, error }) => {
const { t } = useTranslation(['common', 'report'])
const location = useLocation()
const [modalData, setModalData] = useState(null)
Expand Down Expand Up @@ -321,3 +322,7 @@ export const EditViewComplianceReport = ({ reportData, isError, error }) => {
</>
)
}

const EditViewComplianceReportWithAccess = withComplianceReportAccess(EditViewComplianceReport)

export default EditViewComplianceReportWithAccess
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import BCBox from '@/components/BCBox'
import BCModal from '@/components/BCModal'
import Loading from '@/components/Loading'
import { Fab, Stack, Tooltip } from '@mui/material'
import withComplianceReportAccess from '@/utils/withComplianceReportAcess'
import BCTypography from '@/components/BCTypography'
import KeyboardArrowUpIcon from '@mui/icons-material/KeyboardArrowUp'
import KeyboardArrowDownIcon from '@mui/icons-material/KeyboardArrowDown'
Expand All @@ -18,7 +19,7 @@ const iconStyle = {
height: '2rem',
color: colors.white.main
}
export const ViewLegacyComplianceReport = ({ reportData, error, isError }) => {
const ViewLegacyComplianceReport = ({ reportData, error, isError }) => {
const { t } = useTranslation(['common', 'report'])
const [modalData, setModalData] = useState(null)
const alertRef = useRef()
Expand Down Expand Up @@ -141,3 +142,7 @@ export const ViewLegacyComplianceReport = ({ reportData, error, isError }) => {
</>
)
}

const ViewLegacyComplianceReportWithAccess = withComplianceReportAccess(ViewLegacyComplianceReport);

export default ViewLegacyComplianceReportWithAccess
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useTranslation } from 'react-i18next'
import { useLocation, useNavigate, useParams } from 'react-router-dom'
import { BCAlert2 } from '@/components/BCAlert'
import BCBox from '@/components/BCBox'
import withComplianceReportAccess from '@/utils/withComplianceReportAcess'
import { BCGridEditor } from '@/components/BCDataGrid/BCGridEditor'
import { defaultColDef, finalSupplyEquipmentColDefs } from './_schema'
import {
Expand All @@ -16,7 +17,7 @@ import { v4 as uuid } from 'uuid'
import * as ROUTES from '@/constants/routes/routes.js'
import { isArrayEmpty } from '@/utils/formatters'

export const AddEditFinalSupplyEquipments = () => {
const AddEditFinalSupplyEquipments = () => {
const [rowData, setRowData] = useState([])
const gridRef = useRef(null)
const [gridApi, setGridApi] = useState(null)
Expand Down Expand Up @@ -323,3 +324,7 @@ export const AddEditFinalSupplyEquipments = () => {
)
)
}

const AddEditFinalSupplyEquipmentsWithAccess = withComplianceReportAccess(AddEditFinalSupplyEquipments)

export default AddEditFinalSupplyEquipmentsWithAccess
7 changes: 6 additions & 1 deletion frontend/src/views/FuelExports/AddEditFuelExports.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
useSaveFuelExport
} from '@/hooks/useFuelExport'
import { isArrayEmpty } from '@/utils/formatters'
import withComplianceReportAccess from '@/utils/withComplianceReportAcess'
import BCTypography from '@/components/BCTypography'
import Grid2 from '@mui/material/Unstable_Grid2/Grid2'
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
Expand All @@ -19,7 +20,7 @@ import {
PROVISION_APPROVED_FUEL_CODE
} from './_schema'

export const AddEditFuelExports = () => {
const AddEditFuelExports = () => {
const [rowData, setRowData] = useState([])
const gridRef = useRef(null)
const [, setGridApi] = useState()
Expand Down Expand Up @@ -316,3 +317,7 @@ export const AddEditFuelExports = () => {
)
)
}

const AddEditFuelExportsWithAccess = withComplianceReportAccess(AddEditFuelExports)

export default AddEditFuelExportsWithAccess
7 changes: 6 additions & 1 deletion frontend/src/views/FuelSupplies/AddEditFuelSupplies.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
useSaveFuelSupply
} from '@/hooks/useFuelSupply'
import { isArrayEmpty, cleanEmptyStringValues } from '@/utils/formatters'
import withComplianceReportAccess from '@/utils/withComplianceReportAcess'
import BCTypography from '@/components/BCTypography'
import Grid2 from '@mui/material/Unstable_Grid2/Grid2'
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
Expand All @@ -20,7 +21,7 @@ import {
PROVISION_APPROVED_FUEL_CODE
} from './_schema'

export const AddEditFuelSupplies = () => {
const AddEditFuelSupplies = () => {
const [rowData, setRowData] = useState([])
const gridRef = useRef(null)
const [, setGridApi] = useState()
Expand Down Expand Up @@ -398,3 +399,7 @@ export const AddEditFuelSupplies = () => {
)
)
}

const AddEditFuelSuppliesWithAccess = withComplianceReportAccess(AddEditFuelSupplies)

export default AddEditFuelSuppliesWithAccess
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useState, useEffect, useRef, useCallback } from 'react'
import BCTypography from '@/components/BCTypography'
import Grid2 from '@mui/material/Unstable_Grid2/Grid2'
import { useTranslation } from 'react-i18next'
import withComplianceReportAccess from '@/utils/withComplianceReportAcess'
import { useLocation, useNavigate, useParams } from 'react-router-dom'
import BCBox from '@/components/BCBox'
import Loading from '@/components/Loading'
Expand All @@ -16,7 +17,7 @@ import { v4 as uuid } from 'uuid'
import { BCGridEditor } from '@/components/BCDataGrid/BCGridEditor'
import * as ROUTES from '@/constants/routes/routes.js'

export const AddEditNotionalTransfers = () => {
const AddEditNotionalTransfers = () => {
const [rowData, setRowData] = useState([])
const [errors, setErrors] = useState({})
const [columnDefs, setColumnDefs] = useState([])
Expand Down Expand Up @@ -314,3 +315,7 @@ export const AddEditNotionalTransfers = () => {
)
)
}

const AddEditNotionalTransfersWithAccess = withComplianceReportAccess(AddEditNotionalTransfers)

export default AddEditNotionalTransfersWithAccess
7 changes: 6 additions & 1 deletion frontend/src/views/OtherUses/AddEditOtherUses.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
useSaveOtherUses
} from '@/hooks/useOtherUses'
import { cleanEmptyStringValues } from '@/utils/formatters'
import withComplianceReportAccess from '@/utils/withComplianceReportAcess'
import BCTypography from '@/components/BCTypography'
import Grid2 from '@mui/material/Unstable_Grid2/Grid2'
import { useCallback, useEffect, useRef, useState } from 'react'
Expand All @@ -19,7 +20,7 @@ import {
} from './_schema'
import * as ROUTES from '@/constants/routes/routes.js'

export const AddEditOtherUses = () => {
const AddEditOtherUses = () => {
const [rowData, setRowData] = useState([])
const [errors, setErrors] = useState({})

Expand Down Expand Up @@ -386,3 +387,7 @@ export const AddEditOtherUses = () => {
)
)
}

const AddEditOtherUsesWithAccess = withComplianceReportAccess(AddEditOtherUses)

export default AddEditOtherUsesWithAccess

0 comments on commit 4e6fd1f

Please sign in to comment.