mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 16:12: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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -52,7 +52,7 @@ namespace Bit.Core.Services
|
||||
private readonly ICurrentContext _currentContext;
|
||||
private readonly GlobalSettings _globalSettings;
|
||||
private readonly IOrganizationService _organizationService;
|
||||
private readonly ISendRepository _sendRepository;
|
||||
private readonly IProviderUserRepository _providerUserRepository;
|
||||
|
||||
public UserService(
|
||||
IUserRepository userRepository,
|
||||
@ -81,7 +81,7 @@ namespace Bit.Core.Services
|
||||
ICurrentContext currentContext,
|
||||
GlobalSettings globalSettings,
|
||||
IOrganizationService organizationService,
|
||||
ISendRepository sendRepository)
|
||||
IProviderUserRepository providerUserRepository)
|
||||
: base(
|
||||
store,
|
||||
optionsAccessor,
|
||||
@ -115,7 +115,7 @@ namespace Bit.Core.Services
|
||||
_currentContext = currentContext;
|
||||
_globalSettings = globalSettings;
|
||||
_organizationService = organizationService;
|
||||
_sendRepository = sendRepository;
|
||||
_providerUserRepository = providerUserRepository;
|
||||
}
|
||||
|
||||
public Guid? GetProperUserId(ClaimsPrincipal principal)
|
||||
@ -216,11 +216,20 @@ namespace Bit.Core.Services
|
||||
{
|
||||
return IdentityResult.Failed(new IdentityError
|
||||
{
|
||||
Description = "You must leave or delete any organizations that you are the only owner of first."
|
||||
Description = "Cannot delete this user because it is the sole owner of at least one organization. Please delete these organizations or upgrade another user.",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
var onlyOwnerProviderCount = await _providerUserRepository.GetCountByOnlyOwnerAsync(user.Id);
|
||||
if (onlyOwnerProviderCount > 0)
|
||||
{
|
||||
return IdentityResult.Failed(new IdentityError
|
||||
{
|
||||
Description = "Cannot delete this user because it is the sole owner of at least one provider. Please delete these providers or upgrade another user.",
|
||||
});
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(user.GatewaySubscriptionId))
|
||||
{
|
||||
try
|
||||
|
Reference in New Issue
Block a user