Files
hackerspace.sh/lib/actions/roles.ts
T
Moheeb Zara de51000584 feat: forum, polymorphic comments, custom tiers, multi-invite, secrets encryption
Big feature batch driven by the super-audit findings and the user's
follow-up requests.

Schema migration 021 (folded into schema.sql Section 13):
- forum_threads + polymorphic comments table keyed on (entity_type,
  entity_id). Entity types: forum_thread, proposal, incident, policy.
  Trigger maintains forum_threads.comment_count and last_comment_at.
- space_tiers per-space with monthly_price_cents, billing_cadence,
  is_system, sort_order. Trigger seeds plus/basic/associate on every
  space INSERT and backfills existing spaces. space_members.tier_id FK
  added and backfilled from the legacy enum value.
- space_role_labels (rename/recolor built-in roles per space).
  space_custom_roles + space_member_custom_roles for non-privileged
  extra labels. RLS gates by admin/board.
- space_invites: multi-code invites with expires_at, max_uses,
  uses_count, is_enabled. Legacy spaces.invite_code backfilled as a
  permanent enabled invite.
- secrets.encrypted_value bytea + encryption_version. Secrets RLS
  tightened. New comms_channels INSERT policy: any member can create;
  default channels are protected from deletion.
- knowledge_base.render_markdown boolean (default true).

Secrets encryption-at-rest:
- AES-256-GCM in lib/secrets/crypto.ts. Master key from
  SECRETS_ENCRYPTION_KEY env (64 hex chars).
- New revealSecret(id) server action: only path to plaintext, writes
  secret.revealed to activity_log.
- /ops/page.tsx no longer selects value or encrypted_value on the list
  endpoint - the original screenshot bug where ciphertext was already
  in the page bundle before "Reveal" is fixed.

UI:
- /forum index, /forum/new, /forum/[id]. Pin/lock/delete actions for
  admin/board.
- CommentThread component embedded on /proposals/[id], /incidents/[id],
  /policies/[slug]. Markdown rendering via react-markdown + remark-gfm.
- /comms sidebar: inline "+ New channel" form.
- /ops KB: row click opens markdown-rendering viewer modal.
- /settings: new Tiers tab and Invites tab. Inline price edit and
  archive/delete on tiers; copy/enable/disable/delete on invites.
- AtlasLogo replaces the terminal icon on the landing nav and footer.
  public/logo.svg wired as the favicon in app/layout.tsx metadata.
- Sidebar has a Forum nav link.

Server actions added:
- lib/actions/forum.ts, comms.ts, tiers.ts, roles.ts, invites.ts.
- lib/actions/secrets.ts rewritten with updateSecret + revealSecret.

Auth flow:
- joinSpace honors codes from space_invites first (enabled, expiry,
  use cap), falls back to legacy spaces.invite_code, increments
  uses_count on success.

Env:
- SECRETS_ENCRYPTION_KEY documented in .env.example and set on the
  Droplet's /opt/hackerspace-ops/.env.production.

