mirror of
https://github.com/bitwarden/server.git
synced 2025-07-02 16:42:50 -05:00
update cipher subvaults
This commit is contained in:
@ -63,13 +63,34 @@ namespace Bit.Core.Models.Api
|
||||
}
|
||||
}
|
||||
|
||||
public class CipherMoveRequestModel : IValidatableObject
|
||||
public class CipherShareRequestModel : IValidatableObject
|
||||
{
|
||||
[Required]
|
||||
public IEnumerable<string> SubvaultIds { get; set; }
|
||||
[Required]
|
||||
public CipherRequestModel Cipher { get; set; }
|
||||
|
||||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
||||
{
|
||||
if(string.IsNullOrWhiteSpace(Cipher.OrganizationId))
|
||||
{
|
||||
yield return new ValidationResult("Cipher OrganizationId is required.",
|
||||
new string[] { nameof(Cipher.OrganizationId) });
|
||||
}
|
||||
|
||||
if(!SubvaultIds?.Any() ?? false)
|
||||
{
|
||||
yield return new ValidationResult("You must select at least one subvault.",
|
||||
new string[] { nameof(SubvaultIds) });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class CipherSubvaultsRequestModel : IValidatableObject
|
||||
{
|
||||
[Required]
|
||||
public IEnumerable<string> SubvaultIds { get; set; }
|
||||
|
||||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
||||
{
|
||||
if(!SubvaultIds?.Any() ?? false)
|
||||
|
@ -9,5 +9,6 @@ namespace Bit.Core.Repositories
|
||||
{
|
||||
Task<ICollection<SubvaultCipher>> GetManyByUserIdAsync(Guid userId);
|
||||
Task<ICollection<SubvaultCipher>> GetManyByUserIdCipherIdAsync(Guid userId, Guid cipherId);
|
||||
Task UpdateSubvaultsAsync(Guid cipherId, Guid userId, IEnumerable<Guid> subvaultIds);
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ using System.Data.SqlClient;
|
||||
using System.Data;
|
||||
using Dapper;
|
||||
using System.Linq;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Core.Repositories.SqlServer
|
||||
{
|
||||
@ -24,7 +25,7 @@ namespace Bit.Core.Repositories.SqlServer
|
||||
using(var connection = new SqlConnection(ConnectionString))
|
||||
{
|
||||
var results = await connection.QueryAsync<SubvaultCipher>(
|
||||
$"[dbo].[SubvaultCipher_ReadByUserId]",
|
||||
"[dbo].[SubvaultCipher_ReadByUserId]",
|
||||
new { UserId = userId },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
|
||||
@ -37,12 +38,23 @@ namespace Bit.Core.Repositories.SqlServer
|
||||
using(var connection = new SqlConnection(ConnectionString))
|
||||
{
|
||||
var results = await connection.QueryAsync<SubvaultCipher>(
|
||||
$"[dbo].[SubvaultCipher_ReadByUserIdCipherId]",
|
||||
"[dbo].[SubvaultCipher_ReadByUserIdCipherId]",
|
||||
new { UserId = userId, CipherId = cipherId },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
|
||||
return results.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateSubvaultsAsync(Guid cipherId, Guid userId, IEnumerable<Guid> subvaultIds)
|
||||
{
|
||||
using(var connection = new SqlConnection(ConnectionString))
|
||||
{
|
||||
var results = await connection.ExecuteAsync(
|
||||
"[dbo].[SubvaultCipher_UpdateSubvaults]",
|
||||
new { CipherId = cipherId, UserId = userId, SubvaultIds = subvaultIds.ToGuidIdArrayTVP() },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,8 @@ namespace Bit.Core.Services
|
||||
Task DeleteAsync(CipherDetails cipher, Guid deletingUserId);
|
||||
Task SaveFolderAsync(Folder folder);
|
||||
Task DeleteFolderAsync(Folder folder);
|
||||
Task MoveSubvaultAsync(Cipher cipher, Guid organizationId, IEnumerable<Guid> subvaultIds, Guid userId);
|
||||
Task ShareAsync(Cipher cipher, Guid organizationId, IEnumerable<Guid> subvaultIds, Guid userId);
|
||||
Task SaveSubvaultsAsync(Cipher cipher, IEnumerable<Guid> subvaultIds, Guid savingUserId);
|
||||
Task ImportCiphersAsync(List<Folder> folders, List<CipherDetails> ciphers,
|
||||
IEnumerable<KeyValuePair<int, int>> folderRelationships);
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ namespace Bit.Core.Services
|
||||
private readonly IOrganizationRepository _organizationRepository;
|
||||
private readonly IOrganizationUserRepository _organizationUserRepository;
|
||||
private readonly ISubvaultUserRepository _subvaultUserRepository;
|
||||
private readonly ISubvaultCipherRepository _subvaultCipherRepository;
|
||||
private readonly IPushService _pushService;
|
||||
|
||||
public CipherService(
|
||||
@ -26,6 +27,7 @@ namespace Bit.Core.Services
|
||||
IOrganizationRepository organizationRepository,
|
||||
IOrganizationUserRepository organizationUserRepository,
|
||||
ISubvaultUserRepository subvaultUserRepository,
|
||||
ISubvaultCipherRepository subvaultCipherRepository,
|
||||
IPushService pushService)
|
||||
{
|
||||
_cipherRepository = cipherRepository;
|
||||
@ -34,6 +36,7 @@ namespace Bit.Core.Services
|
||||
_organizationRepository = organizationRepository;
|
||||
_organizationUserRepository = organizationUserRepository;
|
||||
_subvaultUserRepository = subvaultUserRepository;
|
||||
_subvaultCipherRepository = subvaultCipherRepository;
|
||||
_pushService = pushService;
|
||||
}
|
||||
|
||||
@ -112,7 +115,7 @@ namespace Bit.Core.Services
|
||||
//await _pushService.PushSyncCipherDeleteAsync(cipher);
|
||||
}
|
||||
|
||||
public async Task MoveSubvaultAsync(Cipher cipher, Guid organizationId, IEnumerable<Guid> subvaultIds, Guid movingUserId)
|
||||
public async Task ShareAsync(Cipher cipher, Guid organizationId, IEnumerable<Guid> subvaultIds, Guid sharingUserId)
|
||||
{
|
||||
if(cipher.Id == default(Guid))
|
||||
{
|
||||
@ -124,14 +127,14 @@ namespace Bit.Core.Services
|
||||
throw new BadRequestException("Already belongs to an organization.");
|
||||
}
|
||||
|
||||
if(!cipher.UserId.HasValue || cipher.UserId.Value != movingUserId)
|
||||
if(!cipher.UserId.HasValue || cipher.UserId.Value != sharingUserId)
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
// We do not need to check if the user belongs to this organization since this call will return no subvaults
|
||||
// and therefore be caught by the .Any() check below.
|
||||
var subvaultUserDetails = await _subvaultUserRepository.GetPermissionsByUserIdAsync(movingUserId, subvaultIds,
|
||||
var subvaultUserDetails = await _subvaultUserRepository.GetPermissionsByUserIdAsync(sharingUserId, subvaultIds,
|
||||
organizationId);
|
||||
|
||||
var writeableSubvaults = subvaultUserDetails.Where(s => !s.ReadOnly).Select(s => s.SubvaultId);
|
||||
@ -149,6 +152,35 @@ namespace Bit.Core.Services
|
||||
//await _pushService.PushSyncCipherUpdateAsync(cipher);
|
||||
}
|
||||
|
||||
public async Task SaveSubvaultsAsync(Cipher cipher, IEnumerable<Guid> subvaultIds, Guid savingUserId)
|
||||
{
|
||||
if(cipher.Id == default(Guid))
|
||||
{
|
||||
throw new BadRequestException(nameof(cipher.Id));
|
||||
}
|
||||
|
||||
if(!cipher.OrganizationId.HasValue)
|
||||
{
|
||||
throw new BadRequestException("Cipher must belong to an organization.");
|
||||
}
|
||||
|
||||
// We do not need to check if the user belongs to this organization since this call will return no subvaults
|
||||
// and therefore be caught by the .Any() check below.
|
||||
var subvaultUserDetails = await _subvaultUserRepository.GetPermissionsByUserIdAsync(savingUserId, subvaultIds,
|
||||
cipher.OrganizationId.Value);
|
||||
|
||||
var writeableSubvaults = subvaultUserDetails.Where(s => !s.ReadOnly).Select(s => s.SubvaultId);
|
||||
if(!writeableSubvaults.Any())
|
||||
{
|
||||
throw new BadRequestException("No subvaults.");
|
||||
}
|
||||
|
||||
await _subvaultCipherRepository.UpdateSubvaultsAsync(cipher.Id, savingUserId, writeableSubvaults);
|
||||
|
||||
// push
|
||||
//await _pushService.PushSyncCipherUpdateAsync(cipher);
|
||||
}
|
||||
|
||||
public async Task ImportCiphersAsync(
|
||||
List<Folder> folders,
|
||||
List<CipherDetails> ciphers,
|
||||
|
Reference in New Issue
Block a user