Files
teamvis-selfhost/supabase/migrations/0004_trusted_access.sql
T
2026-06-25 16:43:22 +02:00

39 lines
1.6 KiB
SQL

-- Vertraulicher Zusatz-Link ("Trusted-Link") pro Mitarbeiter.
--
-- Idee: Die öffentliche Karte (/<slug>) zeigt nur Festnetz (wie show_mobile=false
-- vorgibt). Wer zusätzlich ein gültiges Token (?k=…) mitbringt, sieht auch die
-- Mobilnummer — auf der Karte UND in der vCard. Token kann ablaufen, jeder
-- Zugriff mit Token wird geloggt.
-- 1) Token-Spalten auf employees
alter table public.employees
add column if not exists trusted_token text,
add column if not exists trusted_token_expires_at timestamptz,
add column if not exists trusted_token_created_at timestamptz;
-- Eindeutigkeit, aber nur wenn gesetzt — NULL-Werte dürfen mehrfach vorkommen.
drop index if exists public.employees_trusted_token_key;
create unique index employees_trusted_token_key
on public.employees(trusted_token)
where trusted_token is not null;
-- 2) Audit-Log-Tabelle
create table if not exists public.trusted_access_log (
id uuid primary key default gen_random_uuid(),
employee_id uuid not null references public.employees(id) on delete cascade,
accessed_at timestamptz not null default now(),
route text not null, -- 'card' | 'vcard'
ip text,
user_agent text
);
create index if not exists trusted_access_log_employee_id_idx
on public.trusted_access_log (employee_id, accessed_at desc);
-- 3) RLS: Tabelle komplett zu für anon. Nur service-role (Admin-Server-Code)
-- liest und schreibt.
alter table public.trusted_access_log enable row level security;
drop policy if exists "no_anon_access_trusted_access_log" on public.trusted_access_log;
-- Absichtlich KEINE anon-Policy → Default deny. Service-Role umgeht RLS.