README: live site link added (https://hackerspace.sh).

266/266 unit tests pass. Production build clean. Migration 021 will be
applied on the next deploy via /opt/hackerspace-ops/deploy.sh's
idempotent migration runner.
2026-05-14 17:47:52 -07:00

171 lines
5.3 KiB
TypeScript

'use server'
import { revalidatePath } from 'next/cache'
import { createClient } from '@/lib/supabase/server'
import { requireMemberWithRole, parseInput } from '@/lib/auth-helpers'
import {
upsertRoleLabelSchema,
createCustomRoleSchema,
updateCustomRoleSchema,
uuidSchema,
} from '@/lib/validations'
const ROLE_ADMIN_ROLES = ['admin', 'board'] as const
export async function upsertRoleLabel(input: {
role: 'admin' | 'board' | 'treasurer' | 'member' | 'associate'
display_name?: string | null
description?: string | null
color?: string | null
sort_order?: number
}) {
const v = parseInput(upsertRoleLabelSchema, input)
if (!v.ok) return { error: v.error }
const supabase = await createClient()
const auth = await requireMemberWithRole(supabase, ROLE_ADMIN_ROLES, 'Admin or board access required')
if (!auth.ok) return { error: auth.error }
const { member } = auth
const { error } = await supabase
.from('space_role_labels')
.upsert({
space_id: member.space_id,
role: v.data.role,
display_name: v.data.display_name ?? null,
description: v.data.description ?? null,
color: v.data.color ?? null,
sort_order: v.data.sort_order ?? 0,
}, { onConflict: 'space_id,role' })
if (error) return { error: error.message }
revalidatePath('/settings')
return { success: true as const }
}
export async function createCustomRole(input: {
slug: string
name: string
description?: string | null
color?: string | null
sort_order?: number
}) {
const v = parseInput(createCustomRoleSchema, input)
if (!v.ok) return { error: v.error }
const supabase = await createClient()
const auth = await requireMemberWithRole(supabase, ROLE_ADMIN_ROLES, 'Admin or board access required')
if (!auth.ok) return { error: auth.error }
const { member } = auth
const { data, error } = await supabase
.from('space_custom_roles')
.insert({
space_id: member.space_id,
slug: v.data.slug,
name: v.data.name,
description: v.data.description ?? null,
color: v.data.color ?? null,
sort_order: v.data.sort_order ?? 100,
})
.select('id')
.single()
if (error) return { error: error.message }
revalidatePath('/settings')
return { id: data.id }
}
export async function updateCustomRole(roleId: string, updates: {
name?: string
description?: string | null
color?: string | null
sort_order?: number
}) {
const idCheck = parseInput(uuidSchema, roleId)
if (!idCheck.ok) return { error: 'Invalid role ID' }
const v = parseInput(updateCustomRoleSchema, updates)
if (!v.ok) return { error: v.error }
const supabase = await createClient()
const auth = await requireMemberWithRole(supabase, ROLE_ADMIN_ROLES, 'Admin or board access required')
if (!auth.ok) return { error: auth.error }
const { member } = auth
const patch: Record<string, unknown> = {}
if (v.data.name !== undefined) patch.name = v.data.name
if (v.data.description !== undefined) patch.description = v.data.description
if (v.data.color !== undefined) patch.color = v.data.color
if (v.data.sort_order !== undefined) patch.sort_order = v.data.sort_order
const { error } = await supabase
.from('space_custom_roles')
.update(patch)
.eq('id', idCheck.data)
.eq('space_id', member.space_id)
if (error) return { error: error.message }
revalidatePath('/settings')
return { success: true as const }
}
export async function deleteCustomRole(roleId: string) {
const v = parseInput(uuidSchema, roleId)
if (!v.ok) return { error: 'Invalid role ID' }
const supabase = await createClient()
const auth = await requireMemberWithRole(supabase, ['admin'], 'Admin access required')
if (!auth.ok) return { error: auth.error }
const { member } = auth
const { error } = await supabase
.from('space_custom_roles')
.delete()
.eq('id', v.data)
.eq('space_id', member.space_id)
if (error) return { error: error.message }
revalidatePath('/settings')
return { success: true as const }
}
export async function assignCustomRole(memberId: string, customRoleId: string) {
const a = parseInput(uuidSchema, memberId)
const b = parseInput(uuidSchema, customRoleId)
if (!a.ok || !b.ok) return { error: 'Invalid ID' }
const supabase = await createClient()
const auth = await requireMemberWithRole(supabase, ROLE_ADMIN_ROLES, 'Admin or board access required')
if (!auth.ok) return { error: auth.error }
const { error } = await supabase
.from('space_member_custom_roles')
.insert({ member_id: a.data, custom_role_id: b.data })
if (error) return { error: error.message }
revalidatePath('/members')
revalidatePath('/settings')
return { success: true as const }
}
export async function unassignCustomRole(memberId: string, customRoleId: string) {
const a = parseInput(uuidSchema, memberId)
const b = parseInput(uuidSchema, customRoleId)
if (!a.ok || !b.ok) return { error: 'Invalid ID' }
const supabase = await createClient()
const auth = await requireMemberWithRole(supabase, ROLE_ADMIN_ROLES, 'Admin or board access required')
if (!auth.ok) return { error: auth.error }
const { error } = await supabase
.from('space_member_custom_roles')
.delete()
.eq('member_id', a.data)
.eq('custom_role_id', b.data)
if (error) return { error: error.message }
revalidatePath('/members')
revalidatePath('/settings')
return { success: true as const }
}