From 8f70dd98ba4105b3f7ad50be3c09c5c07671acd2 Mon Sep 17 00:00:00 2001 From: Thomas Rittson <31796059+eliykat@users.noreply.github.com> Date: Thu, 11 Jul 2024 08:01:39 +1000 Subject: [PATCH] [AC-2732] Remove AccessAll - Bump account revision date sprocs (#4490) * Remove AccessAll logic from bump account revision date sprocs and corresponding EF methods --- .../Repositories/DatabaseContextExtensions.cs | 32 +++--- ...rBumpAccountRevisionDateByCipherIdQuery.cs | 16 ++- ...User_BumpAccountRevisionDateByCipherId.sql | 8 +- ..._BumpAccountRevisionDateByCollectionId.sql | 8 +- ...BumpAccountRevisionDateByCollectionIds.sql | 10 +- ...BumpAccountRevisionDateRemoveAccessAll.sql | 107 ++++++++++++++++++ 6 files changed, 138 insertions(+), 43 deletions(-) create mode 100644 util/Migrator/DbScripts/2024-07-11_00_BumpAccountRevisionDateRemoveAccessAll.sql diff --git a/src/Infrastructure.EntityFramework/Repositories/DatabaseContextExtensions.cs b/src/Infrastructure.EntityFramework/Repositories/DatabaseContextExtensions.cs index 223a47d2b..baffa983e 100644 --- a/src/Infrastructure.EntityFramework/Repositories/DatabaseContextExtensions.cs +++ b/src/Infrastructure.EntityFramework/Repositories/DatabaseContextExtensions.cs @@ -48,26 +48,24 @@ public static class DatabaseContextExtensions join ou in context.OrganizationUsers on u.Id equals ou.UserId join cu in context.CollectionUsers - on new { ou.AccessAll, OrganizationUserId = ou.Id, CollectionId = collectionId } equals - new { AccessAll = false, cu.OrganizationUserId, cu.CollectionId } into cu_g + on new { OrganizationUserId = ou.Id, CollectionId = collectionId } equals + new { cu.OrganizationUserId, cu.CollectionId } into cu_g from cu in cu_g.DefaultIfEmpty() join gu in context.GroupUsers - on new { CollectionId = (Guid?)cu.CollectionId, ou.AccessAll, OrganizationUserId = ou.Id } equals - new { CollectionId = (Guid?)null, AccessAll = false, gu.OrganizationUserId } into gu_g + on new { CollectionId = (Guid?)cu.CollectionId, OrganizationUserId = ou.Id } equals + new { CollectionId = (Guid?)null, gu.OrganizationUserId } into gu_g from gu in gu_g.DefaultIfEmpty() join g in context.Groups on gu.GroupId equals g.Id into g_g from g in g_g.DefaultIfEmpty() join cg in context.CollectionGroups - on new { g.AccessAll, gu.GroupId, CollectionId = collectionId } equals - new { AccessAll = false, cg.GroupId, cg.CollectionId } into cg_g + on new { gu.GroupId, CollectionId = collectionId } equals + new { cg.GroupId, cg.CollectionId } into cg_g from cg in cg_g.DefaultIfEmpty() where ou.OrganizationId == organizationId && ou.Status == OrganizationUserStatusType.Confirmed && (cu.CollectionId != null || - cg.CollectionId != null || - ou.AccessAll == true || - g.AccessAll == true) + cg.CollectionId != null) select u; var users = await query.ToListAsync(); @@ -81,26 +79,24 @@ public static class DatabaseContextExtensions join ou in context.OrganizationUsers on u.Id equals ou.UserId join cu in context.CollectionUsers - on new { ou.AccessAll, OrganizationUserId = ou.Id, CollectionId = c.Id } equals - new { AccessAll = false, cu.OrganizationUserId, cu.CollectionId } into cu_g + on new { OrganizationUserId = ou.Id, CollectionId = c.Id } equals + new { cu.OrganizationUserId, cu.CollectionId } into cu_g from cu in cu_g.DefaultIfEmpty() join gu in context.GroupUsers - on new { CollectionId = (Guid?)cu.CollectionId, ou.AccessAll, OrganizationUserId = ou.Id } equals - new { CollectionId = (Guid?)null, AccessAll = false, gu.OrganizationUserId } into gu_g + on new { CollectionId = (Guid?)cu.CollectionId, OrganizationUserId = ou.Id } equals + new { CollectionId = (Guid?)null, gu.OrganizationUserId } into gu_g from gu in gu_g.DefaultIfEmpty() join g in context.Groups on gu.GroupId equals g.Id into g_g from g in g_g.DefaultIfEmpty() join cg in context.CollectionGroups - on new { g.AccessAll, gu.GroupId, CollectionId = c.Id } equals - new { AccessAll = false, cg.GroupId, cg.CollectionId } into cg_g + on new { gu.GroupId, CollectionId = c.Id } equals + new { cg.GroupId, cg.CollectionId } into cg_g from cg in cg_g.DefaultIfEmpty() where ou.OrganizationId == organizationId && collectionIds.Contains(c.Id) && ou.Status == OrganizationUserStatusType.Confirmed && (cu.CollectionId != null || - cg.CollectionId != null || - ou.AccessAll == true || - g.AccessAll == true) + cg.CollectionId != null) select u; var users = await query.ToListAsync(); diff --git a/src/Infrastructure.EntityFramework/Repositories/Queries/UserBumpAccountRevisionDateByCipherIdQuery.cs b/src/Infrastructure.EntityFramework/Repositories/Queries/UserBumpAccountRevisionDateByCipherIdQuery.cs index 55195d09b..ce018313d 100644 --- a/src/Infrastructure.EntityFramework/Repositories/Queries/UserBumpAccountRevisionDateByCipherIdQuery.cs +++ b/src/Infrastructure.EntityFramework/Repositories/Queries/UserBumpAccountRevisionDateByCipherIdQuery.cs @@ -26,13 +26,13 @@ public class UserBumpAccountRevisionDateByCipherIdQuery : IQuery from cc in cc_g.DefaultIfEmpty() join collectionUser in dbContext.CollectionUsers - on new { ou.AccessAll, OrganizationUserId = ou.Id, cc.CollectionId } equals - new { AccessAll = false, collectionUser.OrganizationUserId, collectionUser.CollectionId } into cu_g + on new { OrganizationUserId = ou.Id, cc.CollectionId } equals + new { collectionUser.OrganizationUserId, collectionUser.CollectionId } into cu_g from cu in cu_g.DefaultIfEmpty() join groupUser in dbContext.GroupUsers - on new { CollectionId = (Guid?)cu.CollectionId, ou.AccessAll, OrganizationUserId = ou.Id } equals - new { CollectionId = (Guid?)null, AccessAll = false, groupUser.OrganizationUserId } into gu_g + on new { CollectionId = (Guid?)cu.CollectionId, OrganizationUserId = ou.Id } equals + new { CollectionId = (Guid?)null, groupUser.OrganizationUserId } into gu_g from gu in gu_g.DefaultIfEmpty() join grp in dbContext.Groups @@ -40,16 +40,14 @@ public class UserBumpAccountRevisionDateByCipherIdQuery : IQuery from g in g_g.DefaultIfEmpty() join collectionGroup in dbContext.CollectionGroups - on new { g.AccessAll, gu.GroupId, cc.CollectionId } equals - new { AccessAll = false, collectionGroup.GroupId, collectionGroup.CollectionId } into cg_g + on new { gu.GroupId, cc.CollectionId } equals + new { collectionGroup.GroupId, collectionGroup.CollectionId } into cg_g from cg in cg_g.DefaultIfEmpty() where ou.OrganizationId == _organizationId && ou.Status == OrganizationUserStatusType.Confirmed && (cu.CollectionId != null || - cg.CollectionId != null || - ou.AccessAll || - g.AccessAll) + cg.CollectionId != null) select u; return query; } diff --git a/src/Sql/dbo/Stored Procedures/User_BumpAccountRevisionDateByCipherId.sql b/src/Sql/dbo/Stored Procedures/User_BumpAccountRevisionDateByCipherId.sql index 5b96db54a..1d0304028 100644 --- a/src/Sql/dbo/Stored Procedures/User_BumpAccountRevisionDateByCipherId.sql +++ b/src/Sql/dbo/Stored Procedures/User_BumpAccountRevisionDateByCipherId.sql @@ -16,20 +16,18 @@ BEGIN LEFT JOIN [dbo].[CollectionCipher] CC ON CC.[CipherId] = @CipherId LEFT JOIN - [dbo].[CollectionUser] CU ON OU.[AccessAll] = 0 AND CU.[OrganizationUserId] = OU.[Id] AND CU.[CollectionId] = CC.[CollectionId] + [dbo].[CollectionUser] CU ON CU.[OrganizationUserId] = OU.[Id] AND CU.[CollectionId] = CC.[CollectionId] LEFT JOIN - [dbo].[GroupUser] GU ON CU.[CollectionId] IS NULL AND OU.[AccessAll] = 0 AND GU.[OrganizationUserId] = OU.[Id] + [dbo].[GroupUser] GU ON CU.[CollectionId] IS NULL AND GU.[OrganizationUserId] = OU.[Id] LEFT JOIN [dbo].[Group] G ON G.[Id] = GU.[GroupId] LEFT JOIN - [dbo].[CollectionGroup] CG ON G.[AccessAll] = 0 AND CG.[GroupId] = GU.[GroupId] AND CG.[CollectionId] = CC.[CollectionId] + [dbo].[CollectionGroup] CG ON CG.[GroupId] = GU.[GroupId] AND CG.[CollectionId] = CC.[CollectionId] WHERE OU.[OrganizationId] = @OrganizationId AND OU.[Status] = 2 -- 2 = Confirmed AND ( CU.[CollectionId] IS NOT NULL OR CG.[CollectionId] IS NOT NULL - OR OU.[AccessAll] = 1 - OR G.[AccessAll] = 1 ) END diff --git a/src/Sql/dbo/Stored Procedures/User_BumpAccountRevisionDateByCollectionId.sql b/src/Sql/dbo/Stored Procedures/User_BumpAccountRevisionDateByCollectionId.sql index f001a54cd..bb6df18e8 100644 --- a/src/Sql/dbo/Stored Procedures/User_BumpAccountRevisionDateByCollectionId.sql +++ b/src/Sql/dbo/Stored Procedures/User_BumpAccountRevisionDateByCollectionId.sql @@ -14,20 +14,18 @@ BEGIN INNER JOIN [dbo].[OrganizationUser] OU ON OU.[UserId] = U.[Id] LEFT JOIN - [dbo].[CollectionUser] CU ON OU.[AccessAll] = 0 AND CU.[OrganizationUserId] = OU.[Id] AND CU.[CollectionId] = @CollectionId + [dbo].[CollectionUser] CU ON CU.[OrganizationUserId] = OU.[Id] AND CU.[CollectionId] = @CollectionId LEFT JOIN - [dbo].[GroupUser] GU ON CU.[CollectionId] IS NULL AND OU.[AccessAll] = 0 AND GU.[OrganizationUserId] = OU.[Id] + [dbo].[GroupUser] GU ON CU.[CollectionId] IS NULL AND GU.[OrganizationUserId] = OU.[Id] LEFT JOIN [dbo].[Group] G ON G.[Id] = GU.[GroupId] LEFT JOIN - [dbo].[CollectionGroup] CG ON G.[AccessAll] = 0 AND CG.[GroupId] = GU.[GroupId] AND CG.[CollectionId] = @CollectionId + [dbo].[CollectionGroup] CG ON CG.[GroupId] = GU.[GroupId] AND CG.[CollectionId] = @CollectionId WHERE OU.[OrganizationId] = @OrganizationId AND OU.[Status] = 2 -- 2 = Confirmed AND ( CU.[CollectionId] IS NOT NULL OR CG.[CollectionId] IS NOT NULL - OR OU.[AccessAll] = 1 - OR G.[AccessAll] = 1 ) END diff --git a/src/Sql/dbo/Stored Procedures/User_BumpAccountRevisionDateByCollectionIds.sql b/src/Sql/dbo/Stored Procedures/User_BumpAccountRevisionDateByCollectionIds.sql index d027708a6..430dcec88 100644 --- a/src/Sql/dbo/Stored Procedures/User_BumpAccountRevisionDateByCollectionIds.sql +++ b/src/Sql/dbo/Stored Procedures/User_BumpAccountRevisionDateByCollectionIds.sql @@ -16,20 +16,18 @@ SET INNER JOIN [dbo].[OrganizationUser] OU ON OU.[UserId] = U.[Id] LEFT JOIN - [dbo].[CollectionUser] CU ON OU.[AccessAll] = 0 AND CU.[OrganizationUserId] = OU.[Id] AND CU.[CollectionId] = C.[Id] + [dbo].[CollectionUser] CU ON CU.[OrganizationUserId] = OU.[Id] AND CU.[CollectionId] = C.[Id] LEFT JOIN - [dbo].[GroupUser] GU ON CU.[CollectionId] IS NULL AND OU.[AccessAll] = 0 AND GU.[OrganizationUserId] = OU.[Id] + [dbo].[GroupUser] GU ON CU.[CollectionId] IS NULL AND GU.[OrganizationUserId] = OU.[Id] LEFT JOIN [dbo].[Group] G ON G.[Id] = GU.[GroupId] LEFT JOIN - [dbo].[CollectionGroup] CG ON G.[AccessAll] = 0 AND CG.[GroupId] = GU.[GroupId] AND CG.[CollectionId] = C.[Id] + [dbo].[CollectionGroup] CG ON CG.[GroupId] = GU.[GroupId] AND CG.[CollectionId] = C.[Id] WHERE OU.[OrganizationId] = @OrganizationId AND OU.[Status] = 2 -- 2 = Confirmed AND ( CU.[CollectionId] IS NOT NULL OR CG.[CollectionId] IS NOT NULL - OR OU.[AccessAll] = 1 - OR G.[AccessAll] = 1 - ) + ) END diff --git a/util/Migrator/DbScripts/2024-07-11_00_BumpAccountRevisionDateRemoveAccessAll.sql b/util/Migrator/DbScripts/2024-07-11_00_BumpAccountRevisionDateRemoveAccessAll.sql new file mode 100644 index 000000000..827de97bf --- /dev/null +++ b/util/Migrator/DbScripts/2024-07-11_00_BumpAccountRevisionDateRemoveAccessAll.sql @@ -0,0 +1,107 @@ +-- Remove AccessAll logic from bump account revision date sprocs + +-- User_BumpAccountRevisionDateByCipherId +CREATE OR ALTER PROCEDURE [dbo].[User_BumpAccountRevisionDateByCipherId] + @CipherId UNIQUEIDENTIFIER, + @OrganizationId UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + UPDATE + U + SET + U.[AccountRevisionDate] = GETUTCDATE() + FROM + [dbo].[User] U + INNER JOIN + [dbo].[OrganizationUser] OU ON OU.[UserId] = U.[Id] + LEFT JOIN + [dbo].[CollectionCipher] CC ON CC.[CipherId] = @CipherId + LEFT JOIN + [dbo].[CollectionUser] CU ON CU.[OrganizationUserId] = OU.[Id] AND CU.[CollectionId] = CC.[CollectionId] + LEFT JOIN + [dbo].[GroupUser] GU ON CU.[CollectionId] IS NULL AND GU.[OrganizationUserId] = OU.[Id] + LEFT JOIN + [dbo].[Group] G ON G.[Id] = GU.[GroupId] + LEFT JOIN + [dbo].[CollectionGroup] CG ON CG.[GroupId] = GU.[GroupId] AND CG.[CollectionId] = CC.[CollectionId] + WHERE + OU.[OrganizationId] = @OrganizationId + AND OU.[Status] = 2 -- 2 = Confirmed + AND ( + CU.[CollectionId] IS NOT NULL + OR CG.[CollectionId] IS NOT NULL + ) +END +GO + +-- User_BumpAccountRevisionDateByCollectionId +CREATE OR ALTER PROCEDURE [dbo].[User_BumpAccountRevisionDateByCollectionId] + @CollectionId UNIQUEIDENTIFIER, + @OrganizationId UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + UPDATE + U + SET + U.[AccountRevisionDate] = GETUTCDATE() + FROM + [dbo].[User] U + INNER JOIN + [dbo].[OrganizationUser] OU ON OU.[UserId] = U.[Id] + LEFT JOIN + [dbo].[CollectionUser] CU ON CU.[OrganizationUserId] = OU.[Id] AND CU.[CollectionId] = @CollectionId + LEFT JOIN + [dbo].[GroupUser] GU ON CU.[CollectionId] IS NULL AND GU.[OrganizationUserId] = OU.[Id] + LEFT JOIN + [dbo].[Group] G ON G.[Id] = GU.[GroupId] + LEFT JOIN + [dbo].[CollectionGroup] CG ON CG.[GroupId] = GU.[GroupId] AND CG.[CollectionId] = @CollectionId + WHERE + OU.[OrganizationId] = @OrganizationId + AND OU.[Status] = 2 -- 2 = Confirmed + AND ( + CU.[CollectionId] IS NOT NULL + OR CG.[CollectionId] IS NOT NULL + ) +END +GO + +-- User_BumpAccountRevisionDateByCollectionIds +CREATE OR ALTER PROCEDURE [dbo].[User_BumpAccountRevisionDateByCollectionIds] + @CollectionIds AS [dbo].[GuidIdArray] READONLY, + @OrganizationId UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + +UPDATE + U +SET + U.[AccountRevisionDate] = GETUTCDATE() + FROM + [dbo].[User] U + INNER JOIN + [dbo].[Collection] C ON C.[Id] IN (SELECT [Id] FROM @CollectionIds) + INNER JOIN + [dbo].[OrganizationUser] OU ON OU.[UserId] = U.[Id] + LEFT JOIN + [dbo].[CollectionUser] CU ON CU.[OrganizationUserId] = OU.[Id] AND CU.[CollectionId] = C.[Id] + LEFT JOIN + [dbo].[GroupUser] GU ON CU.[CollectionId] IS NULL AND GU.[OrganizationUserId] = OU.[Id] + LEFT JOIN + [dbo].[Group] G ON G.[Id] = GU.[GroupId] + LEFT JOIN + [dbo].[CollectionGroup] CG ON CG.[GroupId] = GU.[GroupId] AND CG.[CollectionId] = C.[Id] +WHERE + OU.[OrganizationId] = @OrganizationId + AND OU.[Status] = 2 -- 2 = Confirmed + AND ( + CU.[CollectionId] IS NOT NULL + OR CG.[CollectionId] IS NOT NULL + ) +END +GO