1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-04 01:22:50 -05:00

[PS-1928] Add BumpAccountRevisionDate methods (#2458)

* Move RevisionDate Bumps to Extension Class

* Add Tests against live databases

* Run Formatting

* Fix Typo

* Fix Test Solution Typo

* Await ReplaceAsync
This commit is contained in:
Justin Baur
2022-12-02 14:24:30 -05:00
committed by GitHub
parent 41db511872
commit efe91fd0d8
25 changed files with 3788 additions and 309 deletions

View File

@ -70,9 +70,39 @@ public class OrganizationUserRepository : Repository<Core.Entities.OrganizationU
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var orgUser = await dbContext.FindAsync<OrganizationUser>(organizationUserId);
await dbContext.UserBumpAccountRevisionDateByOrganizationUserIdAsync(organizationUserId);
var orgUser = await dbContext.OrganizationUsers
.Where(ou => ou.Id == organizationUserId)
.FirstAsync();
dbContext.Remove(orgUser);
var organizationId = orgUser?.OrganizationId;
var userId = orgUser?.UserId;
if (orgUser?.OrganizationId != null && orgUser?.UserId != null)
{
var ssoUsers = dbContext.SsoUsers
.Where(su => su.UserId == userId && su.OrganizationId == organizationId);
dbContext.SsoUsers.RemoveRange(ssoUsers);
}
var collectionUsers = dbContext.CollectionUsers
.Where(cu => cu.OrganizationUserId == organizationUserId);
dbContext.CollectionUsers.RemoveRange(collectionUsers);
var groupUsers = dbContext.GroupUsers
.Where(gu => gu.OrganizationUserId == organizationUserId);
dbContext.GroupUsers.RemoveRange(groupUsers);
var orgSponsorships = await dbContext.OrganizationSponsorships
.Where(os => os.SponsoringOrganizationUserId == organizationUserId)
.ToListAsync();
foreach (var orgSponsorship in orgSponsorships)
{
orgSponsorship.ToDelete = true;
}
dbContext.OrganizationUsers.Remove(orgUser);
await dbContext.SaveChangesAsync();
}
}
@ -82,7 +112,9 @@ public class OrganizationUserRepository : Repository<Core.Entities.OrganizationU
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
await dbContext.UserBumpAccountRevisionDateByOrganizationUserIdsAsync(organizationUserIds);
var entities = await dbContext.OrganizationUsers
// TODO: Does this work?
.Where(ou => organizationUserIds.Contains(ou.Id))
.ToListAsync();
@ -309,9 +341,20 @@ public class OrganizationUserRepository : Repository<Core.Entities.OrganizationU
}
}
public async override Task ReplaceAsync(Core.Entities.OrganizationUser organizationUser)
{
await base.ReplaceAsync(organizationUser);
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
await dbContext.UserBumpAccountRevisionDateAsync(organizationUser.UserId.GetValueOrDefault());
await dbContext.SaveChangesAsync();
}
}
public async Task ReplaceAsync(Core.Entities.OrganizationUser obj, IEnumerable<SelectionReadOnly> requestedCollections)
{
await base.ReplaceAsync(obj);
await ReplaceAsync(obj);
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
@ -356,7 +399,7 @@ public class OrganizationUserRepository : Repository<Core.Entities.OrganizationU
var dbContext = GetDatabaseContext(scope);
dbContext.UpdateRange(organizationUsers);
await dbContext.SaveChangesAsync();
await UserBumpManyAccountRevisionDates(organizationUsers
await dbContext.UserBumpManyAccountRevisionDatesAsync(organizationUsers
.Where(ou => ou.UserId.HasValue)
.Select(ou => ou.UserId.Value).ToArray());
}
@ -400,7 +443,7 @@ public class OrganizationUserRepository : Repository<Core.Entities.OrganizationU
var delete = procedure.Delete.Run(dbContext);
var deleteData = await delete.ToListAsync();
dbContext.RemoveRange(deleteData);
await UserBumpAccountRevisionDateByOrganizationUserId(orgUserId);
await dbContext.UserBumpAccountRevisionDateByOrganizationUserIdAsync(orgUserId);
await dbContext.SaveChangesAsync();
}
}
@ -449,17 +492,15 @@ public class OrganizationUserRepository : Repository<Core.Entities.OrganizationU
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var orgUser = await GetDbSet(dbContext).FindAsync(id);
if (orgUser != null)
var orgUser = await dbContext.OrganizationUsers.FindAsync(id);
if (orgUser == null)
{
dbContext.Update(orgUser);
orgUser.Status = OrganizationUserStatusType.Revoked;
await dbContext.SaveChangesAsync();
if (orgUser.UserId.HasValue)
{
await UserBumpAccountRevisionDate(orgUser.UserId.Value);
}
return;
}
orgUser.Status = OrganizationUserStatusType.Revoked;
await dbContext.UserBumpAccountRevisionDateByOrganizationUserIdAsync(id);
await dbContext.SaveChangesAsync();
}
}
@ -468,17 +509,17 @@ public class OrganizationUserRepository : Repository<Core.Entities.OrganizationU
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var orgUser = await GetDbSet(dbContext).FindAsync(id);
if (orgUser != null)
var orgUser = await dbContext.OrganizationUsers
.FirstOrDefaultAsync(ou => ou.Id == id && ou.Status == OrganizationUserStatusType.Revoked);
if (orgUser == null)
{
dbContext.Update(orgUser);
orgUser.Status = status;
await dbContext.SaveChangesAsync();
if (orgUser.UserId.HasValue)
{
await UserBumpAccountRevisionDate(orgUser.UserId.Value);
}
return;
}
orgUser.Status = status;
await dbContext.UserBumpAccountRevisionDateByOrganizationUserIdAsync(id);
await dbContext.SaveChangesAsync();
}
}
}