mirror of
https://github.com/bitwarden/server
synced 2026-01-08 19:43:34 +00:00
Null out sponsorship values when foreign key deleted (#1733)
This allows us to maintain record of sponsorships up until they are explicitly removed. Fixes issues where removing sponsorships from organizations with invalid sponsorships would error
This commit is contained in:
@@ -107,10 +107,9 @@ namespace Bit.Core.Repositories.EntityFramework
|
||||
.Where(os =>
|
||||
os.SponsoringOrganizationId == organization.Id ||
|
||||
os.SponsoredOrganizationId == organization.Id);
|
||||
dbContext.RemoveRange(sponsorships.Where(os => os.CloudSponsor));
|
||||
|
||||
Guid? UpdatedOrgId(Guid? orgId) => orgId == organization.Id ? null : organization.Id;
|
||||
foreach (var sponsorship in sponsorships.Where(os => !os.CloudSponsor))
|
||||
foreach (var sponsorship in sponsorships)
|
||||
{
|
||||
sponsorship.SponsoredOrganizationId = UpdatedOrgId(sponsorship.SponsoredOrganizationId);
|
||||
sponsorship.SponsoringOrganizationId = UpdatedOrgId(sponsorship.SponsoringOrganizationId);
|
||||
|
||||
@@ -77,7 +77,11 @@ namespace Bit.Core.Repositories.EntityFramework
|
||||
var sponsorships = dbContext.OrganizationSponsorships
|
||||
.Where(os => os.SponsoringOrganizationUserId != default &&
|
||||
os.SponsoringOrganizationUserId.Value == organizationUserId);
|
||||
dbContext.RemoveRange(sponsorships);
|
||||
foreach (var sponsorship in sponsorships)
|
||||
{
|
||||
sponsorship.SponsoringOrganizationUserId = null;
|
||||
}
|
||||
|
||||
dbContext.Remove(orgUser);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
@@ -92,7 +96,11 @@ namespace Bit.Core.Repositories.EntityFramework
|
||||
var sponsorships = dbContext.OrganizationSponsorships
|
||||
.Where(os => os.SponsoringOrganizationUserId != default &&
|
||||
organizationUserIds.Contains(os.SponsoringOrganizationUserId ?? default));
|
||||
dbContext.RemoveRange(sponsorships);
|
||||
foreach (var sponsorship in sponsorships)
|
||||
{
|
||||
sponsorship.SponsoringOrganizationUserId = null;
|
||||
}
|
||||
|
||||
dbContext.RemoveRange(entities);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user