Skip to content

Commit

Permalink
fix(dash/src): 🔥 More fine grained permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Nudelsuppe42 committed Jan 11, 2025
1 parent 2c0b443 commit 4c34cd4
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 15 deletions.
2 changes: 1 addition & 1 deletion apps/dashboard/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export default async function Page() {
/>
</>
)}
{(websiteData?._count?.permissions ?? 0) > 0 && (
{session?.user?.realm_access?.roles.includes('bte_staff') && (
<>
<Title order={2} mt="xl">
Global Administration
Expand Down
9 changes: 6 additions & 3 deletions apps/dashboard/src/components/ProtectionProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

import { redirect, usePathname } from 'next/navigation';

import { navLinks } from '@/util/links';
import { useSession } from 'next-auth/react';

export default function ProtectionProvider({ children }: { children: React.ReactNode }) {
const pathname = usePathname();
const session = useSession();
const isProtected = pathname.startsWith('/am/');
const isStaff = session.data?.user.realm_access.roles.includes('bte_staff');
const requiredRole = navLinks.find((link) => link.link === pathname.split('/').slice(0, 3).join('/'))?.protected;

if (isProtected && !isStaff) {
if (
requiredRole &&
!session?.data?.user?.realm_access?.roles.includes(typeof requiredRole === 'boolean' ? 'bte_staff' : requiredRole)
) {
redirect('/');
}

Expand Down
3 changes: 1 addition & 2 deletions apps/dashboard/src/components/layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export interface LayoutProps {
*/
export default async function AppLayout(props: LayoutProps) {
const session = await getSession();
const isStaff = session?.user.realm_access.roles.includes('bte_staff');

return (
<AppShell
Expand All @@ -25,7 +24,7 @@ export default async function AppLayout(props: LayoutProps) {
header={{ height: 60 }}
padding="md"
>
<Navbar displayProtected={isStaff} />
<Navbar roles={session?.user.realm_access.roles || []} />

<Header />

Expand Down
7 changes: 5 additions & 2 deletions apps/dashboard/src/components/layout/navbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import { navLinks } from '@/util/links';
import NavLink from './NavLink';

export interface Navbar {
displayProtected?: boolean;
roles: string[];
}

/**
* Main Navbar
*/
export default async function Navbar(props: Navbar) {
const allowedLinks = navLinks.filter((link) => props.displayProtected || !link.protected);
const allowedLinks = navLinks.filter(
(link) =>
!link.protected || props.roles.includes(typeof link.protected === 'boolean' ? 'bte_staff' : link.protected),
);

const links = allowedLinks.map((item) =>
item.divider ? (
Expand Down
14 changes: 7 additions & 7 deletions apps/dashboard/src/util/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,37 +68,37 @@ export const meNavLinks: NavLink[] = [
{
link: '/am/users',
label: 'Website Users',
protected: true,
protected: 'bte_staff_website',
icon: 'UsersGroup',
},
{
link: '/am/teams',
label: 'BuildTeams',
protected: true,
protected: 'bte_staff_website',
icon: 'UsersGroup',
},
{
link: '/am/faq',
label: 'FAQ',
protected: true,
protected: 'bte_staff_website',
icon: 'Bubble',
},
{
link: '/am/claims',
label: 'Map Claims',
protected: true,
protected: 'bte_staff_website',
icon: 'Polygon',
},
{
link: '/am/applications',
label: 'Team Applications',
protected: true,
protected: 'bte_staff_website',
icon: 'Forms',
},
{
link: '/am/sso',
label: 'SSO Configuration and Security',
protected: true,
protected: 'bte_staff_admin',
icon: 'Settings',
},
];
Expand All @@ -118,7 +118,7 @@ type NavLink = {
link: string;
label: string;
icon: any;
protected?: boolean;
protected?: boolean | 'bte_staff_admin' | 'bte_staff_discord' | 'bte_staff_website' | 'bte_staff';
divider?: boolean;
};
interface NavLinkGroup {
Expand Down

0 comments on commit 4c34cd4

Please sign in to comment.