68 lines
1.8 KiB
TypeScript
68 lines
1.8 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
||
import { authenticateAPIRequest } from "@/app/lib/api-auth";
|
||
import { isAdmin, UserRole, updateUserRole } from "@/app/lib/permissions";
|
||
|
||
/**
|
||
* PATCH /api/v1/admin/users/[id]/role
|
||
* Kullanıcının rolünü değiştir (Sadece admin)
|
||
*/
|
||
export async function PATCH(
|
||
request: NextRequest,
|
||
{ params }: { params: Promise<{ id: string }> }
|
||
) {
|
||
const auth = await authenticateAPIRequest(request);
|
||
|
||
if (!auth.authenticated) {
|
||
return NextResponse.json({ error: auth.error }, { status: 401 });
|
||
}
|
||
|
||
// Admin kontrolü
|
||
if (!isAdmin(auth.role!)) {
|
||
return NextResponse.json(
|
||
{ error: "Bu işlem için yetkiniz yok. Sadece adminler rol değiştirebilir." },
|
||
{ status: 403 }
|
||
);
|
||
}
|
||
|
||
try {
|
||
const { id: userId } = await params;
|
||
const body = await request.json();
|
||
const { role } = body;
|
||
|
||
// Role validasyonu
|
||
const validRoles: UserRole[] = ["user", "admin", "moderator"];
|
||
if (!role || !validRoles.includes(role)) {
|
||
return NextResponse.json(
|
||
{ error: "Geçersiz rol. Geçerli roller: user, admin, moderator" },
|
||
{ status: 400 }
|
||
);
|
||
}
|
||
|
||
// Kendi rolünü değiştirmeyi engelle
|
||
if (userId === auth.userId) {
|
||
return NextResponse.json(
|
||
{ error: "Kendi rolünüzü değiştiremezsiniz" },
|
||
{ status: 400 }
|
||
);
|
||
}
|
||
|
||
// Rolü güncelle
|
||
await updateUserRole(userId, role);
|
||
|
||
return NextResponse.json({
|
||
success: true,
|
||
message: "Kullanıcı rolü başarıyla güncellendi",
|
||
data: {
|
||
userId,
|
||
newRole: role,
|
||
},
|
||
});
|
||
} catch (error: any) {
|
||
console.error("Rol güncelleme hatası:", error);
|
||
return NextResponse.json(
|
||
{ error: "Rol güncellenemedi" },
|
||
{ status: 500 }
|
||
);
|
||
}
|
||
}
|