1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-02 00:22:50 -05:00

public apis for groups

This commit is contained in:
Kyle Spearrin
2019-03-01 17:38:22 -05:00
parent 88cb0443b7
commit 92d686ba36
7 changed files with 261 additions and 23 deletions

View File

@ -1,5 +1,11 @@
using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Bit.Core;
using Bit.Core.Models.Api.Public;
using Bit.Core.Repositories;
using Bit.Core.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@ -9,23 +15,121 @@ namespace Bit.Api.Public.Controllers
[Authorize("Organization")]
public class GroupsController : Controller
{
/// <summary>
/// Retrieves a specific product by unique id
/// </summary>
/// <remarks>Awesomeness!</remarks>
/// <response code="200">Group created</response>
/// <response code="400">Group has missing/invalid values</response>
/// <response code="500">Oops! Can't create your product right now</response>
[HttpGet("{id}")]
[ProducesResponseType(typeof(GroupResponseModel), 200)]
public IActionResult Get(Guid id)
private readonly IGroupRepository _groupRepository;
private readonly IGroupService _groupService;
private readonly CurrentContext _currentContext;
public GroupsController(
IGroupRepository groupRepository,
IGroupService groupService,
CurrentContext currentContext)
{
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,
Name = "test",
OrganizationId = Guid.NewGuid()
}));
return new NotFoundResult();
}
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();
}
}
}