diff --git a/src/Api/Controllers/SharesController.cs b/src/Api/Controllers/SharesController.cs index 3ea2021ca..31448b6df 100644 --- a/src/Api/Controllers/SharesController.cs +++ b/src/Api/Controllers/SharesController.cs @@ -16,13 +16,16 @@ namespace Bit.Api.Controllers { private readonly IShareRepository _shareRepository; private readonly IUserService _userService; + private readonly ICipherService _cipherService; public SharesController( IShareRepository shareRepository, - IUserService userService) + IUserService userService, + ICipherService cipherService) { _shareRepository = shareRepository; _userService = userService; + _cipherService = cipherService; } [HttpGet("{id}")] @@ -42,7 +45,7 @@ namespace Bit.Api.Controllers public async Task Post([FromBody]ShareRequestModel model) { var share = model.ToShare(_userService.GetProperUserId(User).Value); - await _shareRepository.CreateAsync(share); + await _cipherService.ShareAsync(share, model.Email); var response = new ShareResponseModel(share); return response; diff --git a/src/Api/Models/Request/ShareRequestModel.cs b/src/Api/Models/Request/ShareRequestModel.cs index 6655b12c5..c66b8ec90 100644 --- a/src/Api/Models/Request/ShareRequestModel.cs +++ b/src/Api/Models/Request/ShareRequestModel.cs @@ -8,8 +8,7 @@ namespace Bit.Api.Models public class ShareRequestModel { [Required] - [StringLength(36)] - public string UserId { get; set; } + public string Email { get; set; } [Required] [StringLength(36)] public string CipherId { get; set; } @@ -25,7 +24,6 @@ namespace Bit.Api.Models public Share ToShare(Share existingShare) { - existingShare.UserId = new Guid(UserId); existingShare.CipherId = new Guid(CipherId); existingShare.Key = Key; diff --git a/src/Core/Services/ICipherService.cs b/src/Core/Services/ICipherService.cs index 789ad0759..2e9cad990 100644 --- a/src/Core/Services/ICipherService.cs +++ b/src/Core/Services/ICipherService.cs @@ -8,6 +8,8 @@ namespace Bit.Core.Services { Task SaveAsync(Cipher cipher); Task DeleteAsync(Cipher cipher); - Task ImportCiphersAsync(List folders, List ciphers, IEnumerable> folderRelationships); + Task ImportCiphersAsync(List folders, List ciphers, + IEnumerable> folderRelationships); + Task ShareAsync(Share share, string email); } } diff --git a/src/Core/Services/Implementations/CipherService.cs b/src/Core/Services/Implementations/CipherService.cs index e8834a51e..3d172ff01 100644 --- a/src/Core/Services/Implementations/CipherService.cs +++ b/src/Core/Services/Implementations/CipherService.cs @@ -10,13 +10,19 @@ namespace Bit.Core.Services public class CipherService : ICipherService { private readonly ICipherRepository _cipherRepository; + private readonly IShareRepository _shareRepository; + private readonly IUserRepository _userRepository; private readonly IPushService _pushService; public CipherService( ICipherRepository cipherRepository, + IShareRepository shareRepository, + IUserRepository userRepository, IPushService pushService) { _cipherRepository = cipherRepository; + _shareRepository = shareRepository; + _userRepository = userRepository; _pushService = pushService; } @@ -84,5 +90,24 @@ namespace Bit.Core.Services await _pushService.PushSyncCiphersAsync(userId.Value); } } + + public async Task ShareAsync(Share share, string email) + { + // TODO: Make sure share does not already exist between these two users. + + var user = await _userRepository.GetByEmailAsync(email); + if(user == null) + { + return; + } + + share.UserId = user.Id; + + // TODO: Permissions and status + share.Permissions = null; + share.Status = Enums.ShareStatusType.Accepted; + + await _shareRepository.CreateAsync(share); + } } }