1
0
mirror of https://github.com/bitwarden/server synced 2025-12-28 14:13:48 +00:00

fix to user already exists checks

This commit is contained in:
Kyle Spearrin
2017-11-13 10:06:54 -05:00
parent 39baf2a9be
commit d03421fe4b
5 changed files with 60 additions and 18 deletions

View File

@@ -14,7 +14,7 @@ namespace Bit.Core.Repositories
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);
Task<int> GetCountByOrganizationAsync(Guid organizationId, string email, bool onlyRegisteredUsers);
Task<OrganizationUser> GetByOrganizationAsync(Guid organizationId, Guid userId);
Task<Tuple<OrganizationUser, ICollection<SelectionReadOnly>>> GetByIdWithCollectionsAsync(Guid id);
Task<ICollection<OrganizationUserUserDetails>> GetManyDetailsByOrganizationAsync(Guid organizationId);

View File

@@ -62,16 +62,16 @@ namespace Bit.Core.Repositories.SqlServer
}
}
public async Task<OrganizationUser> GetByOrganizationAsync(Guid organizationId, string email)
public async Task<int> GetCountByOrganizationAsync(Guid organizationId, string email, bool onlyRegisteredUsers)
{
using(var connection = new SqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<OrganizationUser>(
"[dbo].[OrganizationUser_ReadByOrganizationIdEmail]",
new { OrganizationId = organizationId, Email = email },
var result = await connection.ExecuteScalarAsync<int>(
"[dbo].[OrganizationUser_ReadCountByOrganizationIdEmail]",
new { OrganizationId = organizationId, Email = email, OnlyUsers = onlyRegisteredUsers },
commandType: CommandType.StoredProcedure);
return results.SingleOrDefault();
return result;
}
}

View File

@@ -857,8 +857,9 @@ namespace Bit.Core.Services
foreach(var email in emails)
{
// Make sure user is not already invited
var existingOrgUser = await _organizationUserRepository.GetByOrganizationAsync(organizationId, email);
if(existingOrgUser != null)
var existingOrgUserCount = await _organizationUserRepository.GetCountByOrganizationAsync(
organizationId, email, false);
if(existingOrgUserCount > 0)
{
throw new BadRequestException("User already invited.");
}
@@ -940,8 +941,9 @@ namespace Bit.Core.Services
}
}
var existingOrgUser = await _organizationUserRepository.GetByOrganizationAsync(orgUser.OrganizationId, user.Email);
if(existingOrgUser != null)
var existingOrgUserCount = await _organizationUserRepository.GetCountByOrganizationAsync(
orgUser.OrganizationId, user.Email, true);
if(existingOrgUserCount > 0)
{
throw new BadRequestException("You are already part of this organization.");
}