Skip to content

Commit

Permalink
[AC-2733] Remove AccessAll - misc sprocs (#4477)
Browse files Browse the repository at this point in the history
Remove AccessAll logic from miscellaneous sprocs
and corresponding EF queries
  • Loading branch information
eliykat committed Jul 10, 2024
1 parent fa5da78 commit 4ab608a
Show file tree
Hide file tree
Showing 9 changed files with 306 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ await dbContext.OrganizationUsers
from ou in dbContext.OrganizationUsers
join cu in dbContext.CollectionUsers
on ou.Id equals cu.OrganizationUserId
where !ou.AccessAll &&
ou.Id == id
where ou.Id == id
select cu).ToListAsync();
var collections = query.Select(cu => new CollectionAccessSelection
{
Expand Down Expand Up @@ -257,7 +256,7 @@ public async Task<Tuple<OrganizationUserUserDetails, ICollection<CollectionAcces
var dbContext = GetDatabaseContext(scope);
var query = from ou in dbContext.OrganizationUsers
join cu in dbContext.CollectionUsers on ou.Id equals cu.OrganizationUserId
where !ou.AccessAll && ou.Id == id
where ou.Id == id
select cu;
var collections = await query.Select(cu => new CollectionAccessSelection
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ join ou in dbContext.OrganizationUsers
from ou in ou_g.DefaultIfEmpty()

join cc in dbContext.CollectionCiphers
on new { c.UserId, ou.AccessAll, CipherId = c.Id } equals
new { UserId = (Guid?)null, AccessAll = false, cc.CipherId } into cc_g
on new { c.UserId, CipherId = c.Id } equals
new { UserId = (Guid?)null, cc.CipherId } into cc_g
from cc in cc_g.DefaultIfEmpty()

join cu in dbContext.CollectionUsers
Expand All @@ -41,17 +41,17 @@ join cu in dbContext.CollectionUsers
from cu in cu_g.DefaultIfEmpty()

join gu in dbContext.GroupUsers
on new { c.UserId, CollectionId = (Guid?)cu.CollectionId, ou.AccessAll, OrganizationUserId = ou.Id } equals
new { UserId = (Guid?)null, CollectionId = (Guid?)null, AccessAll = false, gu.OrganizationUserId } into gu_g
on new { c.UserId, CollectionId = (Guid?)cu.CollectionId, OrganizationUserId = ou.Id } equals
new { UserId = (Guid?)null, CollectionId = (Guid?)null, gu.OrganizationUserId } into gu_g
from gu in gu_g.DefaultIfEmpty()

join g in dbContext.Groups
on gu.GroupId equals g.Id into g_g
from g in g_g.DefaultIfEmpty()

join cg in dbContext.CollectionGroups
on new { g.AccessAll, cc.CollectionId, gu.GroupId } equals
new { AccessAll = false, cg.CollectionId, cg.GroupId } into cg_g
on new { cc.CollectionId, gu.GroupId } equals
new { cg.CollectionId, cg.GroupId } into cg_g
from cg in cg_g.DefaultIfEmpty()

where
Expand All @@ -60,10 +60,10 @@ from cg in cg_g.DefaultIfEmpty()
c.UserId == _userId ||
(
!c.UserId.HasValue && ou.Status == OrganizationUserStatusType.Confirmed && o.Enabled &&
(ou.AccessAll || cu.CollectionId != null || g.AccessAll || cg.CollectionId != null)
(cu.CollectionId != null || cg.CollectionId != null)

Check warning on line 63 in src/Infrastructure.EntityFramework/Vault/Repositories/Queries/CipherReadCanEditByIdUserIdQuery.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (EventsProcessor, ./src)

The result of the expression is always 'true' since a value of type 'Guid' is never equal to 'null' of type 'Guid?'

Check warning on line 63 in src/Infrastructure.EntityFramework/Vault/Repositories/Queries/CipherReadCanEditByIdUserIdQuery.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (EventsProcessor, ./src)

The result of the expression is always 'true' since a value of type 'Guid' is never equal to 'null' of type 'Guid?'

Check warning on line 63 in src/Infrastructure.EntityFramework/Vault/Repositories/Queries/CipherReadCanEditByIdUserIdQuery.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Notifications, ./src)

The result of the expression is always 'true' since a value of type 'Guid' is never equal to 'null' of type 'Guid?'

Check warning on line 63 in src/Infrastructure.EntityFramework/Vault/Repositories/Queries/CipherReadCanEditByIdUserIdQuery.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Notifications, ./src)

The result of the expression is always 'true' since a value of type 'Guid' is never equal to 'null' of type 'Guid?'

Check warning on line 63 in src/Infrastructure.EntityFramework/Vault/Repositories/Queries/CipherReadCanEditByIdUserIdQuery.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Api, ./src)

The result of the expression is always 'true' since a value of type 'Guid' is never equal to 'null' of type 'Guid?'

Check warning on line 63 in src/Infrastructure.EntityFramework/Vault/Repositories/Queries/CipherReadCanEditByIdUserIdQuery.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Api, ./src)

