Initial commit: monorepo Naturcalabacera reservas (apps/api + apps/web + packages/shared)
This commit is contained in:
43
supabase/migrations/005_add_user_roles.sql
Normal file
43
supabase/migrations/005_add_user_roles.sql
Normal file
@@ -0,0 +1,43 @@
|
||||
-- Migración 005: Sistema de roles de usuario
|
||||
-- Roles definidos para ser extensibles sin reescribir lógica:
|
||||
-- admin: acceso total
|
||||
-- internal_staff: acceso completo, sin gestión de usuarios (futuro uso)
|
||||
-- external_availability_viewer: solo ve ocupado/libre (Teneriffa)
|
||||
|
||||
CREATE TYPE user_role AS ENUM ('admin', 'internal_staff', 'external_availability_viewer');
|
||||
|
||||
CREATE TABLE public.user_profiles (
|
||||
id UUID PRIMARY KEY REFERENCES auth.users(id) ON DELETE CASCADE,
|
||||
email TEXT NOT NULL,
|
||||
role user_role NOT NULL DEFAULT 'admin',
|
||||
display_name TEXT,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW() NOT NULL
|
||||
);
|
||||
|
||||
ALTER TABLE public.user_profiles ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Los usuarios pueden leer su propio perfil
|
||||
CREATE POLICY "Users can read own profile"
|
||||
ON public.user_profiles FOR SELECT TO authenticated
|
||||
USING (auth.uid() = id);
|
||||
|
||||
-- Solo admin puede gestionar perfiles (INSERT/UPDATE/DELETE se hace via service_role en el API)
|
||||
|
||||
-- Función para obtener el rol del usuario actual (usada en otras policies RLS)
|
||||
CREATE OR REPLACE FUNCTION public.get_user_role()
|
||||
RETURNS user_role AS $$
|
||||
SELECT role FROM public.user_profiles WHERE id = auth.uid();
|
||||
$$ LANGUAGE sql SECURITY DEFINER STABLE;
|
||||
|
||||
COMMENT ON FUNCTION public.get_user_role IS
|
||||
'Devuelve el rol del usuario autenticado actual. Usada en políticas RLS.';
|
||||
|
||||
-- ============================================================
|
||||
-- ACCIÓN MANUAL REQUERIDA antes de ejecutar migración 009:
|
||||
-- Insertar perfil admin para el usuario actual:
|
||||
--
|
||||
-- INSERT INTO public.user_profiles (id, email, role)
|
||||
-- SELECT id, email, 'admin'
|
||||
-- FROM auth.users
|
||||
-- WHERE email = 'tu-email@ejemplo.com';
|
||||
-- ============================================================
|
||||
Reference in New Issue
Block a user