mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-07 09:28:06 +00:00
add workspace-memberships api
This commit is contained in:
@@ -14,11 +14,13 @@ import {
|
|||||||
MembershipOrg,
|
MembershipOrg,
|
||||||
Organization,
|
Organization,
|
||||||
Workspace,
|
Workspace,
|
||||||
IncidentContactOrg
|
IncidentContactOrg,
|
||||||
|
IMembershipOrg
|
||||||
} from '../../models';
|
} from '../../models';
|
||||||
import { createOrganization as create } from '../../helpers/organization';
|
import { createOrganization as create } from '../../helpers/organization';
|
||||||
import { addMembershipsOrg } from '../../helpers/membershipOrg';
|
import { addMembershipsOrg } from '../../helpers/membershipOrg';
|
||||||
import { OWNER, ACCEPTED } from '../../variables';
|
import { OWNER, ACCEPTED } from '../../variables';
|
||||||
|
import _ from 'lodash';
|
||||||
|
|
||||||
export const getOrganizations = async (req: Request, res: Response) => {
|
export const getOrganizations = async (req: Request, res: Response) => {
|
||||||
let organizations;
|
let organizations;
|
||||||
@@ -382,3 +384,42 @@ export const getOrganizationSubscriptions = async (
|
|||||||
subscriptions
|
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);
|
||||||
|
};
|
||||||
@@ -156,4 +156,19 @@ router.get(
|
|||||||
organizationController.getOrganizationSubscriptions
|
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;
|
export default router;
|
||||||
|
|||||||
Reference in New Issue
Block a user