Skip to content

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

add workspace-memberships api

Maidul Islam2f1a671121cc
diff --git a/backend/src/controllers/v1/organizationController.ts b/backend/src/controllers/v1/organizationController.ts
index 6fbccffebf..eaf58fad71 100644
--- a/backend/src/controllers/v1/organizationController.ts
+++ b/backend/src/controllers/v1/organizationController.ts
@@ -14,11 +14,13 @@ import {
MembershipOrg,
Organization,
Workspace,
- IncidentContactOrg
+ IncidentContactOrg,
+ IMembershipOrg
} from '../../models';
import { createOrganization as create } from '../../helpers/organization';
import { addMembershipsOrg } from '../../helpers/membershipOrg';
import { OWNER, ACCEPTED } from '../../variables';
+import _ from 'lodash';
export const getOrganizations = async (req: Request, res: Response) => {
let organizations;
@@ -382,3 +384,42 @@ export const getOrganizationSubscriptions = async (
subscriptions
});
};
+
+
+/**
+ * Given a org id, return the projects each member of the org belongs to
+ * @param req
+ * @param res
+ * @returns
+ */
+export const getOrganizationMembersAndTheirWorkspaces = async (
+ req: Request,
+ res: Response
+) => {
+ const { organizationId } = req.params;
+ const orgMemberships = await MembershipOrg.find({ organization: organizationId });
+ const userIds = orgMemberships.map(orgMembership => orgMembership.user);
+ const memberships = await Membership.find({ user: { $in: userIds } });
+ const userToWorkspaceIds: any = {};
+
+ memberships.forEach(membership => {
+ const user = membership.user.toString();
+ if (userToWorkspaceIds[user]) {
+ userToWorkspaceIds[user].push(membership.workspace);
+ } else {
+ userToWorkspaceIds[user] = [membership.workspace];
+ }
+ });
+
+ const workspaceIds = Object.values(userToWorkspaceIds).flat()
+ const workspacesList = await Workspace.find({
+ organization: organizationId,
+ _id: { $in: workspaceIds }
+ });
+
+ const populatedUserWorkspaces = _.mapValues(userToWorkspaceIds, workspaceIds =>
+ _.map(workspaceIds, id => _.find(workspacesList, { _id: id }))
+ );
+
+ return res.json(populatedUserWorkspaces);
+};
\ No newline at end of file
diff --git a/backend/src/routes/v1/organization.ts b/backend/src/routes/v1/organization.ts
index 65a4b373fa..314f684ad4 100644
--- a/backend/src/routes/v1/organization.ts
+++ b/backend/src/routes/v1/organization.ts
@@ -156,4 +156,19 @@ router.get(
organizationController.getOrganizationSubscriptions
);
+router.get(
+ '/:organizationId/workspace-memberships',
+ requireAuth({
+ acceptedAuthModes: ['jwt']
+ }),
+ requireOrganizationAuth({
+ acceptedRoles: [OWNER, ADMIN, MEMBER],
+ acceptedStatuses: [ACCEPTED]
+ }),
+ param('organizationId').exists().trim(),
+ validateRequest,
+ organizationController.getOrganizationMembersAndTheirWorkspaces
+);
+
+
export default router;