From c92ef0866418e8b8c38fafcf3530ce60dd5b885d Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 17 Jul 2018 13:34:12 -0400 Subject: [PATCH] check collection count on org import --- .../Services/Implementations/CipherService.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Core/Services/Implementations/CipherService.cs b/src/Core/Services/Implementations/CipherService.cs index 69897d691d..fb540438f4 100644 --- a/src/Core/Services/Implementations/CipherService.cs +++ b/src/Core/Services/Implementations/CipherService.cs @@ -16,6 +16,7 @@ namespace Bit.Core.Services { private readonly ICipherRepository _cipherRepository; private readonly IFolderRepository _folderRepository; + private readonly ICollectionRepository _collectionRepository; private readonly IUserRepository _userRepository; private readonly IOrganizationRepository _organizationRepository; private readonly IOrganizationUserRepository _organizationUserRepository; @@ -27,6 +28,7 @@ namespace Bit.Core.Services public CipherService( ICipherRepository cipherRepository, IFolderRepository folderRepository, + ICollectionRepository collectionRepository, IUserRepository userRepository, IOrganizationRepository organizationRepository, IOrganizationUserRepository organizationUserRepository, @@ -37,6 +39,7 @@ namespace Bit.Core.Services { _cipherRepository = cipherRepository; _folderRepository = folderRepository; + _collectionRepository = collectionRepository; _userRepository = userRepository; _organizationRepository = organizationRepository; _organizationUserRepository = organizationUserRepository; @@ -529,6 +532,20 @@ namespace Bit.Core.Services IEnumerable> collectionRelationships, Guid importingUserId) { + if(collections.Count > 0) + { + var org = await _organizationRepository.GetByIdAsync(collections[0].OrganizationId); + if(org != null && org.MaxCollections.HasValue) + { + var collectionCount = await _collectionRepository.GetCountByOrganizationIdAsync(org.Id); + if(org.MaxCollections.Value <= (collectionCount + collections.Count)) + { + throw new BadRequestException("This organization can only have a maximum of " + + $"{org.MaxCollections.Value} collections."); + } + } + } + // Init. ids for ciphers foreach(var cipher in ciphers) {