Skip to content

Commit

Permalink
🔨 minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
casperiv0 committed Oct 28, 2021
1 parent 438ee96 commit 5163b43
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/api/src/controllers/admin/manage/Units.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { NotFound } from "@tsed/exceptions";
import { UseBeforeEach } from "@tsed/platform-middlewares";
import { Get, JsonRequestBody, Put } from "@tsed/schema";
import { prisma } from "../../../lib/prisma";
import { IsAuth, IsAdmin } from "../../../middlewares";
import { IsAuth, IsSupervisor } from "../../../middlewares";

const include = {
rank: true,
Expand All @@ -28,7 +28,7 @@ const include = {
},
};

@UseBeforeEach(IsAuth, IsAdmin)
@UseBeforeEach(IsAuth, IsSupervisor)
@Controller("/units")
export class ManageUnitsController {
@Get("/")
Expand Down
34 changes: 33 additions & 1 deletion packages/api/src/middlewares/Permissions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { User } from ".prisma/client";
import { Middleware, MiddlewareMethods, Req } from "@tsed/common";
import { Forbidden } from "@tsed/exceptions";
import { getSessionUser } from "../lib/auth";
Expand All @@ -11,9 +12,24 @@ export class IsAdmin implements MiddlewareMethods {
throw new Forbidden("Invalid Permissions");
}

if (!["OWNER", "ADMIN"].includes(user.rank)) {
await admin(user);
}
}

@Middleware()
export class IsSupervisor implements MiddlewareMethods {
async use(@Req() req: Req) {
const user = await getSessionUser(req);

if (!user) {
throw new Forbidden("Invalid Permissions");
}

const isSupervisor = await supervisor(user);

if (!isSupervisor) {
await admin(user);
}
}
}

Expand Down Expand Up @@ -46,3 +62,19 @@ export class IsDispatch implements MiddlewareMethods {
}
}
}

async function admin(user: Pick<User, "rank">) {
if (!["OWNER", "ADMIN"].includes(user.rank)) {
throw new Forbidden("Invalid Permissions");
}

return true;
}

async function supervisor(user: Pick<User, "isSupervisor">) {
if (!user.isSupervisor) {
throw new Forbidden("Invalid Permissions");
}

return true;
}
18 changes: 18 additions & 0 deletions packages/client/src/components/nav-dropdowns/OfficerDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { Menu, Transition } from "@headlessui/react";
import { useRouter } from "next/router";
import { ChevronDown } from "react-bootstrap-icons";
import Link from "next/link";
import { useAuth } from "context/AuthContext";

export const OfficerDropdown = () => {
const router = useRouter();
const isActive = (route: string) => router.pathname.startsWith(route);
const { user } = useAuth();

const items = ["My Officers", "My Officer Logs"];

Expand Down Expand Up @@ -68,6 +70,22 @@ export const OfficerDropdown = () => {
</Menu.Item>
);
})}

{user?.isSupervisor ? (
<Menu.Item>
{({ active }) => (
<Link href="/admin/manage/units">
<a
className={`${
active ? "bg-gray-200" : "text-gray-900"
} block hover:bg-gray-200 group rounded-md items-center w-full px-3 py-1.5 text-sm transition-all`}
>
Manage Units
</a>
</Link>
)}
</Menu.Item>
) : null}
</div>
</Menu.Items>
</Transition>
Expand Down
9 changes: 9 additions & 0 deletions packages/client/src/pages/403.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Layout } from "components/Layout";

export default function FourOhFour() {
return (
<Layout>
<h1 className="text-2xl font-semibold">Forbidden.</h1>
</Layout>
);
}
9 changes: 9 additions & 0 deletions packages/client/src/pages/404.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Layout } from "components/Layout";

export default function FourOhFour() {
return (
<Layout>
<h1 className="text-2xl font-semibold">Page not found.</h1>
</Layout>
);
}

0 comments on commit 5163b43

Please sign in to comment.