Files
server/src/Api/Controllers/CollectionsController.cs

179 lines
7.3 KiB
C#
Raw Normal View History

2017-03-07 23:06:14 -05:00
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Bit.Core.Repositories;
using Microsoft.AspNetCore.Authorization;
2017-03-08 21:55:08 -05:00
using Bit.Core.Models.Api;
2017-03-07 23:06:14 -05:00
using Bit.Core.Exceptions;
using Bit.Core.Services;
using Bit.Core.Context;
using Bit.Core.Models.Table;
2018-10-17 22:18:03 -04:00
using System.Collections.Generic;
2017-03-07 23:06:14 -05:00
namespace Bit.Api.Controllers
{
2017-04-27 09:19:30 -04:00
[Route("organizations/{orgId}/collections")]
2017-03-07 23:06:14 -05:00
[Authorize("Application")]
2017-04-27 09:19:30 -04:00
public class CollectionsController : Controller
2017-03-07 23:06:14 -05:00
{
2017-04-27 09:19:30 -04:00
private readonly ICollectionRepository _collectionRepository;
private readonly ICollectionService _collectionService;
2017-03-07 23:06:14 -05:00
private readonly IUserService _userService;
private readonly ICurrentContext _currentContext;
2017-03-07 23:06:14 -05:00
2017-04-27 09:19:30 -04:00
public CollectionsController(
ICollectionRepository collectionRepository,
ICollectionService collectionService,
IUserService userService,
ICurrentContext currentContext)
2017-03-07 23:06:14 -05:00
{
2017-04-27 09:19:30 -04:00
_collectionRepository = collectionRepository;
_collectionService = collectionService;
2017-03-07 23:06:14 -05:00
_userService = userService;
_currentContext = currentContext;
2017-03-07 23:06:14 -05:00
}
[HttpGet("{id}")]
2017-04-27 09:19:30 -04:00
public async Task<CollectionResponseModel> Get(string orgId, string id)
2017-03-07 23:06:14 -05:00
{
var collection = await GetCollectionAsync(new Guid(id), new Guid(orgId));
2017-04-27 09:19:30 -04:00
return new CollectionResponseModel(collection);
2017-03-07 23:06:14 -05:00
}
[HttpGet("{id}/details")]
2018-06-11 14:25:53 -04:00
public async Task<CollectionGroupDetailsResponseModel> GetDetails(string orgId, string id)
{
var orgIdGuid = new Guid(orgId);
if (!ManageAnyCollections(orgIdGuid) && !_currentContext.ManageUsers(orgIdGuid))
{
throw new NotFoundException();
}
var idGuid = new Guid(id);
if (_currentContext.ManageAllCollections(orgIdGuid))
{
var collectionDetails = await _collectionRepository.GetByIdWithGroupsAsync(idGuid);
if (collectionDetails?.Item1 == null || collectionDetails.Item1.OrganizationId != orgIdGuid)
{
throw new NotFoundException();
}
return new CollectionGroupDetailsResponseModel(collectionDetails.Item1, collectionDetails.Item2);
}
else
{
var collectionDetails = await _collectionRepository.GetByIdWithGroupsAsync(idGuid,
_currentContext.UserId.Value);
if (collectionDetails?.Item1 == null || collectionDetails.Item1.OrganizationId != orgIdGuid)
{
throw new NotFoundException();
}
return new CollectionGroupDetailsResponseModel(collectionDetails.Item1, collectionDetails.Item2);
}
}
[HttpGet("")]
2017-04-27 09:19:30 -04:00
public async Task<ListResponseModel<CollectionResponseModel>> Get(string orgId)
2017-03-07 23:06:14 -05:00
{
var orgIdGuid = new Guid(orgId);
if (!_currentContext.ManageAllCollections(orgIdGuid) && !_currentContext.ManageUsers(orgIdGuid))
{
throw new NotFoundException();
}
2017-04-27 09:19:30 -04:00
var collections = await _collectionRepository.GetManyByOrganizationIdAsync(orgIdGuid);
2017-04-27 09:39:21 -04:00
var responses = collections.Select(c => new CollectionResponseModel(c));
2017-04-27 09:19:30 -04:00
return new ListResponseModel<CollectionResponseModel>(responses);
2017-03-07 23:06:14 -05:00
}
2017-04-27 09:19:30 -04:00
[HttpGet("~/collections")]
public async Task<ListResponseModel<CollectionDetailsResponseModel>> GetUser()
2017-03-07 23:06:14 -05:00
{
2017-08-30 15:57:17 -04:00
var collections = await _collectionRepository.GetManyByUserIdAsync(
2018-06-11 14:25:53 -04:00
_userService.GetProperUserId(User).Value);
var responses = collections.Select(c => new CollectionDetailsResponseModel(c));
return new ListResponseModel<CollectionDetailsResponseModel>(responses);
2017-03-07 23:06:14 -05:00
}
2017-05-11 14:52:35 -04:00
[HttpGet("{id}/users")]
2018-10-18 08:38:22 -04:00
public async Task<IEnumerable<SelectionReadOnlyResponseModel>> GetUsers(string orgId, string id)
2017-05-11 14:52:35 -04:00
{
var collection = await GetCollectionAsync(new Guid(id), new Guid(orgId));
2018-10-17 22:18:03 -04:00
var collectionUsers = await _collectionRepository.GetManyUsersByIdAsync(collection.Id);
var responses = collectionUsers.Select(cu => new SelectionReadOnlyResponseModel(cu));
2018-10-18 08:38:22 -04:00
return responses;
2017-05-11 14:52:35 -04:00
}
2017-03-07 23:06:14 -05:00
[HttpPost("")]
2017-04-27 09:19:30 -04:00
public async Task<CollectionResponseModel> Post(string orgId, [FromBody]CollectionRequestModel model)
2017-03-07 23:06:14 -05:00
{
var orgIdGuid = new Guid(orgId);
if (!ManageAnyCollections(orgIdGuid))
{
throw new NotFoundException();
}
2017-04-27 09:19:30 -04:00
var collection = model.ToCollection(orgIdGuid);
await _collectionService.SaveAsync(collection, model.Groups?.Select(g => g.ToSelectionReadOnly()),
!_currentContext.ManageAllCollections(orgIdGuid) ? _currentContext.UserId : null);
2017-04-27 09:19:30 -04:00
return new CollectionResponseModel(collection);
2017-03-07 23:06:14 -05:00
}
[HttpPut("{id}")]
[HttpPost("{id}")]
2017-04-27 09:19:30 -04:00
public async Task<CollectionResponseModel> Put(string orgId, string id, [FromBody]CollectionRequestModel model)
2017-03-07 23:06:14 -05:00
{
var collection = await GetCollectionAsync(new Guid(id), new Guid(orgId));
2017-05-11 12:22:14 -04:00
await _collectionService.SaveAsync(model.ToCollection(collection),
model.Groups?.Select(g => g.ToSelectionReadOnly()));
2017-04-27 09:19:30 -04:00
return new CollectionResponseModel(collection);
2017-03-07 23:06:14 -05:00
}
2018-10-17 22:18:03 -04:00
[HttpPut("{id}/users")]
public async Task PutUsers(string orgId, string id, [FromBody]IEnumerable<SelectionReadOnlyRequestModel> model)
{
var collection = await GetCollectionAsync(new Guid(id), new Guid(orgId));
await _collectionRepository.UpdateUsersAsync(collection.Id, model?.Select(g => g.ToSelectionReadOnly()));
}
2017-03-07 23:06:14 -05:00
[HttpDelete("{id}")]
[HttpPost("{id}/delete")]
2017-03-09 22:09:09 -05:00
public async Task Delete(string orgId, string id)
2017-03-07 23:06:14 -05:00
{
var collection = await GetCollectionAsync(new Guid(id), new Guid(orgId));
await _collectionService.DeleteAsync(collection);
2017-03-07 23:06:14 -05:00
}
2017-05-11 14:52:35 -04:00
[HttpDelete("{id}/user/{orgUserId}")]
[HttpPost("{id}/delete-user/{orgUserId}")]
public async Task Delete(string orgId, string id, string orgUserId)
{
var collection = await GetCollectionAsync(new Guid(id), new Guid(orgId));
await _collectionService.DeleteUserAsync(collection, new Guid(orgUserId));
}
private async Task<Collection> GetCollectionAsync(Guid id, Guid orgId)
{
if (!ManageAnyCollections(orgId))
2017-05-11 14:52:35 -04:00
{
throw new NotFoundException();
}
var collection = _currentContext.OrganizationAdmin(orgId) ?
await _collectionRepository.GetByIdAsync(id) :
await _collectionRepository.GetByIdAsync(id, _currentContext.UserId.Value);
if (collection == null || collection.OrganizationId != orgId)
{
throw new NotFoundException();
}
return collection;
2017-05-11 14:52:35 -04:00
}
private bool ManageAnyCollections(Guid orgId)
{
return _currentContext.ManageAssignedCollections(orgId) || _currentContext.ManageAllCollections(orgId);
}
2017-03-07 23:06:14 -05:00
}
}