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;
|
2021-02-04 12:54:21 -06:00
|
|
|
|
using Bit.Core.Context;
|
2018-10-17 14:58:45 -04:00
|
|
|
|
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;
|
2021-02-04 12:54:21 -06:00
|
|
|
|
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,
|
2017-04-05 16:40:09 -04:00
|
|
|
|
IUserService userService,
|
2021-02-04 12:54:21 -06:00
|
|
|
|
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;
|
2017-04-05 16:40:09 -04:00
|
|
|
|
_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
|
|
|
|
{
|
2018-10-17 14:58:45 -04: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
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-09 12:41:36 -04:00
|
|
|
|
[HttpGet("{id}/details")]
|
2018-06-11 14:25:53 -04:00
|
|
|
|
public async Task<CollectionGroupDetailsResponseModel> GetDetails(string orgId, string id)
|
2017-05-09 12:41:36 -04:00
|
|
|
|
{
|
2018-10-17 14:58:45 -04:00
|
|
|
|
var orgIdGuid = new Guid(orgId);
|
2021-01-12 11:02:39 -05:00
|
|
|
|
if (!ManageAnyCollections(orgIdGuid) && !_currentContext.ManageUsers(orgIdGuid))
|
2017-05-09 12:41:36 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-17 14:58:45 -04:00
|
|
|
|
var idGuid = new Guid(id);
|
2021-01-12 11:02:39 -05:00
|
|
|
|
if (_currentContext.ManageAllCollections(orgIdGuid))
|
2018-10-17 14:58:45 -04:00
|
|
|
|
{
|
|
|
|
|
|
var collectionDetails = await _collectionRepository.GetByIdWithGroupsAsync(idGuid);
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (collectionDetails?.Item1 == null || collectionDetails.Item1.OrganizationId != orgIdGuid)
|
2018-10-17 14:58:45 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
return new CollectionGroupDetailsResponseModel(collectionDetails.Item1, collectionDetails.Item2);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var collectionDetails = await _collectionRepository.GetByIdWithGroupsAsync(idGuid,
|
|
|
|
|
|
_currentContext.UserId.Value);
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (collectionDetails?.Item1 == null || collectionDetails.Item1.OrganizationId != orgIdGuid)
|
2018-10-17 14:58:45 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
return new CollectionGroupDetailsResponseModel(collectionDetails.Item1, collectionDetails.Item2);
|
|
|
|
|
|
}
|
2017-05-09 12:41:36 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-05 16:40:09 -04:00
|
|
|
|
[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
|
|
|
|
{
|
2017-04-05 16:40:09 -04:00
|
|
|
|
var orgIdGuid = new Guid(orgId);
|
2021-01-12 11:02:39 -05:00
|
|
|
|
if (!_currentContext.ManageAllCollections(orgIdGuid) && !_currentContext.ManageUsers(orgIdGuid))
|
2017-04-05 16:40:09 -04:00
|
|
|
|
{
|
|
|
|
|
|
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")]
|
2018-10-17 14:58:45 -04:00
|
|
|
|
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
|
|
|
|
{
|
2018-10-17 14:58:45 -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
|
|
|
|
{
|
2017-04-05 16:40:09 -04:00
|
|
|
|
var orgIdGuid = new Guid(orgId);
|
2021-01-12 11:02:39 -05:00
|
|
|
|
if (!ManageAnyCollections(orgIdGuid))
|
2017-04-05 16:40:09 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-27 09:19:30 -04:00
|
|
|
|
var collection = model.ToCollection(orgIdGuid);
|
2018-10-17 14:58:45 -04:00
|
|
|
|
await _collectionService.SaveAsync(collection, model.Groups?.Select(g => g.ToSelectionReadOnly()),
|
2021-01-12 11:02:39 -05:00
|
|
|
|
!_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
|
|
|
|
{
|
2018-10-17 14:58:45 -04: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
|
|
|
|
{
|
2018-10-17 14:58:45 -04:00
|
|
|
|
var collection = await GetCollectionAsync(new Guid(id), new Guid(orgId));
|
2017-12-01 16:00:30 -05:00
|
|
|
|
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)
|
|
|
|
|
|
{
|
2018-10-17 14:58:45 -04:00
|
|
|
|
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)
|
|
|
|
|
|
{
|
2021-01-12 11:02:39 -05:00
|
|
|
|
if (!ManageAnyCollections(orgId))
|
2017-05-11 14:52:35 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-17 14:58:45 -04:00
|
|
|
|
var collection = _currentContext.OrganizationAdmin(orgId) ?
|
|
|
|
|
|
await _collectionRepository.GetByIdAsync(id) :
|
|
|
|
|
|
await _collectionRepository.GetByIdAsync(id, _currentContext.UserId.Value);
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (collection == null || collection.OrganizationId != orgId)
|
2018-10-17 14:58:45 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return collection;
|
2017-05-11 14:52:35 -04:00
|
|
|
|
}
|
2021-01-12 11:02:39 -05:00
|
|
|
|
|
|
|
|
|
|
private bool ManageAnyCollections(Guid orgId)
|
|
|
|
|
|
{
|
|
|
|
|
|
return _currentContext.ManageAssignedCollections(orgId) || _currentContext.ManageAllCollections(orgId);
|
|
|
|
|
|
}
|
2017-03-07 23:06:14 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|