Continued creating the permission system
This commit is contained in:
@@ -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) => {
|
||||
|
||||
@@ -19,7 +19,7 @@ import { requestUser } from "@/utils/api";
|
||||
import {getAssignment} from "@/utils/assignments.be";
|
||||
import {getEntitiesWithRoles} from "@/utils/entities.be";
|
||||
import {getGroups, getGroupsByEntities} from "@/utils/groups.be";
|
||||
import {checkAccess} from "@/utils/permissions";
|
||||
import {checkAccess, doesEntityAllow, findAllowedEntities} from "@/utils/permissions";
|
||||
import {calculateAverageLevel} from "@/utils/score";
|
||||
import {getEntitiesUsers, getUsers} from "@/utils/users.be";
|
||||
import axios from "axios";
|
||||
@@ -40,42 +40,26 @@ export const getServerSideProps = withIronSessionSsr(async ({req, res, params})
|
||||
const user = await requestUser(req, res)
|
||||
if (!user) return redirect("/login")
|
||||
|
||||
if (!user) {
|
||||
return {
|
||||
redirect: {
|
||||
destination: "/login",
|
||||
permanent: false,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (!checkAccess(user, ["admin", "developer", "corporate", "teacher", "mastercorporate"]))
|
||||
return {
|
||||
redirect: {
|
||||
destination: "/dashboard",
|
||||
permanent: false,
|
||||
},
|
||||
};
|
||||
|
||||
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 = entities.find((e) => assignment.entity === assignment.entity)
|
||||
|
||||
return {props: serialize({user, users, entities, assignment, groups})};
|
||||
if (!entity) return redirect("/assignments")
|
||||
if (!doesEntityAllow(user, entity, 'edit_assignment')) return redirect("/assignments")
|
||||
|
||||
const allowedEntities = findAllowedEntities(user, entities, 'edit_assignment')
|
||||
|
||||
const users = await (checkAccess(user, ["developer", "admin"]) ? getUsers() : getEntitiesUsers(mapBy(allowedEntities, 'id')));
|
||||
const groups = await (checkAccess(user, ["developer", "admin"]) ? getGroups() : getGroupsByEntities(mapBy(allowedEntities, 'id')));
|
||||
|
||||
return {props: serialize({user, users, entities: allowedEntities, assignment, groups})};
|
||||
}, sessionOptions);
|
||||
|
||||
interface Props {
|
||||
|
||||
@@ -18,7 +18,7 @@ import {mapBy, redirect, serialize} from "@/utils";
|
||||
import { requestUser } from "@/utils/api";
|
||||
import {getEntitiesWithRoles} from "@/utils/entities.be";
|
||||
import {getGroups, getGroupsByEntities} from "@/utils/groups.be";
|
||||
import {checkAccess} from "@/utils/permissions";
|
||||
import {checkAccess, findAllowedEntities} from "@/utils/permissions";
|
||||
import {calculateAverageLevel} from "@/utils/score";
|
||||
import {getEntitiesUsers, getUsers} from "@/utils/users.be";
|
||||
import axios from "axios";
|
||||
@@ -39,14 +39,14 @@ export const getServerSideProps = withIronSessionSsr(async ({req, res}) => {
|
||||
const user = await requestUser(req, res)
|
||||
if (!user) return redirect("/login")
|
||||
|
||||
if (!checkAccess(user, ["admin", "developer", "corporate", "teacher", "mastercorporate"]))
|
||||
return redirect("/")
|
||||
|
||||
const entityIDS = mapBy(user.entities, "id") || [];
|
||||
|
||||
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 allowedEntities = findAllowedEntities(user, entities, 'create_assignment')
|
||||
if (allowedEntities.length === 0) return redirect("/assignments")
|
||||
|
||||
const users = await (checkAccess(user, ["developer", "admin"]) ? getUsers() : getEntitiesUsers(mapBy(allowedEntities, 'id')));
|
||||
const groups = await (checkAccess(user, ["developer", "admin"]) ? getGroups() : getGroupsByEntities(mapBy(allowedEntities, 'id')));
|
||||
|
||||
return {props: serialize({user, users, entities, groups})};
|
||||
}, sessionOptions);
|
||||
@@ -535,6 +535,7 @@ export default function AssignmentsPage({user, users, groups, entities}: Props)
|
||||
!name ||
|
||||
!startDate ||
|
||||
!endDate ||
|
||||
!entity ||
|
||||
assignees.length === 0 ||
|
||||
(!useRandomExams && examIDs.length < selectedModules.length)
|
||||
}
|
||||
|
||||
@@ -2,8 +2,10 @@ import Layout from "@/components/High/Layout";
|
||||
import Separator from "@/components/Low/Separator";
|
||||
import AssignmentCard from "@/dashboards/AssignmentCard";
|
||||
import AssignmentView from "@/dashboards/AssignmentView";
|
||||
import { useAllowedEntities } from "@/hooks/useEntityPermissions";
|
||||
import {useListSearch} from "@/hooks/useListSearch";
|
||||
import usePagination from "@/hooks/usePagination";
|
||||
import { EntityWithRoles } from "@/interfaces/entity";
|
||||
import {Assignment} from "@/interfaces/results";
|
||||
import {CorporateUser, Group, User} from "@/interfaces/user";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
@@ -20,7 +22,7 @@ import {
|
||||
import {getAssignments, getEntitiesAssignments} from "@/utils/assignments.be";
|
||||
import {getEntitiesWithRoles} from "@/utils/entities.be";
|
||||
import {getGroups, getGroupsByEntities} from "@/utils/groups.be";
|
||||
import {checkAccess} from "@/utils/permissions";
|
||||
import {checkAccess, findAllowedEntities} from "@/utils/permissions";
|
||||
import {getEntitiesUsers, getUsers} from "@/utils/users.be";
|
||||
import {withIronSessionSsr} from "iron-session/next";
|
||||
import {groupBy} from "lodash";
|
||||
@@ -38,11 +40,18 @@ export const getServerSideProps = withIronSessionSsr(async ({req, res}) => {
|
||||
return redirect("/")
|
||||
|
||||
const entityIDS = mapBy(user.entities, "id") || [];
|
||||
|
||||
const users = await (checkAccess(user, ["developer", "admin"]) ? getUsers() : getEntitiesUsers(entityIDS));
|
||||
const entities = await (checkAccess(user, ["developer", "admin"]) ? getEntitiesWithRoles() : getEntitiesWithRoles(entityIDS));
|
||||
const assignments = await (checkAccess(user, ["developer", "admin"]) ? getAssignments() : getEntitiesAssignments(entityIDS));
|
||||
const groups = await (checkAccess(user, ["developer", "admin"]) ? getGroups() : getGroupsByEntities(entityIDS));
|
||||
|
||||
const allowedEntities = findAllowedEntities(user, entities, "view_assignments")
|
||||
|
||||
const users =
|
||||
await (checkAccess(user, ["developer", "admin"]) ? getUsers() : getEntitiesUsers(mapBy(allowedEntities, 'id')));
|
||||
|
||||
const assignments =
|
||||
await (checkAccess(user, ["developer", "admin"]) ? getAssignments() : getEntitiesAssignments(mapBy(allowedEntities, 'id')));
|
||||
|
||||
const groups =
|
||||
await (checkAccess(user, ["developer", "admin"]) ? getGroups() : getGroupsByEntities(mapBy(allowedEntities, 'id')));
|
||||
|
||||
return {props: serialize({user, users, entities, assignments, groups})};
|
||||
}, sessionOptions);
|
||||
@@ -52,12 +61,16 @@ const SEARCH_FIELDS = [["name"]];
|
||||
interface Props {
|
||||
assignments: Assignment[];
|
||||
corporateAssignments?: ({corporate?: CorporateUser} & Assignment)[];
|
||||
entities: EntityWithRoles[]
|
||||
groups: Group[];
|
||||
user: User;
|
||||
users: User[];
|
||||
}
|
||||
|
||||
export default function AssignmentsPage({assignments, corporateAssignments, user, users, groups}: Props) {
|
||||
export default function AssignmentsPage({assignments, corporateAssignments, entities, user, users, groups}: Props) {
|
||||
const entitiesAllowCreate = useAllowedEntities(user, entities, 'create_assignment')
|
||||
const entitiesAllowEdit = useAllowedEntities(user, entities, 'edit_assignment')
|
||||
|
||||
const activeAssignments = useMemo(() => assignments.filter(activeAssignmentFilter), [assignments]);
|
||||
const plannedAssignments = useMemo(() => assignments.filter(futureAssignmentFilter), [assignments]);
|
||||
const pastAssignments = useMemo(() => assignments.filter(pastAssignmentFilter), [assignments]);
|
||||
@@ -139,13 +152,22 @@ export default function AssignmentsPage({assignments, corporateAssignments, user
|
||||
</div>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<Link
|
||||
href="/assignments/creator"
|
||||
href={entitiesAllowCreate.length > 0 ? "/assignments/creator" : ""}
|
||||
className="w-[250px] h-[200px] flex flex-col gap-2 items-center justify-center bg-white hover:bg-mti-purple-ultralight text-mti-purple-light hover:text-mti-purple-dark border border-mti-gray-platinum hover:drop-shadow p-4 cursor-pointer rounded-xl transition ease-in-out duration-300">
|
||||
<BsPlus className="text-6xl" />
|
||||
<span className="text-lg">New Assignment</span>
|
||||
</Link>
|
||||
{plannedItems.map((a) => (
|
||||
<AssignmentCard {...a} users={users} onClick={() => router.push(`/assignments/creator/${a.id}`)} key={a.id} />
|
||||
<AssignmentCard
|
||||
{...a}
|
||||
users={users}
|
||||
onClick={
|
||||
entitiesAllowEdit.length > 0
|
||||
? () => router.push(`/assignments/creator/${a.id}`)
|
||||
: undefined
|
||||
}
|
||||
key={a.id}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</section>
|
||||
|
||||
Reference in New Issue
Block a user