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

71 lines
2.6 KiB
C#
Raw Normal View History

2017-04-03 12:27:02 -04:00
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Bit.Core.Repositories;
using Microsoft.AspNetCore.Authorization;
using Bit.Core.Models.Api;
using Bit.Core.Exceptions;
using Bit.Core.Services;
2017-04-06 09:50:40 -04:00
using Bit.Core;
2017-04-03 12:27:02 -04:00
namespace Bit.Api.Controllers
{
2017-04-27 09:19:30 -04:00
[Route("organizations/{orgId}/collectionUsers")]
2017-04-03 12:27:02 -04:00
[Authorize("Application")]
2017-04-27 09:19:30 -04:00
public class CollectionUsersController : Controller
2017-04-03 12:27:02 -04:00
{
2017-04-27 09:19:30 -04:00
private readonly ICollectionRepository _collectionRepository;
private readonly ICollectionUserRepository _collectionUserRepository;
2017-04-03 12:27:02 -04:00
private readonly IUserService _userService;
2017-04-06 09:50:40 -04:00
private readonly CurrentContext _currentContext;
2017-04-03 12:27:02 -04:00
2017-04-27 09:19:30 -04:00
public CollectionUsersController(
ICollectionRepository collectionRepository,
ICollectionUserRepository collectionUserRepository,
2017-04-06 09:50:40 -04:00
IUserService userService,
CurrentContext currentContext)
2017-04-03 12:27:02 -04:00
{
2017-04-27 09:19:30 -04:00
_collectionRepository = collectionRepository;
_collectionUserRepository = collectionUserRepository;
2017-04-03 12:27:02 -04:00
_userService = userService;
2017-04-06 09:50:40 -04:00
_currentContext = currentContext;
2017-04-03 12:27:02 -04:00
}
2017-04-27 09:19:30 -04:00
[HttpGet("{collectionId}")]
public async Task<ListResponseModel<CollectionUserResponseModel>> GetByCollection(string orgId, string collectionId)
2017-04-03 12:27:02 -04:00
{
2017-04-27 09:19:30 -04:00
var collectionIdGuid = new Guid(collectionId);
var collection = await _collectionRepository.GetByIdAsync(collectionIdGuid);
if(collection == null || !_currentContext.OrganizationAdmin(collection.OrganizationId))
2017-04-06 09:50:40 -04:00
{
throw new NotFoundException();
}
var collectionUsers = await _collectionUserRepository.GetManyDetailsByCollectionIdAsync(collection.OrganizationId,
collection.Id);
2017-04-27 09:39:21 -04:00
var responses = collectionUsers.Select(c => new CollectionUserResponseModel(c));
2017-04-27 09:19:30 -04:00
return new ListResponseModel<CollectionUserResponseModel>(responses);
2017-04-03 12:27:02 -04:00
}
[HttpDelete("{id}")]
[HttpPost("{id}/delete")]
public async Task Delete(string orgId, string id)
{
2017-04-27 09:19:30 -04:00
var user = await _collectionUserRepository.GetByIdAsync(new Guid(id));
2017-04-03 12:27:02 -04:00
if(user == null)
{
throw new NotFoundException();
}
2017-04-27 09:19:30 -04:00
var collection = await _collectionRepository.GetByIdAsync(user.CollectionId);
if(collection == null || !_currentContext.OrganizationAdmin(collection.OrganizationId))
2017-04-06 09:50:40 -04:00
{
throw new NotFoundException();
}
2017-04-27 09:19:30 -04:00
await _collectionUserRepository.DeleteAsync(user);
2017-04-03 12:27:02 -04:00
}
}
}