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.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
feat: add rls for tools and supplier quotes tables
Brad Barbinf44a31de3299
diff --git a/apps/carbon/app/modules/purchasing/ui/SupplierQuote/SupplierQuoteExplorer.tsx b/apps/carbon/app/modules/purchasing/ui/SupplierQuote/SupplierQuoteExplorer.tsxindex 3beb010bb3..51eba67de5 100644--- a/apps/carbon/app/modules/purchasing/ui/SupplierQuote/SupplierQuoteExplorer.tsx+++ b/apps/carbon/app/modules/purchasing/ui/SupplierQuote/SupplierQuoteExplorer.tsx@@ -53,7 +53,9 @@ export default function SupplierQuoteExplorer() {const newSupplierQuoteLineDisclosure = useDisclosure();const deleteLineDisclosure = useDisclosure();const [deleteLine, setDeleteLine] = useState<SupplierQuoteLine | null>(null);- const isDisabled = routeData?.quote?.status !== "Active";+ const isDisabled =+ !permissions.can("delete", "purchasing") ||+ routeData?.quote?.status !== "Active";const onDeleteLine = (line: SupplierQuoteLine) => {setDeleteLine(line);diff --git a/apps/carbon/app/modules/purchasing/ui/SupplierQuote/SupplierQuoteProperties.tsx b/apps/carbon/app/modules/purchasing/ui/SupplierQuote/SupplierQuoteProperties.tsxindex 12449fedda..63a0db0ee9 100644--- a/apps/carbon/app/modules/purchasing/ui/SupplierQuote/SupplierQuoteProperties.tsx+++ b/apps/carbon/app/modules/purchasing/ui/SupplierQuote/SupplierQuoteProperties.tsx@@ -105,7 +105,7 @@ const SupplierQuoteProperties = () => {const permissions = usePermissions();const isDisabled =- !permissions.can("update", "sales") ||+ !permissions.can("update", "purchasing") ||!["Active"].includes(routeData?.quote?.status ?? "");return (@@ -164,7 +164,7 @@ const SupplierQuoteProperties = () => {table="supplierQuote"value={assignee ?? ""}variant="inline"- isReadOnly={!permissions.can("update", "sales")}+ isReadOnly={!permissions.can("update", "purchasing")}/><ValidatedFormdefaultValues={{ supplierId: routeData?.quote?.supplierId }}diff --git a/apps/carbon/app/modules/sales/ui/Quotes/QuoteExplorer.tsx b/apps/carbon/app/modules/sales/ui/Quotes/QuoteExplorer.tsxindex 34b913fc59..4203988e74 100644--- a/apps/carbon/app/modules/sales/ui/Quotes/QuoteExplorer.tsx+++ b/apps/carbon/app/modules/sales/ui/Quotes/QuoteExplorer.tsx@@ -91,7 +91,8 @@ export default function QuoteExplorer({ methods }: QuoteExplorerProps) {const newQuoteLineDisclosure = useDisclosure();const deleteLineDisclosure = useDisclosure();const [deleteLine, setDeleteLine] = useState<QuotationLine | null>(null);- const isDisabled = quoteData?.quote?.status !== "Draft";+ const isDisabled =+ !permissions.can("delete", "sales") || quoteData?.quote?.status !== "Draft";const onDeleteLine = (line: QuotationLine) => {setDeleteLine(line);diff --git a/packages/database/supabase/migrations/20241206233602_supplier-quote-rls.sql b/packages/database/supabase/migrations/20241206233602_supplier-quote-rls.sqlnew file mode 100644index 0000000000..7263b10501--- /dev/null+++ b/packages/database/supabase/migrations/20241206233602_supplier-quote-rls.sql@@ -0,0 +1,207 @@+ALTER TABLE "supplierQuote" ENABLE ROW LEVEL SECURITY;++CREATE POLICY "Employees with purchasing_view or purchasing_view can view supplier quotes" ON "supplierQuote"+ FOR SELECT+ USING (+ (+ has_company_permission('purchasing_view', "companyId") OR+ has_company_permission('purchasing_view', "companyId") OR+ has_company_permission('parts_view', "companyId")+ ) AND has_role('employee', "companyId")+ );++CREATE POLICY "Employees with purchasing_create can create supplier quotes" ON "supplierQuote"+ FOR INSERT+ WITH CHECK (has_company_permission('purchasing_create', "companyId") AND has_role('employee', "companyId"));+++CREATE POLICY "Employees with purchasing_update can update supplier quotes" ON "supplierQuote"+ FOR UPDATE+ USING (has_company_permission('purchasing_update', "companyId") AND has_role('employee', "companyId"));+++CREATE POLICY "Employees with purchasing_delete can delete supplier quotes" ON "supplierQuote"+ FOR DELETE+ USING (has_company_permission('purchasing_delete', "companyId") AND has_role('employee', "companyId"));++CREATE POLICY "Requests with an API key can access supplier quotes" ON "supplierQuote"+ FOR ALL USING (+ has_valid_api_key_for_company("companyId")+ );+++ALTER TABLE "supplierQuoteLine" ENABLE ROW LEVEL SECURITY;++CREATE POLICY "Employees with purchasing_view can view supplier quote lines" ON "supplierQuoteLine"+ FOR SELECT+ USING (has_company_permission('purchasing_view', "companyId") AND has_role('employee', "companyId"));+++CREATE POLICY "Employees with purchasing_create can create supplier quote lines" ON "supplierQuoteLine"+ FOR INSERT+ WITH CHECK (has_company_permission('purchasing_create', "companyId") AND has_role('employee', "companyId"));++CREATE POLICY "Suppliers with purchasing_create can create lines on their own supplier quote" ON "supplierQuoteLine"+ FOR INSERT+ WITH CHECK (+ has_company_permission('purchasing_create', "companyId")+ AND has_role('supplier', "companyId")+ AND "supplierQuoteId" IN (+ SELECT id FROM "supplierQuote" WHERE "supplierId" IN (+ SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = auth.uid()+ )+ )+ );++CREATE POLICY "Employees with purchasing_update can update supplier quote lines" ON "supplierQuoteLine"+ FOR UPDATE+ USING (has_company_permission('purchasing_update', "companyId") AND has_role('employee', "companyId"));+++CREATE POLICY "Employees with purchasing_delete can delete supplier quote lines" ON "supplierQuoteLine"+ FOR DELETE+ USING (has_company_permission('purchasing_delete', "companyId") AND has_role('employee', "companyId"));+++CREATE POLICY "Requests with an API key can access supplier quotes" ON "supplierQuoteLine"+ FOR ALL USING (+ has_valid_api_key_for_company("companyId")+ );+++ALTER TABLE "supplierQuoteLinePrice" ENABLE ROW LEVEL SECURITY;++CREATE POLICY "Employees with purchasing_view can view supplier quote line pricing" ON "supplierQuoteLinePrice"+ FOR SELECT+ USING (has_company_permission('purchasing_view', get_company_id_from_foreign_key("supplierQuoteId", 'supplierQuote')) AND has_role('employee', get_company_id_from_foreign_key("supplierQuoteId", 'supplierQuote')));++CREATE POLICY "Suppliers with purchasing_view can view their own quote line pricing" ON "supplierQuoteLinePrice"+ FOR SELECT+ USING (+ has_company_permission('purchasing_view', get_company_id_from_foreign_key("supplierQuoteId", 'supplierQuote'))+ AND has_role('supplier', get_company_id_from_foreign_key("supplierQuoteId", 'supplierQuote'))+ AND "supplierQuoteId" IN (+ SELECT id FROM "supplierQuote" WHERE "supplierId" IN (+ SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = auth.uid()+ )+ )+ );++CREATE POLICY "Employees with purchasing_create can insert supplier quote line pricing" ON "supplierQuoteLinePrice"+ FOR INSERT+ WITH CHECK (+ has_role('employee', get_company_id_from_foreign_key("supplierQuoteId", 'supplierQuote')) AND+ has_company_permission('purchasing_create', get_company_id_from_foreign_key("supplierQuoteId", 'supplierQuote'))+ );+++CREATE POLICY "Employees with purchasing_update can update supplier quote line pricing" ON "supplierQuoteLinePrice"+ FOR UPDATE+ USING (+ has_role('employee', get_company_id_from_foreign_key("supplierQuoteId", 'supplierQuote')) AND+ has_company_permission('purchasing_update', get_company_id_from_foreign_key("supplierQuoteId", 'supplierQuote'))+ );+++CREATE POLICY "Employees with purchasing_delete can delete supplier quote line pricing" ON "supplierQuoteLinePrice"+ FOR DELETE+ USING (+ has_role('employee', get_company_id_from_foreign_key("supplierQuoteId", 'supplierQuote')) AND+ has_company_permission('purchasing_delete', get_company_id_from_foreign_key("supplierQuoteId", 'supplierQuote'))+ );++++CREATE POLICY "Requests with an API key can access supplier quote line pricing" ON "supplierQuoteLinePrice"+ FOR ALL USING (+ has_valid_api_key_for_company(get_company_id_from_foreign_key("supplierQuoteId", 'supplierQuote'))+ );+++ALTER TABLE "supplierInteraction" ENABLE ROW LEVEL SECURITY;++CREATE POLICY "Employees with purchasing_view can view supplier interactions" ON "supplierInteraction"+ FOR SELECT+ USING (has_company_permission('purchasing_view', "companyId") AND has_role('employee', "companyId"));++CREATE POLICY "Employees with purchasing_create can create supplier interactions" ON "supplierInteraction"+ FOR INSERT+ WITH CHECK (has_company_permission('purchasing_create', "companyId") AND has_role('employee', "companyId"));++CREATE POLICY "Employees with purchasing_update can update supplier interactions" ON "supplierInteraction"+ FOR UPDATE+ USING (has_company_permission('purchasing_update', "companyId") AND has_role('employee', "companyId"));++CREATE POLICY "Employees with purchasing_delete can delete supplier interactions" ON "supplierInteraction"+ FOR DELETE+ USING (has_company_permission('purchasing_delete', "companyId") AND has_role('employee', "companyId"));++CREATE POLICY "Requests with an API key can access supplier interactions" ON "supplierInteraction"+ FOR ALL USING (+ has_valid_api_key_for_company("companyId")+ );+++CREATE POLICY "Supplier interaction documents view requires purchasing_view" ON storage.objects+FOR SELECT USING (+ bucket_id = 'private'+ AND has_role('employee', (storage.foldername(name))[1])+ AND has_company_permission('purchasing_view', (storage.foldername(name))[1])+ AND (storage.foldername(name))[2] = 'supplier-interaction'+);++CREATE POLICY "Supplier interaction documents insert requires purchasing_create" ON storage.objects+FOR INSERT WITH CHECK (+ bucket_id = 'private'+ AND has_role('employee', (storage.foldername(name))[1])+ AND has_company_permission('purchasing_create', (storage.foldername(name))[1])+ AND (storage.foldername(name))[2] = 'supplier-interaction'+);++CREATE POLICY "Supplier interaction documents update requires purchasing_update" ON storage.objects+FOR UPDATE USING (+ bucket_id = 'private'+ AND has_role('employee', (storage.foldername(name))[1])+ AND has_company_permission('purchasing_update', (storage.foldername(name))[1])+ AND (storage.foldername(name))[2] = 'supplier-interaction'+);++CREATE POLICY "Supplier interaction documents delete requires purchasing_delete" ON storage.objects+FOR DELETE USING (+ bucket_id = 'private'+ AND has_role('employee', (storage.foldername(name))[1])+ AND has_company_permission('purchasing_delete', (storage.foldername(name))[1])+ AND (storage.foldername(name))[2] = 'supplier-interaction'+);++CREATE POLICY "Supplier interaction line document view requires purchasing_view" ON storage.objects+FOR SELECT USING (+ bucket_id = 'private'+ AND has_role('employee', (storage.foldername(name))[1])+ AND has_company_permission('purchasing_view', (storage.foldername(name))[1])+ AND (storage.foldername(name))[2] = 'supplier-interaction-line'+);++CREATE POLICY "Supplier interaction line document insert requires purchasing_create" ON storage.objects+FOR INSERT WITH CHECK (+ bucket_id = 'private'+ AND has_role('employee', (storage.foldername(name))[1])+ AND has_company_permission('purchasing_create', (storage.foldername(name))[1])+ AND (storage.foldername(name))[2] = 'supplier-interaction-line'+);++CREATE POLICY "Supplier interaction line document update requires purchasing_update" ON storage.objects+FOR UPDATE USING (+ bucket_id = 'private'+ AND has_role('employee', (storage.foldername(name))[1])+ AND has_company_permission('purchasing_update', (storage.foldername(name))[1])+ AND (storage.foldername(name))[2] = 'supplier-interaction-line'+);++CREATE POLICY "Supplier interaction line document delete requires purchasing_delete" ON storage.objects+FOR DELETE USING (+ bucket_id = 'private'+ AND has_role('employee', (storage.foldername(name))[1])+ AND has_company_permission('purchasing_delete', (storage.foldername(name))[1])+ AND (storage.foldername(name))[2] = 'supplier-interaction-line'+);diff --git a/packages/database/supabase/migrations/20241207002643_tool-rls.sql b/packages/database/supabase/migrations/20241207002643_tool-rls.sqlnew file mode 100644index 0000000000..d154d221a9--- /dev/null+++ b/packages/database/supabase/migrations/20241207002643_tool-rls.sql@@ -0,0 +1,28 @@+ALTER TABLE "methodOperationTool" ENABLE ROW LEVEL SECURITY;+ALTER TABLE "quoteOperationTool" ENABLE ROW LEVEL SECURITY;+ALTER TABLE "jobOperationTool" ENABLE ROW LEVEL SECURITY;++DROP POLICY IF EXISTS "Employees with production_create can create job operation tools" ON "jobOperationTool";++CREATE POLICY "Employees can view job operation tools" ON "jobOperationTool"+FOR SELECT USING (+ has_role('employee', "companyId")+ AND "companyId" = ANY(+ select "companyId" from "userToCompany" where "userId" = auth.uid()::text+ )+);++CREATE POLICY "Requests with an API key can access job operation tools" ON "jobOperationTool"+ FOR ALL USING (+ has_valid_api_key_for_company("companyId")+ );++CREATE POLICY "Requests with an API key can access method operation tools" ON "methodOperationTool"+ FOR ALL USING (+ has_valid_api_key_for_company("companyId")+ );++CREATE POLICY "Requests with an API key can access quote operation tools" ON "quoteOperationTool"+ FOR ALL USING (+ has_valid_api_key_for_company("companyId")+ );\ No newline at end of file