added require-admin checks
This commit is contained in:
@@ -4,8 +4,14 @@ import db from '@/lib/db';
|
|||||||
import { dataset } from '@/lib/model/dataset';
|
import { dataset } from '@/lib/model/dataset';
|
||||||
import { eq } from 'drizzle-orm';
|
import { eq } from 'drizzle-orm';
|
||||||
import { revalidatePath } from 'next/cache';
|
import { revalidatePath } from 'next/cache';
|
||||||
|
import { requireAdmin } from '@/lib/auth';
|
||||||
|
|
||||||
export async function createDataset({ name }: { name: string }) {
|
export async function createDataset({ name }: { name: string }) {
|
||||||
|
const result = await requireAdmin();
|
||||||
|
if (!result.authenticated || !result.admin) {
|
||||||
|
throw new Error('Unauthorized');
|
||||||
|
}
|
||||||
|
|
||||||
if (!name || !name.trim()) {
|
if (!name || !name.trim()) {
|
||||||
throw new Error('Dataset name is required');
|
throw new Error('Dataset name is required');
|
||||||
}
|
}
|
||||||
@@ -24,6 +30,11 @@ export async function updateDataset(
|
|||||||
id: number,
|
id: number,
|
||||||
updates: { name?: string }
|
updates: { name?: string }
|
||||||
) {
|
) {
|
||||||
|
const result = await requireAdmin();
|
||||||
|
if (!result.authenticated || !result.admin) {
|
||||||
|
throw new Error('Unauthorized');
|
||||||
|
}
|
||||||
|
|
||||||
if (updates.name !== undefined && !updates.name.trim()) {
|
if (updates.name !== undefined && !updates.name.trim()) {
|
||||||
throw new Error('Dataset name cannot be empty');
|
throw new Error('Dataset name cannot be empty');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import { join } from 'path';
|
|||||||
import { existsSync } from 'fs';
|
import { existsSync } from 'fs';
|
||||||
import extract from 'extract-zip';
|
import extract from 'extract-zip';
|
||||||
import { line } from 'drizzle-orm/pg-core';
|
import { line } from 'drizzle-orm/pg-core';
|
||||||
|
import { requireAdmin } from '@/lib/auth';
|
||||||
|
|
||||||
interface DatasetEntryInput {
|
interface DatasetEntryInput {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -24,6 +25,11 @@ export async function uploadDatasetEntries(
|
|||||||
datasetId: number,
|
datasetId: number,
|
||||||
formData: FormData
|
formData: FormData
|
||||||
) {
|
) {
|
||||||
|
const result = await requireAdmin();
|
||||||
|
if (!result.authenticated || !result.admin) {
|
||||||
|
throw new Error('Unauthorized');
|
||||||
|
}
|
||||||
|
|
||||||
const file = formData.get('file') as File;
|
const file = formData.get('file') as File;
|
||||||
|
|
||||||
if (!file) {
|
if (!file) {
|
||||||
|
|||||||
Reference in New Issue
Block a user