mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-09-22 13:39:35 +00:00
feat(instance-management): organizations overview and control
This commit is contained in:
@@ -2,6 +2,7 @@ import { Knex } from "knex";
|
||||
|
||||
import { TDbClient } from "@app/db";
|
||||
import {
|
||||
OrganizationsSchema,
|
||||
OrgMembershipRole,
|
||||
TableName,
|
||||
TOrganizations,
|
||||
@@ -12,7 +13,15 @@ import {
|
||||
TUserEncryptionKeys
|
||||
} from "@app/db/schemas";
|
||||
import { DatabaseError } from "@app/lib/errors";
|
||||
import { buildFindFilter, ormify, selectAllTableCols, TFindFilter, TFindOpt, withTransaction } from "@app/lib/knex";
|
||||
import {
|
||||
buildFindFilter,
|
||||
ormify,
|
||||
selectAllTableCols,
|
||||
sqlNestRelationships,
|
||||
TFindFilter,
|
||||
TFindOpt,
|
||||
withTransaction
|
||||
} from "@app/lib/knex";
|
||||
import { generateKnexQueryFromScim } from "@app/lib/knex/scim";
|
||||
|
||||
import { OrgAuthMethod } from "./org-types";
|
||||
@@ -22,6 +31,110 @@ export type TOrgDALFactory = ReturnType<typeof orgDALFactory>;
|
||||
export const orgDALFactory = (db: TDbClient) => {
|
||||
const orgOrm = ormify(db, TableName.Organization);
|
||||
|
||||
const findOrganizationsByFilter = async ({
|
||||
limit,
|
||||
offset,
|
||||
searchTerm,
|
||||
sortBy
|
||||
}: {
|
||||
limit: number;
|
||||
offset: number;
|
||||
searchTerm: string;
|
||||
sortBy?: keyof TOrganizations;
|
||||
}) => {
|
||||
try {
|
||||
const query = db.replicaNode()(TableName.Organization);
|
||||
|
||||
// Build the subquery for limited organization IDs
|
||||
const orgSubquery = db.replicaNode().select("id").from(TableName.Organization);
|
||||
|
||||
if (searchTerm) {
|
||||
void orgSubquery.where((qb) => {
|
||||
void qb.whereILike(`${TableName.Organization}.name`, `%${searchTerm}%`);
|
||||
});
|
||||
}
|
||||
|
||||
if (sortBy) {
|
||||
void orgSubquery.orderBy(sortBy);
|
||||
}
|
||||
|
||||
void orgSubquery.limit(limit).offset(offset);
|
||||
|
||||
// Main query with joins, limited to the subquery results
|
||||
const docs = await query
|
||||
.whereIn(`${TableName.Organization}.id`, orgSubquery)
|
||||
.leftJoin(TableName.Project, `${TableName.Organization}.id`, `${TableName.Project}.orgId`)
|
||||
.leftJoin(TableName.OrgMembership, `${TableName.Organization}.id`, `${TableName.OrgMembership}.orgId`)
|
||||
.leftJoin(TableName.Users, `${TableName.OrgMembership}.userId`, `${TableName.Users}.id`)
|
||||
.leftJoin(TableName.OrgRoles, `${TableName.OrgMembership}.roleId`, `${TableName.OrgRoles}.id`)
|
||||
.where((qb) => {
|
||||
void qb.where(`${TableName.Users}.isGhost`, false).orWhereNull(`${TableName.Users}.id`);
|
||||
})
|
||||
.select(selectAllTableCols(TableName.Organization))
|
||||
.select(db.ref("name").withSchema(TableName.Project).as("projectName"))
|
||||
.select(db.ref("id").withSchema(TableName.Project).as("projectId"))
|
||||
.select(db.ref("slug").withSchema(TableName.Project).as("projectSlug"))
|
||||
.select(db.ref("createdAt").withSchema(TableName.Project).as("projectCreatedAt"))
|
||||
.select(db.ref("email").withSchema(TableName.Users).as("userEmail"))
|
||||
.select(db.ref("username").withSchema(TableName.Users).as("username"))
|
||||
.select(db.ref("firstName").withSchema(TableName.Users).as("firstName"))
|
||||
.select(db.ref("lastName").withSchema(TableName.Users).as("lastName"))
|
||||
.select(db.ref("id").withSchema(TableName.Users).as("userId"))
|
||||
.select(db.ref("id").withSchema(TableName.OrgMembership).as("orgMembershipId"))
|
||||
.select(db.ref("role").withSchema(TableName.OrgMembership).as("orgMembershipRole"))
|
||||
.select(db.ref("roleId").withSchema(TableName.OrgMembership).as("orgMembershipRoleId"))
|
||||
.select(db.ref("name").withSchema(TableName.OrgRoles).as("orgMembershipRoleName"));
|
||||
|
||||
const formattedDocs = sqlNestRelationships({
|
||||
data: docs,
|
||||
key: "id",
|
||||
parentMapper: (data) => OrganizationsSchema.parse(data),
|
||||
childrenMapper: [
|
||||
{
|
||||
key: "projectId",
|
||||
label: "projects" as const,
|
||||
mapper: ({ projectId, projectName, projectSlug, projectCreatedAt }) => ({
|
||||
id: projectId,
|
||||
name: projectName,
|
||||
slug: projectSlug,
|
||||
createdAt: projectCreatedAt
|
||||
})
|
||||
},
|
||||
{
|
||||
key: "userId",
|
||||
label: "members" as const,
|
||||
mapper: ({
|
||||
userId,
|
||||
userEmail,
|
||||
username,
|
||||
firstName,
|
||||
lastName,
|
||||
orgMembershipId,
|
||||
orgMembershipRole,
|
||||
orgMembershipRoleName,
|
||||
orgMembershipRoleId
|
||||
}) => ({
|
||||
user: {
|
||||
id: userId,
|
||||
email: userEmail,
|
||||
username,
|
||||
firstName,
|
||||
lastName
|
||||
},
|
||||
membershipId: orgMembershipId,
|
||||
role: orgMembershipRoleName || orgMembershipRole, // custom role name or pre-defined role name
|
||||
roleId: orgMembershipRoleId
|
||||
})
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
return formattedDocs;
|
||||
} catch (error) {
|
||||
throw new DatabaseError({ error, name: "Find organizations by filter" });
|
||||
}
|
||||
};
|
||||
|
||||
const findOrgById = async (orgId: string) => {
|
||||
try {
|
||||
const org = (await db
|
||||
@@ -507,6 +620,7 @@ export const orgDALFactory = (db: TDbClient) => {
|
||||
findOrgById,
|
||||
findOrgBySlug,
|
||||
findAllOrgsByUserId,
|
||||
findOrganizationsByFilter,
|
||||
ghostUserExists,
|
||||
findOrgMembersByUsername,
|
||||
findOrgMembersByRole,
|
||||
|
||||
@@ -11,7 +11,7 @@ import { BadRequestError, NotFoundError } from "@app/lib/errors";
|
||||
import { TIdentityDALFactory } from "@app/services/identity/identity-dal";
|
||||
|
||||
import { TAuthLoginFactory } from "../auth/auth-login-service";
|
||||
import { AuthMethod, AuthTokenType } from "../auth/auth-type";
|
||||
import { ActorType, AuthMethod, AuthTokenType } from "../auth/auth-type";
|
||||
import { TIdentityOrgDALFactory } from "../identity/identity-org-dal";
|
||||
import { TIdentityAccessTokenDALFactory } from "../identity-access-token/identity-access-token-dal";
|
||||
import { TIdentityAccessTokenJwtPayload } from "../identity-access-token/identity-access-token-types";
|
||||
@@ -21,7 +21,9 @@ import { TKmsRootConfigDALFactory } from "../kms/kms-root-config-dal";
|
||||
import { TKmsServiceFactory } from "../kms/kms-service";
|
||||
import { RootKeyEncryptionStrategy } from "../kms/kms-types";
|
||||
import { TMicrosoftTeamsServiceFactory } from "../microsoft-teams/microsoft-teams-service";
|
||||
import { TOrgDALFactory } from "../org/org-dal";
|
||||
import { TOrgServiceFactory } from "../org/org-service";
|
||||
import { TOrgMembershipDALFactory } from "../org-membership/org-membership-dal";
|
||||
import { TUserDALFactory } from "../user/user-dal";
|
||||
import { TUserAliasDALFactory } from "../user-alias/user-alias-dal";
|
||||
import { UserAliasType } from "../user-alias/user-alias-types";
|
||||
@@ -33,7 +35,8 @@ import {
|
||||
TAdminBootstrapInstanceDTO,
|
||||
TAdminGetIdentitiesDTO,
|
||||
TAdminGetUsersDTO,
|
||||
TAdminSignUpDTO
|
||||
TAdminSignUpDTO,
|
||||
TGetOrganizationsDTO
|
||||
} from "./super-admin-types";
|
||||
|
||||
type TSuperAdminServiceFactoryDep = {
|
||||
@@ -41,6 +44,8 @@ type TSuperAdminServiceFactoryDep = {
|
||||
identityTokenAuthDAL: TIdentityTokenAuthDALFactory;
|
||||
identityAccessTokenDAL: TIdentityAccessTokenDALFactory;
|
||||
identityOrgMembershipDAL: TIdentityOrgDALFactory;
|
||||
orgDAL: TOrgDALFactory;
|
||||
orgMembershipDAL: TOrgMembershipDALFactory;
|
||||
serverCfgDAL: TSuperAdminDALFactory;
|
||||
userDAL: TUserDALFactory;
|
||||
userAliasDAL: Pick<TUserAliasDALFactory, "findOne">;
|
||||
@@ -73,6 +78,8 @@ export const superAdminServiceFactory = ({
|
||||
serverCfgDAL,
|
||||
userDAL,
|
||||
identityDAL,
|
||||
orgDAL,
|
||||
orgMembershipDAL,
|
||||
userAliasDAL,
|
||||
authService,
|
||||
orgService,
|
||||
@@ -519,6 +526,47 @@ export const superAdminServiceFactory = ({
|
||||
return updatedUser;
|
||||
};
|
||||
|
||||
const getOrganizations = async ({ offset, limit, searchTerm }: TGetOrganizationsDTO) => {
|
||||
const organizations = await orgDAL.findOrganizationsByFilter({
|
||||
offset,
|
||||
searchTerm,
|
||||
sortBy: "name",
|
||||
limit
|
||||
});
|
||||
return organizations;
|
||||
};
|
||||
|
||||
const deleteOrganization = async (organizationId: string) => {
|
||||
const organization = await orgDAL.deleteById(organizationId);
|
||||
return organization;
|
||||
};
|
||||
|
||||
const deleteOrganizationMembership = async (
|
||||
organizationId: string,
|
||||
membershipId: string,
|
||||
actorId: string,
|
||||
actorType: ActorType
|
||||
) => {
|
||||
if (actorType === ActorType.USER) {
|
||||
const orgMembership = await orgMembershipDAL.findById(membershipId);
|
||||
if (!orgMembership) {
|
||||
throw new NotFoundError({ name: "Organization Membership", message: "Organization membership not found" });
|
||||
}
|
||||
|
||||
if (orgMembership.userId === actorId) {
|
||||
throw new BadRequestError({
|
||||
message: "You cannot remove yourself from the organization from the instance management panel."
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const [organizationMembership] = await orgMembershipDAL.delete({
|
||||
orgId: organizationId,
|
||||
id: membershipId
|
||||
});
|
||||
return organizationMembership;
|
||||
};
|
||||
|
||||
const getIdentities = async ({ offset, limit, searchTerm }: TAdminGetIdentitiesDTO) => {
|
||||
const identities = await identityDAL.getIdentitiesByFilter({
|
||||
limit,
|
||||
@@ -661,6 +709,9 @@ export const superAdminServiceFactory = ({
|
||||
deleteIdentitySuperAdminAccess,
|
||||
deleteUserSuperAdminAccess,
|
||||
invalidateCache,
|
||||
checkIfInvalidatingCache
|
||||
checkIfInvalidatingCache,
|
||||
getOrganizations,
|
||||
deleteOrganization,
|
||||
deleteOrganizationMembership
|
||||
};
|
||||
};
|
||||
|
||||
@@ -35,6 +35,12 @@ export type TAdminGetIdentitiesDTO = {
|
||||
searchTerm: string;
|
||||
};
|
||||
|
||||
export type TGetOrganizationsDTO = {
|
||||
offset: number;
|
||||
limit: number;
|
||||
searchTerm: string;
|
||||
};
|
||||
|
||||
export enum LoginMethod {
|
||||
EMAIL = "email",
|
||||
GOOGLE = "google",
|
||||
|
||||
Reference in New Issue
Block a user