mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-14 15:40:47 +00:00
feature: scim group org role mapping
This commit is contained in:
+46
@@ -0,0 +1,46 @@
|
||||
import { Tables } from "knex/types/tables";
|
||||
|
||||
import { TDbClient } from "@app/db";
|
||||
import { TableName } from "@app/db/schemas";
|
||||
import { TExternalGroupOrgRoleMappings } from "@app/db/schemas/external-group-org-role-mappings";
|
||||
import { ormify } from "@app/lib/knex";
|
||||
|
||||
export type TExternalGroupOrgRoleMappingDALFactory = ReturnType<typeof externalGroupOrgRoleMappingDALFactory>;
|
||||
|
||||
export const externalGroupOrgRoleMappingDALFactory = (db: TDbClient) => {
|
||||
const externalGroupOrgRoleMappingOrm = ormify(db, TableName.ExternalGroupOrgRoleMapping);
|
||||
|
||||
const updateExternalGroupOrgRoleMappingForOrg = async (
|
||||
orgId: string,
|
||||
newMappings: readonly Tables[TableName.ExternalGroupOrgRoleMapping]["insert"][]
|
||||
) => {
|
||||
const currentMappings = await externalGroupOrgRoleMappingOrm.find({ orgId });
|
||||
|
||||
const newMap = new Map(newMappings.map((mapping) => [mapping.groupName, mapping]));
|
||||
const currentMap = new Map(currentMappings.map((mapping) => [mapping.groupName, mapping]));
|
||||
|
||||
const mappingsToDelete = currentMappings.filter((mapping) => !newMap.has(mapping.groupName));
|
||||
const mappingsToUpdate = currentMappings
|
||||
.filter((mapping) => newMap.has(mapping.groupName))
|
||||
.map((mapping) => ({ id: mapping.id, ...newMap.get(mapping.groupName) }));
|
||||
const mappingsToInsert = newMappings.filter((mapping) => !currentMap.has(mapping.groupName));
|
||||
|
||||
const mappings = await externalGroupOrgRoleMappingOrm.transaction(async (tx) => {
|
||||
await externalGroupOrgRoleMappingOrm.delete({ $in: { id: mappingsToDelete.map((mapping) => mapping.id) } }, tx);
|
||||
|
||||
const updatedMappings: TExternalGroupOrgRoleMappings[] = [];
|
||||
for await (const { id, ...mappingData } of mappingsToUpdate) {
|
||||
const updatedMapping = await externalGroupOrgRoleMappingOrm.update({ id }, mappingData, tx);
|
||||
updatedMappings.push(updatedMapping[0]);
|
||||
}
|
||||
|
||||
const insertedMappings = await externalGroupOrgRoleMappingOrm.insertMany(mappingsToInsert, tx);
|
||||
|
||||
return [...updatedMappings, ...insertedMappings];
|
||||
});
|
||||
|
||||
return mappings;
|
||||
};
|
||||
|
||||
return { ...externalGroupOrgRoleMappingOrm, updateExternalGroupOrgRoleMappingForOrg };
|
||||
};
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
import { OrgMembershipRole, TOrgRoles } from "@app/db/schemas";
|
||||
import { TLicenseServiceFactory } from "@app/ee/services/license/license-service";
|
||||
import { BadRequestError, NotFoundError } from "@app/lib/errors";
|
||||
import { TOrgRoleDALFactory } from "@app/services/org/org-role-dal";
|
||||
import { isCustomOrgRole } from "@app/services/org/org-role-fns";
|
||||
|
||||
import { TExternalGroupOrgMembershipRoleMappingDTO } from "./external-group-org-role-mapping-types";
|
||||
|
||||
export const constructGroupOrgMembershipRoleMappings = async ({
|
||||
mappingsDTO,
|
||||
orgId,
|
||||
orgRoleDAL,
|
||||
licenseService
|
||||
}: {
|
||||
mappingsDTO: TExternalGroupOrgMembershipRoleMappingDTO[];
|
||||
orgRoleDAL: TOrgRoleDALFactory;
|
||||
licenseService: TLicenseServiceFactory;
|
||||
orgId: string;
|
||||
}) => {
|
||||
const plan = await licenseService.getPlan(orgId);
|
||||
|
||||
// prevent setting custom values if not in plan
|
||||
if (mappingsDTO.some((map) => isCustomOrgRole(map.roleSlug)) && !plan?.rbac)
|
||||
throw new BadRequestError({
|
||||
message:
|
||||
"Failed to set group organization role mapping due to plan RBAC restriction. Upgrade plan to set custom role mapping."
|
||||
});
|
||||
|
||||
const customRoleSlugs = mappingsDTO
|
||||
.filter((mapping) => isCustomOrgRole(mapping.roleSlug))
|
||||
.map((mapping) => mapping.roleSlug);
|
||||
|
||||
let customRolesMap: Map<string, TOrgRoles> = new Map();
|
||||
if (customRoleSlugs.length > 0) {
|
||||
const customRoles = await orgRoleDAL.find({
|
||||
$in: {
|
||||
slug: customRoleSlugs
|
||||
}
|
||||
});
|
||||
|
||||
customRolesMap = new Map(customRoles.map((role) => [role.slug, role]));
|
||||
}
|
||||
|
||||
const mappings = mappingsDTO.map(({ roleSlug, groupName }) => {
|
||||
if (isCustomOrgRole(roleSlug)) {
|
||||
const customRole = customRolesMap.get(roleSlug);
|
||||
|
||||
if (!customRole) throw new NotFoundError({ message: `Custom role ${roleSlug} not found.` });
|
||||
|
||||
return {
|
||||
groupName,
|
||||
role: OrgMembershipRole.Custom,
|
||||
roleId: customRole.id,
|
||||
orgId
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
groupName,
|
||||
role: roleSlug,
|
||||
roleId: null, // need to set explicitly null for updates
|
||||
orgId
|
||||
};
|
||||
});
|
||||
|
||||
return mappings;
|
||||
};
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
import { ForbiddenError } from "@casl/ability";
|
||||
import { FastifyRequest } from "fastify";
|
||||
|
||||
import { TLicenseServiceFactory } from "@app/ee/services/license/license-service";
|
||||
import { OrgPermissionActions, OrgPermissionSubjects } from "@app/ee/services/permission/org-permission";
|
||||
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service";
|
||||
import { constructGroupOrgMembershipRoleMappings } from "@app/services/external-group-org-role-mapping/external-group-org-role-mapping-fns";
|
||||
import { TSyncExternalGroupOrgMembershipRoleMappingsDTO } from "@app/services/external-group-org-role-mapping/external-group-org-role-mapping-types";
|
||||
import { TOrgRoleDALFactory } from "@app/services/org/org-role-dal";
|
||||
|
||||
import { TExternalGroupOrgRoleMappingDALFactory } from "./external-group-org-role-mapping-dal";
|
||||
|
||||
type TExternalGroupOrgRoleMappingServiceFactoryDep = {
|
||||
externalGroupOrgRoleMappingDAL: TExternalGroupOrgRoleMappingDALFactory;
|
||||
permissionService: TPermissionServiceFactory;
|
||||
licenseService: TLicenseServiceFactory;
|
||||
orgRoleDAL: TOrgRoleDALFactory;
|
||||
};
|
||||
|
||||
export type TExternalGroupOrgRoleMappingServiceFactory = ReturnType<typeof externalGroupOrgRoleMappingServiceFactory>;
|
||||
|
||||
export const externalGroupOrgRoleMappingServiceFactory = ({
|
||||
externalGroupOrgRoleMappingDAL,
|
||||
licenseService,
|
||||
permissionService,
|
||||
orgRoleDAL
|
||||
}: TExternalGroupOrgRoleMappingServiceFactoryDep) => {
|
||||
const listExternalGroupOrgRoleMappings = async (actor: FastifyRequest["permission"]) => {
|
||||
const { permission } = await permissionService.getOrgPermission(
|
||||
actor.type,
|
||||
actor.id,
|
||||
actor.orgId,
|
||||
actor.authMethod,
|
||||
actor.orgId
|
||||
);
|
||||
|
||||
// TODO: will need to change if we add support for ldap, oidc, etc.
|
||||
ForbiddenError.from(permission).throwUnlessCan(OrgPermissionActions.Read, OrgPermissionSubjects.Scim);
|
||||
|
||||
const mappings = await externalGroupOrgRoleMappingDAL.find({
|
||||
orgId: actor.orgId
|
||||
});
|
||||
|
||||
return mappings;
|
||||
};
|
||||
|
||||
const updateExternalGroupOrgRoleMappings = async (
|
||||
dto: TSyncExternalGroupOrgMembershipRoleMappingsDTO,
|
||||
actor: FastifyRequest["permission"]
|
||||
) => {
|
||||
const { permission } = await permissionService.getOrgPermission(
|
||||
actor.type,
|
||||
actor.id,
|
||||
actor.orgId,
|
||||
actor.authMethod,
|
||||
actor.orgId
|
||||
);
|
||||
|
||||
// TODO: will need to change if we add support for ldap, oidc, etc.
|
||||
ForbiddenError.from(permission).throwUnlessCan(OrgPermissionActions.Edit, OrgPermissionSubjects.Scim);
|
||||
|
||||
const mappings = await constructGroupOrgMembershipRoleMappings({
|
||||
mappingsDTO: dto.mappings,
|
||||
orgRoleDAL,
|
||||
licenseService,
|
||||
orgId: actor.orgId
|
||||
});
|
||||
|
||||
const data = await externalGroupOrgRoleMappingDAL.updateExternalGroupOrgRoleMappingForOrg(actor.orgId, mappings);
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
return {
|
||||
updateExternalGroupOrgRoleMappings,
|
||||
listExternalGroupOrgRoleMappings
|
||||
};
|
||||
};
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
export type TExternalGroupOrgMembershipRoleMappingDTO = {
|
||||
groupName: string;
|
||||
roleSlug: string;
|
||||
};
|
||||
|
||||
export type TSyncExternalGroupOrgMembershipRoleMappingsDTO = {
|
||||
mappings: TExternalGroupOrgMembershipRoleMappingDTO[];
|
||||
};
|
||||
Reference in New Issue
Block a user