Fix race condition on identity/group membership check

This commit is contained in:
Carlos Monastyrski
2025-11-20 18:11:46 -03:00
parent 56f8245cf3
commit 36059051b1
2 changed files with 26 additions and 19 deletions

View File

@@ -93,15 +93,6 @@ export const membershipGroupServiceFactory = ({
}
const scopeDatabaseFields = factory.getScopeDatabaseFields(dto.scopeData);
const existingMembership = await membershipGroupDAL.findOne({
scope: scopeData.scope,
...scopeDatabaseFields,
actorGroupId: dto.data.groupId
});
if (existingMembership)
throw new BadRequestError({
message: "Group is already a member"
});
await factory.onCreateMembershipGroupGuard(dto);
@@ -122,6 +113,19 @@ export const membershipGroupServiceFactory = ({
const customRolesGroupBySlug = groupBy(customRoles, ({ slug }) => slug);
const membership = await membershipGroupDAL.transaction(async (tx) => {
const existingMembership = await membershipGroupDAL.findOne(
{
scope: scopeData.scope,
...scopeDatabaseFields,
actorGroupId: dto.data.groupId
},
tx
);
if (existingMembership)
throw new BadRequestError({
message: "Group is already a member"
});
const doc = await membershipGroupDAL.create(
{
scope: scopeData.scope,

View File

@@ -102,19 +102,22 @@ export const membershipIdentityServiceFactory = ({
throw new NotFoundError({ message: "One or more custom roles not found" });
}
const existingMembership = await membershipIdentityDAL.findOne({
scope: scopeData.scope,
...scopeDatabaseFields,
actorIdentityId: dto.data.identityId
});
if (existingMembership)
throw new BadRequestError({
message: "Identity is already a member"
});
const customRolesGroupBySlug = groupBy(customRoles, ({ slug }) => slug);
const membership = await membershipIdentityDAL.transaction(async (tx) => {
const existingMembership = await membershipIdentityDAL.findOne(
{
scope: scopeData.scope,
...scopeDatabaseFields,
actorIdentityId: dto.data.identityId
},
tx
);
if (existingMembership)
throw new BadRequestError({
message: "Identity is already a member"
});
const doc = await membershipIdentityDAL.create(
{
scope: scopeData.scope,