The result of the expression is always 'true' since a value of type 'Guid' is never equal to 'null' of type 'Guid?'

Check warning on line 63 in src/Infrastructure.EntityFramework/Vault/Repositories/Queries/CipherReadCanEditByIdUserIdQuery.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Scim, ./bitwarden_license/src, true)

The result of the expression is always 'true' since a value of type 'Guid' is never equal to 'null' of type 'Guid?'

Check warning on line 63 in src/Infrastructure.EntityFramework/Vault/Repositories/Queries/CipherReadCanEditByIdUserIdQuery.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Scim, ./bitwarden_license/src, true)

The result of the expression is always 'true' since a value of type 'Guid' is never equal to 'null' of type 'Guid?'

Check warning on line 63 in src/Infrastructure.EntityFramework/Vault/Repositories/Queries/CipherReadCanEditByIdUserIdQuery.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Identity, ./src)

The result of the expression is always 'true' since a value of type 'Guid' is never equal to 'null' of type 'Guid?'

Check warning on line 63 in src/Infrastructure.EntityFramework/Vault/Repositories/Queries/CipherReadCanEditByIdUserIdQuery.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Identity, ./src)

The result of the expression is always 'true' since a value of type 'Guid' is never equal to 'null' of type 'Guid?'

Check warning on line 63 in src/Infrastructure.EntityFramework/Vault/Repositories/Queries/CipherReadCanEditByIdUserIdQuery.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Sso, ./bitwarden_license/src, true)

The result of the expression is always 'true' since a value of type 'Guid' is never equal to 'null' of type 'Guid?'

Check warning on line 63 in src/Infrastructure.EntityFramework/Vault/Repositories/Queries/CipherReadCanEditByIdUserIdQuery.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Sso, ./bitwarden_license/src, true)

The result of the expression is always 'true' since a value of type 'Guid' is never equal to 'null' of type 'Guid?'

Check warning on line 63 in src/Infrastructure.EntityFramework/Vault/Repositories/Queries/CipherReadCanEditByIdUserIdQuery.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Billing, ./src)

The result of the expression is always 'true' since a value of type 'Guid' is never equal to 'null' of type 'Guid?'

Check warning on line 63 in src/Infrastructure.EntityFramework/Vault/Repositories/Queries/CipherReadCanEditByIdUserIdQuery.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Billing, ./src)

The result of the expression is always 'true' since a value of type 'Guid' is never equal to 'null' of type 'Guid?'

Check warning on line 63 in src/Infrastructure.EntityFramework/Vault/Repositories/Queries/CipherReadCanEditByIdUserIdQuery.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Icons, ./src)

The result of the expression is always 'true' since a value of type 'Guid' is never equal to 'null' of type 'Guid?'

Check warning on line 63 in src/Infrastructure.EntityFramework/Vault/Repositories/Queries/CipherReadCanEditByIdUserIdQuery.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Icons, ./src)

The result of the expression is always 'true' since a value of type 'Guid' is never equal to 'null' of type 'Guid?'

Check warning on line 63 in src/Infrastructure.EntityFramework/Vault/Repositories/Queries/CipherReadCanEditByIdUserIdQuery.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Events, ./src)

The result of the expression is always 'true' since a value of type 'Guid' is never equal to 'null' of type 'Guid?'

