wulf-pulse/app/api/admin/roles/[id]/route.ts
root 9f912aed24 feat: add authentication, user management, and admin features
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
2026-01-31 12:43:14 -05:00

171 lines
4.5 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { Pool } from "pg";
import { requireAdmin, requireSuperAdmin } from "@/lib/auth-utils";
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
});
// GET /api/admin/roles/[id] - Get a single role
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const { error } = await requireAdmin();
if (error) return error;
try {
const { id } = await params;
const result = await pool.query(
`SELECT
r.id, r.name, r.description, r.permissions, r.is_system, r.created_at, r.updated_at,
COUNT(DISTINCT u.id) as user_count
FROM "role" r
LEFT JOIN "user" u ON u.role = r.name
WHERE r.id = $1
GROUP BY r.id`,
[id]
);
if (result.rows.length === 0) {
return NextResponse.json({ error: "Role not found" }, { status: 404 });
}
return NextResponse.json({ role: result.rows[0] });
} catch (error) {
console.error("Error fetching role:", error);
return NextResponse.json(
{ error: "Failed to fetch role" },
{ status: 500 }
);
}
}
// PATCH /api/admin/roles/[id] - Update a role
export async function PATCH(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const { error } = await requireSuperAdmin();
if (error) return error;
try {
const { id } = await params;
const body = await request.json();
const { name, description, permissions } = body;
// Check if role exists
const existingRole = await pool.query(
'SELECT id, is_system, name FROM "role" WHERE id = $1',
[id]
);
if (existingRole.rows.length === 0) {
return NextResponse.json({ error: "Role not found" }, { status: 404 });
}
// Prevent modifying system roles' names
if (existingRole.rows[0].is_system && name && name !== existingRole.rows[0].name) {
return NextResponse.json(
{ error: "Cannot rename system roles" },
{ status: 400 }
);
}
// Build update query
const updates: string[] = [];
const values: (string | null)[] = [];
let paramIndex = 1;
if (name !== undefined) {
updates.push(`name = $${paramIndex++}`);
values.push(name);
}
if (description !== undefined) {
updates.push(`description = $${paramIndex++}`);
values.push(description);
}
if (permissions !== undefined) {
updates.push(`permissions = $${paramIndex++}`);
values.push(JSON.stringify(permissions));
}
if (updates.length === 0) {
return NextResponse.json(
{ error: "No fields to update" },
{ status: 400 }
);
}
updates.push(`updated_at = NOW()`);
values.push(id);
const result = await pool.query(
`UPDATE "role" SET ${updates.join(", ")} WHERE id = $${paramIndex}
RETURNING id, name, description, permissions, is_system, updated_at`,
values
);
return NextResponse.json({ role: result.rows[0] });
} catch (error) {
console.error("Error updating role:", error);
return NextResponse.json(
{ error: "Failed to update role" },
{ status: 500 }
);
}
}
// DELETE /api/admin/roles/[id] - Delete a role
export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const { error } = await requireSuperAdmin();
if (error) return error;
try {
const { id } = await params;
// Check if role exists and is not a system role
const existingRole = await pool.query(
'SELECT id, is_system, name FROM "role" WHERE id = $1',
[id]
);
if (existingRole.rows.length === 0) {
return NextResponse.json({ error: "Role not found" }, { status: 404 });
}
if (existingRole.rows[0].is_system) {
return NextResponse.json(
{ error: "Cannot delete system roles" },
{ status: 400 }
);
}
// Check if role is assigned to any users
const usersWithRole = await pool.query(
'SELECT COUNT(*) as count FROM "user" WHERE role = $1',
[existingRole.rows[0].name]
);
if (parseInt(usersWithRole.rows[0].count) > 0) {
return NextResponse.json(
{ error: "Cannot delete role that is assigned to users" },
{ status: 400 }
);
}
await pool.query('DELETE FROM "role" WHERE id = $1', [id]);
return NextResponse.json({ success: true });
} catch (error) {
console.error("Error deleting role:", error);
return NextResponse.json(
{ error: "Failed to delete role" },
{ status: 500 }
);
}
}