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.

feat: rls for method operations and materials

Brad Barbin1df23d39895a
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 25627f80e4..878dd9dced 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -7514,6 +7514,7 @@ export type Database = {
companyId: string
createdAt: string
createdBy: string
+ customFields: Json | null
id: string
itemId: string
updatedAt: string | null
@@ -7523,6 +7524,7 @@ export type Database = {
companyId: string
createdAt?: string
createdBy: string
+ customFields?: Json | null
id?: string
itemId: string
updatedAt?: string | null
@@ -7532,6 +7534,7 @@ export type Database = {
companyId?: string
createdAt?: string
createdBy?: string
+ customFields?: Json | null
id?: string
itemId?: string
updatedAt?: string | null
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index 25627f80e4..878dd9dced 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -7514,6 +7514,7 @@ export type Database = {
companyId: string
createdAt: string
createdBy: string
+ customFields: Json | null
id: string
itemId: string
updatedAt: string | null
@@ -7523,6 +7524,7 @@ export type Database = {
companyId: string
createdAt?: string
createdBy: string
+ customFields?: Json | null
id?: string
itemId: string
updatedAt?: string | null
@@ -7532,6 +7534,7 @@ export type Database = {
companyId?: string
createdAt?: string
createdBy?: string
+ customFields?: Json | null
id?: string
itemId?: string
updatedAt?: string | null
diff --git a/packages/database/supabase/migrations/20240621114604_method-rls.sql b/packages/database/supabase/migrations/20240621114604_method-rls.sql
index ca330d92f8..c196da6c5b 100644
--- a/packages/database/supabase/migrations/20240621114604_method-rls.sql
+++ b/packages/database/supabase/migrations/20240621114604_method-rls.sql
@@ -1,12 +1,13 @@
+ALTER TABLE "makeMethod" ADD COLUMN "customFields" JSONB;
ALTER TABLE "methodOperation" ADD COLUMN "customFields" JSONB;
ALTER TABLE "methodMaterial" ADD COLUMN "customFields" JSONB;
INSERT INTO "customFieldTable" ("table", "module", "name")
VALUES
+ ('makeMethod', 'Items', 'Make Method'),
('methodOperation', 'Items', 'Method Operation'),
('methodMaterial', 'Items', 'Method Material');
-
ALTER TABLE "makeMethod" ADD CONSTRAINT "makeMethod_unique_itemId" UNIQUE ("itemId", "companyId");
DROP TRIGGER IF EXISTS create_part_make_method_related_records ON public.part;
@@ -61,4 +62,127 @@ $$ LANGUAGE plpgsql SECURITY DEFINER;
CREATE TRIGGER create_part_make_method_related_records
AFTER INSERT on public."methodOperation"
- FOR EACH ROW EXECUTE PROCEDURE public.create_method_operation_work_instruction();
\ No newline at end of file
+ FOR EACH ROW EXECUTE PROCEDURE public.create_method_operation_work_instruction();
+
+
+ALTER TABLE "makeMethod" ENABLE ROW LEVEL SECURITY;
+
+CREATE POLICY "Employees with parts_view can view make methods" ON "makeMethod"
+ FOR SELECT
+ USING (
+ has_role('employee') AND
+ has_company_permission('parts_view', "companyId")
+ );
+
+CREATE POLICY "Employees with parts_create can create make methods" ON "makeMethod"
+ FOR INSERT
+ WITH CHECK (
+ has_role('employee') AND
+ has_company_permission('parts_create', "companyId")
+ );
+
+CREATE POLICY "Employees with parts_update can update make methods" ON "makeMethod"
+ FOR UPDATE
+ USING (
+ has_role('employee') AND
+ has_company_permission('parts_update', "companyId")
+ );
+
+CREATE POLICY "Employees with parts_delete can delete make methods" ON "makeMethod"
+ FOR DELETE
+ USING (
+ has_role('employee') AND
+ has_company_permission('parts_delete', "companyId")
+ );
+
+ALTER TABLE "methodMaterial" ENABLE ROW LEVEL SECURITY;
+
+CREATE POLICY "Employees with parts_view can view method materials" ON "methodMaterial"
+ FOR SELECT
+ USING (
+ has_role('employee') AND
+ has_company_permission('parts_view', "companyId")
+ );
+
+CREATE POLICY "Employees with parts_create can create method materials" ON "methodMaterial"
+ FOR INSERT
+ WITH CHECK (
+ has_role('employee') AND
+ has_company_permission('parts_create', "companyId")
+ );
+
+CREATE POLICY "Employees with parts_update can update method materials" ON "methodMaterial"
+ FOR UPDATE
+ USING (
+ has_role('employee') AND
+ has_company_permission('parts_update', "companyId")
+ );
+
+CREATE POLICY "Employees with parts_delete can delete method materials" ON "methodMaterial"
+ FOR DELETE
+ USING (
+ has_role('employee') AND
+ has_company_permission('parts_delete', "companyId")
+ );
+
+ALTER TABLE "methodOperation" ENABLE ROW LEVEL SECURITY;
+
+CREATE POLICY "Employees with parts_view can view method operation" ON "methodOperation"
+ FOR SELECT
+ USING (
+ has_role('employee') AND
+ has_company_permission('parts_view', "companyId")
+ );
+
+CREATE POLICY "Employees with parts_create can create method operation" ON "methodOperation"
+ FOR INSERT
+ WITH CHECK (
+ has_role('employee') AND
+ has_company_permission('parts_create', "companyId")
+ );
+
+CREATE POLICY "Employees with parts_update can update method operation" ON "methodOperation"
+ FOR UPDATE
+ USING (
+ has_role('employee') AND
+ has_company_permission('parts_update', "companyId")
+ );
+
+CREATE POLICY "Employees with parts_delete can delete method operation" ON "methodOperation"
+ FOR DELETE
+ USING (
+ has_role('employee') AND
+ has_company_permission('parts_delete', "companyId")
+ );
+
+ALTER TABLE "methodOperationWorkInstruction" ENABLE ROW LEVEL SECURITY;
+
+CREATE POLICY "Employees can view work instructions" ON "methodOperationWorkInstruction"
+ FOR SELECT
+ USING (
+ has_role('employee') AND
+ "companyId" = ANY(
+ select "companyId" from "userToCompany" where "userId" = auth.uid()::text
+ )
+ );
+
+CREATE POLICY "Employees with parts_create can create work instructions" ON "methodOperationWorkInstruction"
+ FOR INSERT
+ WITH CHECK (
+ has_role('employee') AND
+ has_company_permission('parts_create', "companyId")
+ );
+
+CREATE POLICY "Employees with parts_update can update work instructions" ON "methodOperationWorkInstruction"
+ FOR UPDATE
+ USING (
+ has_role('employee') AND
+ has_company_permission('parts_update', "companyId")
+ );
+
+CREATE POLICY "Employees with parts_delete can delete work instructions" ON "methodOperationWorkInstruction"
+ FOR DELETE
+ USING (
+ has_role('employee') AND
+ has_company_permission('parts_delete', "companyId")
+ );
\ No newline at end of file