mirror of
https://github.com/bitwarden/server.git
synced 2025-07-06 10:32:49 -05: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 =>
|
.Where(os =>
|
||||||
os.SponsoringOrganizationId == organization.Id ||
|
os.SponsoringOrganizationId == organization.Id ||
|
||||||
os.SponsoredOrganizationId == organization.Id);
|
os.SponsoredOrganizationId == organization.Id);
|
||||||
dbContext.RemoveRange(sponsorships.Where(os => os.CloudSponsor));
|
|
||||||
|
|
||||||
Guid? UpdatedOrgId(Guid? orgId) => orgId == organization.Id ? null : organization.Id;
|
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.SponsoredOrganizationId = UpdatedOrgId(sponsorship.SponsoredOrganizationId);
|
||||||
sponsorship.SponsoringOrganizationId = UpdatedOrgId(sponsorship.SponsoringOrganizationId);
|
sponsorship.SponsoringOrganizationId = UpdatedOrgId(sponsorship.SponsoringOrganizationId);
|
||||||
|
@ -77,7 +77,11 @@ namespace Bit.Core.Repositories.EntityFramework
|
|||||||
var sponsorships = dbContext.OrganizationSponsorships
|
var sponsorships = dbContext.OrganizationSponsorships
|
||||||
.Where(os => os.SponsoringOrganizationUserId != default &&
|
.Where(os => os.SponsoringOrganizationUserId != default &&
|
||||||
os.SponsoringOrganizationUserId.Value == organizationUserId);
|
os.SponsoringOrganizationUserId.Value == organizationUserId);
|
||||||
dbContext.RemoveRange(sponsorships);
|
foreach (var sponsorship in sponsorships)
|
||||||
|
{
|
||||||
|
sponsorship.SponsoringOrganizationUserId = null;
|
||||||
|
}
|
||||||
|
|
||||||
dbContext.Remove(orgUser);
|
dbContext.Remove(orgUser);
|
||||||
await dbContext.SaveChangesAsync();
|
await dbContext.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
@ -92,7 +96,11 @@ namespace Bit.Core.Repositories.EntityFramework
|
|||||||
var sponsorships = dbContext.OrganizationSponsorships
|
var sponsorships = dbContext.OrganizationSponsorships
|
||||||
.Where(os => os.SponsoringOrganizationUserId != default &&
|
.Where(os => os.SponsoringOrganizationUserId != default &&
|
||||||
organizationUserIds.Contains(os.SponsoringOrganizationUserId ?? default));
|
organizationUserIds.Contains(os.SponsoringOrganizationUserId ?? default));
|
||||||
dbContext.RemoveRange(sponsorships);
|
foreach (var sponsorship in sponsorships)
|
||||||
|
{
|
||||||
|
sponsorship.SponsoringOrganizationUserId = null;
|
||||||
|
}
|
||||||
|
|
||||||
dbContext.RemoveRange(entities);
|
dbContext.RemoveRange(entities);
|
||||||
await dbContext.SaveChangesAsync();
|
await dbContext.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
@ -9,23 +9,13 @@ BEGIN
|
|||||||
SET
|
SET
|
||||||
[SponsoringOrganizationId] = NULL
|
[SponsoringOrganizationId] = NULL
|
||||||
WHERE
|
WHERE
|
||||||
[SponsoringOrganizationId] = @OrganizationId AND
|
[SponsoringOrganizationId] = @OrganizationId
|
||||||
[CloudSponsor] = 0
|
|
||||||
|
|
||||||
UPDATE
|
UPDATE
|
||||||
[dbo].[OrganizationSponsorship]
|
[dbo].[OrganizationSponsorship]
|
||||||
SET
|
SET
|
||||||
[SponsoredOrganizationId] = NULL
|
[SponsoredOrganizationId] = NULL
|
||||||
WHERE
|
WHERE
|
||||||
[SponsoredOrganizationId] = @OrganizationId AND
|
[SponsoredOrganizationId] = @OrganizationId
|
||||||
[CloudSponsor] = 0
|
|
||||||
|
|
||||||
DELETE
|
|
||||||
FROM
|
|
||||||
[dbo].[OrganizationSponsorship]
|
|
||||||
WHERE
|
|
||||||
[CloudSponsor] = 1 AND
|
|
||||||
([SponsoredOrganizationId] = @OrganizationId OR
|
|
||||||
[SponsoringOrganizationId] = @OrganizationId)
|
|
||||||
END
|
END
|
||||||
GO
|
GO
|
||||||
|
@ -4,9 +4,12 @@ AS
|
|||||||
BEGIN
|
BEGIN
|
||||||
SET NOCOUNT ON
|
SET NOCOUNT ON
|
||||||
|
|
||||||
DELETE
|
UPDATE
|
||||||
|
OS
|
||||||
|
SET
|
||||||
|
[SponsoringOrganizationUserId] = NULL
|
||||||
FROM
|
FROM
|
||||||
[dbo].[OrganizationSponsorship]
|
[dbo].[OrganizationSponsorship] OS
|
||||||
WHERE
|
WHERE
|
||||||
[SponsoringOrganizationUserId] = @OrganizationUserId
|
[SponsoringOrganizationUserId] = @OrganizationUserId
|
||||||
END
|
END
|
||||||
|
@ -4,22 +4,13 @@ AS
|
|||||||
BEGIN
|
BEGIN
|
||||||
SET NOCOUNT ON
|
SET NOCOUNT ON
|
||||||
|
|
||||||
DECLARE @BatchSize AS INT;
|
UPDATE
|
||||||
SET @BatchSize = 100;
|
OS
|
||||||
|
SET
|
||||||
WHILE @BatchSize > 0
|
[SponsoringOrganizationUserId] = NULL
|
||||||
BEGIN
|
|
||||||
BEGIN TRANSACTION OrganizationSponsorship_DeleteOUs
|
|
||||||
|
|
||||||
DELETE TOP(@BatchSize) OS
|
|
||||||
FROM
|
FROM
|
||||||
[dbo].[OrganizationSponsorship] OS
|
[dbo].[OrganizationSponsorship] OS
|
||||||
INNER JOIN
|
INNER JOIN
|
||||||
@SponsoringOrganizationUserIds I ON I.Id = OS.SponsoringOrganizationUserId
|
@SponsoringOrganizationUserIds I ON I.Id = OS.SponsoringOrganizationUserId
|
||||||
|
|
||||||
SET @BatchSize = @@ROWCOUNT
|
|
||||||
|
|
||||||
COMMIT TRANSACTION OrganizationSponsorship_DeleteOUs
|
|
||||||
END
|
|
||||||
END
|
END
|
||||||
GO
|
GO
|
||||||
|
@ -0,0 +1,86 @@
|
|||||||
|
-- OrganizationSponsorship_OrganizationDeleted
|
||||||
|
IF OBJECT_ID('[dbo].[OrganizationSponsorship_OrganizationDeleted]') IS NOT NULL
|
||||||
|
BEGIN
|
||||||
|
DROP PROCEDURE [dbo].[OrganizationSponsorship_OrganizationDeleted]
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
CREATE PROCEDURE [dbo].[OrganizationSponsorship_OrganizationDeleted]
|
||||||
|
@OrganizationId UNIQUEIDENTIFIER
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
UPDATE
|
||||||
|
[dbo].[OrganizationSponsorship]
|
||||||
|
SET
|
||||||
|
[SponsoringOrganizationId] = NULL
|
||||||
|
WHERE
|
||||||
|
[SponsoringOrganizationId] = @OrganizationId AND
|
||||||
|
[CloudSponsor] = 0
|
||||||
|
|
||||||
|
UPDATE
|
||||||
|
[dbo].[OrganizationSponsorship]
|
||||||
|
SET
|
||||||
|
[SponsoredOrganizationId] = NULL
|
||||||
|
WHERE
|
||||||
|
[SponsoredOrganizationId] = @OrganizationId AND
|
||||||
|
[CloudSponsor] = 0
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
FROM
|
||||||
|
[dbo].[OrganizationSponsorship]
|
||||||
|
WHERE
|
||||||
|
[CloudSponsor] = 1 AND
|
||||||
|
([SponsoredOrganizationId] = @OrganizationId OR
|
||||||
|
[SponsoringOrganizationId] = @OrganizationId)
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
-- OrganizationSponsorship_OrganizationUserDeleted
|
||||||
|
IF OBJECT_ID('[dbo].[OrganizationSponsorship_OrganizationUserDeleted]') IS NOT NULL
|
||||||
|
BEGIN
|
||||||
|
DROP PROCEDURE [dbo].[OrganizationSponsorship_OrganizationUserDeleted]
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
CREATE PROCEDURE [dbo].[OrganizationSponsorship_OrganizationUserDeleted]
|
||||||
|
@OrganizationUserId UNIQUEIDENTIFIER
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
UPDATE
|
||||||
|
OS
|
||||||
|
SET
|
||||||
|
[SponsoringOrganizationUserId] = NULL
|
||||||
|
FROM
|
||||||
|
[dbo].[OrganizationSponsorship] OS
|
||||||
|
WHERE
|
||||||
|
[SponsoringOrganizationUserId] = @OrganizationUserId
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
-- OrganizationSponsorship_OrganizationUsersDeleted
|
||||||
|
IF OBJECT_ID('[dbo].[OrganizationSponsorship_OrganizationUsersDeleted]') IS NOT NULL
|
||||||
|
BEGIN
|
||||||
|
DROP PROCEDURE [dbo].[OrganizationSponsorship_OrganizationUsersDeleted]
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
CREATE PROCEDURE [dbo].[OrganizationSponsorship_OrganizationUsersDeleted]
|
||||||
|
@SponsoringOrganizationUserIds [dbo].[GuidIdArray] READONLY
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
UPDATE
|
||||||
|
OS
|
||||||
|
SET
|
||||||
|
[SponsoringOrganizationUserId] = NULL
|
||||||
|
FROM
|
||||||
|
[dbo].[OrganizationSponsorship] OS
|
||||||
|
INNER JOIN
|
||||||
|
@SponsoringOrganizationUserIds I ON I.Id = OS.SponsoringOrganizationUserId
|
||||||
|
END
|
||||||
|
GO
|
Reference in New Issue
Block a user