Check warning on line 63 in src/Infrastructure.EntityFramework/Vault/Repositories/Queries/CipherReadCanEditByIdUserIdQuery.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Events, ./src)

The result of the expression is always 'true' since a value of type 'Guid' is never equal to 'null' of type 'Guid?'

Check warning on line 63 in src/Infrastructure.EntityFramework/Vault/Repositories/Queries/CipherReadCanEditByIdUserIdQuery.cs

View workflow job for this annotation

GitHub Actions / Run tests

The result of the expression is always 'true' since a value of type 'Guid' is never equal to 'null' of type 'Guid?'

Check warning on line 63 in src/Infrastructure.EntityFramework/Vault/Repositories/Queries/CipherReadCanEditByIdUserIdQuery.cs

View workflow job for this annotation

GitHub Actions / Run tests

The result of the expression is always 'true' since a value of type 'Guid' is never equal to 'null' of type 'Guid?'

Check warning on line 63 in src/Infrastructure.EntityFramework/Vault/Repositories/Queries/CipherReadCanEditByIdUserIdQuery.cs

View workflow job for this annotation

GitHub Actions / Upload

The result of the expression is always 'true' since a value of type 'Guid' is never equal to 'null' of type 'Guid?'

Check warning on line 63 in src/Infrastructure.EntityFramework/Vault/Repositories/Queries/CipherReadCanEditByIdUserIdQuery.cs

View workflow job for this annotation

GitHub Actions / Upload

The result of the expression is always 'true' since a value of type 'Guid' is never equal to 'null' of type 'Guid?'
)
) &&
(c.UserId.HasValue || ou.AccessAll || !cu.ReadOnly || g.AccessAll || !cg.ReadOnly)
(c.UserId.HasValue || !cu.ReadOnly || !cg.ReadOnly)
select c;
return query;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ BEGIN

