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.

wip

Brad Barbin04086fcbc4b9
diff --git a/apps/carbon/app/modules/accounting/accounting.service.ts b/apps/carbon/app/modules/accounting/accounting.service.ts
index ddfbdd8f64..3d920517b9 100644
--- a/apps/carbon/app/modules/accounting/accounting.service.ts
+++ b/apps/carbon/app/modules/accounting/accounting.service.ts
@@ -196,12 +196,14 @@ export async function getAccountCategoriesList(
export async function getAccountCategory(
client: SupabaseClient<Database>,
- accountCategoryId: string
+ accountCategoryId: string,
+ companyId: string
) {
return client
.from("accountCategory")
.select("*")
.eq("id", accountCategoryId)
+ .eq("companyId", companyId)
.single();
}
@@ -232,12 +234,14 @@ export async function getAccountSubcategories(
export async function getAccountSubcategoriesByCategory(
client: SupabaseClient<Database>,
- accountCategoryId: string
+ accountCategoryId: string,
+ companyId: string
) {
return client
.from("accountSubcategory")
.select("*")
.eq("accountCategoryId", accountCategoryId)
+ .eq("companyId", companyId)
.eq("active", true);
}
diff --git a/apps/carbon/app/routes/api+/accounting.subcategories.ts b/apps/carbon/app/routes/api+/accounting.subcategories.ts
index 2f2aaa9efe..420e9eeeb3 100644
--- a/apps/carbon/app/routes/api+/accounting.subcategories.ts
+++ b/apps/carbon/app/routes/api+/accounting.subcategories.ts
@@ -6,7 +6,7 @@ import { json } from "@vercel/remix";
import { getAccountSubcategoriesByCategory } from "~/modules/accounting";
export async function loader({ request }: LoaderFunctionArgs) {
- const { client } = await requirePermissions(request, {
+ const { client, companyId } = await requirePermissions(request, {
view: "accounting",
});
@@ -21,7 +21,8 @@ export async function loader({ request }: LoaderFunctionArgs) {
const subcategories = await getAccountSubcategoriesByCategory(
client,
- accountCategoryId
+ accountCategoryId,
+ companyId
);
if (subcategories.error) {
return json(
diff --git a/apps/carbon/app/routes/x+/accounting+/categories.list.$categoryId.tsx b/apps/carbon/app/routes/x+/accounting+/categories.list.$categoryId.tsx
index 240d774c6e..0dcaac7fb9 100644
--- a/apps/carbon/app/routes/x+/accounting+/categories.list.$categoryId.tsx
+++ b/apps/carbon/app/routes/x+/accounting+/categories.list.$categoryId.tsx
@@ -13,7 +13,7 @@ import { AccountCategoryDetail } from "~/modules/accounting/ui/AccountCategories
import { path } from "~/utils/path";
export async function loader({ request, params }: LoaderFunctionArgs) {
- const { client } = await requirePermissions(request, {
+ const { client, companyId } = await requirePermissions(request, {
view: "accounting",
role: "employee",
});
@@ -22,8 +22,8 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
if (!categoryId) throw notFound("Invalid categoryId");
const [accountCategory, accountSubcategories] = await Promise.all([
- getAccountCategory(client, categoryId),
- getAccountSubcategoriesByCategory(client, categoryId),
+ getAccountCategory(client, categoryId, companyId),
+ getAccountSubcategoriesByCategory(client, categoryId, companyId),
]);
if (accountCategory.error) {
throw redirect(
diff --git a/apps/carbon/app/routes/x+/accounting+/charts.tsx b/apps/carbon/app/routes/x+/accounting+/charts.tsx
index 0e662d40bc..8af74504e9 100644
--- a/apps/carbon/app/routes/x+/accounting+/charts.tsx
+++ b/apps/carbon/app/routes/x+/accounting+/charts.tsx
@@ -20,6 +20,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "accounting",
role: "employee",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/inventory+/quantities.tsx b/apps/carbon/app/routes/x+/inventory+/quantities.tsx
index 42b78ac652..5da181851b 100644
--- a/apps/carbon/app/routes/x+/inventory+/quantities.tsx
+++ b/apps/carbon/app/routes/x+/inventory+/quantities.tsx
@@ -28,6 +28,7 @@ import { getGenericQueryFilters } from "~/utils/query";
export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId, userId } = await requirePermissions(request, {
view: "inventory",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/items+/consumables.tsx b/apps/carbon/app/routes/x+/items+/consumables.tsx
index 87d30d29f6..8dabf4ad9e 100644
--- a/apps/carbon/app/routes/x+/items+/consumables.tsx
+++ b/apps/carbon/app/routes/x+/items+/consumables.tsx
@@ -20,6 +20,7 @@ export const handle: Handle = {
export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "parts",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/items+/materials.tsx b/apps/carbon/app/routes/x+/items+/materials.tsx
index 8716248d58..50d660d301 100644
--- a/apps/carbon/app/routes/x+/items+/materials.tsx
+++ b/apps/carbon/app/routes/x+/items+/materials.tsx
@@ -20,6 +20,7 @@ export const handle: Handle = {
export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "parts",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/items+/parts.tsx b/apps/carbon/app/routes/x+/items+/parts.tsx
index dd89c68f5f..35712af9d8 100644
--- a/apps/carbon/app/routes/x+/items+/parts.tsx
+++ b/apps/carbon/app/routes/x+/items+/parts.tsx
@@ -20,6 +20,7 @@ export const handle: Handle = {
export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "parts",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/items+/tools.tsx b/apps/carbon/app/routes/x+/items+/tools.tsx
index 323c3c59c1..2cf9261242 100644
--- a/apps/carbon/app/routes/x+/items+/tools.tsx
+++ b/apps/carbon/app/routes/x+/items+/tools.tsx
@@ -20,6 +20,7 @@ export const handle: Handle = {
export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "parts",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/people+/departments.tsx b/apps/carbon/app/routes/x+/people+/departments.tsx
index e55173b38b..a925cc9977 100644
--- a/apps/carbon/app/routes/x+/people+/departments.tsx
+++ b/apps/carbon/app/routes/x+/people+/departments.tsx
@@ -20,6 +20,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "people",
role: "employee",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/people+/holidays.tsx b/apps/carbon/app/routes/x+/people+/holidays.tsx
index 3bd72f2c6b..aa5768ff61 100644
--- a/apps/carbon/app/routes/x+/people+/holidays.tsx
+++ b/apps/carbon/app/routes/x+/people+/holidays.tsx
@@ -20,6 +20,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "people",
role: "employee",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/people+/people.tsx b/apps/carbon/app/routes/x+/people+/people.tsx
index 1d7fa386fa..0734ddaef2 100644
--- a/apps/carbon/app/routes/x+/people+/people.tsx
+++ b/apps/carbon/app/routes/x+/people+/people.tsx
@@ -15,6 +15,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "people",
role: "employee",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/people+/shifts.tsx b/apps/carbon/app/routes/x+/people+/shifts.tsx
index ef32a65abb..ddbe1eb2f0 100644
--- a/apps/carbon/app/routes/x+/people+/shifts.tsx
+++ b/apps/carbon/app/routes/x+/people+/shifts.tsx
@@ -21,6 +21,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "people",
role: "employee",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/production+/jobs.tsx b/apps/carbon/app/routes/x+/production+/jobs.tsx
index f045859b32..3a9d3f1994 100644
--- a/apps/carbon/app/routes/x+/production+/jobs.tsx
+++ b/apps/carbon/app/routes/x+/production+/jobs.tsx
@@ -22,6 +22,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "production",
role: "employee",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/purchasing+/orders.tsx b/apps/carbon/app/routes/x+/purchasing+/orders.tsx
index 9a7cbfb8fd..2b0979124f 100644
--- a/apps/carbon/app/routes/x+/purchasing+/orders.tsx
+++ b/apps/carbon/app/routes/x+/purchasing+/orders.tsx
@@ -19,6 +19,7 @@ export const handle: Handle = {
export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "purchasing",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/purchasing+/quotes.tsx b/apps/carbon/app/routes/x+/purchasing+/quotes.tsx
index 55c23ee158..0f6d7e72f9 100644
--- a/apps/carbon/app/routes/x+/purchasing+/quotes.tsx
+++ b/apps/carbon/app/routes/x+/purchasing+/quotes.tsx
@@ -20,6 +20,7 @@ export const handle: Handle = {
export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "purchasing",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/purchasing+/suppliers.tsx b/apps/carbon/app/routes/x+/purchasing+/suppliers.tsx
index 8e797b9c51..e8f694d49b 100644
--- a/apps/carbon/app/routes/x+/purchasing+/suppliers.tsx
+++ b/apps/carbon/app/routes/x+/purchasing+/suppliers.tsx
@@ -19,6 +19,7 @@ export const handle: Handle = {
export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "purchasing",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/quote+/$quoteId.finalize.tsx b/apps/carbon/app/routes/x+/quote+/$quoteId.finalize.tsx
index 31a8c78810..2eae60d122 100644
--- a/apps/carbon/app/routes/x+/quote+/$quoteId.finalize.tsx
+++ b/apps/carbon/app/routes/x+/quote+/$quoteId.finalize.tsx
@@ -33,6 +33,7 @@ export async function action(args: ActionFunctionArgs) {
const { client, companyId, userId } = await requirePermissions(request, {
create: "sales",
role: "employee",
+ bypassRls: true,
});
const { quoteId } = params;
diff --git a/apps/carbon/app/routes/x+/resources+/abilities.tsx b/apps/carbon/app/routes/x+/resources+/abilities.tsx
index 324a9b426d..025c750ac9 100644
--- a/apps/carbon/app/routes/x+/resources+/abilities.tsx
+++ b/apps/carbon/app/routes/x+/resources+/abilities.tsx
@@ -19,6 +19,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "resources",
role: "employee",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/resources+/contractors.tsx b/apps/carbon/app/routes/x+/resources+/contractors.tsx
index 6f807d6663..068fc04a04 100644
--- a/apps/carbon/app/routes/x+/resources+/contractors.tsx
+++ b/apps/carbon/app/routes/x+/resources+/contractors.tsx
@@ -23,6 +23,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "resources",
role: "employee",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/resources+/locations.tsx b/apps/carbon/app/routes/x+/resources+/locations.tsx
index 13e06c2206..0b1a84b6cd 100644
--- a/apps/carbon/app/routes/x+/resources+/locations.tsx
+++ b/apps/carbon/app/routes/x+/resources+/locations.tsx
@@ -19,6 +19,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "resources",
role: "employee",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/resources+/partners.tsx b/apps/carbon/app/routes/x+/resources+/partners.tsx
index c5df7314e3..04d47a8a6e 100644
--- a/apps/carbon/app/routes/x+/resources+/partners.tsx
+++ b/apps/carbon/app/routes/x+/resources+/partners.tsx
@@ -23,6 +23,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "resources",
role: "employee",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/resources+/processes.tsx b/apps/carbon/app/routes/x+/resources+/processes.tsx
index 5f6f0a46cf..112691c0e6 100644
--- a/apps/carbon/app/routes/x+/resources+/processes.tsx
+++ b/apps/carbon/app/routes/x+/resources+/processes.tsx
@@ -19,6 +19,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "resources",
role: "employee",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/resources+/work-centers.tsx b/apps/carbon/app/routes/x+/resources+/work-centers.tsx
index b3bd9b5fec..ef9276b443 100644
--- a/apps/carbon/app/routes/x+/resources+/work-centers.tsx
+++ b/apps/carbon/app/routes/x+/resources+/work-centers.tsx
@@ -23,6 +23,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "resources",
role: "employee",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/sales+/customers.tsx b/apps/carbon/app/routes/x+/sales+/customers.tsx
index 97e4e4359f..e25dbaabb9 100644
--- a/apps/carbon/app/routes/x+/sales+/customers.tsx
+++ b/apps/carbon/app/routes/x+/sales+/customers.tsx
@@ -19,6 +19,7 @@ export const handle: Handle = {
export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "sales",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/sales+/orders.tsx b/apps/carbon/app/routes/x+/sales+/orders.tsx
index a516d3721b..c4ae22b8cd 100644
--- a/apps/carbon/app/routes/x+/sales+/orders.tsx
+++ b/apps/carbon/app/routes/x+/sales+/orders.tsx
@@ -19,6 +19,7 @@ export const handle: Handle = {
export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "sales",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/sales+/quotes.tsx b/apps/carbon/app/routes/x+/sales+/quotes.tsx
index 0471cf3f5a..face43bb09 100644
--- a/apps/carbon/app/routes/x+/sales+/quotes.tsx
+++ b/apps/carbon/app/routes/x+/sales+/quotes.tsx
@@ -19,6 +19,7 @@ export const handle: Handle = {
export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "sales",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/sales+/rfqs.tsx b/apps/carbon/app/routes/x+/sales+/rfqs.tsx
index ca168ed7f6..7ece564303 100644
--- a/apps/carbon/app/routes/x+/sales+/rfqs.tsx
+++ b/apps/carbon/app/routes/x+/sales+/rfqs.tsx
@@ -19,6 +19,7 @@ export const handle: Handle = {
export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "sales",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/schedule+/index.tsx b/apps/carbon/app/routes/x+/schedule+/index.tsx
index c86630bf6b..c8019e9a6f 100644
--- a/apps/carbon/app/routes/x+/schedule+/index.tsx
+++ b/apps/carbon/app/routes/x+/schedule+/index.tsx
@@ -55,6 +55,7 @@ export const handle: Handle = {
export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId, userId } = await requirePermissions(request, {
view: "production",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/users+/customers.tsx b/apps/carbon/app/routes/x+/users+/customers.tsx
index 311c9f94b0..d680e44d91 100644
--- a/apps/carbon/app/routes/x+/users+/customers.tsx
+++ b/apps/carbon/app/routes/x+/users+/customers.tsx
@@ -19,6 +19,7 @@ export const handle: Handle = {
export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "users",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/users+/employees.tsx b/apps/carbon/app/routes/x+/users+/employees.tsx
index 3f107b5a5b..8f3d4904ee 100644
--- a/apps/carbon/app/routes/x+/users+/employees.tsx
+++ b/apps/carbon/app/routes/x+/users+/employees.tsx
@@ -23,6 +23,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "users",
role: "employee",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/apps/carbon/app/routes/x+/users+/suppliers.tsx b/apps/carbon/app/routes/x+/users+/suppliers.tsx
index 4058bc05bd..e29af8aa63 100644
--- a/apps/carbon/app/routes/x+/users+/suppliers.tsx
+++ b/apps/carbon/app/routes/x+/users+/suppliers.tsx
@@ -19,6 +19,7 @@ export const handle: Handle = {
export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "users",
+ bypassRls: true,
});
const url = new URL(request.url);
diff --git a/packages/auth/src/services/auth.server.ts b/packages/auth/src/services/auth.server.ts
index fe32900b7a..f63fe16d46 100644
--- a/packages/auth/src/services/auth.server.ts
+++ b/packages/auth/src/services/auth.server.ts
@@ -93,6 +93,7 @@ export async function requirePermissions(
update?: string | string[];
delete?: string | string[];
role?: string;
+ bypassRls?: boolean;
}
): Promise<{
client: SupabaseClient<Database>;
@@ -104,13 +105,21 @@ export async function requirePermissions(
request
);
- const client = getCarbon(accessToken);
- // early exit if no requiredPermissions are required
- if (Object.keys(requiredPermissions).length === 0)
- return { client, companyId, email, userId };
-
const myClaims = await getUserClaims(userId, companyId);
+ // early exit if no requiredPermissions are required
+ if (Object.keys(requiredPermissions).length === 0) {
+ return {
+ client:
+ requiredPermissions.bypassRls && myClaims.role === "employee"
+ ? getCarbonServiceRole()
+ : getCarbon(accessToken),
+ companyId,
+ email,
+ userId,
+ };
+ }
+
const hasRequiredPermissions = Object.entries(requiredPermissions).every(
([action, permission]) => {
if (typeof permission === "string") {
@@ -156,7 +165,15 @@ export async function requirePermissions(
);
}
- return { client, companyId, email, userId };
+ return {
+ client:
+ requiredPermissions.bypassRls && myClaims.role === "employee"
+ ? getCarbonServiceRole()
+ : getCarbon(accessToken),
+ companyId,
+ email,
+ userId,
+ };
}
export async function resetPassword(accessToken: string, password: string) {
diff --git a/packages/database/supabase/migrations/20241210140215_rls-performance.sql b/packages/database/supabase/migrations/20241210140215_rls-performance.sql
new file mode 100644
index 0000000000..73c622f3c3
--- /dev/null
+++ b/packages/database/supabase/migrations/20241210140215_rls-performance.sql
@@ -0,0 +1,1181 @@
+CREATE OR REPLACE FUNCTION has_role(required_role text, company text) RETURNS "bool"
+ LANGUAGE "plpgsql" SECURITY DEFINER SET search_path = public
+ AS $$
+ DECLARE
+ user_role text;
+ BEGIN
+ SELECT role INTO user_role FROM public."userToCompany" WHERE "userId" = (SELECT auth.uid()::text) AND "companyId" = company;
+ return user_role = required_role;
+ END;
+$$;
+
+CREATE OR REPLACE FUNCTION has_company_permission(claim text, company text) RETURNS "bool"
+ LANGUAGE "plpgsql" SECURITY DEFINER SET search_path = public
+ AS $$
+ DECLARE
+ permission_value text[];
+ BEGIN
+ -- TODO: (current_setting('request.jwt.claims', true)::jsonb)->'app_metadata'->claim
+ SELECT jsonb_to_text_array(coalesce(permissions->claim, '[]')) INTO permission_value FROM public."userPermission" WHERE id = (SELECT auth.uid()::text);
+ IF permission_value IS NULL THEN
+ return false;
+ ELSIF '0' = ANY(permission_value::text[]) THEN
+ return true;
+ ELSIF company = ANY(permission_value::text[]) THEN
+ return true;
+ ELSE
+ return false;
+ END IF;
+ END;
+$$;
+
+ALTER POLICY "Authenticated users can view company" ON "company"
+ USING (
+ (SELECT auth.role()) = 'authenticated' AND "id" IN (
+ SELECT "companyId" FROM "userToCompany" WHERE "userId" = (SELECT auth.uid()::text)
+ )
+ );
+
+
+ALTER POLICY "Users can modify themselves" ON "user" USING (auth.uid() = id::uuid);
+ALTER POLICY "Users can view other users from their same company" ON "user" USING (
+ "id" IN (
+ SELECT "userId" FROM "userToCompany" WHERE "companyId" IN (
+ SELECT "companyId" FROM "userToCompany" WHERE "userId" = (SELECT auth.uid()::text)
+ )
+ )
+);
+
+DROP POLICY "Users can view their own permissions" ON "userPermission";
+ALTER POLICY "Users with users_update can view permissions of other users in their company" ON "userPermission" USING (
+ "id" IN (
+ SELECT "userId" FROM "userToCompany" WHERE "companyId" IN (
+ SELECT "companyId" FROM "userToCompany" WHERE "userId" = (SELECT auth.uid()::text)
+ )
+ )
+);
+
+ALTER POLICY "Employees can view employees from their company" ON "employee" USING (
+ "companyId" IN (
+ SELECT "companyId" FROM "userToCompany" WHERE "userId" = (SELECT auth.uid()::text)
+ )
+);
+
+
+ALTER POLICY "Users can delete their own avatars" ON storage.objects
+USING (
+ bucket_id = 'avatars'
+ AND (auth.role() = 'authenticated')
+ AND storage.filename(name) LIKE concat((SELECT auth.uid()::text), '%')
+);
+
+ALTER POLICY "Users can update their own avatars" ON storage.objects
+USING (
+ bucket_id = 'avatars'
+ AND (auth.role() = 'authenticated')
+ AND storage.filename(name) LIKE concat((SELECT auth.uid()::text), '%')
+);
+
+ALTER POLICY "Users can insert their own avatars" ON storage.objects
+WITH CHECK (
+ bucket_id = 'avatars'
+ AND (auth.role() = 'authenticated')
+ AND storage.filename(name) LIKE concat((SELECT auth.uid()::text), '%')
+);
+
+ALTER POLICY "Users can insert attributes for themselves" ON "userAttributeValue" WITH CHECK ((SELECT auth.uid()) = "userId"::uuid);
+ALTER POLICY "Users can modify attributes for themselves" ON "userAttributeValue" WITH CHECK ((SELECT auth.uid()) = "userId"::uuid);
+ALTER POLICY "Users can view their own attributes" ON "userAttributeValue" USING ((SELECT auth.uid()) = "userId"::uuid);
+
+
+
+
+ALTER POLICY "Suppliers with purchasing_view can view contacts from their organization" ON "contact"
+ USING (
+ has_role('supplier', "companyId")
+ AND has_company_permission('sales_view', "companyId")
+ AND (
+ id IN (
+ SELECT "contactId" FROM "customerContact" WHERE "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Customers with sales_view can view contacts from their organization" ON "contact"
+ USING (
+ has_role('customer', "companyId")
+ AND has_company_permission('sales_view', "companyId")
+ AND (
+ id IN (
+ SELECT "contactId" FROM "customerContact" WHERE "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+
+
+ALTER POLICY "Suppliers with purchasing_create can create contacts from their organization" ON "contact"
+ WITH CHECK (
+ has_role('supplier', "companyId")
+ AND has_company_permission('purchasing_create', "companyId")
+ AND (
+ id IN (
+ SELECT "contactId" FROM "supplierContact" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "supplierAccount" sa WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Customers with sales_create can create contacts from their organization" ON "contact"
+ WITH CHECK (
+ has_role('customer', "companyId")
+ AND has_company_permission('sales_create', "companyId")
+ AND (
+ id IN (
+ SELECT "contactId" FROM "customerContact" WHERE "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+
+ALTER POLICY "Suppliers with purchasing_update can update contacts from their organization" ON "contact"
+ USING (
+ has_role('supplier', "companyId")
+ AND has_company_permission('purchasing_update', "companyId")
+ AND (
+ id IN (
+ SELECT "contactId" FROM "supplierContact" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "supplierAccount" sa WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+
+
+ALTER POLICY "Customers with sales_update can update contacts from their organization" ON "contact"
+ USING (
+ has_role('customer', "companyId")
+ AND has_company_permission('sales_update', "companyId")
+ AND (
+ id IN (
+ SELECT "contactId" FROM "customerContact" WHERE "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" sa WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+
+ALTER POLICY "Suppliers with purchasing_delete can delete contacts from their organization" ON "contact"
+ USING (
+ has_role('supplier', "companyId")
+ AND has_company_permission('purchasing_delete', "companyId")
+ AND (
+ id IN (
+ SELECT "contactId" FROM "supplierContact" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "supplierAccount" sa WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+
+
+ALTER POLICY "Customers with sales_delete can delete contacts from their organization" ON "contact"
+ USING (
+ has_role('customer', "companyId")
+ AND has_company_permission('sales_delete', "companyId")
+ AND (
+ id IN (
+ SELECT "contactId" FROM "customerContact" WHERE "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" sa WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+
+
+ALTER POLICY "Customers with sales_view can their own organization" ON "customer"
+ USING (
+ has_role('customer', "companyId")
+ AND has_company_permission('sales_view', "companyId")
+ AND id IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ );
+
+
+ALTER POLICY "Customers with sales_update can update their own organization" ON "customer"
+ USING (
+ has_role('customer', "companyId")
+ AND has_company_permission('sales_update', "companyId")
+ AND id IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ );
+
+
+ALTER POLICY "Customers with sales_view can their own customer contacts" ON "customerContact"
+ USING (
+ has_role('customer', get_company_id_from_foreign_key("customerId", 'customer')) AND
+ has_company_permission('sales_view', get_company_id_from_foreign_key("customerId", 'customer'))
+ AND "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ );
+
+
+ALTER POLICY "Customers with sales_create can create customer contacts" ON "customerContact"
+ WITH CHECK (
+ has_role('customer', get_company_id_from_foreign_key("customerId", 'customer')) AND
+ has_company_permission('sales_create', get_company_id_from_foreign_key("customerId", 'customer'))
+ AND "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ );
+
+ALTER POLICY "Customers with sales_update can update their customer contacts" ON "customerContact"
+ USING (
+ has_role('customer', get_company_id_from_foreign_key("customerId", 'customer'))
+ AND has_company_permission('sales_update', get_company_id_from_foreign_key("customerId", 'customer'))
+ AND "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ );
+
+
+ALTER POLICY "Suppliers with purchasing_view can their own organization" ON "supplier"
+ USING (
+ has_role('supplier', "companyId")
+ AND has_company_permission('purchasing_view', "companyId")
+ AND id IN (
+ SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ );
+
+
+ALTER POLICY "Suppliers with purchasing_update can update their own organization" ON "supplier"
+ USING (
+ has_role('supplier', "companyId")
+ AND has_company_permission('purchasing_update', "companyId")
+ AND id IN (
+ SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ );
+
+
+ALTER POLICY "Suppliers with purchasing_view can their own supplier contacts" ON "supplierContact"
+ USING (
+ has_role('supplier', get_company_id_from_foreign_key("supplierId", 'supplier')) AND
+ has_company_permission('purchasing_view', get_company_id_from_foreign_key("supplierId", 'supplier'))
+ AND "supplierId" IN (
+ SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ );
+
+ALTER POLICY "Anyone can read public buckets"
+ON storage.objects USING (
+ bucket_id = 'public'
+ AND ((SELECT auth.role()) = 'authenticated')
+);
+
+ALTER POLICY "Anyone with settings_create can insert into the public bucket"
+ON storage.objects WITH CHECK (
+ bucket_id = 'public'
+ AND (
+ (storage.foldername(name))[1] = ANY(
+ get_permission_companies('settings_create')
+ )
+ )
+);
+
+ALTER POLICY "Anyone with settings_update can update the public bucket"
+ON storage.objects USING (
+ bucket_id = 'public'
+ AND (
+ (storage.foldername(name))[1] = ANY(
+ get_permission_companies('settings_update')
+ )
+ )
+);
+
+ALTER POLICY "Anyone with settings_delete can delete from public bucket"
+ON storage.objects USING (
+ bucket_id = 'public'
+ AND (
+ (storage.foldername(name))[1] = ANY(
+ get_permission_companies('settings_delete')
+ )
+ )
+);
+
+
+ALTER POLICY "Employees can view notes" ON "note"
+ USING (
+ has_role('employee', "companyId")
+ AND "companyId" = ANY(
+ SELECT "companyId" from "userToCompany" where "userId" = (SELECT auth.uid())::text
+ )
+ );
+
+ALTER POLICY "Employees can insert notes" ON "note"
+ WITH CHECK (
+ has_role('employee', "companyId")
+ AND "companyId" = ANY(
+ SELECT "companyId" from "userToCompany" where "userId" = (SELECT auth.uid())::text
+ )
+);
+
+ALTER POLICY "Employees can update their own notes" ON "note"
+ USING (
+ has_role('employee', "companyId")
+ AND "createdBy"::uuid = (SELECT auth.uid())
+ );
+
+ALTER POLICY "Employees can delete their own notes" ON "note"
+ USING (
+ has_role('employee', "companyId")
+ AND "createdBy"::uuid = (SELECT auth.uid())
+ );
+
+ALTER POLICY "Employees can view locations for their companies" ON "location"
+ USING (
+ has_role('employee', "companyId")
+ AND "companyId" = ANY(
+ select "companyId" from "userToCompany" where "userId" = (SELECT auth.uid())::text
+ )
+ );
+
+ALTER POLICY "Employees can view shifts" ON "shift"
+ USING (
+ has_role('employee', "companyId") AND
+ "companyId" = ANY(
+ select "companyId" from "userToCompany" where "userId" = (SELECT auth.uid())::text
+ )
+ );
+
+ALTER POLICY "Employees can view employee jobs" ON "employeeJob"
+ USING (
+ has_role('employee', "companyId") AND
+ "companyId" = ANY(
+ SELECT "companyId" from "userToCompany" where "userId" = (SELECT auth.uid())::text
+ )
+ );
+
+ALTER POLICY "Employees can view departments" ON "department"
+ USING (
+ has_role('employee', "companyId") AND
+ "companyId" = ANY(
+ select "companyId" from "userToCompany" where "userId" = (SELECT auth.uid())::text
+ )
+ );
+
+ALTER POLICY "Employees can view holidays" ON "holiday"
+ USING (
+ has_role('employee', "companyId") AND
+ "companyId" = ANY(
+ select "companyId" from "userToCompany" where "userId" = (SELECT auth.uid())::text
+ )
+ );
+
+ALTER POLICY "Authenticated users can view currencies" ON "currency"
+ USING (
+ (SELECT auth.role()) = 'authenticated'
+ );
+
+DROP POLICY "Authenticated users can view units of measure" ON "unitOfMeasure";
+CREATE POLICY "Employees can view units of measure" ON "unitOfMeasure" FOR SELECT
+ USING (
+ "companyId" = ANY(
+ select "companyId" from "userToCompany" where "userId" = (SELECT auth.uid())::text
+ )
+ );
+
+ALTER POLICY "Employees can view items" ON "item"
+ USING (
+ has_role('employee', "companyId")
+ AND "companyId" = ANY(
+ select "companyId" from "userToCompany" where "userId" = (SELECT auth.uid())::text
+ )
+ );
+
+ALTER POLICY "Employees can view parts" ON "part"
+ USING (
+ has_role('employee', "companyId")
+ AND "companyId" = ANY(
+ select "companyId" from "userToCompany" where "userId" = (SELECT auth.uid())::text
+ )
+ );
+
+ALTER POLICY "Suppliers with parts_view can view their own part suppliers" ON "supplierPart"
+ USING (
+ has_role('supplier', "companyId") AND
+ has_company_permission('parts_view', "companyId") AND
+ "supplierId" IN (
+ SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ );
+
+ALTER POLICY "Suppliers with parts_view can view items they created or supply" ON "item"
+ USING (
+ has_role('supplier', "companyId") AND
+ has_company_permission('parts_view', "companyId")
+ AND (
+ "createdBy" = (SELECT auth.uid())::text
+ OR (
+ id IN (
+ SELECT "itemId" FROM "supplierPart" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Suppliers with parts_update can update parts that they created or supply" ON "item"
+ WITH CHECK (
+ has_role('supplier', "companyId") AND
+ has_company_permission('parts_create', "companyId")
+ AND (
+ "createdBy" = (SELECT auth.uid())::text
+ OR (
+ id IN (
+ SELECT "itemId" FROM "supplierPart" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Suppliers with parts_delete can delete items that they created or supply" ON "item"
+ USING (
+ has_role('supplier', "companyId") AND
+ has_company_permission('parts_create', "companyId")
+ AND (
+ "createdBy" = (SELECT auth.uid())::text
+ OR (
+ id IN (
+ SELECT "itemId" FROM "supplierPart" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Suppliers with parts_view can view parts they created or supply" ON "part"
+ USING (
+ has_role('supplier', "companyId") AND
+ has_company_permission('parts_view', "companyId")
+ AND (
+ "createdBy" = (SELECT auth.uid())::text
+ OR (
+ "itemId" IN (
+ SELECT "itemId" FROM "supplierPart" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Supliers with parts_create can insert parts" ON "part"
+ WITH CHECK (
+ has_role('supplier', "companyId") AND
+ has_company_permission('parts_create', "companyId")
+ );
+
+ALTER POLICY "Suppliers with parts_update can update parts that they created or supply" ON "part"
+ WITH CHECK (
+ has_role('supplier', "companyId") AND
+ has_company_permission('parts_create', "companyId")
+ AND (
+ "createdBy" = (SELECT auth.uid())::text
+ OR (
+ "itemId" IN (
+ SELECT "itemId" FROM "supplierPart" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Suppliers with parts_delete can delete parts that they created or supply" ON "part"
+ USING (
+ has_role('supplier', "companyId") AND
+ has_company_permission('parts_create', "companyId")
+ AND (
+ "createdBy" = (SELECT auth.uid())::text
+ OR (
+ "itemId" IN (
+ SELECT "itemId" FROM "supplierPart" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Suppliers with parts_view can view item costs they supply" ON "itemCost"
+ USING (
+ has_role('supplier', "companyId") AND
+ has_company_permission('parts_view', "companyId")
+ AND (
+ "itemId" IN (
+ SELECT "itemId" FROM "supplierPart" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Suppliers with parts_update can update parts costs that they supply" ON "itemCost"
+ WITH CHECK (
+ has_role('supplier', "companyId") AND
+ has_company_permission('parts_update', "companyId")
+ AND (
+ "itemId" IN (
+ SELECT "itemId" FROM "supplierPart" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Suppliers with parts_view can view part replenishment they supply" ON "itemReplenishment"
+ USING (
+ has_role('supplier', "companyId") AND
+ has_company_permission('parts_view', "companyId")
+ AND (
+ "itemId" IN (
+ SELECT "itemId" FROM "supplierPart" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Suppliers with parts_update can update parts replenishments that they supply" ON "itemReplenishment"
+ WITH CHECK (
+ has_role('supplier', "companyId") AND
+ has_company_permission('parts_update', "companyId")
+ AND (
+ "itemId" IN (
+ SELECT "itemId" FROM "supplierPart" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+
+ALTER POLICY "Employees can view warehouses" ON "warehouse"
+ USING (
+ has_role('employee', "companyId")
+ AND "companyId" = ANY(
+ select "companyId" from "userToCompany" where "userId" = (SELECT auth.uid())::text
+ )
+ );
+
+ALTER POLICY "Authenticated users can view shelves" ON "shelf"
+ USING (
+ has_role('employee', "companyId")
+ AND "companyId" = ANY(
+ select "companyId" from "userToCompany" where "userId" = (SELECT auth.uid())::text
+ )
+ );
+
+ALTER POLICY "Authenticated users can view shelves" ON "shelf"
+ USING (
+ has_role('employee', "companyId")
+ AND "companyId" = ANY(
+ select "companyId" from "userToCompany" where "userId" = (SELECT auth.uid())::text
+ )
+ );
+
+
+
+ALTER POLICY "Users with documents can view documents where they are in the readGroups" ON "document"
+ USING (
+ has_company_permission('documents_view', "companyId") AND
+ ((SELECT(groups_for_user((SELECT auth.uid())::text)) && "readGroups") = true)
+ );
+
+ALTER POLICY "Users with documents_create can create documents where they are in the writeGroups" ON "document"
+ WITH CHECK (
+ has_company_permission('documents_create', "companyId") AND
+ ((SELECT(groups_for_user((SELECT auth.uid())::text)) && "writeGroups") = true)
+ );
+
+ALTER POLICY "Users with documents_update can update documents where they are in the writeGroups" ON "document"
+ USING (
+ has_company_permission('documents_update', "companyId") AND
+ ((SELECT(groups_for_user((SELECT auth.uid())::text)) && "writeGroups") = true)
+ );
+
+ALTER POLICY "Users with documents_delete can delete documents where they are in the writeGroups" ON "document"
+ USING (
+ has_company_permission('documents_delete', "companyId") AND
+ ((SELECT(groups_for_user((SELECT auth.uid())::text)) && "writeGroups") = true)
+ );
+
+ALTER POLICY "Private buckets view requires ownership or document.readGroups" ON storage.objects
+ USING (
+ bucket_id = 'private'
+ AND ((SELECT auth.role()) = 'authenticated')
+ AND (
+ (storage.foldername(name))[1] = (SELECT auth.uid())::text
+ OR "name" IN (
+ SELECT "path" FROM public.document WHERE ((SELECT(groups_for_user((SELECT auth.uid())::text)) && "readGroups") = true)
+ )
+ )
+ );
+
+ALTER POLICY "Private buckets insert requires ownership or document.writeGroups" ON storage.objects
+ WITH CHECK (
+ bucket_id = 'private'
+ AND ((SELECT auth.role()) = 'authenticated')
+ AND has_any_company_permission('documents_create')
+ AND (
+ (storage.foldername(name))[1] = (SELECT auth.uid())::text
+ OR "name" IN (
+ SELECT "path" FROM public.document WHERE ((SELECT(groups_for_user((SELECT auth.uid())::text)) && "writeGroups") = true)
+ )
+ )
+ );
+
+ALTER POLICY "Private buckets update requires ownership or document.writeGroups" ON storage.objects
+ USING (
+ bucket_id = 'private'
+ AND ((SELECT auth.role()) = 'authenticated')
+ AND has_any_company_permission('documents_update')
+ AND (
+ (storage.foldername(name))[1] = (SELECT auth.uid())::text
+ OR "name" IN (
+ SELECT "path" FROM public.document WHERE ((SELECT(groups_for_user((SELECT auth.uid())::text)) && "writeGroups") = true)
+ )
+ )
+ );
+
+ALTER POLICY "Private buckets delete requires ownership or document.writeGroups" ON storage.objects
+ USING (
+ bucket_id = 'private'
+ AND ((SELECT auth.role()) = 'authenticated')
+ AND has_any_company_permission('documents_delete')
+ AND (
+ (storage.foldername(name))[1] = (SELECT auth.uid())::text
+ OR "name" IN (
+ SELECT "path" FROM public.document WHERE ((SELECT(groups_for_user((SELECT auth.uid())::text)) && "writeGroups") = true)
+ )
+ )
+ );
+
+ALTER POLICY "Users can view their own document favorites" ON "documentFavorite"
+ USING (
+ (SELECT auth.uid())::text = "userId"
+ );
+
+ALTER POLICY "Users can create their own document favorites" ON "documentFavorite"
+ WITH CHECK (
+ (SELECT auth.uid())::text = "userId"
+ );
+
+ALTER POLICY "Users can delete their own document favorites" ON "documentFavorite"
+ USING (
+ (SELECT auth.uid())::text = "userId"
+ );
+
+ALTER POLICY "Users can view their own purchase order favorites" ON "purchaseOrderFavorite"
+ USING (
+ (SELECT auth.uid())::text = "userId"
+ );
+
+ALTER POLICY "Users can create their own purchase order favorites" ON "purchaseOrderFavorite"
+ WITH CHECK (
+ (SELECT auth.uid())::text = "userId"
+ );
+
+ALTER POLICY "Users can delete their own purchase order favorites" ON "purchaseOrderFavorite"
+ USING (
+ (SELECT auth.uid())::text = "userId"
+ );
+
+ALTER POLICY "Suppliers with purchasing_view can their own purchase orders" ON "purchaseOrder"
+ USING (
+ has_company_permission('purchasing_view', "companyId")
+ AND has_role('supplier', "companyId")
+ AND "supplierId" IN (
+ SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ );
+
+ALTER POLICY "Suppliers with purchasing_update can their own purchase orders" ON "purchaseOrder"
+ USING (
+ has_company_permission('purchasing_update', "companyId")
+ AND has_role('supplier', "companyId")
+ AND "supplierId" IN (
+ SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ );
+
+ALTER POLICY "Suppliers with purchasing_view can search for their own purchase orders" ON "search"
+ USING (
+ has_company_permission('purchasing_view', "companyId")
+ AND has_role('supplier', "companyId")
+ AND entity = 'Purchase Order'
+ AND uuid IN (
+ SELECT id FROM "purchaseOrder" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "purchaseOrder" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Suppliers with purchasing_view can their own purchase order lines" ON "purchaseOrderLine"
+ USING (
+ has_company_permission('purchasing_view', "companyId")
+ AND has_role('supplier', "companyId")
+ AND "purchaseOrderId" IN (
+ SELECT id FROM "purchaseOrder" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "purchaseOrder" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Suppliers with purchasing_create can create lines on their own purchase order" ON "purchaseOrderLine"
+ WITH CHECK (
+ has_company_permission('purchasing_create', "companyId")
+ AND has_role('supplier', "companyId")
+ AND "purchaseOrderId" IN (
+ SELECT id FROM "purchaseOrder" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "purchaseOrder" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Suppliers with purchasing_update can their own purchase order lines" ON "purchaseOrderLine"
+ USING (
+ has_company_permission('purchasing_update', "companyId")
+ AND has_role('supplier', "companyId")
+ AND "purchaseOrderId" IN (
+ SELECT id FROM "purchaseOrder" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "purchaseOrder" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Suppliers with purchasing_delete can delete lines on their own purchase order" ON "purchaseOrderLine"
+ USING (
+ has_company_permission('purchasing_delete', "companyId")
+ AND has_role('supplier', "companyId")
+ AND "purchaseOrderId" IN (
+ SELECT id FROM "purchaseOrder" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "purchaseOrder" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Suppliers with purchasing_view can their own purchase order deliveries" ON "purchaseOrderDelivery"
+ USING (
+ has_company_permission('purchasing_view', "companyId")
+ AND has_role('supplier', "companyId")
+ AND id IN (
+ SELECT id FROM "purchaseOrder" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "purchaseOrder" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Suppliers with purchasing_update can their own purchase order deliveries" ON "purchaseOrderDelivery"
+ USING (
+ has_company_permission('purchasing_update', "companyId")
+ AND has_role('supplier', "companyId")
+ AND id IN (
+ SELECT id FROM "purchaseOrder" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "purchaseOrder" WHERE "supplierId" IN (
+ SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Authenticated users can view integrations." ON "integration"
+ USING (
+ (SELECT auth.role()) = 'authenticated'
+ );
+
+ALTER POLICY "Authenticated users can view custom field tables" ON "customFieldTable"
+ USING (
+ (SELECT auth.role()) = 'authenticated'
+ );
+
+
+-- pointer
+
+ALTER POLICY "Users can view their own sales order favorites" ON "salesOrderFavorite"
+ USING (
+ (SELECT auth.uid())::text = "userId"
+ );
+
+ALTER POLICY "Users can create their own sales order favorites" ON "salesOrderFavorite"
+ WITH CHECK (
+ (SELECT auth.uid())::text = "userId"
+ );
+
+ALTER POLICY "Users can delete their own sales order favorites" ON "salesOrderFavorite"
+ USING (
+ (SELECT auth.uid())::text = "userId"
+ );
+
+ALTER POLICY "Customers with sales_view can their own sales orders" ON "salesOrder"
+ USING (
+ has_role('customer', "companyId") AND
+ has_company_permission('sales_view', "companyId") AND
+ "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ );
+
+ALTER POLICY "Customers with sales_update can update their own sales orders" ON "salesOrder"
+ USING (
+ has_role('customer', "companyId") AND
+ has_company_permission('sales_update', "companyId")
+ AND "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ );
+
+ALTER POLICY "Customers with sales_view can search for their own sales orders" ON "search"
+ USING (
+ has_role('customer', "companyId") AND
+ has_company_permission('sales_view', "companyId") AND
+ entity = 'Sales Order' AND
+ uuid IN (
+ SELECT id FROM "salesOrder" WHERE "customerId" IN (
+ SELECT "customerId" FROM "salesOrder" WHERE "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Customers with sales_view can their own sales order lines" ON "salesOrderLine"
+ USING (
+ has_role('customer', "companyId") AND
+ has_company_permission('sales_view', "companyId") AND
+ "salesOrderId" IN (
+ SELECT id FROM "salesOrder" WHERE "customerId" IN (
+ SELECT "customerId" FROM "salesOrder" WHERE "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Customers with sales_create can create lines on their own sales order" ON "salesOrderLine"
+ WITH CHECK (
+ has_role('customer', "companyId") AND
+ has_company_permission('sales_create', "companyId") AND
+ "salesOrderId" IN (
+ SELECT id FROM "salesOrder" WHERE "customerId" IN (
+ SELECT "customerId" FROM "salesOrder" WHERE "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Customers with sales_update can update their own sales order lines" ON "salesOrderLine"
+ USING (
+ has_role('customer', "companyId") AND
+ has_company_permission('sales_update', "companyId") AND
+ "salesOrderId" IN (
+ SELECT id FROM "salesOrder" WHERE "customerId" IN (
+ SELECT "customerId" FROM "salesOrder" WHERE "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Customers with sales_delete can delete lines on their own sales order" ON "salesOrderLine"
+ USING (
+ has_role('customer', "companyId") AND
+ has_company_permission('sales_delete', "companyId") AND
+ "salesOrderId" IN (
+ SELECT id FROM "salesOrder" WHERE "customerId" IN (
+ SELECT "customerId" FROM "salesOrder" WHERE "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Customers with sales_view can their own sales order shipments" ON "salesOrderShipment"
+ USING (
+ has_role('customer', "companyId") AND
+ has_company_permission('sales_view', "companyId") AND
+ id IN (
+ SELECT id FROM "salesOrder" WHERE "customerId" IN (
+ SELECT "customerId" FROM "salesOrder" WHERE "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Customers with sales_update can their own sales order shipments" ON "salesOrderShipment"
+ USING (
+ has_role('customer', "companyId") AND
+ has_company_permission('sales_update', "companyId") AND
+ id IN (
+ SELECT id FROM "salesOrder" WHERE "customerId" IN (
+ SELECT "customerId" FROM "salesOrder" WHERE "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Employees can view tools" ON "tool"
+ USING (
+ has_role('employee', "companyId")
+ AND "companyId" = ANY(
+ select "companyId" from "userToCompany" where "userId" = (SELECT auth.uid())::text
+ )
+ );
+
+ALTER POLICY "Employees can view consumables" ON "consumable"
+ USING (
+ has_role('employee', "companyId")
+ AND "companyId" = ANY(
+ select "companyId" from "userToCompany" where "userId" = (SELECT auth.uid())::text
+ )
+ );
+
+ALTER POLICY "Employees can view fixtures" ON "fixture"
+ USING (
+ has_role('employee', "companyId")
+ AND "companyId" = ANY(
+ select "companyId" from "userToCompany" where "userId" = (SELECT auth.uid())::text
+ )
+ );
+
+ALTER POLICY "Employees can view items" ON "item"
+ USING (
+ has_role('employee', "companyId")
+ AND (
+ "companyId" IS NULL OR
+ "companyId" = ANY(
+ select "companyId" from "userToCompany" where "userId" = (SELECT auth.uid())::text
+ )
+ )
+ );
+
+ALTER POLICY "Authenticated users can view global material forms" ON "materialForm"
+ USING (
+ (SELECT auth.role()) = 'authenticated' AND
+ "companyId" IS NULL
+ );
+
+ALTER POLICY "Employees can view material forms" ON "materialForm"
+ USING (
+ has_role('employee', "companyId")
+ AND (
+ "companyId" IS NULL OR
+ "companyId" = ANY(
+ select "companyId" from "userToCompany" where "userId" = (SELECT auth.uid())::text
+ )
+ )
+ );
+
+ALTER TABLE "materialSubstance" ENABLE ROW LEVEL SECURITY;
+ALTER POLICY "Employees can view material substances" ON "materialSubstance"
+ USING (
+ has_role('employee', "companyId")
+ AND (
+ "companyId" IS NULL OR
+ "companyId" = ANY(
+ select "companyId" from "userToCompany" where "userId" = (SELECT auth.uid())::text
+ )
+ )
+ );
+
+ALTER POLICY "Authenticated users can view global material substances" ON "materialSubstance"
+ USING (
+ (SELECT auth.role()) = 'authenticated' AND
+ "companyId" IS NULL
+ );
+
+ALTER POLICY "Employees can view materials" ON "material"
+ USING (
+ has_role('employee', "companyId")
+ AND (
+ "companyId" IS NULL OR
+ "companyId" = ANY(
+ select "companyId" from "userToCompany" where "userId" = (SELECT auth.uid())::text
+ )
+ )
+ );
+
+
+
+ALTER POLICY "Users can view their own salesRfq favorites" ON "salesRfqFavorite"
+ USING (
+ (SELECT auth.uid())::text = "userId"
+ );
+
+ALTER POLICY "Users can create their own salesRfq favorites" ON "salesRfqFavorite"
+ WITH CHECK (
+ (SELECT auth.uid())::text = "userId"
+ );
+
+ALTER POLICY "Users can delete their own salesRfq favorites" ON "salesRfqFavorite"
+ USING (
+ (SELECT auth.uid())::text = "userId"
+ );
+
+ALTER POLICY "Customer with sales_view can view their own sales rfqs" ON "salesRfq"
+ USING (
+ has_company_permission('sales_view', "companyId") AND
+ has_role('customer', "companyId") AND
+ "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ );
+
+ALTER POLICY "Customers with sales_view can their own purchase order lines" ON "salesRfqLine"
+ USING (
+ has_company_permission('sales_view', "companyId") AND
+ has_role('customer', "companyId") AND
+ "salesRfqId" IN (
+ SELECT id FROM "salesRfq" WHERE "customerId" IN (
+ SELECT "customerId" FROM "salesRfq" WHERE "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Users can view their own quote favorites" ON "quoteFavorite"
+ USING (
+ (SELECT auth.uid())::text = "userId"
+ );
+
+ALTER POLICY "Users can create their own quote favorites" ON "quoteFavorite"
+ WITH CHECK (
+ (SELECT auth.uid())::text = "userId"
+ );
+
+ALTER POLICY "Users can delete their own quote favorites" ON "quoteFavorite"
+ USING (
+ (SELECT auth.uid())::text = "userId"
+ );
+
+ALTER POLICY "Customers with sales_view can their own quotes" ON "quote"
+ USING (
+ has_role('customer', "companyId") AND
+ has_company_permission('sales_view', "companyId") AND
+ "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ );
+
+ALTER POLICY "Customers with sales_update can their own quotes" ON "quote"
+ USING (
+ has_role('employee', "companyId") AND
+ has_company_permission('sales_update', "companyId") AND
+ "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ );
+
+ALTER POLICY "Customers with sales_view can search for their own quotes" ON "search"
+ USING (
+ has_role('customer', "companyId") AND
+ has_company_permission('sales_view', "companyId") AND
+ entity = 'Quotation' AND
+ uuid IN (
+ SELECT id FROM "quote" WHERE "customerId" IN (
+ SELECT "customerId" FROM "quote" WHERE "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Customers with sales_view can their own quote lines" ON "quoteLine"
+ USING (
+ has_role('customer', "companyId") AND
+ has_company_permission('sales_view', "companyId") AND
+ "quoteId" IN (
+ SELECT id FROM "quote" WHERE "customerId" IN (
+ SELECT "customerId" FROM "quote" WHERE "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Customers with sales_create can create lines on their own quote" ON "quoteLine"
+ WITH CHECK (
+ has_role('customer', "companyId") AND
+ has_company_permission('sales_create', "companyId") AND
+ "quoteId" IN (
+ SELECT id FROM "quote" WHERE "customerId" IN (
+ SELECT "customerId" FROM "quote" WHERE "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Customers with sales_update can their own quote lines" ON "quoteLine"
+ USING (
+ has_role('customer', "companyId") AND
+ has_company_permission('sales_update', "companyId") AND
+ "quoteId" IN (
+ SELECT id FROM "quote" WHERE "customerId" IN (
+ SELECT "customerId" FROM "quote" WHERE "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+ALTER POLICY "Customers with sales_delete can delete lines on their own quote" ON "quoteLine"
+ USING (
+ has_role('customer', "companyId") AND
+ has_company_permission('sales_delete', "companyId") AND
+ "quoteId" IN (
+ SELECT id FROM "quote" WHERE "customerId" IN (
+ SELECT "customerId" FROM "quote" WHERE "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = (SELECT auth.uid())
+ )
+ )
+ )
+ );
+
+DROP POLICY "Customers with sales_view can their own quote make methods" ON "quoteMakeMethod";
\ No newline at end of file