diff --git a/src/Api/Controllers/GroupsController.cs b/src/Api/Controllers/GroupsController.cs index 60b5ddec7a..735f5edd3a 100644 --- a/src/Api/Controllers/GroupsController.cs +++ b/src/Api/Controllers/GroupsController.cs @@ -16,16 +16,16 @@ namespace Bit.Api.Controllers public class GroupsController : Controller { private readonly IGroupRepository _groupRepository; - private readonly IUserService _userService; + private readonly IGroupService _groupService; private readonly CurrentContext _currentContext; public GroupsController( IGroupRepository groupRepository, - IUserService userService, + IGroupService groupService, CurrentContext currentContext) { _groupRepository = groupRepository; - _userService = userService; + _groupService = groupService; _currentContext = currentContext; } @@ -77,15 +77,7 @@ namespace Bit.Api.Controllers } var group = model.ToGroup(orgIdGuid); - if(model.CollectionIds == null) - { - await _groupRepository.CreateAsync(group); - } - else - { - await _groupRepository.CreateAsync(group, model.CollectionIds.Select(c => new Guid(c))); - } - + await _groupService.SaveAsync(group, model.CollectionIds?.Select(c => new Guid(c))); return new GroupResponseModel(group); } @@ -99,15 +91,7 @@ namespace Bit.Api.Controllers throw new NotFoundException(); } - if(model.CollectionIds == null) - { - await _groupRepository.ReplaceAsync(model.ToGroup(group)); - } - else - { - await _groupRepository.ReplaceAsync(model.ToGroup(group), model.CollectionIds.Select(c => new Guid(c))); - } - + await _groupService.SaveAsync(model.ToGroup(group), model.CollectionIds?.Select(c => new Guid(c))); return new GroupResponseModel(group); } diff --git a/src/Core/Services/IGroupService.cs b/src/Core/Services/IGroupService.cs new file mode 100644 index 0000000000..303fc36849 --- /dev/null +++ b/src/Core/Services/IGroupService.cs @@ -0,0 +1,12 @@ +using System.Threading.Tasks; +using Bit.Core.Models.Table; +using System.Collections.Generic; +using System; + +namespace Bit.Core.Services +{ + public interface IGroupService + { + Task SaveAsync(Group group, IEnumerable collectionIds = null); + } +} diff --git a/src/Core/Services/Implementations/GroupService.cs b/src/Core/Services/Implementations/GroupService.cs new file mode 100644 index 0000000000..9e8bc0ba21 --- /dev/null +++ b/src/Core/Services/Implementations/GroupService.cs @@ -0,0 +1,60 @@ +using System; +using System.Threading.Tasks; +using Bit.Core.Exceptions; +using Bit.Core.Models.Table; +using Bit.Core.Repositories; +using System.Collections.Generic; + +namespace Bit.Core.Services +{ + public class GroupService : IGroupService + { + private readonly IOrganizationRepository _organizationRepository; + private readonly IGroupRepository _groupRepository; + + public GroupService( + IOrganizationRepository organizationRepository, + IGroupRepository groupRepository) + { + _organizationRepository = organizationRepository; + _groupRepository = groupRepository; + } + + public async Task SaveAsync(Group group, IEnumerable collectionIds = null) + { + var org = await _organizationRepository.GetByIdAsync(group.OrganizationId); + if(org == null) + { + throw new BadRequestException("Organization not found"); + } + + if(!org.UseGroups) + { + throw new BadRequestException("This organization cannot use groups."); + } + + if(group.Id == default(Guid)) + { + if(collectionIds == null) + { + await _groupRepository.CreateAsync(group); + } + else + { + await _groupRepository.CreateAsync(group, collectionIds); + } + } + else + { + if(collectionIds == null) + { + await _groupRepository.ReplaceAsync(group); + } + else + { + await _groupRepository.ReplaceAsync(group, collectionIds); + } + } + } + } +} diff --git a/src/Core/Utilities/ServiceCollectionExtensions.cs b/src/Core/Utilities/ServiceCollectionExtensions.cs index ee304a811a..91f13724cf 100644 --- a/src/Core/Utilities/ServiceCollectionExtensions.cs +++ b/src/Core/Utilities/ServiceCollectionExtensions.cs @@ -45,6 +45,7 @@ namespace Bit.Core.Utilities services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); } public static void AddDefaultServices(this IServiceCollection services)