-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[PM-34146] Add GetManyConfirmedAcceptedByUserIdAsync(Guid userId) to the IPolicyRepository interface
#7392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JimmyVo16
wants to merge
8
commits into
main
Choose a base branch
from
ac/pm-34146/add-sprocs-for-accepted-users
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[PM-34146] Add GetManyConfirmedAcceptedByUserIdAsync(Guid userId) to the IPolicyRepository interface
#7392
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7255abc
[PM-34146] Add stored procedures for accepted users
JimmyVo16 925e8fb
[PM-34146] Add unit tests
JimmyVo16 c53e9f1
[PM-34146] Test to fix linter issue
JimmyVo16 068059a
[PM-34146] Update xmldoc
JimmyVo16 ea00622
[PM-34146] Update sproc per code review
JimmyVo16 9b2394a
[PM-34146] Fix method name per code review
JimmyVo16 1b77c85
[PM-34146] Fix method name per code review
JimmyVo16 c244a32
Merge branch 'main' into ac/pm-34146/add-sprocs-for-accepted-users
JimmyVo16 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...ramework/AdminConsole/Repositories/Queries/PolicyReadByUserIdConfirmedAndAcceptedQuery.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| ο»Ώusing Bit.Core.Enums; | ||
| using Bit.Infrastructure.EntityFramework.AdminConsole.Models; | ||
| using Bit.Infrastructure.EntityFramework.Repositories; | ||
| using Bit.Infrastructure.EntityFramework.Repositories.Queries; | ||
|
|
||
| namespace Bit.Infrastructure.EntityFramework.AdminConsole.Repositories.Queries; | ||
|
|
||
| public class PolicyReadByUserIdConfirmedAndAcceptedQuery : IQuery<Policy> | ||
| { | ||
| private readonly Guid _userId; | ||
|
|
||
| public PolicyReadByUserIdConfirmedAndAcceptedQuery(Guid userId) | ||
| { | ||
| _userId = userId; | ||
| } | ||
|
|
||
| public IQueryable<Policy> Run(DatabaseContext dbContext) | ||
| { | ||
| var query = from p in dbContext.Policies | ||
| join ou in dbContext.OrganizationUsers | ||
| on p.OrganizationId equals ou.OrganizationId | ||
| join o in dbContext.Organizations | ||
| on ou.OrganizationId equals o.Id | ||
| where ou.UserId == _userId && | ||
| (ou.Status == OrganizationUserStatusType.Confirmed || | ||
| ou.Status == OrganizationUserStatusType.Accepted) | ||
| select p; | ||
|
|
||
| return query; | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
src/Sql/dbo/Stored Procedures/Policy_ReadByUserIdWithConfirmedAndAccepted.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| CREATE PROCEDURE [dbo].[Policy_ReadByUserIdWithConfirmedAndAccepted] | ||
JimmyVo16 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @UserId UNIQUEIDENTIFIER | ||
| AS | ||
| BEGIN | ||
| SET NOCOUNT ON | ||
|
|
||
| SELECT | ||
| P.* | ||
| FROM | ||
| [dbo].[PolicyView] P | ||
| INNER JOIN | ||
| [dbo].[OrganizationUser] OU ON P.[OrganizationId] = OU.[OrganizationId] | ||
| INNER JOIN | ||
| [dbo].[Organization] O ON OU.[OrganizationId] = O.[Id] | ||
| WHERE | ||
| OU.[UserId] = @UserId | ||
| AND OU.[Status] IN (1, 2) -- 1 = Accepted, 2 = Confirmed | ||
| END | ||
207 changes: 207 additions & 0 deletions
207
...AdminConsole/Repositories/PolicyRepository/GetManyConfirmedAndAcceptedByUserAsyncTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| ο»Ώusing Bit.Core.AdminConsole.Entities; | ||
| using Bit.Core.AdminConsole.Enums; | ||
| using Bit.Core.AdminConsole.Repositories; | ||
| using Bit.Core.Entities; | ||
| using Bit.Core.Enums; | ||
| using Bit.Core.Repositories; | ||
| using Xunit; | ||
|
|
||
| namespace Bit.Infrastructure.IntegrationTest.AdminConsole.Repositories.PolicyRepository; | ||
|
|
||
| public class GetManyConfirmedAndAcceptedByUserAsyncTests | ||
| { | ||
| [Theory, DatabaseData] | ||
| public async Task ReturnsPolicies_WhenUserIsConfirmed( | ||
| IUserRepository userRepository, | ||
| IOrganizationRepository organizationRepository, | ||
| IOrganizationUserRepository organizationUserRepository, | ||
| IPolicyRepository policyRepository) | ||
| { | ||
| // Arrange | ||
| var user = await userRepository.CreateTestUserAsync(); | ||
| var organization = await organizationRepository.CreateTestOrganizationAsync(); | ||
| await organizationUserRepository.CreateConfirmedTestOrganizationUserAsync(organization, user); | ||
| var policy = await policyRepository.CreateAsync(new Policy | ||
| { | ||
| OrganizationId = organization.Id, | ||
| Type = PolicyType.TwoFactorAuthentication, | ||
| Enabled = true | ||
| }); | ||
|
|
||
| // Act | ||
| var results = await policyRepository.GetManyConfirmedAndAcceptedByUserAsync(user.Id); | ||
|
|
||
| // Assert | ||
| Assert.Contains(results, p => p.Id == policy.Id); | ||
|
|
||
| // Annul | ||
| await organizationRepository.DeleteAsync(organization); | ||
| await userRepository.DeleteAsync(user); | ||
| } | ||
|
|
||
| [Theory, DatabaseData] | ||
| public async Task ReturnsPolicies_WhenUserIsAccepted( | ||
| IUserRepository userRepository, | ||
| IOrganizationRepository organizationRepository, | ||
| IOrganizationUserRepository organizationUserRepository, | ||
| IPolicyRepository policyRepository) | ||
| { | ||
| // Arrange | ||
| var user = await userRepository.CreateTestUserAsync(); | ||
| var organization = await organizationRepository.CreateTestOrganizationAsync(); | ||
| await organizationUserRepository.CreateAcceptedTestOrganizationUserAsync(organization, user); | ||
| var policy = await policyRepository.CreateAsync(new Policy | ||
| { | ||
| OrganizationId = organization.Id, | ||
| Type = PolicyType.TwoFactorAuthentication, | ||
| Enabled = true | ||
| }); | ||
|
|
||
| // Act | ||
| var results = await policyRepository.GetManyConfirmedAndAcceptedByUserAsync(user.Id); | ||
|
|
||
| // Assert | ||
| Assert.Contains(results, p => p.Id == policy.Id); | ||
|
|
||
| // Annul | ||
| await organizationRepository.DeleteAsync(organization); | ||
| await userRepository.DeleteAsync(user); | ||
| } | ||
|
|
||
| [Theory, DatabaseData] | ||
| public async Task ReturnsPoliciesAcrossMultipleOrganizations_WhenUserIsConfirmedOrAccepted( | ||
| IUserRepository userRepository, | ||
| IOrganizationRepository organizationRepository, | ||
| IOrganizationUserRepository organizationUserRepository, | ||
| IPolicyRepository policyRepository) | ||
| { | ||
| // Arrange | ||
| var user = await userRepository.CreateTestUserAsync(); | ||
|
|
||
| var confirmedOrg = await organizationRepository.CreateTestOrganizationAsync(); | ||
| await organizationUserRepository.CreateConfirmedTestOrganizationUserAsync(confirmedOrg, user); | ||
| var confirmedPolicy = await policyRepository.CreateAsync(new Policy | ||
| { | ||
| OrganizationId = confirmedOrg.Id, | ||
| Type = PolicyType.TwoFactorAuthentication, | ||
| Enabled = true | ||
| }); | ||
|
|
||
| var acceptedOrg = await organizationRepository.CreateTestOrganizationAsync(); | ||
| await organizationUserRepository.CreateAcceptedTestOrganizationUserAsync(acceptedOrg, user); | ||
| var acceptedPolicy = await policyRepository.CreateAsync(new Policy | ||
| { | ||
| OrganizationId = acceptedOrg.Id, | ||
| Type = PolicyType.TwoFactorAuthentication, | ||
| Enabled = true | ||
| }); | ||
|
|
||
| // Act | ||
| var results = await policyRepository.GetManyConfirmedAndAcceptedByUserAsync(user.Id); | ||
|
|
||
| // Assert | ||
| Assert.Contains(results, p => p.Id == confirmedPolicy.Id); | ||
| Assert.Contains(results, p => p.Id == acceptedPolicy.Id); | ||
|
|
||
| // Annul | ||
| await organizationRepository.DeleteAsync(confirmedOrg); | ||
| await organizationRepository.DeleteAsync(acceptedOrg); | ||
| await userRepository.DeleteAsync(user); | ||
| } | ||
|
|
||
| [Theory, DatabaseData] | ||
| public async Task DoesNotReturnPolicies_WhenUserIsInvited( | ||
| IUserRepository userRepository, | ||
| IOrganizationRepository organizationRepository, | ||
| IOrganizationUserRepository organizationUserRepository, | ||
| IPolicyRepository policyRepository) | ||
| { | ||
| // Arrange | ||
| var user = await userRepository.CreateTestUserAsync(); | ||
| var organization = await organizationRepository.CreateTestOrganizationAsync(); | ||
| await organizationUserRepository.CreateAsync(new OrganizationUser | ||
| { | ||
| OrganizationId = organization.Id, | ||
| UserId = null, | ||
| Email = user.Email, | ||
| Status = OrganizationUserStatusType.Invited, | ||
| Type = OrganizationUserType.User | ||
| }); | ||
| var policy = await policyRepository.CreateAsync(new Policy | ||
| { | ||
| OrganizationId = organization.Id, | ||
| Type = PolicyType.TwoFactorAuthentication, | ||
| Enabled = true | ||
| }); | ||
|
|
||
| // Act | ||
| var results = await policyRepository.GetManyConfirmedAndAcceptedByUserAsync(user.Id); | ||
|
|
||
| // Assert | ||
| Assert.DoesNotContain(results, p => p.Id == policy.Id); | ||
|
|
||
| // Annul | ||
| await organizationRepository.DeleteAsync(organization); | ||
| await userRepository.DeleteAsync(user); | ||
| } | ||
|
|
||
| [Theory, DatabaseData] | ||
| public async Task DoesNotReturnPolicies_WhenUserIsRevoked( | ||
| IUserRepository userRepository, | ||
| IOrganizationRepository organizationRepository, | ||
| IOrganizationUserRepository organizationUserRepository, | ||
| IPolicyRepository policyRepository) | ||
| { | ||
| // Arrange | ||
| var user = await userRepository.CreateTestUserAsync(); | ||
| var organization = await organizationRepository.CreateTestOrganizationAsync(); | ||
| await organizationUserRepository.CreateRevokedTestOrganizationUserAsync(organization, user); | ||
| var policy = await policyRepository.CreateAsync(new Policy | ||
| { | ||
| OrganizationId = organization.Id, | ||
| Type = PolicyType.TwoFactorAuthentication, | ||
| Enabled = true | ||
| }); | ||
|
|
||
| // Act | ||
| var results = await policyRepository.GetManyConfirmedAndAcceptedByUserAsync(user.Id); | ||
|
|
||
| // Assert | ||
| Assert.DoesNotContain(results, p => p.Id == policy.Id); | ||
|
|
||
| // Annul | ||
| await organizationRepository.DeleteAsync(organization); | ||
| await userRepository.DeleteAsync(user); | ||
| } | ||
|
|
||
| [Theory, DatabaseData] | ||
| public async Task DoesNotReturnPolicies_ForOtherUsers( | ||
| IUserRepository userRepository, | ||
| IOrganizationRepository organizationRepository, | ||
| IOrganizationUserRepository organizationUserRepository, | ||
| IPolicyRepository policyRepository) | ||
| { | ||
| // Arrange | ||
| var targetUser = await userRepository.CreateTestUserAsync(); | ||
| var otherUser = await userRepository.CreateTestUserAsync(); | ||
|
|
||
| var organization = await organizationRepository.CreateTestOrganizationAsync(); | ||
| await organizationUserRepository.CreateConfirmedTestOrganizationUserAsync(organization, otherUser); | ||
| var policy = await policyRepository.CreateAsync(new Policy | ||
| { | ||
| OrganizationId = organization.Id, | ||
| Type = PolicyType.TwoFactorAuthentication, | ||
| Enabled = true | ||
| }); | ||
|
|
||
| // Act | ||
| var results = await policyRepository.GetManyConfirmedAndAcceptedByUserAsync(targetUser.Id); | ||
|
|
||
| // Assert | ||
| Assert.DoesNotContain(results, p => p.Id == policy.Id); | ||
|
|
||
| // Annul | ||
| await organizationRepository.DeleteAsync(organization); | ||
| await userRepository.DeleteManyAsync([targetUser, otherUser]); | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
util/Migrator/DbScripts/2026-04-06_00_AddPolicy_ReadByUserIdWithConfirmedAndAccepted.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| IF OBJECT_ID('[dbo].[Policy_ReadByUserIdWithConfirmedAndAccepted]') IS NOT NULL | ||
JimmyVo16 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| BEGIN | ||
| DROP PROCEDURE [dbo].[Policy_ReadByUserIdWithConfirmedAndAccepted] | ||
| END | ||
| GO | ||
|
|
||
| CREATE PROCEDURE [dbo].[Policy_ReadByUserIdWithConfirmedAndAccepted] | ||
| @UserId UNIQUEIDENTIFIER | ||
| AS | ||
| BEGIN | ||
| SET NOCOUNT ON | ||
|
|
||
| SELECT | ||
| P.* | ||
| FROM | ||
| [dbo].[PolicyView] P | ||
| INNER JOIN | ||
| [dbo].[OrganizationUser] OU ON P.[OrganizationId] = OU.[OrganizationId] | ||
| INNER JOIN | ||
| [dbo].[Organization] O ON OU.[OrganizationId] = O.[Id] | ||
| WHERE | ||
| OU.[UserId] = @UserId | ||
| AND OU.[Status] IN (1, 2) -- 1 = Accepted, 2 = Confirmed | ||
| END | ||
| GO | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.