1
0
mirror of https://github.com/bitwarden/server synced 2025-12-22 19:23:45 +00:00

[PM-26632] - Adding Idempotent Confirm User (#6459)

* Added repo call for idempotent user confirm. PLUS TESTS!

* Code review changes
This commit is contained in:
Jared McCannon
2025-10-16 11:19:48 -05:00
committed by GitHub
parent 132db95fb7
commit 449603d180
7 changed files with 284 additions and 7 deletions

View File

@@ -87,4 +87,13 @@ public interface IOrganizationUserRepository : IRepository<OrganizationUser, Gui
Task<IEnumerable<OrganizationUserUserDetails>> GetManyDetailsByRoleAsync(Guid organizationId, OrganizationUserType role);
Task CreateManyAsync(IEnumerable<CreateOrganizationUser> organizationUserCollection);
/// <summary>
/// It will only confirm if the user is in the `Accepted` state.
///
/// This is an idempotent operation.
/// </summary>
/// <param name="organizationUser">Accepted OrganizationUser to confirm</param>
/// <returns>True, if the user was updated. False, if not performed.</returns>
Task<bool> ConfirmOrganizationUserAsync(OrganizationUser organizationUser);
}

View File

@@ -15,8 +15,6 @@ using Dapper;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;
#nullable enable
namespace Bit.Infrastructure.Dapper.Repositories;
public class OrganizationUserRepository : Repository<OrganizationUser, Guid>, IOrganizationUserRepository
@@ -672,4 +670,20 @@ public class OrganizationUserRepository : Repository<OrganizationUser, Guid>, IO
},
commandType: CommandType.StoredProcedure);
}
public async Task<bool> ConfirmOrganizationUserAsync(OrganizationUser organizationUser)
{
await using var connection = new SqlConnection(_marsConnectionString);
var rowCount = await connection.ExecuteScalarAsync<int>(
$"[{Schema}].[OrganizationUser_ConfirmById]",
new
{
organizationUser.Id,
organizationUser.UserId,
RevisionDate = DateTime.UtcNow.Date
});
return rowCount > 0;
}
}

View File

@@ -942,4 +942,24 @@ public class OrganizationUserRepository : Repository<Core.Entities.OrganizationU
await dbContext.SaveChangesAsync();
}
public async Task<bool> ConfirmOrganizationUserAsync(Core.Entities.OrganizationUser organizationUser)
{
using var scope = ServiceScopeFactory.CreateScope();
await using var dbContext = GetDatabaseContext(scope);
var result = await dbContext.OrganizationUsers
.Where(ou => ou.Id == organizationUser.Id && ou.Status == OrganizationUserStatusType.Accepted)
.ExecuteUpdateAsync(x =>
x.SetProperty(y => y.Status, OrganizationUserStatusType.Confirmed));
if (result <= 0)
{
return false;
}
await dbContext.UserBumpAccountRevisionDateByOrganizationUserIdAsync(organizationUser.Id);
return true;
}
}

View File

@@ -0,0 +1,28 @@
CREATE PROCEDURE [dbo].[OrganizationUser_ConfirmById]
@Id UNIQUEIDENTIFIER,
@UserId UNIQUEIDENTIFIER,
@RevisionDate DATETIME2(7)
AS
BEGIN
SET NOCOUNT ON
DECLARE @RowCount INT;
UPDATE
[dbo].[OrganizationUser]
SET
[Status] = 2, -- Set to Confirmed
[RevisionDate] = @RevisionDate
WHERE
[Id] = @Id
AND [Status] = 1 -- Only update if status is Accepted
SET @RowCount = @@ROWCOUNT;
IF @RowCount > 0
BEGIN
EXEC [dbo].[User_BumpAccountRevisionDate] @UserId
END
SELECT @RowCount;
END