1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-23 17:07:42 +01:00

allow user delete if they are not the only owner

This commit is contained in:
Kyle Spearrin 2017-10-25 11:36:54 -04:00
parent 461be7a14f
commit 8ba3e27a7d
7 changed files with 33 additions and 25 deletions

View File

@ -11,7 +11,7 @@ namespace Bit.Core.Repositories
{
Task<int> GetCountByOrganizationIdAsync(Guid organizationId);
Task<int> GetCountByFreeOrganizationAdminUserAsync(Guid userId);
Task<int> GetCountByOrganizationOwnerUserAsync(Guid userId);
Task<int> GetCountByOnlyOwnerAsync(Guid userId);
Task<ICollection<OrganizationUser>> GetManyByUserAsync(Guid userId);
Task<ICollection<OrganizationUser>> GetManyByOrganizationAsync(Guid organizationId, OrganizationUserType? type);
Task<OrganizationUser> GetByOrganizationAsync(Guid organizationId, string email);

View File

@ -49,12 +49,12 @@ namespace Bit.Core.Repositories.SqlServer
}
}
public async Task<int> GetCountByOrganizationOwnerUserAsync(Guid userId)
public async Task<int> GetCountByOnlyOwnerAsync(Guid userId)
{
using(var connection = new SqlConnection(ConnectionString))
{
var results = await connection.ExecuteScalarAsync<int>(
"[dbo].[OrganizationUser_ReadCountByOrganizationOwnerUser]",
"[dbo].[OrganizationUser_ReadCountByOnlyOwner]",
new { UserId = userId },
commandType: CommandType.StoredProcedure);

View File

@ -7,7 +7,6 @@ using Microsoft.Extensions.Options;
using Bit.Core.Models.Table;
using Bit.Core.Repositories;
using System.Linq;
using Microsoft.AspNetCore.Builder;
using Bit.Core.Enums;
using System.Security.Claims;
using Bit.Core.Models;
@ -161,13 +160,13 @@ namespace Bit.Core.Services
public override async Task<IdentityResult> DeleteAsync(User user)
{
// Check if user is the owner of any organizations.
var organizationOwnerCount = await _organizationUserRepository.GetCountByOrganizationOwnerUserAsync(user.Id);
if(organizationOwnerCount > 0)
// Check if user is the only owner of any organizations.
var onlyOwnerCount = await _organizationUserRepository.GetCountByOnlyOwnerAsync(user.Id);
if(onlyOwnerCount > 0)
{
return IdentityResult.Failed(new IdentityError
{
Description = "You must leave or delete any organizations that you are the owner of first."
Description = "You must leave or delete any organizations that you are the only owner of first."
});
}

View File

@ -114,7 +114,7 @@
<Build Include="dbo\Stored Procedures\OrganizationUser_ReadCountByFreeOrganizationAdminUser.sql" />
<Build Include="dbo\Stored Procedures\User_ReadAccountRevisionDateById.sql" />
<Build Include="dbo\Stored Procedures\OrganizationUser_ReadCountByOrganizationId.sql" />
<Build Include="dbo\Stored Procedures\OrganizationUser_ReadCountByOrganizationOwnerUser.sql" />
<Build Include="dbo\Stored Procedures\OrganizationUser_ReadCountByOnlyOwner.sql" />
<Build Include="dbo\Stored Procedures\OrganizationUser_Update.sql" />
<Build Include="dbo\Stored Procedures\OrganizationUserOrganizationDetails_ReadByUserIdStatus.sql" />
<Build Include="dbo\Stored Procedures\OrganizationUserUserDetails_ReadByOrganizationId.sql" />

View File

@ -0,0 +1,25 @@
CREATE PROCEDURE [dbo].[OrganizationUser_ReadCountByOnlyOwner]
@UserId UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
;WITH [OwnerCountCTE] AS
(
SELECT
OU.[UserId],
COUNT(1) OVER (PARTITION BY OU.[OrganizationId]) [ConfirmedOwnerCount]
FROM
[dbo].[OrganizationUser] OU
WHERE
OU.[Type] = 0 -- 0 = Owner
AND OU.[Status] = 2 -- 2 = Confirmed
)
SELECT
COUNT(1)
FROM
[OwnerCountCTE] OC
WHERE
OC.[UserId] = @UserId
AND OC.[ConfirmedOwnerCount] = 1
END

View File

@ -1,15 +0,0 @@
CREATE PROCEDURE [dbo].[OrganizationUser_ReadCountByOrganizationOwnerUser]
@UserId UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
SELECT
COUNT(1)
FROM
[dbo].[OrganizationUser] OU
WHERE
OU.[UserId] = @UserId
AND OU.[Type] = 0
AND OU.[Status] = 2 -- 2 = Confirmed
END

View File

@ -64,7 +64,6 @@ BEGIN
[dbo].[OrganizationUser]
WHERE
[UserId] = @Id
AND [Type] != 0 -- 0 = owner
-- Finally, delete the user
DELETE