first commit
This commit is contained in:
70
app/api/admin/users/[id]/role/route.ts
Normal file
70
app/api/admin/users/[id]/role/route.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { auth } from "@/app/lib/auth";
|
||||
import { isAdmin, UserRole, updateUserRole } from "@/app/lib/permissions";
|
||||
|
||||
/**
|
||||
* PATCH /api/admin/users/[id]/role
|
||||
* Kullanıcının rolünü değiştir (Sadece admin - Web Session)
|
||||
*/
|
||||
export async function PATCH(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
const session = await auth.api.getSession({
|
||||
headers: request.headers,
|
||||
});
|
||||
|
||||
if (!session?.user) {
|
||||
return NextResponse.json({ error: "Giriş yapmalısınız" }, { status: 401 });
|
||||
}
|
||||
|
||||
// Admin kontrolü
|
||||
const userRole = (session.user as any).role || "user";
|
||||
if (!isAdmin(userRole)) {
|
||||
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 === session.user.id) {
|
||||
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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user