Continued creating the permission system

This commit is contained in:
Tiago Ribeiro
2024-10-11 10:47:35 +01:00
parent 55204e2ce1
commit a53ee79c0a
12 changed files with 196 additions and 121 deletions

View File

@@ -20,12 +20,12 @@ import {BsBook, BsChevronLeft, BsClipboard, BsHeadphones, BsMegaphone, BsPen} fr
import {toast} from "react-toastify";
import {futureAssignmentFilter} from "@/utils/assignments";
import {withIronSessionSsr} from "iron-session/next";
import {checkAccess} from "@/utils/permissions";
import {checkAccess, doesEntityAllow} from "@/utils/permissions";
import {mapBy, redirect, serialize} from "@/utils";
import {getAssignment} from "@/utils/assignments.be";
import {getEntitiesUsers, getUsers} from "@/utils/users.be";
import {getEntitiesWithRoles} from "@/utils/entities.be";
import {getGroups, getGroupsByEntities} from "@/utils/groups.be";
import {getEntitiesUsers, getEntityUsers, getUsers} from "@/utils/users.be";
import {getEntitiesWithRoles, getEntityWithRoles} from "@/utils/entities.be";
import {getGroups, getGroupsByEntities, getGroupsByEntity} from "@/utils/groups.be";
import {sessionOptions} from "@/lib/session";
import {EntityWithRoles} from "@/interfaces/entity";
import Head from "next/head";
@@ -33,6 +33,7 @@ import Layout from "@/components/High/Layout";
import Separator from "@/components/Low/Separator";
import Link from "next/link";
import { requestUser } from "@/utils/api";
import { useEntityPermission } from "@/hooks/useEntityPermissions";
export const getServerSideProps = withIronSessionSsr(async ({req, res, params}) => {
const user = await requestUser(req, res)
@@ -44,33 +45,31 @@ export const getServerSideProps = withIronSessionSsr(async ({req, res, params})
res.setHeader("Cache-Control", "public, s-maxage=10, stale-while-revalidate=59");
const {id} = params as {id: string};
const entityIDS = mapBy(user.entities, "id") || [];
const assignment = await getAssignment(id);
if (!assignment)
return {
redirect: {
destination: "/assignments",
permanent: false,
},
};
if (!assignment) return redirect("/assignments")
const users = await (checkAccess(user, ["developer", "admin"]) ? getUsers() : getEntitiesUsers(entityIDS));
const entities = await (checkAccess(user, ["developer", "admin"]) ? getEntitiesWithRoles() : getEntitiesWithRoles(entityIDS));
const groups = await (checkAccess(user, ["developer", "admin"]) ? getGroups() : getGroupsByEntities(entityIDS));
const entity = await getEntityWithRoles(assignment.entity || "")
if (!entity) return redirect("/assignments")
return {props: serialize({user, users, entities, assignment, groups})};
if (!doesEntityAllow(user, entity, 'view_assignments')) return redirect("/assignments")
const users = await (checkAccess(user, ["developer", "admin"]) ? getUsers() : getEntityUsers(entity.id));
return {props: serialize({user, users, entity, assignment})};
}, sessionOptions);
interface Props {
user: User;
users: User[];
assignment: Assignment;
groups: Group[];
entities: EntityWithRoles[];
entity: EntityWithRoles
}
export default function AssignmentView({user, users, entities, groups, assignment}: Props) {
export default function AssignmentView({user, users, entity, assignment}: Props) {
const canDeleteAssignment = useEntityPermission(user, entity, 'delete_assignment')
const canStartAssignment = useEntityPermission(user, entity, 'start_assignment')
const setExams = useExamStore((state) => state.setExams);
const setShowSolutions = useExamStore((state) => state.setShowSolutions);
const setUserSolutions = useExamStore((state) => state.setUserSolutions);
@@ -79,6 +78,7 @@ export default function AssignmentView({user, users, entities, groups, assignmen
const router = useRouter();
const deleteAssignment = async () => {
if (!canDeleteAssignment) return
if (!confirm("Are you sure you want to delete this assignment?")) return;
axios
@@ -89,18 +89,19 @@ export default function AssignmentView({user, users, entities, groups, assignmen
};
const startAssignment = () => {
if (assignment) {
axios
.post(`/api/assignments/${assignment.id}/start`)
.then(() => {
toast.success(`The assignment "${assignment.name}" has been started successfully!`);
router.replace(router.asPath);
})
.catch((e) => {
console.log(e);
toast.error("Something went wrong, please try again later!");
});
}
if (!canStartAssignment) return
if (!confirm("Are you sure you want to start this assignment?")) return;
axios
.post(`/api/assignments/${assignment.id}/start`)
.then(() => {
toast.success(`The assignment "${assignment.name}" has been started successfully!`);
router.replace(router.asPath);
})
.catch((e) => {
console.log(e);
toast.error("Something went wrong, please try again later!");
});
};
const formatTimestamp = (timestamp: string) => {