;WITH [CTE] AS (
SELECT
CASE
WHEN C.[UserId] IS NOT NULL OR OU.[AccessAll] = 1 OR CU.[ReadOnly] = 0 OR G.[AccessAll] = 1 OR CG.[ReadOnly] = 0 THEN 1
CASE
WHEN C.[UserId] IS NOT NULL OR CU.[ReadOnly] = 0 OR CG.[ReadOnly] = 0 THEN 1
ELSE 0
END [Edit]
FROM
Expand All @@ -20,15 +20,15 @@ BEGIN
LEFT JOIN
[dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId
LEFT JOIN
[dbo].[CollectionCipher] CC ON C.[UserId] IS NULL AND OU.[AccessAll] = 0 AND CC.[CipherId] = C.[Id]
[dbo].[CollectionCipher] CC ON C.[UserId] IS NULL AND CC.[CipherId] = C.[Id]
LEFT JOIN
[dbo].[CollectionUser] CU ON CU.[CollectionId] = CC.[CollectionId] AND CU.[OrganizationUserId] = OU.[Id]
LEFT JOIN
[dbo].[GroupUser] GU ON C.[UserId] IS NULL AND CU.[CollectionId] IS NULL AND OU.[AccessAll] = 0 AND GU.[OrganizationUserId] = OU.[Id]
[dbo].[GroupUser] GU ON C.[UserId] IS NULL AND 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.[CollectionId] = CC.[CollectionId] AND CG.[GroupId] = GU.[GroupId]
[dbo].[CollectionGroup] CG ON CG.[CollectionId] = CC.[CollectionId] AND CG.[GroupId] = GU.[GroupId]
WHERE
C.Id = @Id
AND (
Expand All @@ -38,9 +38,7 @@ BEGIN
AND OU.[Status] = 2 -- 2 = Confirmed
AND O.[Enabled] = 1
AND (
OU.[AccessAll] = 1
OR CU.[CollectionId] IS NOT NULL
OR G.[AccessAll] = 1
CU.[CollectionId] IS NOT NULL
OR CG.[CollectionId] IS NOT NULL
)
)
Expand All @@ -57,4 +55,4 @@ BEGIN
[Edit] = 1

SELECT @CanEdit
END
END
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,19 @@ BEGIN
INNER JOIN
[dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId
LEFT JOIN
[dbo].[CollectionUser] CU ON OU.[AccessAll] = 0 AND CU.[CollectionId] = C.[Id] AND CU.[OrganizationUserId] = OU.[Id]
[dbo].[CollectionUser] CU ON CU.[CollectionId] = C.[Id] AND CU.[OrganizationUserId] = OU.[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.[CollectionId] = C.[Id] AND CG.[GroupId] = GU.[GroupId]
[dbo].[CollectionGroup] CG ON CG.[CollectionId] = C.[Id] AND CG.[GroupId] = GU.[GroupId]
WHERE
O.[Id] = @OrganizationId
AND O.[Enabled] = 1
AND OU.[Status] = 2 -- Confirmed
AND (
OU.[AccessAll] = 1
OR CU.[ReadOnly] = 0
OR G.[AccessAll] = 1
CU.[ReadOnly] = 0
OR CG.[ReadOnly] = 0
)
END
Expand All @@ -77,4 +75,4 @@ BEGIN
[Id] IN (SELECT [Id] FROM #AvailableCollections)

RETURN(0)
END
END
15 changes: 6 additions & 9 deletions src/Sql/Vault/dbo/Stored Procedures/Folder/Folder_DeleteById.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ BEGIN
;WITH [CTE] AS (
SELECT
[Id],
[OrganizationId],
[AccessAll]
[OrganizationId]
FROM
[OrganizationUser]
WHERE
Expand All @@ -29,20 +28,18 @@ BEGIN
INNER JOIN
[dbo].[Organization] O ON O.[Id] = OU.[OrganizationId] AND O.[Id] = C.[OrganizationId] AND O.[Enabled] = 1
LEFT JOIN
[dbo].[CollectionCipher] CC ON OU.[AccessAll] = 0 AND CC.[CipherId] = C.[Id]
[dbo].[CollectionCipher] CC ON CC.[CipherId] = C.[Id]
LEFT JOIN
[dbo].[CollectionUser] CU ON CU.[CollectionId] = CC.[CollectionId] AND CU.[OrganizationUserId] = OU.[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.[CollectionId] = CC.[CollectionId] AND CG.[GroupId] = GU.[GroupId]
[dbo].[CollectionGroup] CG ON CG.[CollectionId] = CC.[CollectionId] AND CG.[GroupId] = GU.[GroupId]
WHERE
(
OU.[AccessAll] = 1
OR CU.[CollectionId] IS NOT NULL
OR G.[AccessAll] = 1
CU.[CollectionId] IS NOT NULL
OR CG.[CollectionId] IS NOT NULL
)
AND JSON_VALUE(C.[Folders], @UserIdPath) = @Id
Expand All @@ -64,4 +61,4 @@ BEGIN
[Id] = @Id

EXEC [dbo].[User_BumpAccountRevisionDate] @UserId
END
END
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ BEGIN
FROM
[dbo].[OrganizationUser] OU
INNER JOIN
[dbo].[CollectionUser] CU ON OU.[AccessAll] = 0 AND CU.[OrganizationUserId] = OU.[Id]
[dbo].[CollectionUser] CU ON CU.[OrganizationUserId] = OU.[Id]
INNER JOIN
@OrganizationUserIds OUI ON OUI.[Id] = OU.[Id]
END
END
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ BEGIN
FROM
[dbo].[OrganizationUser] OU
INNER JOIN
[dbo].[CollectionUser] CU ON OU.[AccessAll] = 0 AND CU.[OrganizationUserId] = [OU].[Id]
[dbo].[CollectionUser] CU ON CU.[OrganizationUserId] = [OU].[Id]
WHERE
[OrganizationUserId] = @Id
END
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ BEGIN
FROM
[dbo].[OrganizationUser] OU
INNER JOIN
[dbo].[CollectionUser] CU ON OU.[AccessAll] = 0 AND CU.[OrganizationUserId] = [OU].[Id]
[dbo].[CollectionUser] CU ON CU.[OrganizationUserId] = [OU].[Id]
WHERE
[OrganizationUserId] = @Id
END
Loading

0 comments on commit 4ab608a

Please sign in to comment.