Skip to content

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

feat(migration): add migration to backfill request-access permission for org-level custom roles

Victor Hugo dos Santosbb1c1798c7dc
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
new file mode 100644
index 0000000000..f646d91ecd
--- /dev/null
+++ b/backend/src/db/migrations/20260508140759_backfill-org-custom-role-project-request-access.ts
@@ -0,0 +1,92 @@
+/* eslint-disable no-await-in-loop */
+import { packRules, unpackRules } from "@casl/ability/extra";
+import { Knex } from "knex";
+
+import { TableName } from "../schemas";
+
+const PROJECT_SUBJECT = "project";
+const REQUEST_ACCESS_ACTION = "request-access";
+
+const CHUNK_SIZE = 1000;
+
+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();
+ }
+}
+
+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;
+ }[];
+
+ const filteredRules = rules.filter((rule) => {
+ if (rule.subject !== PROJECT_SUBJECT || rule.inverted) return true;
+ if (rule.action === REQUEST_ACCESS_ACTION) return false;
+ 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;
+ }
+ return true;
+ });
+
+ if (filteredRules.length === rules.length) 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();
+ }
+}
diff --git a/frontend/src/pages/organization/ProjectsPage/components/AllProjectView.tsx b/frontend/src/pages/organization/ProjectsPage/components/AllProjectView.tsx
index 311888f80b..25bc5c0087 100644
--- a/frontend/src/pages/organization/ProjectsPage/components/AllProjectView.tsx
+++ b/frontend/src/pages/organization/ProjectsPage/components/AllProjectView.tsx
@@ -136,11 +136,9 @@ export const AllProjectView = ({
return (
<div>
<div className="flex w-full flex-row flex-wrap gap-2 md:flex-nowrap md:gap-0">
- <ProjectListToggle
- value={projectListView}
- onChange={onProjectListViewChange}
- showAllProjects={showAllProjects}
- />
+ {showAllProjects && (
+ <ProjectListToggle value={projectListView} onChange={onProjectListViewChange} />
+ )}
<Input
className="h-[2.3rem] bg-mineshaft-800 text-sm placeholder-mineshaft-50 duration-200 focus:bg-mineshaft-700/80"
containerClassName="w-full ml-2"
diff --git a/frontend/src/pages/organization/ProjectsPage/components/MyProjectView.tsx b/frontend/src/pages/organization/ProjectsPage/components/MyProjectView.tsx
index fb8fa5c729..bac4bf027f 100644
--- a/frontend/src/pages/organization/ProjectsPage/components/MyProjectView.tsx
+++ b/frontend/src/pages/organization/ProjectsPage/components/MyProjectView.tsx
@@ -370,11 +370,9 @@ export const MyProjectView = ({
return (
<div>
<div className="flex w-full flex-row flex-wrap gap-2 md:flex-nowrap md:gap-0">
- <ProjectListToggle
- value={projectListView}
- onChange={onProjectListViewChange}
- showAllProjects={showAllProjects}
- />
+ {showAllProjects && (
+ <ProjectListToggle value={projectListView} onChange={onProjectListViewChange} />
+ )}
<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"
diff --git a/frontend/src/pages/organization/ProjectsPage/components/ProjectListToggle.tsx b/frontend/src/pages/organization/ProjectsPage/components/ProjectListToggle.tsx
index e0bc9e352b..9d17891d3d 100644
--- a/frontend/src/pages/organization/ProjectsPage/components/ProjectListToggle.tsx
+++ b/frontend/src/pages/organization/ProjectsPage/components/ProjectListToggle.tsx
@@ -8,10 +8,9 @@ export enum ProjectListView {
type Props = {
value: ProjectListView;
onChange: (value: ProjectListView) => void;
- showAllProjects?: boolean;
};
-export const ProjectListToggle = ({ value, onChange, showAllProjects = true }: Props) => {
+export const ProjectListToggle = ({ value, onChange }: Props) => {
return (
<div className="flex gap-x-0.5 rounded-md border border-mineshaft-600 bg-mineshaft-800 p-1">
<Button
@@ -26,20 +25,18 @@ export const ProjectListToggle = ({ value, onChange, showAllProjects = true }: P
>
My Projects
</Button>
- {showAllProjects && (
- <Button
- variant="outline_bg"
- onClick={() => {
- onChange(ProjectListView.AllProjects);
- }}
- size="xs"
- className={`${
- value === ProjectListView.AllProjects ? "bg-mineshaft-500" : "bg-transparent"
- } min-w-[2.4rem] rounded border-none hover:bg-mineshaft-600`}
- >
- All Projects
- </Button>
- )}
+ <Button
+ variant="outline_bg"
+ onClick={() => {
+ onChange(ProjectListView.AllProjects);
+ }}
+ size="xs"
+ className={`${
+ value === ProjectListView.AllProjects ? "bg-mineshaft-500" : "bg-transparent"
+ } min-w-[2.4rem] rounded border-none hover:bg-mineshaft-600`}
+ >
+ All Projects
+ </Button>
</div>
);
};