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
This commit is contained in:
parent
d8e6931b85
commit
9f912aed24
68 changed files with 7651 additions and 3 deletions
105
app/api/admin/audit-log/route.ts
Normal file
105
app/api/admin/audit-log/route.ts
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { Pool } from "pg";
|
||||
import { requireAdmin } from "@/lib/auth-utils";
|
||||
|
||||
const pool = new Pool({
|
||||
connectionString: process.env.DATABASE_URL,
|
||||
});
|
||||
|
||||
// GET /api/admin/audit-log - Get audit logs with pagination and filters
|
||||
export async function GET(request: NextRequest) {
|
||||
const { error } = await requireAdmin();
|
||||
if (error) return error;
|
||||
|
||||
try {
|
||||
const searchParams = request.nextUrl.searchParams;
|
||||
const page = parseInt(searchParams.get("page") || "1");
|
||||
const limit = parseInt(searchParams.get("limit") || "50");
|
||||
const offset = (page - 1) * limit;
|
||||
|
||||
const userId = searchParams.get("userId");
|
||||
const action = searchParams.get("action");
|
||||
const resource = searchParams.get("resource");
|
||||
const startDate = searchParams.get("startDate");
|
||||
const endDate = searchParams.get("endDate");
|
||||
|
||||
let query = `
|
||||
SELECT
|
||||
al.id, al.timestamp, al.user_id, al.user_email, al.action,
|
||||
al.resource, al.resource_id, al.details, al.ip_address,
|
||||
u.name as user_name
|
||||
FROM "audit_log" al
|
||||
LEFT JOIN "user" u ON al.user_id = u.id
|
||||
WHERE 1=1
|
||||
`;
|
||||
const params: (string | number)[] = [];
|
||||
let paramIndex = 1;
|
||||
|
||||
if (userId) {
|
||||
query += ` AND al.user_id = $${paramIndex++}`;
|
||||
params.push(userId);
|
||||
}
|
||||
|
||||
if (action) {
|
||||
query += ` AND al.action = $${paramIndex++}`;
|
||||
params.push(action);
|
||||
}
|
||||
|
||||
if (resource) {
|
||||
query += ` AND al.resource = $${paramIndex++}`;
|
||||
params.push(resource);
|
||||
}
|
||||
|
||||
if (startDate) {
|
||||
query += ` AND al.timestamp >= $${paramIndex++}`;
|
||||
params.push(startDate);
|
||||
}
|
||||
|
||||
if (endDate) {
|
||||
query += ` AND al.timestamp <= $${paramIndex++}`;
|
||||
params.push(endDate);
|
||||
}
|
||||
|
||||
// Get total count
|
||||
const countQuery = query.replace(
|
||||
/SELECT[\s\S]*?FROM/,
|
||||
"SELECT COUNT(*) as total FROM"
|
||||
);
|
||||
const countResult = await pool.query(countQuery, params);
|
||||
const total = parseInt(countResult.rows[0].total);
|
||||
|
||||
// Add pagination
|
||||
query += ` ORDER BY al.timestamp DESC LIMIT $${paramIndex++} OFFSET $${paramIndex}`;
|
||||
params.push(limit, offset);
|
||||
|
||||
const result = await pool.query(query, params);
|
||||
|
||||
// Get unique actions and resources for filters
|
||||
const actionsResult = await pool.query(
|
||||
'SELECT DISTINCT action FROM "audit_log" ORDER BY action'
|
||||
);
|
||||
const resourcesResult = await pool.query(
|
||||
'SELECT DISTINCT resource FROM "audit_log" ORDER BY resource'
|
||||
);
|
||||
|
||||
return NextResponse.json({
|
||||
logs: result.rows,
|
||||
pagination: {
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
totalPages: Math.ceil(total / limit),
|
||||
},
|
||||
filters: {
|
||||
actions: actionsResult.rows.map((r) => r.action),
|
||||
resources: resourcesResult.rows.map((r) => r.resource),
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error fetching audit logs:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to fetch audit logs" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue