Files
errors.bitreport.pt/db/migrations/001_admin_users.sql
T
1sonhadorandPaperclip eeb5039a3c feat(BIT-183): admin auth, RBAC, audit logging, rate limiting
Implements BIT-154.7 — Auth administrativa, RBAC e quotas:

- api/src/types/auth.ts: 3 roles (superadmin/admin/viewer), 18 permissions,
  ROLE_PERMISSIONS map as single source of truth
- api/src/plugins/auth.ts: JWT via HTTP-only __Host- cookies (15min access /
  7d refresh), token rotation, timing-safe login
- api/src/plugins/rbac.ts: requirePermission() + requireRole() Fastify
  decorators for route-level enforcement
- api/src/plugins/audit.ts: auditLog() helper + Fastify plugin; writes to
  audit_logs table; failure never aborts main operation
- api/src/plugins/rate-limit.ts: per-route limits (login: 10/5min IP,
  ingest: 1000/min token, global: 500/min) + payload size enforcement
- api/src/routes/admin/auth.ts: POST login, POST refresh, POST logout,
  GET /me — with audit events on all operations
- db/migrations/001–004: admin_users, refresh_tokens, audit_logs tables +
  dev seed (bcrypt superadmin)
- web/src/app/(admin)/login: Velzon Bootstrap 5 login page (Next.js client
  component, credentials:include, HTTP-only cookie flow)
- docs/permission-matrix.md: full permission matrix + audit event catalogue
  + rate/payload limit table (BIT-183 acceptance criterion)

Co-Authored-By: Paperclip <[email protected]>
2026-03-22 04:22:57 +00:00

36 lines
1.4 KiB
PL/PgSQL

-- Migration 001: admin_users table
-- Stores admin panel users with role-based access control.
-- Passwords are hashed with bcrypt (cost 12) — never store plaintext.
--
-- Roles: superadmin | admin | viewer (see api/src/types/auth.ts)
CREATE TABLE IF NOT EXISTS admin_users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
email VARCHAR(320) NOT NULL,
password_hash VARCHAR(60) NOT NULL, -- bcrypt output is always 60 chars
role VARCHAR(20) NOT NULL DEFAULT 'viewer'
CHECK (role IN ('superadmin', 'admin', 'viewer')),
is_active BOOLEAN NOT NULL DEFAULT TRUE,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE UNIQUE INDEX IF NOT EXISTS admin_users_email_idx
ON admin_users (LOWER(email));
-- Auto-update updated_at on row change
CREATE OR REPLACE FUNCTION set_updated_at()
RETURNS TRIGGER LANGUAGE plpgsql AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$;
CREATE TRIGGER admin_users_updated_at
BEFORE UPDATE ON admin_users
FOR EACH ROW EXECUTE FUNCTION set_updated_at();
COMMENT ON TABLE admin_users IS 'Admin panel users (RBAC). Phase 1: global roles, no per-project scoping.';
COMMENT ON COLUMN admin_users.role IS 'superadmin = full access; admin = project mgmt; viewer = read-only';