diff --git a/ddl/19-product-attributes.sql b/ddl/19-product-attributes.sql index 85394b1..f5f7c09 100644 --- a/ddl/19-product-attributes.sql +++ b/ddl/19-product-attributes.sql @@ -144,3 +144,41 @@ COMMENT ON COLUMN products.product_variants.price_extra IS 'Ajuste de precio sob COMMENT ON COLUMN products.product_variants.cost_extra IS 'Ajuste de costo sobre el costo base del producto'; COMMENT ON TABLE products.product_variant_attributes IS 'Tabla de union entre variantes y valores de atributos'; + +-- ===================== +-- RLS (Row Level Security) +-- ===================== +ALTER TABLE products.product_attributes ENABLE ROW LEVEL SECURITY; +ALTER TABLE products.product_attribute_values ENABLE ROW LEVEL SECURITY; +ALTER TABLE products.product_variants ENABLE ROW LEVEL SECURITY; +ALTER TABLE products.product_variant_attributes ENABLE ROW LEVEL SECURITY; + +-- Politicas RLS para product_attributes +DROP POLICY IF EXISTS product_attributes_tenant_isolation ON products.product_attributes; +CREATE POLICY product_attributes_tenant_isolation ON products.product_attributes + FOR ALL USING (tenant_id = current_setting('app.current_tenant_id', true)::uuid); + +-- Politicas RLS para product_variants +DROP POLICY IF EXISTS product_variants_tenant_isolation ON products.product_variants; +CREATE POLICY product_variants_tenant_isolation ON products.product_variants + FOR ALL USING (tenant_id = current_setting('app.current_tenant_id', true)::uuid); + +-- Politicas RLS para product_attribute_values (via JOIN con attribute) +DROP POLICY IF EXISTS product_attribute_values_tenant_isolation ON products.product_attribute_values; +CREATE POLICY product_attribute_values_tenant_isolation ON products.product_attribute_values + FOR ALL USING ( + attribute_id IN ( + SELECT id FROM products.product_attributes + WHERE tenant_id = current_setting('app.current_tenant_id', true)::uuid + ) + ); + +-- Politicas RLS para product_variant_attributes (via JOIN con variant) +DROP POLICY IF EXISTS product_variant_attributes_tenant_isolation ON products.product_variant_attributes; +CREATE POLICY product_variant_attributes_tenant_isolation ON products.product_variant_attributes + FOR ALL USING ( + variant_id IN ( + SELECT id FROM products.product_variants + WHERE tenant_id = current_setting('app.current_tenant_id', true)::uuid + ) + );