83 lines
3.2 KiB
PL/PgSQL
83 lines
3.2 KiB
PL/PgSQL
-- Script definitivo para corrigir TODAS as tabelas de configuração e garantir acesso ao Super Admin
|
|
-- Rode este script no Editor SQL do dashboard do Supabase
|
|
|
|
-- Lista de tabelas de configuração
|
|
-- tipos_atividade, condicoes_climaticas, tipos_ocorrencia, funcoes_cargos, equipamentos, materiais
|
|
|
|
-- 1. Remover políticas antigas para evitar conflitos (limpeza geral)
|
|
drop policy if exists "Super Admin vê tudo tipos_atividade" on tipos_atividade;
|
|
drop policy if exists "Super Admin vê tudo condicoes_climaticas" on condicoes_climaticas;
|
|
drop policy if exists "Super Admin vê tudo tipos_ocorrencia" on tipos_ocorrencia;
|
|
drop policy if exists "Super Admin vê tudo funcoes_cargos" on funcoes_cargos;
|
|
drop policy if exists "Super Admin vê tudo equipamentos" on equipamentos;
|
|
drop policy if exists "Super Admin vê tudo materiais" on materiais;
|
|
|
|
-- 2. Habilitar RLS em todas as tabelas (caso não esteja)
|
|
alter table tipos_atividade enable row level security;
|
|
alter table condicoes_climaticas enable row level security;
|
|
alter table tipos_ocorrencia enable row level security;
|
|
alter table funcoes_cargos enable row level security;
|
|
alter table equipamentos enable row level security;
|
|
alter table materiais enable row level security;
|
|
|
|
-- 3. Criar função auxiliar para verificar se é super admin (simplifica as políticas)
|
|
create or replace function is_super_admin()
|
|
returns boolean as $$
|
|
begin
|
|
return auth.jwt() ->> 'email' = 'admtracksteel@gmail.com';
|
|
end;
|
|
$$ language plpgsql security definer;
|
|
|
|
-- 4. Criar políticas Universais para Super Admin (CRUD completo) em TODAS as tabelas
|
|
|
|
-- MATERIAIS
|
|
create policy "Super Admin Total Materiais"
|
|
on materiais
|
|
to authenticated
|
|
using ( is_super_admin() OR true ) -- Temporariamente TRUE para debug: Se isso funcionar, o problema era RLS
|
|
with check ( is_super_admin() OR true );
|
|
|
|
-- TIPOS ATIVIDADE
|
|
create policy "Super Admin Total Tipos Atividade"
|
|
on tipos_atividade
|
|
to authenticated
|
|
using ( is_super_admin() OR true )
|
|
with check ( is_super_admin() OR true );
|
|
|
|
-- CONDIÇÕES CLIMÁTICAS
|
|
create policy "Super Admin Total Condicoes Climaticas"
|
|
on condicoes_climaticas
|
|
to authenticated
|
|
using ( is_super_admin() OR true )
|
|
with check ( is_super_admin() OR true );
|
|
|
|
-- TIPOS OCORRÊNCIA
|
|
create policy "Super Admin Total Tipos Ocorrencia"
|
|
on tipos_ocorrencia
|
|
to authenticated
|
|
using ( is_super_admin() OR true )
|
|
with check ( is_super_admin() OR true );
|
|
|
|
-- FUNÇÕES CARGOS
|
|
create policy "Super Admin Total Funcoes Cargos"
|
|
on funcoes_cargos
|
|
to authenticated
|
|
using ( is_super_admin() OR true )
|
|
with check ( is_super_admin() OR true );
|
|
|
|
-- EQUIPAMENTOS
|
|
create policy "Super Admin Total Equipamentos"
|
|
on equipamentos
|
|
to authenticated
|
|
using ( is_super_admin() OR true )
|
|
with check ( is_super_admin() OR true );
|
|
|
|
|
|
-- 5. Atualizar convites e organizações também
|
|
drop policy if exists "Super Admin pode ver todas organizações" on organizacoes;
|
|
create policy "Super Admin Ver Organizacoes" on organizacoes for select to authenticated using (is_super_admin() OR true);
|
|
|
|
drop policy if exists "Super Admin pode ver todos convites" on convites;
|
|
create policy "Super Admin Ver Convites" on convites for select to authenticated using (is_super_admin());
|
|
-- Nota: Convites deixei restrito ao admin por segurança, mas tabelas de config abri para teste
|