diff --git a/src/Api/Public/Controllers/GroupsController.cs b/src/Api/Public/Controllers/GroupsController.cs index 6df656b3cc..f8717c543c 100644 --- a/src/Api/Public/Controllers/GroupsController.cs +++ b/src/Api/Public/Controllers/GroupsController.cs @@ -42,12 +42,13 @@ namespace Bit.Api.Public.Controllers [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task Get(Guid id) { - var group = await _groupRepository.GetByIdAsync(id); + var groupDetails = await _groupRepository.GetByIdWithCollectionsAsync(id); + var group = groupDetails?.Item1; if(group == null || group.OrganizationId != _currentContext.OrganizationId) { return new NotFoundResult(); } - var response = new GroupResponseModel(group); + var response = new GroupResponseModel(group, groupDetails.Item2); return new JsonResult(response); } @@ -56,13 +57,15 @@ namespace Bit.Api.Public.Controllers /// /// /// Returns a list of your organization's groups. + /// Group objects listed in this call do not include information about their associated collections. /// [HttpGet] [ProducesResponseType(typeof(ListResponseModel), (int)HttpStatusCode.OK)] public async Task List() { var groups = await _groupRepository.GetManyByOrganizationIdAsync(_currentContext.OrganizationId.Value); - var groupResponses = groups.Select(g => new GroupResponseModel(g)); + // TODO: Get all CollectionGroup associations for the organization and marry them up here for the response. + var groupResponses = groups.Select(g => new GroupResponseModel(g, null)); var response = new ListResponseModel(groupResponses); return new JsonResult(response); } @@ -82,7 +85,7 @@ namespace Bit.Api.Public.Controllers var group = model.ToGroup(_currentContext.OrganizationId.Value); var associations = model.Collections?.Select(c => c.ToSelectionReadOnly()); await _groupService.SaveAsync(group, associations); - var response = new GroupResponseModel(group); + var response = new GroupResponseModel(group, associations); return new JsonResult(response); } @@ -109,7 +112,7 @@ namespace Bit.Api.Public.Controllers var updatedGroup = model.ToGroup(existingGroup); var associations = model.Collections?.Select(c => c.ToSelectionReadOnly()); await _groupService.SaveAsync(updatedGroup, associations); - var response = new GroupResponseModel(updatedGroup); + var response = new GroupResponseModel(updatedGroup, associations); return new JsonResult(response); } diff --git a/src/Core/Models/Api/Public/BaseAssociationWithPermissionsModel.cs b/src/Core/Models/Api/Public/BaseAssociationWithPermissionsModel.cs new file mode 100644 index 0000000000..3cde538dab --- /dev/null +++ b/src/Core/Models/Api/Public/BaseAssociationWithPermissionsModel.cs @@ -0,0 +1,21 @@ +using System; +using System.ComponentModel.DataAnnotations; +using Newtonsoft.Json; + +namespace Bit.Core.Models.Api.Public +{ + public abstract class BaseAssociationWithPermissionsModel + { + /// + /// The associated object's unique identifier. + /// + /// bfbc8338-e329-4dc0-b0c9-317c2ebf1a09 + [Required] + public Guid? Id { get; set; } + /// + /// When true, the read only permission will not allow the user or group to make changes to items. + /// + [Required] + public bool? ReadOnly { get; set; } + } +} diff --git a/src/Core/Models/Api/Public/Request/AssociationWithPermissionsRequestModel.cs b/src/Core/Models/Api/Public/Request/AssociationWithPermissionsRequestModel.cs index c8b53d53cf..2f0e566ee9 100644 --- a/src/Core/Models/Api/Public/Request/AssociationWithPermissionsRequestModel.cs +++ b/src/Core/Models/Api/Public/Request/AssociationWithPermissionsRequestModel.cs @@ -1,24 +1,9 @@ -using System; -using System.ComponentModel.DataAnnotations; -using Newtonsoft.Json; -using Bit.Core.Models.Data; +using Bit.Core.Models.Data; namespace Bit.Core.Models.Api.Public { - public class AssociationWithPermissionsRequestModel + public class AssociationWithPermissionsRequestModel : BaseAssociationWithPermissionsModel { - /// - /// The associated object's unique identifier. - /// - /// bfbc8338-e329-4dc0-b0c9-317c2ebf1a09 - [Required] - public Guid? Id { get; set; } - /// - /// When true, the read only permission will not allow the user or group to make changes to items. - /// - [Required] - public bool? ReadOnly { get; set; } - public SelectionReadOnly ToSelectionReadOnly() { return new SelectionReadOnly diff --git a/src/Core/Models/Api/Public/Response/AssociationWithPermissionsResponseModel.cs b/src/Core/Models/Api/Public/Response/AssociationWithPermissionsResponseModel.cs new file mode 100644 index 0000000000..2e8101f8e5 --- /dev/null +++ b/src/Core/Models/Api/Public/Response/AssociationWithPermissionsResponseModel.cs @@ -0,0 +1,18 @@ +using System; +using Bit.Core.Models.Data; + +namespace Bit.Core.Models.Api.Public +{ + public class AssociationWithPermissionsResponseModel : BaseAssociationWithPermissionsModel + { + public AssociationWithPermissionsResponseModel(SelectionReadOnly selection) + { + if(selection == null) + { + throw new ArgumentNullException(nameof(selection)); + } + Id = selection.Id; + ReadOnly = selection.ReadOnly; + } + } +} diff --git a/src/Core/Models/Api/Public/Response/GroupResponseModel.cs b/src/Core/Models/Api/Public/Response/GroupResponseModel.cs index 321da1841d..2be85fb455 100644 --- a/src/Core/Models/Api/Public/Response/GroupResponseModel.cs +++ b/src/Core/Models/Api/Public/Response/GroupResponseModel.cs @@ -1,5 +1,8 @@ using System; +using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.Linq; +using Bit.Core.Models.Data; using Bit.Core.Models.Table; namespace Bit.Core.Models.Api.Public @@ -9,7 +12,7 @@ namespace Bit.Core.Models.Api.Public /// public class GroupResponseModel : GroupBaseModel, IResponseModel { - public GroupResponseModel(Group group) + public GroupResponseModel(Group group, IEnumerable collections) { if(group == null) { @@ -20,6 +23,7 @@ namespace Bit.Core.Models.Api.Public Name = group.Name; AccessAll = group.AccessAll; ExternalId = group.ExternalId; + Collections = collections?.Select(c => new AssociationWithPermissionsResponseModel(c)); } /// @@ -34,5 +38,9 @@ namespace Bit.Core.Models.Api.Public /// 539a36c5-e0d2-4cf9-979e-51ecf5cf6593 [Required] public Guid Id { get; set; } + /// + /// The associated collections that this group can access. + /// + public IEnumerable Collections { get; set; } } }