mirror of
https://github.com/bitwarden/server.git
synced 2025-05-22 03:54:33 -05:00
public apis for groups
This commit is contained in:
parent
88cb0443b7
commit
92d686ba36
@ -1,5 +1,11 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Bit.Core;
|
||||||
using Bit.Core.Models.Api.Public;
|
using Bit.Core.Models.Api.Public;
|
||||||
|
using Bit.Core.Repositories;
|
||||||
|
using Bit.Core.Services;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
@ -9,23 +15,121 @@ namespace Bit.Api.Public.Controllers
|
|||||||
[Authorize("Organization")]
|
[Authorize("Organization")]
|
||||||
public class GroupsController : Controller
|
public class GroupsController : Controller
|
||||||
{
|
{
|
||||||
/// <summary>
|
private readonly IGroupRepository _groupRepository;
|
||||||
/// Retrieves a specific product by unique id
|
private readonly IGroupService _groupService;
|
||||||
/// </summary>
|
private readonly CurrentContext _currentContext;
|
||||||
/// <remarks>Awesomeness!</remarks>
|
|
||||||
/// <response code="200">Group created</response>
|
public GroupsController(
|
||||||
/// <response code="400">Group has missing/invalid values</response>
|
IGroupRepository groupRepository,
|
||||||
/// <response code="500">Oops! Can't create your product right now</response>
|
IGroupService groupService,
|
||||||
[HttpGet("{id}")]
|
CurrentContext currentContext)
|
||||||
[ProducesResponseType(typeof(GroupResponseModel), 200)]
|
|
||||||
public IActionResult Get(Guid id)
|
|
||||||
{
|
{
|
||||||
return new JsonResult(new GroupResponseModel(new Core.Models.Table.Group
|
_groupRepository = groupRepository;
|
||||||
|
_groupService = groupService;
|
||||||
|
_currentContext = currentContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve a group.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Retrieves the details of an existing group. You need only supply the unique group identifier
|
||||||
|
/// that was returned upon group creation.
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="id">The identifier of the group to be retrieved.</param>
|
||||||
|
[HttpGet("{id}")]
|
||||||
|
[ProducesResponseType(typeof(GroupResponseModel), (int)HttpStatusCode.OK)]
|
||||||
|
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||||
|
public async Task<IActionResult> Get(Guid id)
|
||||||
|
{
|
||||||
|
var group = await _groupRepository.GetByIdAsync(id);
|
||||||
|
if(group == null || group.OrganizationId != _currentContext.OrganizationId)
|
||||||
{
|
{
|
||||||
Id = id,
|
return new NotFoundResult();
|
||||||
Name = "test",
|
}
|
||||||
OrganizationId = Guid.NewGuid()
|
var response = new GroupResponseModel(group);
|
||||||
}));
|
return new JsonResult(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// List all groups.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Returns a list of your organization's groups.
|
||||||
|
/// </remarks>
|
||||||
|
[HttpGet]
|
||||||
|
[ProducesResponseType(typeof(ListResponseModel<GroupResponseModel>), (int)HttpStatusCode.OK)]
|
||||||
|
public async Task<IActionResult> List()
|
||||||
|
{
|
||||||
|
var groups = await _groupRepository.GetManyByOrganizationIdAsync(_currentContext.OrganizationId.Value);
|
||||||
|
var groupResponses = groups.Select(g => new GroupResponseModel(g));
|
||||||
|
var response = new ListResponseModel<GroupResponseModel>(groupResponses);
|
||||||
|
return new JsonResult(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create a group.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Creates a new group object.
|
||||||
|
/// </remarks>
|
||||||
|
[HttpPost]
|
||||||
|
[ProducesResponseType(typeof(GroupResponseModel), (int)HttpStatusCode.OK)]
|
||||||
|
[ProducesResponseType(typeof(ErrorResponseModel), (int)HttpStatusCode.BadRequest)]
|
||||||
|
public async Task<IActionResult> Post([FromBody]GroupCreateUpdateRequestModel model)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
return new JsonResult(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Update a group.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Updates the specified group object. If a property is not provided,
|
||||||
|
/// the value of the existing property will be reset.
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="id">The identifier of the group to be updated.</param>
|
||||||
|
[HttpPut("{id}")]
|
||||||
|
[ProducesResponseType(typeof(GroupResponseModel), (int)HttpStatusCode.OK)]
|
||||||
|
[ProducesResponseType(typeof(ErrorResponseModel), (int)HttpStatusCode.BadRequest)]
|
||||||
|
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||||
|
public async Task<IActionResult> Put(Guid id, [FromBody]GroupCreateUpdateRequestModel model)
|
||||||
|
{
|
||||||
|
var existingGroup = await _groupRepository.GetByIdAsync(id);
|
||||||
|
if(existingGroup == null || existingGroup.OrganizationId != _currentContext.OrganizationId)
|
||||||
|
{
|
||||||
|
return new NotFoundResult();
|
||||||
|
}
|
||||||
|
var updatedGroup = model.ToGroup(existingGroup);
|
||||||
|
var associations = model.Collections?.Select(c => c.ToSelectionReadOnly());
|
||||||
|
await _groupService.SaveAsync(updatedGroup, associations);
|
||||||
|
var response = new GroupResponseModel(updatedGroup);
|
||||||
|
return new JsonResult(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Delete a group.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Permanently deletes a group. This cannot be undone.
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="id">The identifier of the group to be deleted.</param>
|
||||||
|
[HttpDelete("{id}")]
|
||||||
|
[ProducesResponseType((int)HttpStatusCode.OK)]
|
||||||
|
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||||
|
public async Task<IActionResult> Delete(Guid id)
|
||||||
|
{
|
||||||
|
var group = await _groupRepository.GetByIdAsync(id);
|
||||||
|
if(group == null || group.OrganizationId != _currentContext.OrganizationId)
|
||||||
|
{
|
||||||
|
return new NotFoundResult();
|
||||||
|
}
|
||||||
|
await _groupRepository.DeleteAsync(group);
|
||||||
|
return new OkResult();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
27
src/Core/Models/Api/Public/GroupBaseModel.cs
Normal file
27
src/Core/Models/Api/Public/GroupBaseModel.cs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace Bit.Core.Models.Api.Public
|
||||||
|
{
|
||||||
|
public abstract class GroupBaseModel
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The name of the group.
|
||||||
|
/// </summary>
|
||||||
|
/// <example>Development Team</example>
|
||||||
|
[Required]
|
||||||
|
[StringLength(100)]
|
||||||
|
public string Name { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Determines if this group can access all collections within the organization, or only the associated
|
||||||
|
/// collections. If set to <c>true</c>, this option overrides any collection assignments.
|
||||||
|
/// </summary>
|
||||||
|
[Required]
|
||||||
|
public bool? AccessAll { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// External identifier linking this group to another system, such as a user directory.
|
||||||
|
/// </summary>
|
||||||
|
/// <example>external_id_123456</example>
|
||||||
|
[StringLength(300)]
|
||||||
|
public string ExternalId { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
using System;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Bit.Core.Models.Data;
|
||||||
|
|
||||||
|
namespace Bit.Core.Models.Api.Public
|
||||||
|
{
|
||||||
|
public class AssociationWithPermissionsRequestModel
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The associated object's unique identifier.
|
||||||
|
/// </summary>
|
||||||
|
/// <example>bfbc8338-e329-4dc0-b0c9-317c2ebf1a09</example>
|
||||||
|
[Required]
|
||||||
|
public Guid? Id { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// When true, the read only permission will not allow the user or group to make changes to items.
|
||||||
|
/// </summary>
|
||||||
|
[Required]
|
||||||
|
public bool? ReadOnly { get; set; }
|
||||||
|
|
||||||
|
public SelectionReadOnly ToSelectionReadOnly()
|
||||||
|
{
|
||||||
|
return new SelectionReadOnly
|
||||||
|
{
|
||||||
|
Id = Id.Value,
|
||||||
|
ReadOnly = ReadOnly.Value
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Bit.Core.Models.Table;
|
||||||
|
|
||||||
|
namespace Bit.Core.Models.Api.Public
|
||||||
|
{
|
||||||
|
public class GroupCreateUpdateRequestModel : GroupBaseModel
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The associated collections that this group can access.
|
||||||
|
/// </summary>
|
||||||
|
public IEnumerable<AssociationWithPermissionsRequestModel> Collections { get; set; }
|
||||||
|
|
||||||
|
public Group ToGroup(Guid orgId)
|
||||||
|
{
|
||||||
|
return ToGroup(new Group
|
||||||
|
{
|
||||||
|
OrganizationId = orgId
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public Group ToGroup(Group existingGroup)
|
||||||
|
{
|
||||||
|
existingGroup.Name = Name;
|
||||||
|
existingGroup.AccessAll = AccessAll.Value;
|
||||||
|
existingGroup.ExternalId = ExternalId;
|
||||||
|
return existingGroup;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,12 +1,15 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
using Bit.Core.Models.Table;
|
using Bit.Core.Models.Table;
|
||||||
|
|
||||||
namespace Bit.Core.Models.Api.Public
|
namespace Bit.Core.Models.Api.Public
|
||||||
{
|
{
|
||||||
public class GroupResponseModel : ResponseModel
|
/// <summary>
|
||||||
|
/// A user group.
|
||||||
|
/// </summary>
|
||||||
|
public class GroupResponseModel : GroupBaseModel, IResponseModel
|
||||||
{
|
{
|
||||||
public GroupResponseModel(Group group, string obj = "group")
|
public GroupResponseModel(Group group)
|
||||||
: base(obj)
|
|
||||||
{
|
{
|
||||||
if(group == null)
|
if(group == null)
|
||||||
{
|
{
|
||||||
@ -14,16 +17,22 @@ namespace Bit.Core.Models.Api.Public
|
|||||||
}
|
}
|
||||||
|
|
||||||
Id = group.Id;
|
Id = group.Id;
|
||||||
OrganizationId = group.OrganizationId;
|
|
||||||
Name = group.Name;
|
Name = group.Name;
|
||||||
AccessAll = group.AccessAll;
|
AccessAll = group.AccessAll;
|
||||||
ExternalId = group.ExternalId;
|
ExternalId = group.ExternalId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// String representing the object's type. Objects of the same type share the same properties.
|
||||||
|
/// </summary>
|
||||||
|
/// <example>group</example>
|
||||||
|
[Required]
|
||||||
|
public string Object => "group";
|
||||||
|
/// <summary>
|
||||||
|
/// The group's unique identifier.
|
||||||
|
/// </summary>
|
||||||
|
/// <example>539a36c5-e0d2-4cf9-979e-51ecf5cf6593</example>
|
||||||
|
[Required]
|
||||||
public Guid Id { get; set; }
|
public Guid Id { get; set; }
|
||||||
public Guid OrganizationId { get; set; }
|
|
||||||
public string Name { get; set; }
|
|
||||||
public bool AccessAll { get; set; }
|
|
||||||
public string ExternalId { get; set; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
7
src/Core/Models/Api/Public/Response/IResponseModel.cs
Normal file
7
src/Core/Models/Api/Public/Response/IResponseModel.cs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
namespace Bit.Core.Models.Api.Public
|
||||||
|
{
|
||||||
|
public interface IResponseModel
|
||||||
|
{
|
||||||
|
string Object { get; }
|
||||||
|
}
|
||||||
|
}
|
30
src/Core/Models/Api/Public/Response/ListResponseModel.cs
Normal file
30
src/Core/Models/Api/Public/Response/ListResponseModel.cs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace Bit.Core.Models.Api.Public
|
||||||
|
{
|
||||||
|
public class ListResponseModel<T> : IResponseModel where T : IResponseModel
|
||||||
|
{
|
||||||
|
public ListResponseModel(IEnumerable<T> data, string continuationToken = null)
|
||||||
|
{
|
||||||
|
Data = data;
|
||||||
|
ContinuationToken = continuationToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// String representing the object's type. Objects of the same type share the same properties.
|
||||||
|
/// </summary>
|
||||||
|
/// <example>list</example>
|
||||||
|
[Required]
|
||||||
|
public string Object => "list";
|
||||||
|
/// <summary>
|
||||||
|
/// An array containing the actual response elements, paginated by any request parameters.
|
||||||
|
/// </summary>
|
||||||
|
[Required]
|
||||||
|
public IEnumerable<T> Data { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// A cursor for use in pagination.
|
||||||
|
/// </summary>
|
||||||
|
public string ContinuationToken { get; set; }
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user