wulf-pulse/migrations/014_create_admin_settings.sql
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

78 lines
3.3 KiB
SQL

-- Admin Settings Tables Migration
-- Creates app_settings, session_policy, and email_template tables
-- App settings table (key-value store for application settings)
CREATE TABLE IF NOT EXISTS "app_settings" (
id TEXT PRIMARY KEY,
key TEXT NOT NULL UNIQUE,
value TEXT,
description TEXT,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);
-- Session policy table (CIDR-based session timeout policies)
CREATE TABLE IF NOT EXISTS "session_policy" (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
cidr TEXT NOT NULL,
timeout_seconds INTEGER NOT NULL DEFAULT 86400,
priority INTEGER NOT NULL DEFAULT 0,
enabled BOOLEAN NOT NULL DEFAULT TRUE,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);
-- Email template table
CREATE TABLE IF NOT EXISTS "email_template" (
id TEXT PRIMARY KEY,
type TEXT NOT NULL UNIQUE,
subject TEXT NOT NULL,
body_html TEXT NOT NULL,
body_text TEXT,
variables TEXT, -- JSON array of available variables
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);
-- Audit log table
CREATE TABLE IF NOT EXISTS "audit_log" (
id TEXT PRIMARY KEY,
timestamp TIMESTAMP NOT NULL DEFAULT NOW(),
user_id TEXT REFERENCES "user"(id) ON DELETE SET NULL,
user_email TEXT,
action TEXT NOT NULL,
resource TEXT NOT NULL,
resource_id TEXT,
details JSONB,
ip_address TEXT,
user_agent TEXT
);
-- Indexes
CREATE INDEX IF NOT EXISTS idx_app_settings_key ON "app_settings"(key);
CREATE INDEX IF NOT EXISTS idx_session_policy_priority ON "session_policy"(priority DESC);
CREATE INDEX IF NOT EXISTS idx_email_template_type ON "email_template"(type);
CREATE INDEX IF NOT EXISTS idx_audit_log_timestamp ON "audit_log"(timestamp DESC);
CREATE INDEX IF NOT EXISTS idx_audit_log_user_id ON "audit_log"(user_id);
CREATE INDEX IF NOT EXISTS idx_audit_log_action ON "audit_log"(action);
CREATE INDEX IF NOT EXISTS idx_audit_log_resource ON "audit_log"(resource);
-- Insert default app settings
INSERT INTO "app_settings" (id, key, value, description) VALUES
('setting_microsoft_tenant', 'microsoft_tenant_id', 'common', 'Microsoft Entra ID tenant ID'),
('setting_session_timeout', 'default_session_timeout', '86400', 'Default session timeout in seconds'),
('setting_audit_retention', 'audit_log_retention_days', '90', 'Number of days to retain audit logs')
ON CONFLICT (key) DO NOTHING;
-- Insert default email templates
INSERT INTO "email_template" (id, type, subject, body_html, body_text, variables) VALUES
('template_magic_link', 'magic_link', 'Sign in to Pulse',
'<h1>Sign in to Pulse</h1><p>Click the link below to sign in:</p><a href="{{url}}">Sign in</a><p>This link expires in 5 minutes.</p>',
'Sign in to Pulse\n\nClick the link below to sign in:\n{{url}}\n\nThis link expires in 5 minutes.',
'["url", "email"]'),
('template_invitation', 'invitation', 'You''re invited to Pulse',
'<h1>You''re invited!</h1><p>{{inviter_name}} has invited you to join Pulse.</p><a href="{{url}}">Accept Invitation</a>',
'You''re invited!\n\n{{inviter_name}} has invited you to join Pulse.\n\nAccept invitation: {{url}}',
'["url", "email", "inviter_name"]')
ON CONFLICT (type) DO NOTHING;