Added comprehensive authentication and authorization system: Authentication System: - Better Auth integration with session management - Login/logout pages and API routes - Middleware for route protection - Auth utilities and client libraries User Management: - User list, detail, and invite pages - User API endpoints (CRUD operations) - Session management for users - Profile settings page Role-Based Access Control: - Role management pages (list, create, edit) - Permission system with granular controls - Role assignment to users - Role API endpoints Admin Features: - Audit log page for tracking system events - Admin settings page - Audit service for logging user actions Additional Features: - Quotes management pages and components - SalesBldr API integration - Email service for notifications Configuration & Documentation: - Updated docker-compose.yml - MCP server configuration (mcp.json) - CVE-2025-55182 security review documentation - Standards guide and PRD documents - Re-enabling authentication documentation Database Migrations: - 012: Auth tables (users, sessions, accounts, verifications) - 013: Role tables (roles, permissions, role_permissions, user_roles) - 014: Admin settings table UI Updates: - Updated dashboard layout - Enhanced app layout with auth integration
176 lines
4.9 KiB
TypeScript
176 lines
4.9 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
import { useForm } from "react-hook-form";
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
import { z } from "zod";
|
|
import { Loader2, Save } from "lucide-react";
|
|
import { toast } from "sonner";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Input } from "@/components/ui/input";
|
|
import { Textarea } from "@/components/ui/textarea";
|
|
import {
|
|
Form,
|
|
FormControl,
|
|
FormDescription,
|
|
FormField,
|
|
FormItem,
|
|
FormLabel,
|
|
FormMessage,
|
|
} from "@/components/ui/form";
|
|
import { PermissionPicker } from "./permission-picker";
|
|
|
|
const formSchema = z.object({
|
|
name: z.string().min(1, "Name is required").regex(/^[a-z0-9-]+$/, "Name must be lowercase with hyphens only"),
|
|
description: z.string().optional(),
|
|
permissions: z.record(z.string(), z.array(z.string())),
|
|
});
|
|
|
|
type FormData = z.infer<typeof formSchema>;
|
|
|
|
interface Role {
|
|
id: string;
|
|
name: string;
|
|
description: string | null;
|
|
permissions: Record<string, string[]>;
|
|
is_system: boolean;
|
|
}
|
|
|
|
interface RoleFormProps {
|
|
role?: Role;
|
|
}
|
|
|
|
export function RoleForm({ role }: RoleFormProps) {
|
|
const router = useRouter();
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
const isEditing = !!role;
|
|
|
|
const form = useForm<FormData>({
|
|
resolver: zodResolver(formSchema),
|
|
defaultValues: {
|
|
name: role?.name || "",
|
|
description: role?.description || "",
|
|
permissions: role?.permissions || {},
|
|
},
|
|
});
|
|
|
|
async function onSubmit(data: FormData) {
|
|
setIsLoading(true);
|
|
try {
|
|
const url = isEditing ? `/api/admin/roles/${role.id}` : "/api/admin/roles";
|
|
const method = isEditing ? "PATCH" : "POST";
|
|
|
|
const response = await fetch(url, {
|
|
method,
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(data),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const result = await response.json();
|
|
throw new Error(result.error || `Failed to ${isEditing ? "update" : "create"} role`);
|
|
}
|
|
|
|
toast.success(`Role ${isEditing ? "updated" : "created"} successfully`);
|
|
router.push("/admin/roles");
|
|
router.refresh();
|
|
} catch (error) {
|
|
toast.error(error instanceof Error ? error.message : "An error occurred");
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Form {...form}>
|
|
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
|
|
<FormField
|
|
control={form.control}
|
|
name="name"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Name</FormLabel>
|
|
<FormControl>
|
|
<Input
|
|
placeholder="custom-role"
|
|
{...field}
|
|
disabled={isLoading || role?.is_system}
|
|
/>
|
|
</FormControl>
|
|
<FormDescription>
|
|
Lowercase letters, numbers, and hyphens only
|
|
</FormDescription>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
|
|
<FormField
|
|
control={form.control}
|
|
name="description"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Description</FormLabel>
|
|
<FormControl>
|
|
<Textarea
|
|
placeholder="Describe what this role is for..."
|
|
{...field}
|
|
disabled={isLoading}
|
|
/>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
|
|
<FormField
|
|
control={form.control}
|
|
name="permissions"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Permissions</FormLabel>
|
|
<FormControl>
|
|
<div className="border rounded-lg p-4">
|
|
<PermissionPicker
|
|
value={field.value}
|
|
onChange={field.onChange}
|
|
disabled={isLoading}
|
|
/>
|
|
</div>
|
|
</FormControl>
|
|
<FormDescription>
|
|
Select the permissions this role should have
|
|
</FormDescription>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
|
|
<div className="flex gap-4">
|
|
<Button type="submit" disabled={isLoading}>
|
|
{isLoading ? (
|
|
<>
|
|
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
|
{isEditing ? "Saving..." : "Creating..."}
|
|
</>
|
|
) : (
|
|
<>
|
|
<Save className="mr-2 h-4 w-4" />
|
|
{isEditing ? "Save Changes" : "Create Role"}
|
|
</>
|
|
)}
|
|
</Button>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() => router.back()}
|
|
disabled={isLoading}
|
|
>
|
|
Cancel
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
</Form>
|
|
);
|
|
}
|