Skip to content

Commit 7255abc

Browse files
committed
[PM-34146] Add stored procedures for accepted users
1 parent 93604aa commit 7255abc

6 files changed

Lines changed: 108 additions & 0 deletions

File tree

src/Core/AdminConsole/Repositories/IPolicyRepository.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ public interface IPolicyRepository : IRepository<Policy, Guid>
2121
Task<ICollection<Policy>> GetManyByOrganizationIdAsync(Guid organizationId);
2222
Task<ICollection<Policy>> GetManyByUserIdAsync(Guid userId);
2323

24+
/// <summary>
25+
/// Gets all policies for a user across organizations where the user is in the Confirmed or Accepted status.
26+
/// </summary>
27+
/// <remarks>
28+
/// WARNING: do not use this to enforce policies against a user! It returns raw data and does not take into account
29+
/// various business rules. Use <see cref="IPolicyRequirementQuery"/> instead.
30+
/// </remarks>
31+
Task<ICollection<Policy>> GetManyConfirmedAndAcceptedByUserAsync(Guid userId);
32+
2433
/// <summary>
2534
/// Retrieves <see cref="OrganizationPolicyDetails"/> of the specified <paramref name="policyType"/>
2635
/// for users in the given organization and for any other organizations those users belong to.

src/Infrastructure.Dapper/AdminConsole/Repositories/PolicyRepository.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ public async Task<ICollection<Policy>> GetManyByUserIdAsync(Guid userId)
6161
}
6262
}
6363

64+
public async Task<ICollection<Policy>> GetManyConfirmedAndAcceptedByUserAsync(Guid userId)
65+
{
66+
using (var connection = new SqlConnection(ConnectionString))
67+
{
68+
var results = await connection.QueryAsync<Policy>(
69+
$"[{Schema}].[{Table}_ReadByUserIdWithConfirmedAndAccepted]",
70+
new { UserId = userId },
71+
commandType: CommandType.StoredProcedure);
72+
73+
return results.ToList();
74+
}
75+
}
76+
6477
public async Task<IEnumerable<OrganizationPolicyDetails>> GetPolicyDetailsByUserIdsAndPolicyType(IEnumerable<Guid> userIds, PolicyType type)
6578
{
6679
await using var connection = new SqlConnection(ConnectionString);

src/Infrastructure.EntityFramework/AdminConsole/Repositories/PolicyRepository.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ public PolicyRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper
5656
}
5757
}
5858

59+
public async Task<ICollection<AdminConsoleEntities.Policy>> GetManyConfirmedAndAcceptedByUserAsync(Guid userId)
60+
{
61+
using (var scope = ServiceScopeFactory.CreateScope())
62+
{
63+
var dbContext = GetDatabaseContext(scope);
64+
65+
var query = new PolicyReadByUserIdConfirmedAndAcceptedQuery(userId);
66+
var results = await query.Run(dbContext).ToListAsync();
67+
return Mapper.Map<List<AdminConsoleEntities.Policy>>(results);
68+
}
69+
}
70+
5971
public async Task<IEnumerable<OrganizationPolicyDetails>> GetPolicyDetailsByOrganizationIdAsync(Guid organizationId, PolicyType policyType)
6072
{
6173
using var scope = ServiceScopeFactory.CreateScope();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Bit.Core.Enums;
2+
using Bit.Infrastructure.EntityFramework.AdminConsole.Models;
3+
using Bit.Infrastructure.EntityFramework.Repositories;
4+
using Bit.Infrastructure.EntityFramework.Repositories.Queries;
5+
6+
namespace Bit.Infrastructure.EntityFramework.AdminConsole.Repositories.Queries;
7+
8+
public class PolicyReadByUserIdConfirmedAndAcceptedQuery : IQuery<Policy>
9+
{
10+
private readonly Guid _userId;
11+
12+
public PolicyReadByUserIdConfirmedAndAcceptedQuery(Guid userId)
13+
{
14+
_userId = userId;
15+
}
16+
17+
public IQueryable<Policy> Run(DatabaseContext dbContext)
18+
{
19+
var query = from p in dbContext.Policies
20+
join ou in dbContext.OrganizationUsers
21+
on p.OrganizationId equals ou.OrganizationId
22+
join o in dbContext.Organizations
23+
on ou.OrganizationId equals o.Id
24+
where ou.UserId == _userId &&
25+
(ou.Status == OrganizationUserStatusType.Confirmed ||
26+
ou.Status == OrganizationUserStatusType.Accepted)
27+
select p;
28+
29+
return query;
30+
}
31+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
CREATE PROCEDURE [dbo].[Policy_ReadByUserIdWithConfirmedAndAccepted]
2+
@UserId UNIQUEIDENTIFIER
3+
AS
4+
BEGIN
5+
SET NOCOUNT ON
6+
7+
SELECT
8+
P.*
9+
FROM
10+
[dbo].[PolicyView] P
11+
INNER JOIN
12+
[dbo].[OrganizationUser] OU ON P.[OrganizationId] = OU.[OrganizationId]
13+
INNER JOIN
14+
[dbo].[Organization] O ON OU.[OrganizationId] = O.[Id]
15+
WHERE
16+
OU.[UserId] = @UserId
17+
AND OU.[Status] IN (1, 2) -- 1 = Accepted, 2 = Confirmed
18+
END
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
IF OBJECT_ID('[dbo].[Policy_ReadByUserIdWithConfirmedAndAccepted]') IS NOT NULL
2+
BEGIN
3+
DROP PROCEDURE [dbo].[Policy_ReadByUserIdWithConfirmedAndAccepted]
4+
END
5+
GO
6+
7+
CREATE PROCEDURE [dbo].[Policy_ReadByUserIdWithConfirmedAndAccepted]
8+
@UserId UNIQUEIDENTIFIER
9+
AS
10+
BEGIN
11+
SET NOCOUNT ON
12+
13+
SELECT
14+
P.*
15+
FROM
16+
[dbo].[PolicyView] P
17+
INNER JOIN
18+
[dbo].[OrganizationUser] OU ON P.[OrganizationId] = OU.[OrganizationId]
19+
INNER JOIN
20+
[dbo].[Organization] O ON OU.[OrganizationId] = O.[Id]
21+
WHERE
22+
OU.[UserId] = @UserId
23+
AND OU.[Status] IN (1, 2) -- 1 = Accepted, 2 = Confirmed
24+
END
25+
GO

0 commit comments

Comments
 (0)