refactor: use permission system instead of direct session checks in admin pages
This commit is contained in:
@@ -2,7 +2,7 @@ import { prisma } from "@/lib/prisma"
|
||||
import Navigation from "@/components/Navigation"
|
||||
import Link from "next/link"
|
||||
import { notFound, redirect } from "next/navigation"
|
||||
import { getSession } from "@/lib/auth-simple"
|
||||
import { canManageTournament, hasRole } from "@/lib/permissions"
|
||||
import { getTournamentStatus } from "@/lib/tournamentUtils"
|
||||
|
||||
interface PageProps {
|
||||
@@ -12,21 +12,16 @@ interface PageProps {
|
||||
}
|
||||
|
||||
export default async function TournamentDetailPage({ params }: PageProps) {
|
||||
const session = await getSession()
|
||||
|
||||
if (!session) {
|
||||
const tournamentId = parseInt(params.id)
|
||||
|
||||
// Check if user can manage this tournament
|
||||
const permission = await canManageTournament(tournamentId)
|
||||
if (!permission.allowed) {
|
||||
redirect("/auth/login")
|
||||
}
|
||||
|
||||
// Fetch user role from database
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: session.user.id },
|
||||
select: { role: true },
|
||||
});
|
||||
|
||||
const isAdmin = user?.role === "club_admin"
|
||||
|
||||
const tournamentId = parseInt(params.id)
|
||||
// Check if user can create tournaments (tournament_admin or club_admin)
|
||||
const canCreate = await hasRole('tournament_admin')
|
||||
|
||||
let tournament = await prisma.event.findUnique({
|
||||
where: { id: tournamentId },
|
||||
@@ -158,7 +153,7 @@ export default async function TournamentDetailPage({ params }: PageProps) {
|
||||
)}
|
||||
</div>
|
||||
<div className="flex space-x-2">
|
||||
{isAdmin && (
|
||||
{permission.allowed && (
|
||||
<>
|
||||
<Link
|
||||
href={`/admin/tournaments/${tournament.id}/edit`}
|
||||
|
||||
Reference in New Issue
Block a user