1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-22 12:15:36 +01:00

user vault associations

This commit is contained in:
Kyle Spearrin 2017-03-13 22:54:24 -04:00
parent cfb4d1453c
commit c8d6a26ec3
4 changed files with 16 additions and 16 deletions

View File

@ -59,7 +59,7 @@ namespace Bit.Api.Controllers
{
var user = await _userService.GetUserByPrincipalAsync(User);
var result = await _organizationService.InviteUserAsync(new Guid(orgId), model.Email,
model.Subvaults.Select(s => s.ToSubvaultUser()));
model.Subvaults?.Select(s => s.ToSubvaultUser()));
}
[HttpPut("{id}/accept")]
@ -87,7 +87,7 @@ namespace Bit.Api.Controllers
throw new NotFoundException();
}
await _organizationService.SaveUserAsync(organizationUser, model.Subvaults.Select(s => s.ToSubvaultUser()));
await _organizationService.SaveUserAsync(organizationUser, model.Subvaults?.Select(s => s.ToSubvaultUser()));
}
[HttpDelete("{id}")]

View File

@ -28,7 +28,6 @@ namespace Bit.Core.Models.Api
public class OrganizationUserSubvaultRequestModel
{
public string Id { get; set; }
public string SubvaultId { get; set; }
public bool Admin { get; set; }
public bool ReadOnly { get; set; }
@ -46,11 +45,6 @@ namespace Bit.Core.Models.Api
subvault.SubvaultId = new Guid(SubvaultId);
}
if(!string.IsNullOrWhiteSpace(Id))
{
subvault.Id = new Guid(Id);
}
return subvault;
}
}

View File

@ -164,26 +164,33 @@ namespace Bit.Core.Services
private async Task SaveUserSubvaultsAsync(OrganizationUser user, IEnumerable<SubvaultUser> subvaults, bool newUser)
{
if(subvaults == null)
{
subvaults = new List<SubvaultUser>();
}
var orgSubvaults = await _subvaultRepository.GetManyByOrganizationIdAsync(user.OrganizationId);
var currentUserSubvaults = newUser ? null : await _subvaultUserRepository.GetManyByOrganizationUserIdAsync(user.Id);
// Let's make sure all these belong to this user and organization.
var filteredSubvaults = subvaults.Where(s => orgSubvaults.Any(os => os.Id == s.SubvaultId));
if(!newUser)
{
filteredSubvaults = filteredSubvaults.Where(s =>
s.Id == default(Guid) || currentUserSubvaults.Any(cs => cs.Id == s.Id));
}
foreach(var subvault in filteredSubvaults)
{
var existingSubvaultUser = currentUserSubvaults?.FirstOrDefault(cs => cs.SubvaultId == subvault.SubvaultId);
if(existingSubvaultUser != null)
{
subvault.Id = existingSubvaultUser.Id;
subvault.CreationDate = existingSubvaultUser.CreationDate;
}
subvault.OrganizationUserId = user.Id;
await _subvaultUserRepository.UpsertAsync(subvault);
}
if(!newUser)
{
var subvaultsToDelete = currentUserSubvaults.Where(cs => !subvaults.Any(s => s.Id == cs.Id));
var subvaultsToDelete = currentUserSubvaults.Where(cs =>
!filteredSubvaults.Any(s => s.SubvaultId == cs.SubvaultId));
foreach(var subvault in subvaultsToDelete)
{
await _subvaultUserRepository.DeleteAsync(subvault);

View File

@ -2,7 +2,6 @@
@Id UNIQUEIDENTIFIER,
@SubvaultId UNIQUEIDENTIFIER,
@OrganizationUserId UNIQUEIDENTIFIER,
@Key VARCHAR(MAX),
@Admin BIT,
@ReadOnly BIT,
@CreationDate DATETIME2(7),