Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

refactor(migration): optimize request-access permission handling in custom roles

Victor Hugo dos Santos5050c1970061
diff --git a/backend/src/db/migrations/20260508140759_backfill-org-custom-role-project-request-access.ts b/backend/src/db/migrations/20260508140759_backfill-org-custom-role-project-request-access.ts
index a802f5be26..8c8841fe1b 100644
--- a/backend/src/db/migrations/20260508140759_backfill-org-custom-role-project-request-access.ts
+++ b/backend/src/db/migrations/20260508140759_backfill-org-custom-role-project-request-access.ts
@@ -1,4 +1,3 @@
-/* eslint-disable no-await-in-loop */
import { packRules, unpackRules } from "@casl/ability/extra";
import { Knex } from "knex";
@@ -7,96 +6,92 @@ import { TableName } from "../schemas";
const PROJECT_SUBJECT = "project";
const REQUEST_ACCESS_ACTION = "request-access";
-const CHUNK_SIZE = 1000;
+// `unpackRules` normalises `subject` and `action` to array form, so accept either shape.
+const includes = (value: string | string[] | undefined, target: string) =>
+ value === target || (Array.isArray(value) && value.includes(target));
export async function up(knex: Knex): Promise<void> {
// Org-level custom roles are rows in `roles` where projectId IS NULL.
// Built-in slugs (admin/member/no-access) are handled in code and not stored here.
- const customRoles = await knex(TableName.Role).whereNull("projectId").select("*");
-
- const toUpdate = customRoles
- .map((role) => {
- const rules = unpackRules((role.permissions ?? []) as Parameters<typeof unpackRules>[0]) as {
- action: string | string[];
- subject: string;
- inverted?: boolean;
- }[];
-
- // Skip if request-access on project is already present
- const alreadyHasPermission = rules.some(
- (rule) =>
- !rule.inverted &&
- rule.subject === PROJECT_SUBJECT &&
- (rule.action === REQUEST_ACCESS_ACTION ||
- (Array.isArray(rule.action) && rule.action.includes(REQUEST_ACCESS_ACTION)))
- );
-
- if (alreadyHasPermission) return null;
-
- const updatedRules = [...rules, { action: REQUEST_ACCESS_ACTION, subject: PROJECT_SUBJECT }];
-
- return {
- ...role,
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
- // @ts-ignore-error packRules type mismatch
- permissions: JSON.stringify(packRules(updatedRules))
- };
- })
- .filter(Boolean) as typeof customRoles;
-
- for (let i = 0; i < toUpdate.length; i += CHUNK_SIZE) {
- const chunk = toUpdate.slice(i, i + CHUNK_SIZE);
- await knex(TableName.Role).insert(chunk).onConflict("id").merge();
- }
+ await knex.transaction(async (trx) => {
+ const customRoles = await trx(TableName.Role).whereNull("projectId").select("*");
+
+ const toUpdate = customRoles
+ .map((role) => {
+ const rules = unpackRules((role.permissions ?? []) as Parameters<typeof unpackRules>[0]) as {
+ action: string | string[];
+ subject: string;
+ inverted?: boolean;
+ }[];
+
+ // Skip if request-access on project is already present
+ const alreadyHasPermission = rules.some(
+ (rule) => !rule.inverted && rule.subject === PROJECT_SUBJECT && includes(rule.action, REQUEST_ACCESS_ACTION)
+ );
+
+ if (alreadyHasPermission) return null;
+
+ const updatedRules = [...rules, { action: REQUEST_ACCESS_ACTION, subject: PROJECT_SUBJECT }];
+
+ return {
+ ...role,
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-ignore-error packRules type mismatch
+ permissions: JSON.stringify(packRules(updatedRules))
+ };
+ })
+ .filter(Boolean) as typeof customRoles;
+
+ if (toUpdate.length > 0) {
+ await trx(TableName.Role).insert(toUpdate).onConflict("id").merge();
+ }
+ });
}
export async function down(knex: Knex): Promise<void> {
- const customRoles = await knex(TableName.Role).whereNull("projectId").select("*");
-
- const toUpdate = customRoles
- .map((role) => {
- const rules = unpackRules((role.permissions ?? []) as Parameters<typeof unpackRules>[0]) as {
- action: string | string[];
- subject: string;
- inverted?: boolean;
- }[];
-
- let wasModified = false;
- const filteredRules = rules.filter((rule) => {
- if (rule.subject !== PROJECT_SUBJECT || rule.inverted) return true;
- if (rule.action === REQUEST_ACCESS_ACTION) {
+ await knex.transaction(async (trx) => {
+ const customRoles = await trx(TableName.Role).whereNull("projectId").select("*");
+
+ const toUpdate = customRoles
+ .map((role) => {
+ const rules = unpackRules((role.permissions ?? []) as Parameters<typeof unpackRules>[0]) as {
+ action: string | string[];
+ subject: string;
+ inverted?: boolean;
+ }[];
+
+ let wasModified = false;
+ const filteredRules = rules.filter((rule) => {
+ if (rule.subject !== PROJECT_SUBJECT || rule.inverted) return true;
+ if (!includes(rule.action, REQUEST_ACCESS_ACTION)) return true;
+
wasModified = true;
- return false;
- }
- if (Array.isArray(rule.action)) {
- const withoutRequestAccess = rule.action.filter((a) => a !== REQUEST_ACCESS_ACTION);
- if (withoutRequestAccess.length === 0) {
- wasModified = true;
+ if (Array.isArray(rule.action)) {
+ const withoutRequestAccess = rule.action.filter((a) => a !== REQUEST_ACCESS_ACTION);
+ if (withoutRequestAccess.length === 0) return false;
+ // mutate in place to drop the action from the array
+ // eslint-disable-next-line no-param-reassign
+ rule.action = withoutRequestAccess;
+ } else {
+ // Single action that is REQUEST_ACCESS_ACTION, remove the rule
return false;
}
- if (withoutRequestAccess.length !== rule.action.length) {
- wasModified = true;
- }
- // mutate in place to drop the action from the array
- // eslint-disable-next-line no-param-reassign
- rule.action = withoutRequestAccess;
- }
- return true;
- });
-
- if (!wasModified) return null;
-
- return {
- ...role,
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
- // @ts-ignore-error packRules type mismatch
- permissions: JSON.stringify(packRules(filteredRules))
- };
- })
- .filter(Boolean) as typeof customRoles;
-
- for (let i = 0; i < toUpdate.length; i += CHUNK_SIZE) {
- const chunk = toUpdate.slice(i, i + CHUNK_SIZE);
- await knex(TableName.Role).insert(chunk).onConflict("id").merge();
- }
+ return true;
+ });
+
+ if (!wasModified) return null;
+
+ return {
+ ...role,
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-ignore-error packRules type mismatch
+ permissions: JSON.stringify(packRules(filteredRules))
+ };
+ })
+ .filter(Boolean) as typeof customRoles;
+
+ if (toUpdate.length > 0) {
+ await trx(TableName.Role).insert(toUpdate).onConflict("id").merge();
+ }
+ });
}
diff --git a/frontend/src/pages/organization/ProjectsPage/components/MyProjectView.tsx b/frontend/src/pages/organization/ProjectsPage/components/MyProjectView.tsx
index bac4bf027f..2155630ed1 100644
--- a/frontend/src/pages/organization/ProjectsPage/components/MyProjectView.tsx
+++ b/frontend/src/pages/organization/ProjectsPage/components/MyProjectView.tsx
@@ -375,7 +375,7 @@ export const MyProjectView = ({
)}
<Input
className="h-[2.3rem] bg-mineshaft-800 text-sm placeholder-mineshaft-50/60 duration-200 focus:bg-mineshaft-700/80"
- containerClassName="w-full ml-2"
+ containerClassName={`w-full ${showAllProjects ? "ml-2" : ""}`}
placeholder="Search by project name..."
value={searchFilter}
onChange={(e) => setSearchFilter(e.target.value)}