mirror of
https://github.com/bitwarden/server.git
synced 2025-07-06 10:32:49 -05:00
Resolve error when deleting an account connected to a provider (#1580)
This commit is contained in:
@ -155,5 +155,11 @@ namespace Bit.Core.Repositories.EntityFramework
|
||||
return organizationUsers;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int> GetCountByOnlyOwnerAsync(Guid userId)
|
||||
{
|
||||
var query = new ProviderUserReadCountByOnlyOwnerQuery(userId);
|
||||
return await GetCountFromQuery(query);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,11 +19,12 @@ namespace Bit.Core.Repositories.EntityFramework.Queries
|
||||
{
|
||||
var owners = from ou in dbContext.OrganizationUsers
|
||||
where ou.Type == OrganizationUserType.Owner &&
|
||||
ou.Status == OrganizationUserStatusType.Confirmed
|
||||
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()
|
||||
OrgUser = g.Select(x => new {x.UserId, x.Id}).FirstOrDefault(),
|
||||
ConfirmedOwnerCount = g.Count(),
|
||||
};
|
||||
|
||||
var query = from owner in owners
|
||||
|
@ -0,0 +1,39 @@
|
||||
using System.Linq;
|
||||
using System;
|
||||
using Bit.Core.Enums.Provider;
|
||||
using Bit.Core.Models.EntityFramework.Provider;
|
||||
|
||||
namespace Bit.Core.Repositories.EntityFramework.Queries
|
||||
{
|
||||
public class ProviderUserReadCountByOnlyOwnerQuery : IQuery<ProviderUser>
|
||||
{
|
||||
private readonly Guid _userId;
|
||||
|
||||
public ProviderUserReadCountByOnlyOwnerQuery(Guid userId)
|
||||
{
|
||||
_userId = userId;
|
||||
}
|
||||
|
||||
public IQueryable<ProviderUser> 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -20,5 +20,6 @@ namespace Bit.Core.Repositories
|
||||
Task<IEnumerable<ProviderUserOrganizationDetails>> GetManyOrganizationDetailsByUserAsync(Guid userId, ProviderUserStatusType? status = null);
|
||||
Task DeleteManyAsync(IEnumerable<Guid> userIds);
|
||||
Task<IEnumerable<ProviderUserPublicKey>> GetManyPublicKeysByProviderUserAsync(Guid providerId, IEnumerable<Guid> Ids);
|
||||
Task<int> GetCountByOnlyOwnerAsync(Guid userId);
|
||||
}
|
||||
}
|
||||
|
@ -151,5 +151,18 @@ namespace Bit.Core.Repositories.SqlServer
|
||||
return results.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int> GetCountByOnlyOwnerAsync(Guid userId)
|
||||
{
|
||||
using (var connection = new SqlConnection(ConnectionString))
|
||||
{
|
||||
var results = await connection.ExecuteScalarAsync<int>(
|
||||
"[dbo].[ProviderUser_ReadCountByOnlyOwner]",
|
||||
new { UserId = userId },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
|
||||
return results;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user