From 169249e2432655c44cc443497aca5517b38a2587 Mon Sep 17 00:00:00 2001 From: Rushikesh-Sonawane99 Date: Mon, 8 Jul 2024 19:52:31 +0530 Subject: [PATCH] Issue #PS-1157 feat: Implemented Role based access for TL and Facilitator on all pages in app --- app.config.ts | 17 ++++++++++++++--- src/components/MenuDrawer.tsx | 8 +++++++- src/pages/attendance-history.tsx | 5 ++++- src/pages/attendance-overview.tsx | 7 +++++-- src/pages/centers/index.tsx | 22 ++++++++++++++-------- src/pages/dashboard.tsx | 13 +++++++------ src/pages/learner-attendance-history.tsx | 5 ++++- src/pages/learner/[userId].tsx | 4 +++- src/pages/profile.tsx | 4 +++- src/utils/Helper.ts | 10 +++++++++- src/utils/hoc/withAccessControl.tsx | 17 ++++++++--------- 11 files changed, 78 insertions(+), 34 deletions(-) diff --git a/app.config.ts b/app.config.ts index 86a788ca..26635ee6 100644 --- a/app.config.ts +++ b/app.config.ts @@ -26,7 +26,18 @@ export const dropoutReasons = [ ]; export const accessControl: { [key: string]: Role[] } = { - viewDashboard: [Role.TEACHER, Role.TEAM_LEADER], - viewAttendance: [Role.TEACHER, Role.TEAM_LEADER], - createLearner: [Role.TEACHER, Role.TEAM_LEADER], + accessDashboard: [Role.TEACHER, Role.TEAM_LEADER], + accessAttendanceHistory: [Role.TEACHER, Role.TEAM_LEADER], + accessAttendanceOverview: [Role.TEACHER, Role.TEAM_LEADER], + accessProfile: [Role.TEACHER, Role.TEAM_LEADER], + accessLearnerProfile: [Role.TEACHER, Role.TEAM_LEADER], + accessLearnerAttendanceHistory: [Role.TEACHER, Role.TEAM_LEADER], + showTeachingCenter : [Role.TEAM_LEADER], + showBlockLevelCohort : [Role.TEAM_LEADER], + showTeacherCohorts: [Role.TEACHER], + showBlockLevelData: [Role.TEAM_LEADER], + showCreateCenterButton: [Role.TEAM_LEADER], + showBlockLevelCenterData: [Role.TEAM_LEADER], + showTeacherLevelCenterData: [Role.TEACHER], }; + diff --git a/src/components/MenuDrawer.tsx b/src/components/MenuDrawer.tsx index 0834b9c6..dcc19dcb 100644 --- a/src/components/MenuDrawer.tsx +++ b/src/components/MenuDrawer.tsx @@ -15,6 +15,10 @@ import { useTheme } from '@mui/material/styles'; import { useTranslation } from 'next-i18next'; import PeopleOutlineOutlinedIcon from '@mui/icons-material/PeopleOutlineOutlined'; import EditNoteIcon from '@mui/icons-material/EditNote'; +import { Role } from '@/utils/app.constant'; +import useStore from '@/store/store'; +import { accessGranted } from '@/utils/Helper'; +import { accessControl } from '../../app.config'; interface DrawerProps { toggleDrawer: (open: boolean) => () => void; open: boolean; @@ -33,6 +37,8 @@ const MenuDrawer: React.FC = ({ const [isTeamLeader, setIsTeamLeader] = useState(false); const { t } = useTranslation(); const router = useRouter(); + const store = useStore(); + const userRole = store.userRole; useEffect(() => setIsOpen(open), [open]); @@ -206,7 +212,7 @@ const MenuDrawer: React.FC = ({ router.push(`/centers`); // Check route }} > - {isTeamLeader + {accessGranted('showTeachingCenter', accessControl, userRole) ? t('DASHBOARD.TEACHING_CENTERS') : t('DASHBOARD.MY_TEACHING_CENTERS')} diff --git a/src/pages/attendance-history.tsx b/src/pages/attendance-history.tsx index 51838439..9941ab50 100644 --- a/src/pages/attendance-history.tsx +++ b/src/pages/attendance-history.tsx @@ -49,6 +49,8 @@ import { logEvent } from '@/utils/googleAnalytics'; import { showToastMessage } from '@/components/Toastify'; import { Status } from '@/utils/app.constant'; import CohortSelectionSection from '@/components/CohortSelectionSection'; +import withAccessControl from '@/utils/hoc/withAccessControl'; +import { accessControl } from '../../app.config'; interface user { memberStatus: string; @@ -829,4 +831,5 @@ export async function getStaticProps({ locale }: any) { }; } -export default UserAttendanceHistory; +// export default UserAttendanceHistory; +export default withAccessControl('accessAttendanceHistory', accessControl)(UserAttendanceHistory); diff --git a/src/pages/attendance-overview.tsx b/src/pages/attendance-overview.tsx index 6a8928e2..2ef20a06 100644 --- a/src/pages/attendance-overview.tsx +++ b/src/pages/attendance-overview.tsx @@ -42,7 +42,7 @@ import SortingModal from '@/components/SortingModal'; import StudentsStatsList from '@/components/LearnerAttendanceStatsListView'; import UpDownButton from '@/components/UpDownButton'; import { getMyCohortMemberList } from '@/services/MyClassDetailsService'; -import { lowLearnerAttendanceLimit } from './../../app.config'; +import { accessControl, lowLearnerAttendanceLimit } from './../../app.config'; import { serverSideTranslations } from 'next-i18next/serverSideTranslations'; import { usePathname } from 'next/navigation'; import { useRouter } from 'next/router'; @@ -51,6 +51,7 @@ import { useTranslation } from 'next-i18next'; import { logEvent } from '@/utils/googleAnalytics'; import { showToastMessage } from '@/components/Toastify'; import CohortSelectionSection from '@/components/CohortSelectionSection'; +import withAccessControl from '@/utils/hoc/withAccessControl'; interface AttendanceOverviewProps { // buttonText: string; @@ -798,4 +799,6 @@ export async function getStaticProps({ locale }: any) { }; } -export default AttendanceOverview; +// export default AttendanceOverview; +export default withAccessControl('accessAttendanceOverview', accessControl)(AttendanceOverview); + diff --git a/src/pages/centers/index.tsx b/src/pages/centers/index.tsx index dec2d07d..30e0efc8 100644 --- a/src/pages/centers/index.tsx +++ b/src/pages/centers/index.tsx @@ -31,13 +31,19 @@ import ManageUser from '@/components/ManageUser'; import { setTimeout } from 'timers'; import SmartDisplayOutlinedIcon from '@mui/icons-material/SmartDisplayOutlined'; import CreateCenterModal from '@/components/center/CreateCenterModal'; -import { toPascalCase } from '@/utils/Helper'; +import { accessGranted, toPascalCase } from '@/utils/Helper'; +import { Role } from '@/utils/app.constant'; +import useStore from '@/store/store'; +import { accessControl } from '../../../app.config'; const TeachingCenters = () => { const [loading, setLoading] = React.useState(false); const { t } = useTranslation(); const theme = useTheme(); const router = useRouter(); + const store = useStore(); + const userRole = store.userRole; + const [cohortsData, setCohortsData] = React.useState>([]); const [value, setValue] = React.useState(1); const [blockData, setBlockData] = @@ -71,7 +77,7 @@ const TeachingCenters = () => { try { if (typeof window !== 'undefined' && window.localStorage) { const userId = localStorage.getItem('userId'); - if (userId && isTeamLeader) { + if (userId && accessGranted('showBlockLevelCohort', accessControl, userRole)) { const response = await getCohortList(userId, { customField: 'true', }); @@ -107,7 +113,7 @@ const TeachingCenters = () => { setCenterData(centerData); }); } - if (userId && !isTeamLeader) { + if (userId && accessGranted('showTeacherCohorts', accessControl, userRole)) { const response = await getCohortList(userId); const cohortData = response.map((block: any) => { const cohortName = block.cohortName; @@ -136,7 +142,7 @@ const TeachingCenters = () => {
{loading && } - {isTeamLeader ? ( + {accessGranted('showBlockLevelData', accessControl, userRole) ? ( <> {blockData?.length !== 0 && blockData?.map((block: any) => ( @@ -166,7 +172,7 @@ const TeachingCenters = () => { {t('DASHBOARD.MY_TEACHING_CENTERS')} )} - {isTeamLeader && ( + {accessGranted('showBlockLevelData', accessControl, userRole) && ( { - {isTeamLeader && ( + {accessGranted('showCreateCenterButton', accessControl, userRole) && (