Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix: rls policy for documents

Brad Barbinbda9b1c73353
diff --git a/packages/database/supabase/migrations/20250206124502_rls-document-fixes.sql b/packages/database/supabase/migrations/20250206124502_rls-document-fixes.sql
new file mode 100644
index 0000000000..afae458202
--- /dev/null
+++ b/packages/database/supabase/migrations/20250206124502_rls-document-fixes.sql
@@ -0,0 +1,55 @@
+ALTER POLICY "SELECT" ON "public"."document"
+ USING (
+ "companyId" = ANY (
+ (
+ SELECT
+ get_companies_with_permission ('documents_view')
+ )::text[]
+ )
+ AND (
+ has_valid_api_key_for_company("companyId") OR
+ (groups_for_user(auth.uid()::text) && "readGroups") = true
+ )
+ );
+
+ALTER POLICY "INSERT" ON "public"."document"
+ WITH CHECK (
+ "companyId" = ANY (
+ (
+ SELECT
+ get_companies_with_permission ('documents_create')
+ )::text[]
+ )
+ AND (
+ has_valid_api_key_for_company("companyId") OR
+ (groups_for_user(auth.uid()::text) && "writeGroups") = true
+ )
+ );
+
+ALTER POLICY "UPDATE" ON "public"."document"
+ USING (
+ "companyId" = ANY (
+ (
+ SELECT
+ get_companies_with_permission ('documents_update')
+ )::text[]
+ )
+ AND (
+ has_valid_api_key_for_company("companyId") OR
+ (groups_for_user(auth.uid()::text) && "writeGroups") = true
+ )
+ );
+
+ALTER POLICY "DELETE" ON "public"."document"
+ USING (
+ "companyId" = ANY (
+ (
+ SELECT
+ get_companies_with_permission ('documents_delete')
+ )::text[]
+ )
+ AND (
+ has_valid_api_key_for_company("companyId") OR
+ (groups_for_user(auth.uid()::text) && "writeGroups") = true
+ )
+ );
\ No newline at end of file