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

128 lines
4.9 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;
2017-05-11 12:22:14 -04:00
using Bit.Core.Models.Data;
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 ICollectionUserRepository _collectionUserRepository;
2017-04-27 09:19:30 -04:00
private readonly ICollectionService _collectionService;
2017-03-07 23:06:14 -05:00
private readonly IUserService _userService;
private readonly CurrentContext _currentContext;
2017-03-07 23:06:14 -05:00
2017-04-27 09:19:30 -04:00
public CollectionsController(
ICollectionRepository collectionRepository,
ICollectionUserRepository collectionUserRepository,
2017-04-27 09:19:30 -04:00
ICollectionService collectionService,
IUserService userService,
CurrentContext currentContext)
2017-03-07 23:06:14 -05:00
{
2017-04-27 09:19:30 -04:00
_collectionRepository = collectionRepository;
_collectionUserRepository = collectionUserRepository;
2017-04-27 09:19:30 -04:00
_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
{
2017-04-27 09:19:30 -04:00
var collection = await _collectionRepository.GetByIdAsync(new Guid(id));
if(collection == null || !_currentContext.OrganizationAdmin(collection.OrganizationId))
2017-03-07 23:06:14 -05:00
{
throw new NotFoundException();
}
2017-04-27 09:19:30 -04:00
return new CollectionResponseModel(collection);
2017-03-07 23:06:14 -05:00
}
[HttpGet("{id}/details")]
public async Task<CollectionDetailsResponseModel> GetDetails(string orgId, string id)
{
var collectionDetails = await _collectionRepository.GetByIdWithGroupsAsync(new Guid(id));
if(collectionDetails?.Item1 == null || !_currentContext.OrganizationAdmin(collectionDetails.Item1.OrganizationId))
{
throw new NotFoundException();
}
return new CollectionDetailsResponseModel(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.OrganizationAdmin(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<CollectionResponseModel>> GetUser()
2017-03-07 23:06:14 -05:00
{
var collections = await _collectionRepository.GetManyByUserIdAsync(_userService.GetProperUserId(User).Value);
var responses = collections.Select(c => new CollectionResponseModel(c));
return new ListResponseModel<CollectionResponseModel>(responses);
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(!_currentContext.OrganizationAdmin(orgIdGuid))
{
throw new NotFoundException();
}
2017-04-27 09:19:30 -04:00
var collection = model.ToCollection(orgIdGuid);
2017-05-11 12:22:14 -04:00
await _collectionService.SaveAsync(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
}
[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
{
2017-04-27 09:19:30 -04:00
var collection = await _collectionRepository.GetByIdAsync(new Guid(id));
if(collection == null || !_currentContext.OrganizationAdmin(collection.OrganizationId))
2017-03-07 23:06:14 -05:00
{
throw new NotFoundException();
}
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
}
[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
{
2017-04-27 09:19:30 -04:00
var collection = await _collectionRepository.GetByIdAsync(new Guid(id));
if(collection == null || !_currentContext.OrganizationAdmin(collection.OrganizationId))
2017-03-07 23:06:14 -05:00
{
throw new NotFoundException();
}
2017-04-27 09:19:30 -04:00
await _collectionRepository.DeleteAsync(collection);
2017-03-07 23:06:14 -05:00
}
}
}