Updated the generation page to now work with the entity permission system
This commit is contained in:
@@ -10,38 +10,56 @@ import clsx from "clsx";
|
|||||||
import { MODULE_ARRAY } from "@/utils/moduleUtils";
|
import { MODULE_ARRAY } from "@/utils/moduleUtils";
|
||||||
import { capitalize } from "lodash";
|
import { capitalize } from "lodash";
|
||||||
import Input from "@/components/Low/Input";
|
import Input from "@/components/Low/Input";
|
||||||
import { checkAccess } from "@/utils/permissions";
|
import { checkAccess, findAllowedEntities } from "@/utils/permissions";
|
||||||
import { User } from "@/interfaces/user";
|
import { User } from "@/interfaces/user";
|
||||||
import useExamEditorStore from "@/stores/examEditor";
|
import useExamEditorStore from "@/stores/examEditor";
|
||||||
import ExamEditorStore from "@/stores/examEditor/types";
|
import ExamEditorStore from "@/stores/examEditor/types";
|
||||||
import ExamEditor from "@/components/ExamEditor";
|
import ExamEditor from "@/components/ExamEditor";
|
||||||
import MultipleAudioUploader from "@/components/ExamEditor/Shared/AudioEdit";
|
import MultipleAudioUploader from "@/components/ExamEditor/Shared/AudioEdit";
|
||||||
import { redirect, serialize } from "@/utils";
|
import { mapBy, redirect, serialize } from "@/utils";
|
||||||
import { requestUser } from "@/utils/api";
|
import { requestUser } from "@/utils/api";
|
||||||
import { Module } from "@/interfaces";
|
import { Module } from "@/interfaces";
|
||||||
import { getExam, getExams } from "@/utils/exams.be";
|
import { getExam, getExams } from "@/utils/exams.be";
|
||||||
import { Exam } from "@/interfaces/exam";
|
import { Exam } from "@/interfaces/exam";
|
||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
|
import { getEntitiesWithRoles } from "@/utils/entities.be";
|
||||||
|
import { isAdmin } from "@/utils/users";
|
||||||
|
|
||||||
|
type Permission = { [key in Module]: boolean }
|
||||||
|
|
||||||
export const getServerSideProps = withIronSessionSsr(async ({ req, res, query }) => {
|
export const getServerSideProps = withIronSessionSsr(async ({ req, res, query }) => {
|
||||||
const user = await requestUser(req, res)
|
const user = await requestUser(req, res)
|
||||||
if (!user) return redirect("/login")
|
if (!user) return redirect("/login")
|
||||||
|
|
||||||
if (shouldRedirectHome(user) || !checkAccess(user, ["admin", "mastercorporate", "developer", "corporate"]))
|
if (shouldRedirectHome(user)) return redirect("/")
|
||||||
return redirect("/")
|
|
||||||
|
const entityIDs = mapBy(user.entities, 'id')
|
||||||
|
const entities = await getEntitiesWithRoles(isAdmin(user) ? undefined : entityIDs)
|
||||||
|
|
||||||
|
const permissions: Permission = {
|
||||||
|
reading: findAllowedEntities(user, entities, `generate_reading`).length > 0,
|
||||||
|
listening: findAllowedEntities(user, entities, `generate_listening`).length > 0,
|
||||||
|
writing: findAllowedEntities(user, entities, `generate_writing`).length > 0,
|
||||||
|
speaking: findAllowedEntities(user, entities, `generate_speaking`).length > 0,
|
||||||
|
level: findAllowedEntities(user, entities, `generate_level`).length > 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Object.keys(permissions).every(p => !permissions[p as Module])) return redirect("/")
|
||||||
|
|
||||||
const { id, module } = query as { id?: string, module?: Module }
|
const { id, module } = query as { id?: string, module?: Module }
|
||||||
if (!id || !module) return { props: serialize({ user }) };
|
if (!id || !module) return { props: serialize({ user, permissions }) };
|
||||||
|
|
||||||
|
if (!permissions[module]) return redirect("/generation")
|
||||||
|
|
||||||
const exam = await getExam(module, id)
|
const exam = await getExam(module, id)
|
||||||
if (!exam) return redirect("/generation")
|
if (!exam) return redirect("/generation")
|
||||||
|
|
||||||
return {
|
return {
|
||||||
props: serialize({ user, exam }),
|
props: serialize({ user, exam, permissions }),
|
||||||
};
|
};
|
||||||
}, sessionOptions);
|
}, sessionOptions);
|
||||||
|
|
||||||
export default function Generation({ user, exam }: { user: User; exam?: Exam }) {
|
export default function Generation({ user, exam, permissions }: { user: User; exam?: Exam, permissions: Permission }) {
|
||||||
const { title, currentModule, dispatch } = useExamEditorStore();
|
const { title, currentModule, dispatch } = useExamEditorStore();
|
||||||
|
|
||||||
const updateRoot = (updates: Partial<ExamEditorStore>) => {
|
const updateRoot = (updates: Partial<ExamEditorStore>) => {
|
||||||
@@ -84,7 +102,7 @@ export default function Generation({ user, exam }: { user: User; exam?: Exam })
|
|||||||
value={currentModule}
|
value={currentModule}
|
||||||
onChange={(currentModule) => updateRoot({ currentModule })}
|
onChange={(currentModule) => updateRoot({ currentModule })}
|
||||||
className="flex flex-row -2xl:flex-wrap w-full gap-4 -md:justify-center justify-between">
|
className="flex flex-row -2xl:flex-wrap w-full gap-4 -md:justify-center justify-between">
|
||||||
{[...MODULE_ARRAY].map((x) => (
|
{[...MODULE_ARRAY].filter(m => permissions[m]).map((x) => (
|
||||||
<Radio value={x} key={x}>
|
<Radio value={x} key={x}>
|
||||||
{({ checked }) => (
|
{({ checked }) => (
|
||||||
<span
|
<span
|
||||||
|
|||||||
Reference in New Issue
Block a user