Files
cotizador-wmc/SUPABASE-ACCESOS.sql
2026-08-29 08:39:57 -04:00

99 lines
4.0 KiB
SQL

-- ============================================================
-- Cotizador Walmart Connect - Control de acceso por usuario
-- GomezLee Marketing
-- ============================================================
-- Script completo para una instalación nueva.
-- Si la tabla YA existe, para agregar únicamente el permiso del
-- Tarifario usa SUPABASE-PERMISO-TARIFARIO.sql.
create table if not exists public.cotizador_wmc_access (
email text primary key,
full_name text not null,
is_active boolean not null default true,
can_use_tarifario boolean not null default false,
created_at timestamptz not null default now(),
constraint cotizador_wmc_access_email_normalized_chk
check (email = lower(btrim(email))),
constraint cotizador_wmc_access_glm_domain_chk
check (email like '%@gomezleemarketing.com')
);
-- Si esta tabla fue creada con una versión anterior, agrega la columna nueva.
alter table public.cotizador_wmc_access
add column if not exists can_use_tarifario boolean not null default false;
comment on table public.cotizador_wmc_access is
'Usuarios autorizados para ingresar al Cotizador Walmart Connect.';
comment on column public.cotizador_wmc_access.is_active is
'true = acceso general permitido; false = acceso general revocado.';
comment on column public.cotizador_wmc_access.can_use_tarifario is
'true = puede visualizar/utilizar el módulo Tarifario; false = no tiene acceso al módulo.';
-- RLS: el navegador solo puede comprobar SU PROPIO acceso y únicamente
-- cuando la fila está activa. No puede leer usuarios ajenos ni modificar la tabla.
alter table public.cotizador_wmc_access enable row level security;
revoke all on table public.cotizador_wmc_access from anon;
revoke insert, update, delete, truncate, references, trigger
on table public.cotizador_wmc_access from authenticated;
grant select on table public.cotizador_wmc_access to authenticated;
drop policy if exists "cotizador_wmc_read_own_active_access"
on public.cotizador_wmc_access;
create policy "cotizador_wmc_read_own_active_access"
on public.cotizador_wmc_access
for select
to authenticated
using (
is_active = true
and email = lower(coalesce((select auth.jwt() ->> 'email'), ''))
);
-- Usuarios autorizados iniciales.
-- El permiso de Tarifario queda false por defecto y se administra por separado.
insert into public.cotizador_wmc_access (email, full_name, is_active)
values
('iaracena@gomezleemarketing.com', 'Isaac Aracena', true),
('lmatos@gomezleemarketing.com', 'Luis Matos', true),
('ethen@gomezleemarketing.com', 'Eidan Then', true),
('mgomez@gomezleemarketing.com', 'Máximo Gómez', true),
('jgomez@gomezleemarketing.com', 'José Leopoldo Gómez', true),
('wlopez@gomezleemarketing.com', 'William Lopez', true)
on conflict (email) do update
set full_name = excluded.full_name;
-- ============================================================
-- EJEMPLOS DE ADMINISTRACIÓN (NO es necesario ejecutarlos ahora)
-- ============================================================
-- QUITAR ACCESO GENERAL sin borrar el usuario:
-- update public.cotizador_wmc_access
-- set is_active = false
-- where email = 'usuario@gomezleemarketing.com';
-- VOLVER A DAR ACCESO GENERAL:
-- update public.cotizador_wmc_access
-- set is_active = true
-- where email = 'usuario@gomezleemarketing.com';
-- DAR ACCESO AL TARIFARIO:
-- update public.cotizador_wmc_access
-- set can_use_tarifario = true
-- where email = 'usuario@gomezleemarketing.com';
-- QUITAR ACCESO AL TARIFARIO:
-- update public.cotizador_wmc_access
-- set can_use_tarifario = false
-- where email = 'usuario@gomezleemarketing.com';
-- DAR ACCESO GENERAL A UNA PERSONA NUEVA:
-- insert into public.cotizador_wmc_access (email, full_name, is_active)
-- values ('usuario@gomezleemarketing.com', 'Nombre Apellido', true)
-- on conflict (email) do update
-- set full_name = excluded.full_name, is_active = true;
-- ELIMINAR COMPLETAMENTE UN REGISTRO:
-- delete from public.cotizador_wmc_access
-- where email = 'usuario@gomezleemarketing.com';