mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-17 18:19:50 +00:00
feat: first iteration changes for sub org routers
This commit is contained in:
@@ -27,6 +27,7 @@ import {
|
||||
import { generateKnexQueryFromScim } from "@app/lib/knex/scim";
|
||||
|
||||
import { OrgAuthMethod } from "./org-types";
|
||||
import { ActorType } from "../auth/auth-type";
|
||||
|
||||
export type TOrgDALFactory = ReturnType<typeof orgDALFactory>;
|
||||
|
||||
@@ -64,6 +65,7 @@ export const orgDALFactory = (db: TDbClient) => {
|
||||
const buildBaseQuery = (orgIdSubquery: Knex.QueryBuilder) => {
|
||||
return db
|
||||
.replicaNode()(TableName.Organization)
|
||||
.whereNull(`${TableName.Organization}.parentOrgId`)
|
||||
.whereIn(`${TableName.Organization}.id`, orgIdSubquery)
|
||||
.leftJoin(TableName.Project, `${TableName.Organization}.id`, `${TableName.Project}.orgId`)
|
||||
.leftJoin(TableName.Membership, `${TableName.Organization}.id`, `${TableName.Membership}.scopeOrgId`)
|
||||
@@ -154,11 +156,47 @@ export const orgDALFactory = (db: TDbClient) => {
|
||||
}
|
||||
};
|
||||
|
||||
const listSubOrganizations = async (dto: {
|
||||
actorId: string;
|
||||
actorType: ActorType;
|
||||
orgId: string;
|
||||
isAccessible?: boolean;
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
}) => {
|
||||
try {
|
||||
// TODO(sub-org:group): check this when implement group support
|
||||
const query = db
|
||||
.replicaNode()(TableName.Organization)
|
||||
.where(`${TableName.Organization}.parentOrgId`, dto.orgId)
|
||||
.select(selectAllTableCols(TableName.Organization));
|
||||
|
||||
if (dto.isAccessible) {
|
||||
void query.leftJoin(`${TableName.Membership}`, (qb) => {
|
||||
void qb.on(`${TableName.Membership}.scope`, AccessScope.Organization);
|
||||
if (dto.actorType === ActorType.IDENTITY) {
|
||||
void qb.andOn(`${TableName.Membership}.actorIdentityId`, dto.actorId);
|
||||
} else {
|
||||
void qb.andOn(`${TableName.Membership}.actorUserId`, dto.actorId);
|
||||
}
|
||||
});
|
||||
}
|
||||
if (dto.limit) void query.limit(dto.limit);
|
||||
if (dto.offset) void query.offset(dto.offset);
|
||||
|
||||
const orgs = await query;
|
||||
return orgs;
|
||||
} catch (error) {
|
||||
throw new DatabaseError({ error, name: "List sub organization" });
|
||||
}
|
||||
};
|
||||
|
||||
const findOrgById = async (orgId: string) => {
|
||||
try {
|
||||
const org = (await db
|
||||
.replicaNode()(TableName.Organization)
|
||||
.where({ [`${TableName.Organization}.id` as "id"]: orgId })
|
||||
.whereNull(`${TableName.Organization}.parentOrgId`)
|
||||
.leftJoin(TableName.SamlConfig, (qb) => {
|
||||
qb.on(`${TableName.SamlConfig}.orgId`, "=", `${TableName.Organization}.id`).andOn(
|
||||
`${TableName.SamlConfig}.isActive`,
|
||||
@@ -195,6 +233,7 @@ export const orgDALFactory = (db: TDbClient) => {
|
||||
try {
|
||||
const org = (await db
|
||||
.replicaNode()(TableName.Organization)
|
||||
.whereNull(`${TableName.Organization}.parentOrgId`)
|
||||
.where({ [`${TableName.Organization}.slug` as "slug"]: orgSlug })
|
||||
.leftJoin(TableName.SamlConfig, (qb) => {
|
||||
qb.on(`${TableName.SamlConfig}.orgId`, "=", `${TableName.Organization}.id`).andOn(
|
||||
@@ -240,6 +279,7 @@ export const orgDALFactory = (db: TDbClient) => {
|
||||
.whereNotNull(`${TableName.Membership}.actorUserId`)
|
||||
.join(TableName.MembershipRole, `${TableName.Membership}.id`, `${TableName.MembershipRole}.membershipId`)
|
||||
.join(TableName.Organization, `${TableName.Membership}.scopeOrgId`, `${TableName.Organization}.id`)
|
||||
.whereNull(`${TableName.Organization}.parentOrgId`)
|
||||
.leftJoin(TableName.SamlConfig, (qb) => {
|
||||
qb.on(`${TableName.SamlConfig}.orgId`, "=", `${TableName.Organization}.id`).andOn(
|
||||
`${TableName.SamlConfig}.isActive`,
|
||||
@@ -337,6 +377,7 @@ export const orgDALFactory = (db: TDbClient) => {
|
||||
}
|
||||
};
|
||||
|
||||
// TODO(sub-org): updated this logic later
|
||||
const countAllOrgMembers = async (orgId: string) => {
|
||||
try {
|
||||
interface CountResult {
|
||||
@@ -610,6 +651,7 @@ export const orgDALFactory = (db: TDbClient) => {
|
||||
})
|
||||
.join(TableName.Users, `${TableName.Users}.id`, `${TableName.Membership}.actorUserId`)
|
||||
.join(TableName.Organization, `${TableName.Organization}.id`, `${TableName.Membership}.scopeOrgId`)
|
||||
.whereNull(`${TableName.Organization}.parentOrgId`)
|
||||
.leftJoin(TableName.UserAliases, function joinUserAlias() {
|
||||
this.on(`${TableName.UserAliases}.userId`, "=", `${TableName.Membership}.actorUserId`)
|
||||
.andOn(`${TableName.UserAliases}.orgId`, "=", `${TableName.Membership}.scopeOrgId`)
|
||||
@@ -648,6 +690,7 @@ export const orgDALFactory = (db: TDbClient) => {
|
||||
.replicaNode()(TableName.Membership)
|
||||
.where({ actorIdentityId: identityId })
|
||||
.where(`${TableName.Membership}.scope`, AccessScope.Organization)
|
||||
.whereNull(`${TableName.Organization}.parentOrgId`)
|
||||
.whereNotNull(`${TableName.Membership}.actorIdentityId`)
|
||||
.join(TableName.MembershipRole, `${TableName.Membership}.id`, `${TableName.MembershipRole}.membershipId`)
|
||||
.join(TableName.Organization, `${TableName.Membership}.scopeOrgId`, `${TableName.Organization}.id`)
|
||||
@@ -667,6 +710,7 @@ export const orgDALFactory = (db: TDbClient) => {
|
||||
findOrgByProjectId,
|
||||
findAllOrgMembers,
|
||||
countAllOrgMembers,
|
||||
listSubOrganizations,
|
||||
findOrgById,
|
||||
findOrgBySlug,
|
||||
findAllOrgsByUserId,
|
||||
|
||||
Reference in New Issue
Block a user