diff --git a/src/Infrastructure.EntityFramework/Repositories/OrganizationUserRepository.cs b/src/Infrastructure.EntityFramework/Repositories/OrganizationUserRepository.cs index 22cccfa565..2423c5f67d 100644 --- a/src/Infrastructure.EntityFramework/Repositories/OrganizationUserRepository.cs +++ b/src/Infrastructure.EntityFramework/Repositories/OrganizationUserRepository.cs @@ -179,8 +179,16 @@ public class OrganizationUserRepository : Repository GetCountByOnlyOwnerAsync(Guid userId) { - var query = new OrganizationUserReadCountByOnlyOwnerQuery(userId); - return await GetCountFromQuery(query); + using (var scope = ServiceScopeFactory.CreateScope()) + { + var dbContext = GetDatabaseContext(scope); + return await dbContext.OrganizationUsers + .Where(ou => ou.Type == OrganizationUserType.Owner && ou.Status == OrganizationUserStatusType.Confirmed) + .GroupBy(ou => ou.UserId) + .Select(g => new { UserId = g.Key, ConfirmedOwnerCount = g.Count() }) + .Where(oc => oc.UserId == userId && oc.ConfirmedOwnerCount == 1) + .CountAsync(); + } } public async Task GetCountByOrganizationAsync(Guid organizationId, string email, bool onlyRegisteredUsers) diff --git a/src/Infrastructure.EntityFramework/Repositories/ProviderUserRepository.cs b/src/Infrastructure.EntityFramework/Repositories/ProviderUserRepository.cs index 859b5ce367..76236a1c38 100644 --- a/src/Infrastructure.EntityFramework/Repositories/ProviderUserRepository.cs +++ b/src/Infrastructure.EntityFramework/Repositories/ProviderUserRepository.cs @@ -167,7 +167,15 @@ public class ProviderUserRepository : public async Task GetCountByOnlyOwnerAsync(Guid userId) { - var query = new ProviderUserReadCountByOnlyOwnerQuery(userId); - return await GetCountFromQuery(query); + using (var scope = ServiceScopeFactory.CreateScope()) + { + var dbContext = GetDatabaseContext(scope); + return await dbContext.ProviderUsers + .Where(pu => pu.Type == ProviderUserType.ProviderAdmin && pu.Status == ProviderUserStatusType.Confirmed) + .GroupBy(pu => pu.UserId) + .Select(g => new { UserId = g.Key, ConfirmedOwnerCount = g.Count() }) + .Where(oc => oc.UserId == userId && oc.ConfirmedOwnerCount == 1) + .CountAsync(); + } } } diff --git a/src/Infrastructure.EntityFramework/Repositories/Queries/OrganizationUserReadCountByOnlyOwnerQuery.cs b/src/Infrastructure.EntityFramework/Repositories/Queries/OrganizationUserReadCountByOnlyOwnerQuery.cs deleted file mode 100644 index 53977e8b74..0000000000 --- a/src/Infrastructure.EntityFramework/Repositories/Queries/OrganizationUserReadCountByOnlyOwnerQuery.cs +++ /dev/null @@ -1,36 +0,0 @@ -using Bit.Core.Enums; -using Bit.Infrastructure.EntityFramework.Models; - -namespace Bit.Infrastructure.EntityFramework.Repositories.Queries; - -public class OrganizationUserReadCountByOnlyOwnerQuery : IQuery -{ - private readonly Guid _userId; - - public OrganizationUserReadCountByOnlyOwnerQuery(Guid userId) - { - _userId = userId; - } - - public IQueryable Run(DatabaseContext dbContext) - { - var owners = from ou in dbContext.OrganizationUsers - where ou.Type == OrganizationUserType.Owner && - ou.Status == OrganizationUserStatusType.Confirmed - group ou by ou.OrganizationId into g - select new - { - OrgUser = g.Select(x => new { x.UserId, x.Id }).FirstOrDefault(), - ConfirmedOwnerCount = g.Count(), - }; - - var query = from owner in owners - join ou in dbContext.OrganizationUsers - on owner.OrgUser.Id equals ou.Id - where owner.OrgUser.UserId == _userId && - owner.ConfirmedOwnerCount == 1 - select ou; - - return query; - } -} diff --git a/src/Infrastructure.EntityFramework/Repositories/Queries/ProviderUserReadCountByOnlyOwnerQuery.cs b/src/Infrastructure.EntityFramework/Repositories/Queries/ProviderUserReadCountByOnlyOwnerQuery.cs deleted file mode 100644 index 899c78b547..0000000000 --- a/src/Infrastructure.EntityFramework/Repositories/Queries/ProviderUserReadCountByOnlyOwnerQuery.cs +++ /dev/null @@ -1,36 +0,0 @@ -using Bit.Core.Enums.Provider; -using Bit.Infrastructure.EntityFramework.Models; - -namespace Bit.Infrastructure.EntityFramework.Repositories.Queries; - -public class ProviderUserReadCountByOnlyOwnerQuery : IQuery -{ - private readonly Guid _userId; - - public ProviderUserReadCountByOnlyOwnerQuery(Guid userId) - { - _userId = userId; - } - - public IQueryable Run(DatabaseContext dbContext) - { - var owners = from pu in dbContext.ProviderUsers - where pu.Type == ProviderUserType.ProviderAdmin && - pu.Status == ProviderUserStatusType.Confirmed - group pu by pu.ProviderId into g - select new - { - ProviderUser = g.Select(x => new { x.UserId, x.Id }).FirstOrDefault(), - ConfirmedOwnerCount = g.Count(), - }; - - var query = from owner in owners - join pu in dbContext.ProviderUsers - on owner.ProviderUser.Id equals pu.Id - where owner.ProviderUser.UserId == _userId && - owner.ConfirmedOwnerCount == 1 - select pu; - - return query; - } -} diff --git a/src/Infrastructure.EntityFramework/Repositories/UserRepository.cs b/src/Infrastructure.EntityFramework/Repositories/UserRepository.cs index be75e85ff8..8ab7ff7905 100644 --- a/src/Infrastructure.EntityFramework/Repositories/UserRepository.cs +++ b/src/Infrastructure.EntityFramework/Repositories/UserRepository.cs @@ -142,4 +142,40 @@ public class UserRepository : Repository, IUserR return await users.ToListAsync(); } } + + public override async Task DeleteAsync(Core.Entities.User user) + { + using (var scope = ServiceScopeFactory.CreateScope()) + { + var dbContext = GetDatabaseContext(scope); + + var transaction = await dbContext.Database.BeginTransactionAsync(); + + dbContext.Ciphers.RemoveRange(dbContext.Ciphers.Where(c => c.UserId == user.Id)); + dbContext.Folders.RemoveRange(dbContext.Folders.Where(f => f.UserId == user.Id)); + dbContext.Devices.RemoveRange(dbContext.Devices.Where(d => d.UserId == user.Id)); + var collectionUsers = from cu in dbContext.CollectionUsers + join ou in dbContext.OrganizationUsers on cu.OrganizationUserId equals ou.Id + where ou.UserId == user.Id + select cu; + dbContext.CollectionUsers.RemoveRange(collectionUsers); + var groupUsers = from gu in dbContext.GroupUsers + join ou in dbContext.OrganizationUsers on gu.OrganizationUserId equals ou.Id + where ou.UserId == user.Id + select gu; + dbContext.GroupUsers.RemoveRange(groupUsers); + dbContext.OrganizationUsers.RemoveRange(dbContext.OrganizationUsers.Where(ou => ou.UserId == user.Id)); + dbContext.ProviderUsers.RemoveRange(dbContext.ProviderUsers.Where(pu => pu.UserId == user.Id)); + dbContext.SsoUsers.RemoveRange(dbContext.SsoUsers.Where(su => su.UserId == user.Id)); + dbContext.EmergencyAccesses.RemoveRange( + dbContext.EmergencyAccesses.Where(ea => ea.GrantorId == user.Id || ea.GranteeId == user.Id)); + dbContext.Sends.RemoveRange(dbContext.Sends.Where(s => s.UserId == user.Id)); + + var mappedUser = Mapper.Map(user); + dbContext.Users.Remove(mappedUser); + + await transaction.CommitAsync(); + await dbContext.SaveChangesAsync(); + } + } }