mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-09-22 13:39:35 +00:00
feat: fix project query by slug (now accepts an org ID)
This commit is contained in:
@@ -138,8 +138,10 @@ export const registerProjectRouter = async (server: FastifyZodProvider) => {
|
|||||||
onRequest: verifyAuth([AuthMode.JWT, AuthMode.SERVICE_TOKEN, AuthMode.IDENTITY_ACCESS_TOKEN]),
|
onRequest: verifyAuth([AuthMode.JWT, AuthMode.SERVICE_TOKEN, AuthMode.IDENTITY_ACCESS_TOKEN]),
|
||||||
handler: async (req) => {
|
handler: async (req) => {
|
||||||
const workspace = await server.services.project.getAProject({
|
const workspace = await server.services.project.getAProject({
|
||||||
filterType: ProjectFilterType.ID,
|
filter: {
|
||||||
filter: req.params.workspaceId,
|
type: ProjectFilterType.ID,
|
||||||
|
projectId: req.params.workspaceId
|
||||||
|
},
|
||||||
actorId: req.permission.id,
|
actorId: req.permission.id,
|
||||||
actor: req.permission.type,
|
actor: req.permission.type,
|
||||||
actorOrgId: req.permission.orgId
|
actorOrgId: req.permission.orgId
|
||||||
@@ -191,8 +193,10 @@ export const registerProjectRouter = async (server: FastifyZodProvider) => {
|
|||||||
onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
|
onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
|
||||||
handler: async (req) => {
|
handler: async (req) => {
|
||||||
const workspace = await server.services.project.deleteProject({
|
const workspace = await server.services.project.deleteProject({
|
||||||
filterType: ProjectFilterType.ID,
|
filter: {
|
||||||
filter: req.params.workspaceId,
|
type: ProjectFilterType.ID,
|
||||||
|
projectId: req.params.workspaceId
|
||||||
|
},
|
||||||
actorId: req.permission.id,
|
actorId: req.permission.id,
|
||||||
actor: req.permission.type,
|
actor: req.permission.type,
|
||||||
actorOrgId: req.permission.orgId
|
actorOrgId: req.permission.orgId
|
||||||
@@ -259,8 +263,10 @@ export const registerProjectRouter = async (server: FastifyZodProvider) => {
|
|||||||
onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
|
onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
|
||||||
handler: async (req) => {
|
handler: async (req) => {
|
||||||
const workspace = await server.services.project.updateProject({
|
const workspace = await server.services.project.updateProject({
|
||||||
filterType: ProjectFilterType.ID,
|
filter: {
|
||||||
filter: req.params.workspaceId,
|
type: ProjectFilterType.ID,
|
||||||
|
projectId: req.params.workspaceId
|
||||||
|
},
|
||||||
update: {
|
update: {
|
||||||
name: req.body.name,
|
name: req.body.name,
|
||||||
autoCapitalization: req.body.autoCapitalization
|
autoCapitalization: req.body.autoCapitalization
|
||||||
|
|||||||
@@ -195,8 +195,11 @@ export const registerProjectRouter = async (server: FastifyZodProvider) => {
|
|||||||
|
|
||||||
handler: async (req) => {
|
handler: async (req) => {
|
||||||
const project = await server.services.project.deleteProject({
|
const project = await server.services.project.deleteProject({
|
||||||
filterType: ProjectFilterType.SLUG,
|
filter: {
|
||||||
filter: req.params.slug,
|
type: ProjectFilterType.SLUG,
|
||||||
|
slug: req.params.slug,
|
||||||
|
orgId: req.permission.orgId
|
||||||
|
},
|
||||||
actorId: req.permission.id,
|
actorId: req.permission.id,
|
||||||
actorOrgId: req.permission.orgId,
|
actorOrgId: req.permission.orgId,
|
||||||
actor: req.permission.type
|
actor: req.permission.type
|
||||||
@@ -221,8 +224,11 @@ export const registerProjectRouter = async (server: FastifyZodProvider) => {
|
|||||||
onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
|
onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
|
||||||
handler: async (req) => {
|
handler: async (req) => {
|
||||||
const project = await server.services.project.getAProject({
|
const project = await server.services.project.getAProject({
|
||||||
filter: req.params.slug,
|
filter: {
|
||||||
filterType: ProjectFilterType.SLUG,
|
slug: req.params.slug,
|
||||||
|
orgId: req.permission.orgId,
|
||||||
|
type: ProjectFilterType.SLUG
|
||||||
|
},
|
||||||
actorId: req.permission.id,
|
actorId: req.permission.id,
|
||||||
actorOrgId: req.permission.orgId,
|
actorOrgId: req.permission.orgId,
|
||||||
actor: req.permission.type
|
actor: req.permission.type
|
||||||
@@ -252,8 +258,11 @@ export const registerProjectRouter = async (server: FastifyZodProvider) => {
|
|||||||
onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
|
onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
|
||||||
handler: async (req) => {
|
handler: async (req) => {
|
||||||
const project = await server.services.project.updateProject({
|
const project = await server.services.project.updateProject({
|
||||||
filterType: ProjectFilterType.SLUG,
|
filter: {
|
||||||
filter: req.params.slug,
|
type: ProjectFilterType.SLUG,
|
||||||
|
slug: req.params.slug,
|
||||||
|
orgId: req.permission.orgId
|
||||||
|
},
|
||||||
update: {
|
update: {
|
||||||
name: req.body.name,
|
name: req.body.name,
|
||||||
autoCapitalization: req.body.autoCapitalization
|
autoCapitalization: req.body.autoCapitalization
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { ProjectsSchema, ProjectUpgradeStatus, ProjectVersion, TableName, TProje
|
|||||||
import { BadRequestError, DatabaseError } from "@app/lib/errors";
|
import { BadRequestError, DatabaseError } from "@app/lib/errors";
|
||||||
import { ormify, selectAllTableCols, sqlNestRelationships } from "@app/lib/knex";
|
import { ormify, selectAllTableCols, sqlNestRelationships } from "@app/lib/knex";
|
||||||
|
|
||||||
import { ProjectFilterType } from "./project-types";
|
import { Filter, ProjectFilterType } from "./project-types";
|
||||||
|
|
||||||
export type TProjectDALFactory = ReturnType<typeof projectDALFactory>;
|
export type TProjectDALFactory = ReturnType<typeof projectDALFactory>;
|
||||||
|
|
||||||
@@ -168,10 +168,11 @@ export const projectDALFactory = (db: TDbClient) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const findProjectBySlug = async (slug: string) => {
|
const findProjectBySlug = async (slug: string, orgId: string) => {
|
||||||
try {
|
try {
|
||||||
const projects = await db(TableName.ProjectMembership)
|
const projects = await db(TableName.ProjectMembership)
|
||||||
.where(`${TableName.Project}.slug`, slug)
|
.where(`${TableName.Project}.slug`, slug)
|
||||||
|
.where(`${TableName.Project}.orgId`, orgId)
|
||||||
.join(TableName.Project, `${TableName.ProjectMembership}.projectId`, `${TableName.Project}.id`)
|
.join(TableName.Project, `${TableName.ProjectMembership}.projectId`, `${TableName.Project}.id`)
|
||||||
.join(TableName.Environment, `${TableName.Environment}.projectId`, `${TableName.Project}.id`)
|
.join(TableName.Environment, `${TableName.Environment}.projectId`, `${TableName.Project}.id`)
|
||||||
.select(
|
.select(
|
||||||
@@ -185,6 +186,7 @@ export const projectDALFactory = (db: TDbClient) => {
|
|||||||
{ column: `${TableName.Project}.name`, order: "asc" },
|
{ column: `${TableName.Project}.name`, order: "asc" },
|
||||||
{ column: `${TableName.Environment}.position`, order: "asc" }
|
{ column: `${TableName.Environment}.position`, order: "asc" }
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const project = sqlNestRelationships({
|
const project = sqlNestRelationships({
|
||||||
data: projects,
|
data: projects,
|
||||||
key: "id",
|
key: "id",
|
||||||
@@ -212,17 +214,26 @@ export const projectDALFactory = (db: TDbClient) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const findProjectByFilter = async (filter: string, type: ProjectFilterType) => {
|
const findProjectByFilter = async (filter: Filter) => {
|
||||||
try {
|
try {
|
||||||
if (type === ProjectFilterType.ID) {
|
if (filter.type === ProjectFilterType.ID) {
|
||||||
return await findProjectById(filter);
|
return await findProjectById(filter.projectId);
|
||||||
}
|
}
|
||||||
if (type === ProjectFilterType.SLUG) {
|
if (filter.type === ProjectFilterType.SLUG) {
|
||||||
return await findProjectBySlug(filter);
|
if (!filter.orgId) {
|
||||||
|
throw new BadRequestError({
|
||||||
|
message: "Organization ID is required when querying with slugs"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return await findProjectBySlug(filter.slug, filter.orgId);
|
||||||
}
|
}
|
||||||
throw new BadRequestError({ message: "Invalid filter type" });
|
throw new BadRequestError({ message: "Invalid filter type" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new DatabaseError({ error, name: `Failed to find project by ${type}` });
|
if (error instanceof BadRequestError) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
throw new DatabaseError({ error, name: `Failed to find project by ${filter.type}` });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -312,8 +312,8 @@ export const projectServiceFactory = ({
|
|||||||
return results;
|
return results;
|
||||||
};
|
};
|
||||||
|
|
||||||
const deleteProject = async ({ actor, actorId, actorOrgId, filter, filterType }: TDeleteProjectDTO) => {
|
const deleteProject = async ({ actor, actorId, actorOrgId, filter }: TDeleteProjectDTO) => {
|
||||||
const project = await projectDAL.findProjectByFilter(filter, filterType);
|
const project = await projectDAL.findProjectByFilter(filter);
|
||||||
|
|
||||||
const { permission } = await permissionService.getProjectPermission(actor, actorId, project.id, actorOrgId);
|
const { permission } = await permissionService.getProjectPermission(actor, actorId, project.id, actorOrgId);
|
||||||
ForbiddenError.from(permission).throwUnlessCan(ProjectPermissionActions.Delete, ProjectPermissionSub.Project);
|
ForbiddenError.from(permission).throwUnlessCan(ProjectPermissionActions.Delete, ProjectPermissionSub.Project);
|
||||||
@@ -338,15 +338,15 @@ export const projectServiceFactory = ({
|
|||||||
return workspaces;
|
return workspaces;
|
||||||
};
|
};
|
||||||
|
|
||||||
const getAProject = async ({ actorId, actorOrgId, filter, filterType, actor }: TGetProjectDTO) => {
|
const getAProject = async ({ actorId, actorOrgId, filter, actor }: TGetProjectDTO) => {
|
||||||
const project = await projectDAL.findProjectByFilter(filter, filterType);
|
const project = await projectDAL.findProjectByFilter(filter);
|
||||||
|
|
||||||
await permissionService.getProjectPermission(actor, actorId, project.id, actorOrgId);
|
await permissionService.getProjectPermission(actor, actorId, project.id, actorOrgId);
|
||||||
return project;
|
return project;
|
||||||
};
|
};
|
||||||
|
|
||||||
const updateProject = async ({ actor, actorId, actorOrgId, update, filter, filterType }: TUpdateProjectDTO) => {
|
const updateProject = async ({ actor, actorId, actorOrgId, update, filter }: TUpdateProjectDTO) => {
|
||||||
const project = await projectDAL.findProjectByFilter(filter, filterType);
|
const project = await projectDAL.findProjectByFilter(filter);
|
||||||
|
|
||||||
const { permission } = await permissionService.getProjectPermission(actor, actorId, project.id, actorOrgId);
|
const { permission } = await permissionService.getProjectPermission(actor, actorId, project.id, actorOrgId);
|
||||||
ForbiddenError.from(permission).throwUnlessCan(ProjectPermissionActions.Edit, ProjectPermissionSub.Settings);
|
ForbiddenError.from(permission).throwUnlessCan(ProjectPermissionActions.Edit, ProjectPermissionSub.Settings);
|
||||||
|
|||||||
@@ -8,6 +8,17 @@ export enum ProjectFilterType {
|
|||||||
SLUG = "slug"
|
SLUG = "slug"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type Filter =
|
||||||
|
| {
|
||||||
|
type: ProjectFilterType.ID;
|
||||||
|
projectId: string;
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
type: ProjectFilterType.SLUG;
|
||||||
|
slug: string;
|
||||||
|
orgId: string | undefined;
|
||||||
|
};
|
||||||
|
|
||||||
export type TCreateProjectDTO = {
|
export type TCreateProjectDTO = {
|
||||||
actor: ActorType;
|
actor: ActorType;
|
||||||
actorId: string;
|
actorId: string;
|
||||||
@@ -25,8 +36,7 @@ export type TDeleteProjectBySlugDTO = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export type TGetProjectDTO = {
|
export type TGetProjectDTO = {
|
||||||
filter: string;
|
filter: Filter;
|
||||||
filterType: ProjectFilterType;
|
|
||||||
} & Omit<TProjectPermission, "projectId">;
|
} & Omit<TProjectPermission, "projectId">;
|
||||||
|
|
||||||
export type TToggleProjectAutoCapitalizationDTO = {
|
export type TToggleProjectAutoCapitalizationDTO = {
|
||||||
@@ -37,8 +47,7 @@ export type TUpdateProjectNameDTO = {
|
|||||||
} & TProjectPermission;
|
} & TProjectPermission;
|
||||||
|
|
||||||
export type TUpdateProjectDTO = {
|
export type TUpdateProjectDTO = {
|
||||||
filter: string;
|
filter: Filter;
|
||||||
filterType: ProjectFilterType;
|
|
||||||
update: {
|
update: {
|
||||||
name?: string;
|
name?: string;
|
||||||
autoCapitalization?: boolean;
|
autoCapitalization?: boolean;
|
||||||
@@ -46,8 +55,7 @@ export type TUpdateProjectDTO = {
|
|||||||
} & Omit<TProjectPermission, "projectId">;
|
} & Omit<TProjectPermission, "projectId">;
|
||||||
|
|
||||||
export type TDeleteProjectDTO = {
|
export type TDeleteProjectDTO = {
|
||||||
filter: string;
|
filter: Filter;
|
||||||
filterType: ProjectFilterType;
|
|
||||||
actor: ActorType;
|
actor: ActorType;
|
||||||
actorId: string;
|
actorId: string;
|
||||||
actorOrgId?: string;
|
actorOrgId?: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user