first commit
This commit is contained in:
87
app/api/v1/admin/users/[id]/api-keys/[keyId]/route.ts
Normal file
87
app/api/v1/admin/users/[id]/api-keys/[keyId]/route.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { db } from "@/db";
|
||||
import { apiKeys } from "@/db/schema";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { authenticateWebOrAPIRequest } from "@/app/lib/api-auth";
|
||||
import { isAdmin } from "@/app/lib/permissions";
|
||||
import { maskApiKey } from "@/app/lib/jwt";
|
||||
import {
|
||||
expiresAtFromDays,
|
||||
getDaysRemaining,
|
||||
getExpiryRemainingLabel,
|
||||
parseExpiresInDaysOptional,
|
||||
} from "@/app/lib/api-key-utils";
|
||||
|
||||
/**
|
||||
* PATCH /api/v1/admin/users/[id]/api-keys/[keyId]
|
||||
*
|
||||
* Body: { "expiresInDays": number | null }
|
||||
* — null veya 0: süresiz; 1–3650: bugünden itibaren o kadar gün sonra sona erer
|
||||
*/
|
||||
export async function PATCH(
|
||||
request: NextRequest,
|
||||
context: { params: Promise<{ id: string; keyId: string }> }
|
||||
) {
|
||||
const auth = await authenticateWebOrAPIRequest(request);
|
||||
if (!auth.authenticated) {
|
||||
return NextResponse.json({ error: auth.error ?? "Yetkisiz" }, { status: 401 });
|
||||
}
|
||||
if (!isAdmin(auth.role!)) {
|
||||
return NextResponse.json(
|
||||
{ error: "Bu işlem için admin yetkisi gerekir." },
|
||||
{ status: 403 }
|
||||
);
|
||||
}
|
||||
|
||||
const { id: userId, keyId } = await context.params;
|
||||
|
||||
let body: { expiresInDays?: unknown };
|
||||
try {
|
||||
body = await request.json();
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Geçersiz JSON" }, { status: 400 });
|
||||
}
|
||||
|
||||
const parsed = parseExpiresInDaysOptional(body.expiresInDays);
|
||||
if (!parsed.ok) {
|
||||
return NextResponse.json({ error: parsed.error }, { status: 400 });
|
||||
}
|
||||
|
||||
const expiresAt =
|
||||
parsed.value === null ? null : expiresAtFromDays(parsed.value);
|
||||
|
||||
const updated = await db
|
||||
.update(apiKeys)
|
||||
.set({ expiresAt, updatedAt: new Date() })
|
||||
.where(and(eq(apiKeys.id, keyId), eq(apiKeys.userId, userId)))
|
||||
.returning({
|
||||
id: apiKeys.id,
|
||||
name: apiKeys.name,
|
||||
key: apiKeys.key,
|
||||
expiresAt: apiKeys.expiresAt,
|
||||
isActive: apiKeys.isActive,
|
||||
});
|
||||
|
||||
if (updated.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: "Anahtar bulunamadı veya bu kullanıcıya ait değil." },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
const r = updated[0];
|
||||
const exp = r.expiresAt ?? null;
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: "API anahtarı süresi güncellendi.",
|
||||
data: {
|
||||
id: r.id,
|
||||
name: r.name,
|
||||
keyPreview: maskApiKey(r.key),
|
||||
expiresAt: exp?.toISOString() ?? null,
|
||||
daysRemaining: getDaysRemaining(exp),
|
||||
remainingLabel: getExpiryRemainingLabel(exp),
|
||||
isActive: r.isActive,
|
||||
},
|
||||
});
|
||||
}
|
||||
63
app/api/v1/admin/users/[id]/api-keys/route.ts
Normal file
63
app/api/v1/admin/users/[id]/api-keys/route.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { db } from "@/db";
|
||||
import { apiKeys } from "@/db/schema";
|
||||
import { eq, desc } from "drizzle-orm";
|
||||
import { authenticateWebOrAPIRequest } from "@/app/lib/api-auth";
|
||||
import { isAdmin } from "@/app/lib/permissions";
|
||||
import { maskApiKey } from "@/app/lib/jwt";
|
||||
import { getDaysRemaining, getExpiryRemainingLabel } from "@/app/lib/api-key-utils";
|
||||
|
||||
/**
|
||||
* GET /api/v1/admin/users/[id]/api-keys — Admin: seçilen kullanıcının API anahtarları
|
||||
*/
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
context: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
const auth = await authenticateWebOrAPIRequest(request);
|
||||
if (!auth.authenticated) {
|
||||
return NextResponse.json({ error: auth.error ?? "Yetkisiz" }, { status: 401 });
|
||||
}
|
||||
if (!isAdmin(auth.role!)) {
|
||||
return NextResponse.json(
|
||||
{ error: "Bu işlem için admin yetkisi gerekir." },
|
||||
{ status: 403 }
|
||||
);
|
||||
}
|
||||
|
||||
const { id: userId } = await context.params;
|
||||
|
||||
const rows = await db
|
||||
.select({
|
||||
id: apiKeys.id,
|
||||
name: apiKeys.name,
|
||||
key: apiKeys.key,
|
||||
expiresAt: apiKeys.expiresAt,
|
||||
lastUsedAt: apiKeys.lastUsedAt,
|
||||
isActive: apiKeys.isActive,
|
||||
createdAt: apiKeys.createdAt,
|
||||
})
|
||||
.from(apiKeys)
|
||||
.where(eq(apiKeys.userId, userId))
|
||||
.orderBy(desc(apiKeys.createdAt));
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: {
|
||||
keys: rows.map((r) => {
|
||||
const exp = r.expiresAt ?? null;
|
||||
return {
|
||||
id: r.id,
|
||||
name: r.name,
|
||||
keyPreview: maskApiKey(r.key),
|
||||
expiresAt: exp?.toISOString() ?? null,
|
||||
daysRemaining: getDaysRemaining(exp),
|
||||
remainingLabel: getExpiryRemainingLabel(exp),
|
||||
lastUsedAt: r.lastUsedAt?.toISOString() ?? null,
|
||||
isActive: r.isActive,
|
||||
createdAt: r.createdAt.toISOString(),
|
||||
};
|
||||
}),
|
||||
},
|
||||
});
|
||||
}
|
||||
67
app/api/v1/admin/users/[id]/role/route.ts
Normal file
67
app/api/v1/admin/users/[id]/role/route.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
72
app/api/v1/admin/users/[id]/route.ts
Normal file
72
app/api/v1/admin/users/[id]/route.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { authenticateAPIRequest } from "@/app/lib/api-auth";
|
||||
import { hasPermission, PERMISSIONS } from "@/app/lib/permissions";
|
||||
import { db } from "@/db";
|
||||
import { user, images, apiKeys } from "@/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
/**
|
||||
* DELETE /api/v1/admin/users/[id]
|
||||
* Kullanıcıyı sil (Sadece admin)
|
||||
* Kullanıcının tüm resimleri ve API anahtarları da silinir
|
||||
*/
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
const auth = await authenticateAPIRequest(request);
|
||||
|
||||
if (!auth.authenticated) {
|
||||
return NextResponse.json({ error: auth.error }, { status: 401 });
|
||||
}
|
||||
|
||||
// Permission kontrolü
|
||||
if (!hasPermission(auth.role!, PERMISSIONS.USER_DELETE)) {
|
||||
return NextResponse.json(
|
||||
{ error: "Bu işlem için yetkiniz yok. Sadece adminler kullanıcı silebilir." },
|
||||
{ status: 403 }
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const { id: userId } = await params;
|
||||
|
||||
// Kendi hesabını silmeyi engelle
|
||||
if (userId === auth.userId) {
|
||||
return NextResponse.json(
|
||||
{ error: "Kendi hesabınızı silemezsiniz" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Kullanıcının var olup olmadığını kontrol et
|
||||
const targetUser = await db.select().from(user).where(eq(user.id, userId)).limit(1);
|
||||
if (targetUser.length === 0) {
|
||||
return NextResponse.json({ error: "Kullanıcı bulunamadı" }, { status: 404 });
|
||||
}
|
||||
|
||||
// Kullanıcının resimlerini sil
|
||||
const deletedImages = await db.delete(images).where(eq(images.userId, userId));
|
||||
|
||||
// Kullanıcının API anahtarlarını sil
|
||||
await db.delete(apiKeys).where(eq(apiKeys.userId, userId));
|
||||
|
||||
// Kullanıcıyı sil
|
||||
await db.delete(user).where(eq(user.id, userId));
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: "Kullanıcı başarıyla silindi",
|
||||
data: {
|
||||
deletedUserId: userId,
|
||||
deletedUser: targetUser[0].email,
|
||||
},
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error("Kullanıcı silme hatası:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Kullanıcı silinemedi" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
69
app/api/v1/admin/users/[id]/verification/route.ts
Normal file
69
app/api/v1/admin/users/[id]/verification/route.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { authenticateAPIRequest } from "@/app/lib/api-auth";
|
||||
import { isAdmin } from "@/app/lib/permissions";
|
||||
import { db } from "@/db";
|
||||
import { user } from "@/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
/**
|
||||
* PATCH /api/v1/admin/users/[id]/verification
|
||||
* Kullanıcının email doğrulamasını değiştir (Sadece admin - JWT)
|
||||
*/
|
||||
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 doğrulama değiştirebilir." },
|
||||
{ status: 403 }
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const { id: userId } = await params;
|
||||
const body = await request.json();
|
||||
const { emailVerified } = body;
|
||||
|
||||
// Boolean validasyonu
|
||||
if (typeof emailVerified !== "boolean") {
|
||||
return NextResponse.json(
|
||||
{ error: "emailVerified boolean olmalıdır" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Email doğrulama durumunu güncelle
|
||||
const result = await db
|
||||
.update(user)
|
||||
.set({ emailVerified })
|
||||
.where(eq(user.id, userId))
|
||||
.returning();
|
||||
|
||||
if (result.length === 0) {
|
||||
return NextResponse.json({ error: "Kullanıcı bulunamadı" }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: `Email doğrulama ${emailVerified ? "aktif edildi" : "pasif edildi"}`,
|
||||
data: {
|
||||
userId,
|
||||
emailVerified,
|
||||
},
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error("Email doğrulama güncelleme hatası:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Email doğrulama güncellenemedi" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
54
app/api/v1/admin/users/route.ts
Normal file
54
app/api/v1/admin/users/route.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { authenticateAPIRequest } from "@/app/lib/api-auth";
|
||||
import { isAdmin } from "@/app/lib/permissions";
|
||||
import { db } from "@/db";
|
||||
import { user } from "@/db/schema";
|
||||
import { desc } from "drizzle-orm";
|
||||
|
||||
/**
|
||||
* GET /api/v1/admin/users
|
||||
* Tüm kullanıcıları listele (Sadece admin)
|
||||
*/
|
||||
export async function GET(request: NextRequest) {
|
||||
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 kullanıcıları görüntüleyebilir." },
|
||||
{ status: 403 }
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const users = await db
|
||||
.select({
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
role: user.role,
|
||||
emailVerified: user.emailVerified,
|
||||
createdAt: user.createdAt,
|
||||
})
|
||||
.from(user)
|
||||
.orderBy(desc(user.createdAt));
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: {
|
||||
users,
|
||||
total: users.length,
|
||||
},
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error("Kullanıcı listesi hatası:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Kullanıcılar yüklenemedi" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user