diff --git a/src/Api/Controllers/SubvaultsController.cs b/src/Api/Controllers/SubvaultsController.cs index 4d8b60986b..5c0beea805 100644 --- a/src/Api/Controllers/SubvaultsController.cs +++ b/src/Api/Controllers/SubvaultsController.cs @@ -16,15 +16,18 @@ namespace Bit.Api.Controllers public class SubvaultsController : Controller { private readonly ISubvaultRepository _subvaultRepository; + private readonly ISubvaultService _subvaultService; private readonly IUserService _userService; private readonly CurrentContext _currentContext; public SubvaultsController( ISubvaultRepository subvaultRepository, + ISubvaultService subvaultService, IUserService userService, CurrentContext currentContext) { _subvaultRepository = subvaultRepository; + _subvaultService = subvaultService; _userService = userService; _currentContext = currentContext; } @@ -73,7 +76,7 @@ namespace Bit.Api.Controllers } var subvault = model.ToSubvault(orgIdGuid); - await _subvaultRepository.CreateAsync(subvault); + await _subvaultService.SaveAsync(subvault); return new SubvaultResponseModel(subvault); } @@ -87,7 +90,7 @@ namespace Bit.Api.Controllers throw new NotFoundException(); } - await _subvaultRepository.ReplaceAsync(model.ToSubvault(subvault)); + await _subvaultService.SaveAsync(model.ToSubvault(subvault)); return new SubvaultResponseModel(subvault); } diff --git a/src/Core/Services/ISubvaultService.cs b/src/Core/Services/ISubvaultService.cs index a1a7c8b8a4..43cd839b75 100644 --- a/src/Core/Services/ISubvaultService.cs +++ b/src/Core/Services/ISubvaultService.cs @@ -1,13 +1,10 @@ using System.Threading.Tasks; -using Bit.Core.Models.Business; using Bit.Core.Models.Table; -using System; -using System.Collections.Generic; namespace Bit.Core.Services { public interface ISubvaultService { - + Task SaveAsync(Subvault subvault); } } diff --git a/src/Core/Services/Implementations/SubvaultService.cs b/src/Core/Services/Implementations/SubvaultService.cs index 89af03cdb0..b03cbf2bd9 100644 --- a/src/Core/Services/Implementations/SubvaultService.cs +++ b/src/Core/Services/Implementations/SubvaultService.cs @@ -1,12 +1,8 @@ using System; -using System.Linq; using System.Threading.Tasks; -using Bit.Core.Repositories; -using Bit.Core.Models.Business; -using Bit.Core.Models.Table; -using Bit.Core.Utilities; using Bit.Core.Exceptions; -using System.Collections.Generic; +using Bit.Core.Models.Table; +using Bit.Core.Repositories; namespace Bit.Core.Services { @@ -35,6 +31,32 @@ namespace Bit.Core.Services _mailService = mailService; } + public async Task SaveAsync(Subvault subvault) + { + if(subvault.Id == default(Guid)) + { + var org = await _organizationRepository.GetByIdAsync(subvault.OrganizationId); + if(org == null) + { + throw new BadRequestException("Org not found"); + } + if(org.MaxSubvaults.HasValue) + { + var subvaultCount = await _subvaultRepository.GetCountByOrganizationIdAsync(org.Id); + if(org.MaxSubvaults.Value <= subvaultCount) + { + throw new BadRequestException("You have reached the maximum number of subvaults " + + $"({org.MaxSubvaults.Value}) for this organization."); + } + } + + await _subvaultRepository.CreateAsync(subvault); + } + else + { + await _subvaultRepository.ReplaceAsync(subvault); + } + } } }