Migration 023 (folded into schema.sql section 15). ADDITIVE and backward compatible: with no permission/ACL rows the app behaves exactly as before. Schema: - space_role_permissions (space_id, subject, permission); ops_acl (space_id, entity_type secret|kb|process|area_lead, entity_id, role). Full RLS, writes admin/board. - user_effective_roles(uid,sid): built-in role + custom-role slugs + area_lead:<id> for areas the member leads. SECURITY DEFINER. - user_has_permission(uid,sid,perm): admin implicit-all else a grant via an effective role. SECURITY DEFINER. - secrets/knowledge_base SELECT rewritten as `exact existing rule OR ops_acl match` (only widens; zero change with no ACL rows). Seed + backfill default role permissions. Area-lead roles reuse the existing area_leads table rather than duplicating the concept: a row is a role, lead_id NULL = Vacant, assigning sets lead_id, the member then holds area_lead:<id>. App: - lib/permissions-catalog.ts (codes + per-role defaults + guardrails), Zod schemas, lib/actions/permissions.ts (setRolePermissions, setOpsAcl, createAreaLeadRole, assign/unassign/delete). - Customize gains Permissions (role x permission matrix, admin shown implicit-all/locked) and Area leads (create/Vacant/assign) panels, modular under customize/panels/. Reusable components/ops/ops-acl-editor.tsx wired into the Secrets list. Audit-found latent fixes: - revealSecret errors on encrypted-but-no-ciphertext corruption instead of silently returning the stale plaintext column. - upsertAreaLead rejects null/empty area_code (NULLs are distinct in unique constraints; allowed duplicate rows). types/database.ts: added the two new tables. 271/271 tests pass, build clean. KB-modal ACL wiring + members-row shortcut deferred and tracked in HANDOFF.
52 lines
1.6 KiB
TypeScript
52 lines
1.6 KiB
TypeScript
'use server'
|
|
|
|
import { revalidatePath } from 'next/cache'
|
|
import { createClient } from '@/lib/supabase/server'
|
|
import { requireMemberWithRole, parseInput } from '@/lib/auth-helpers'
|
|
import { ADMIN_ROLES } from '@/lib/permissions'
|
|
import { upsertAreaLeadSchema } from '@/lib/validations'
|
|
|
|
export async function upsertAreaLead(formData: {
|
|
area_code: string
|
|
area_name: string
|
|
lead_id?: string
|
|
lead_handle?: string
|
|
status?: string
|
|
}) {
|
|
const v = parseInput(upsertAreaLeadSchema, formData)
|
|
if (!v.ok) return { error: v.error }
|
|
|
|
// area_code is the (space_id, area_code) upsert conflict target. Postgres
|
|
// treats NULLs as distinct in unique constraints, so a null/empty code
|
|
// would let duplicate area-lead rows accumulate. Require it.
|
|
if (!v.data.area_code || !v.data.area_code.trim()) {
|
|
return { error: 'An area code is required.' }
|
|
}
|
|
|
|
const supabase = await createClient()
|
|
const auth = await requireMemberWithRole(supabase, ADMIN_ROLES, 'Admin access required')
|
|
if (!auth.ok) return { error: auth.error }
|
|
const { member } = auth
|
|
|
|
const { data, error } = await supabase
|
|
.from('area_leads')
|
|
.upsert(
|
|
{
|
|
space_id: member.space_id,
|
|
area_code: v.data.area_code,
|
|
area_name: v.data.area_name,
|
|
lead_id: v.data.lead_id ?? null,
|
|
lead_handle: v.data.lead_handle ?? null,
|
|
status: v.data.status,
|
|
updated_at: new Date().toISOString(),
|
|
},
|
|
{ onConflict: 'space_id,area_code' },
|
|
)
|
|
.select()
|
|
.single()
|
|
|
|
if (error) return { error: error.message }
|
|
revalidatePath('/ops')
|
|
return { data }
|
|
}
|