Continued creating the permission system
This commit is contained in:
@@ -14,8 +14,12 @@ import {uniq} from "lodash";
|
||||
import {BsPlus} from "react-icons/bs";
|
||||
import CardList from "@/components/High/CardList";
|
||||
import Separator from "@/components/Low/Separator";
|
||||
import {mapBy, redirect} from "@/utils";
|
||||
import {mapBy, redirect, serialize} from "@/utils";
|
||||
import { requestUser } from "@/utils/api";
|
||||
import { findAllowedEntities } from "@/utils/permissions";
|
||||
import { getEntities, getEntitiesWithRoles } from "@/utils/entities.be";
|
||||
import { useAllowedEntities } from "@/hooks/useEntityPermissions";
|
||||
import { EntityWithRoles } from "@/interfaces/entity";
|
||||
|
||||
export const getServerSideProps = withIronSessionSsr(async ({req, res}) => {
|
||||
const user = await requestUser(req, res)
|
||||
@@ -24,13 +28,16 @@ export const getServerSideProps = withIronSessionSsr(async ({req, res}) => {
|
||||
if (shouldRedirectHome(user)) return redirect("/")
|
||||
|
||||
const entityIDS = mapBy(user.entities, "id");
|
||||
const groups = await getGroupsForEntities(entityIDS);
|
||||
const entities = await getEntitiesWithRoles(entityIDS)
|
||||
const allowedEntities = findAllowedEntities(user, entities, "view_classrooms")
|
||||
|
||||
const groups = await getGroupsForEntities(mapBy(allowedEntities, 'id'));
|
||||
|
||||
const users = await getSpecificUsers(uniq(groups.flatMap((g) => [...g.participants.slice(0, 5), g.admin])));
|
||||
const groupsWithUsers: GroupWithUsers[] = groups.map((g) => convertToUsers(g, users));
|
||||
|
||||
return {
|
||||
props: {user, groups: JSON.parse(JSON.stringify(groupsWithUsers))},
|
||||
props: serialize({user, groups: groupsWithUsers, entities: allowedEntities}),
|
||||
};
|
||||
}, sessionOptions);
|
||||
|
||||
@@ -47,8 +54,11 @@ const SEARCH_FIELDS = [
|
||||
interface Props {
|
||||
user: User;
|
||||
groups: GroupWithUsers[];
|
||||
entities: EntityWithRoles[]
|
||||
}
|
||||
export default function Home({user, groups}: Props) {
|
||||
export default function Home({user, groups, entities}: Props) {
|
||||
const entitiesAllowCreate = useAllowedEntities(user, entities, 'create_classroom')
|
||||
|
||||
const renderCard = (group: GroupWithUsers) => (
|
||||
<Link
|
||||
href={`/classrooms/${group.id}`}
|
||||
@@ -98,7 +108,12 @@ export default function Home({user, groups}: Props) {
|
||||
<Separator />
|
||||
</div>
|
||||
|
||||
<CardList<GroupWithUsers> list={groups} searchFields={SEARCH_FIELDS} renderCard={renderCard} firstCard={firstCard} />
|
||||
<CardList<GroupWithUsers>
|
||||
list={groups}
|
||||
searchFields={SEARCH_FIELDS}
|
||||
renderCard={renderCard}
|
||||
firstCard={entitiesAllowCreate.length === 0 ? undefined : firstCard}
|
||||
/>
|
||||
</section>
|
||||
</Layout>
|
||||
</>
|
||||
|
||||
Reference in New Issue
Block a user