feat: add admin role management with user authentication checks and update sidebar for admin access

This commit is contained in:
Julien Froidefond
2025-10-16 22:39:04 +02:00
parent 83f523c11a
commit 9899789fce
25 changed files with 1636 additions and 6 deletions

View File

@@ -0,0 +1,28 @@
import { NextResponse } from "next/server";
import { AdminService } from "@/lib/services/admin.service";
import { AppError } from "@/utils/errors";
export async function GET() {
try {
const stats = await AdminService.getUserStats();
return NextResponse.json(stats);
} catch (error) {
console.error("Erreur lors de la récupération des stats:", error);
if (error instanceof AppError) {
return NextResponse.json(
{ error: error.message, code: error.code },
{
status: error.code === "AUTH_FORBIDDEN" ? 403 :
error.code === "AUTH_UNAUTHENTICATED" ? 401 : 500
}
);
}
return NextResponse.json(
{ error: "Erreur lors de la récupération des stats" },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,56 @@
import { NextRequest, NextResponse } from "next/server";
import { AdminService } from "@/lib/services/admin.service";
import { AppError } from "@/utils/errors";
import { AuthServerService } from "@/lib/services/auth-server.service";
export async function PUT(
request: NextRequest,
{ params }: { params: Promise<{ userId: string }> }
) {
try {
const { userId } = await params;
const body = await request.json();
const { newPassword } = body;
if (!newPassword) {
return NextResponse.json(
{ error: "Nouveau mot de passe manquant" },
{ status: 400 }
);
}
// Vérifier que le mot de passe est fort
if (!AuthServerService.isPasswordStrong(newPassword)) {
return NextResponse.json(
{
error: "Le mot de passe doit contenir au moins 8 caractères, une majuscule et un chiffre"
},
{ status: 400 }
);
}
await AdminService.resetUserPassword(userId, newPassword);
return NextResponse.json({ success: true });
} catch (error) {
console.error("Erreur lors de la réinitialisation du mot de passe:", error);
if (error instanceof AppError) {
return NextResponse.json(
{ error: error.message, code: error.code },
{
status: error.code === "AUTH_FORBIDDEN" ? 403 :
error.code === "AUTH_UNAUTHENTICATED" ? 401 :
error.code === "AUTH_USER_NOT_FOUND" ? 404 :
error.code === "ADMIN_CANNOT_RESET_OWN_PASSWORD" ? 400 : 500
}
);
}
return NextResponse.json(
{ error: "Erreur lors de la réinitialisation du mot de passe" },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,75 @@
import { NextRequest, NextResponse } from "next/server";
import { AdminService } from "@/lib/services/admin.service";
import { AppError } from "@/utils/errors";
export async function PATCH(
request: NextRequest,
{ params }: { params: Promise<{ userId: string }> }
) {
try {
const { userId } = await params;
const body = await request.json();
const { roles } = body;
if (!roles || !Array.isArray(roles)) {
return NextResponse.json(
{ error: "Rôles invalides" },
{ status: 400 }
);
}
await AdminService.updateUserRoles(userId, roles);
return NextResponse.json({ success: true });
} catch (error) {
console.error("Erreur lors de la mise à jour de l'utilisateur:", error);
if (error instanceof AppError) {
return NextResponse.json(
{ error: error.message, code: error.code },
{
status: error.code === "AUTH_FORBIDDEN" ? 403 :
error.code === "AUTH_UNAUTHENTICATED" ? 401 :
error.code === "AUTH_USER_NOT_FOUND" ? 404 : 500
}
);
}
return NextResponse.json(
{ error: "Erreur lors de la mise à jour de l'utilisateur" },
{ status: 500 }
);
}
}
export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ userId: string }> }
) {
try {
const { userId } = await params;
await AdminService.deleteUser(userId);
return NextResponse.json({ success: true });
} catch (error) {
console.error("Erreur lors de la suppression de l'utilisateur:", error);
if (error instanceof AppError) {
return NextResponse.json(
{ error: error.message, code: error.code },
{
status: error.code === "AUTH_FORBIDDEN" ? 403 :
error.code === "AUTH_UNAUTHENTICATED" ? 401 :
error.code === "AUTH_USER_NOT_FOUND" ? 404 :
error.code === "ADMIN_CANNOT_DELETE_SELF" ? 400 : 500
}
);
}
return NextResponse.json(
{ error: "Erreur lors de la suppression de l'utilisateur" },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,27 @@
import { NextResponse } from "next/server";
import { AdminService } from "@/lib/services/admin.service";
import { AppError } from "@/utils/errors";
export async function GET() {
try {
const users = await AdminService.getAllUsers();
return NextResponse.json(users);
} catch (error) {
console.error("Erreur lors de la récupération des utilisateurs:", error);
if (error instanceof AppError) {
return NextResponse.json(
{ error: error.message, code: error.code },
{
status: error.code === "AUTH_FORBIDDEN" ? 403 :
error.code === "AUTH_UNAUTHENTICATED" ? 401 : 500
}
);
}
return NextResponse.json(
{ error: "Erreur lors de la récupération des utilisateurs" },
{ status: 500 }
);
}
}