2017-03-04 21:28:41 -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-04 21:28:41 -05:00
|
|
|
|
using Bit.Core.Exceptions;
|
|
|
|
|
|
using Bit.Core.Services;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Api.Controllers
|
|
|
|
|
|
{
|
|
|
|
|
|
[Route("organizations/{orgId}/users")]
|
|
|
|
|
|
[Authorize("Application")]
|
|
|
|
|
|
public class OrganizationUsersController : Controller
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IOrganizationRepository _organizationRepository;
|
|
|
|
|
|
private readonly IOrganizationUserRepository _organizationUserRepository;
|
|
|
|
|
|
private readonly IOrganizationService _organizationService;
|
2017-03-11 15:34:57 -05:00
|
|
|
|
private readonly ISubvaultRepository _subvaultRepository;
|
2017-03-04 21:28:41 -05:00
|
|
|
|
private readonly IUserService _userService;
|
|
|
|
|
|
|
|
|
|
|
|
public OrganizationUsersController(
|
|
|
|
|
|
IOrganizationRepository organizationRepository,
|
|
|
|
|
|
IOrganizationUserRepository organizationUserRepository,
|
|
|
|
|
|
IOrganizationService organizationService,
|
2017-03-11 15:34:57 -05:00
|
|
|
|
ISubvaultRepository subvaultRepository,
|
2017-03-04 21:28:41 -05:00
|
|
|
|
IUserService userService)
|
|
|
|
|
|
{
|
|
|
|
|
|
_organizationRepository = organizationRepository;
|
|
|
|
|
|
_organizationUserRepository = organizationUserRepository;
|
|
|
|
|
|
_organizationService = organizationService;
|
2017-03-11 15:34:57 -05:00
|
|
|
|
_subvaultRepository = subvaultRepository;
|
2017-03-04 21:28:41 -05:00
|
|
|
|
_userService = userService;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("{id}")]
|
2017-03-11 15:34:57 -05:00
|
|
|
|
public async Task<OrganizationUserDetailsResponseModel> Get(string orgId, string id)
|
2017-03-04 21:28:41 -05:00
|
|
|
|
{
|
|
|
|
|
|
var organizationUser = await _organizationUserRepository.GetDetailsByIdAsync(new Guid(id));
|
|
|
|
|
|
if(organizationUser == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-11 15:34:57 -05:00
|
|
|
|
return new OrganizationUserDetailsResponseModel(organizationUser.Item1, organizationUser.Item2);
|
2017-03-04 21:28:41 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("")]
|
|
|
|
|
|
public async Task<ListResponseModel<OrganizationUserResponseModel>> Get(string orgId)
|
|
|
|
|
|
{
|
2017-03-06 20:51:13 -05:00
|
|
|
|
var organizationUsers = await _organizationUserRepository.GetManyDetailsByOrganizationAsync(new Guid(orgId));
|
2017-03-04 21:28:41 -05:00
|
|
|
|
var responses = organizationUsers.Select(o => new OrganizationUserResponseModel(o));
|
|
|
|
|
|
return new ListResponseModel<OrganizationUserResponseModel>(responses);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("invite")]
|
|
|
|
|
|
public async Task Invite(string orgId, [FromBody]OrganizationUserInviteRequestModel model)
|
|
|
|
|
|
{
|
|
|
|
|
|
var user = await _userService.GetUserByPrincipalAsync(User);
|
2017-03-11 22:42:27 -05:00
|
|
|
|
var result = await _organizationService.InviteUserAsync(new Guid(orgId), model.Email,
|
2017-03-13 22:54:24 -04:00
|
|
|
|
model.Subvaults?.Select(s => s.ToSubvaultUser()));
|
2017-03-04 21:28:41 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-09 23:58:43 -05:00
|
|
|
|
[HttpPut("{id}/accept")]
|
2017-03-04 21:28:41 -05:00
|
|
|
|
[HttpPost("{id}/accept")]
|
|
|
|
|
|
public async Task Accept(string orgId, string id, [FromBody]OrganizationUserAcceptRequestModel model)
|
|
|
|
|
|
{
|
|
|
|
|
|
var user = await _userService.GetUserByPrincipalAsync(User);
|
|
|
|
|
|
var result = await _organizationService.AcceptUserAsync(new Guid(id), user, model.Token);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-09 23:58:43 -05:00
|
|
|
|
[HttpPut("{id}/confirm")]
|
2017-03-04 21:28:41 -05:00
|
|
|
|
[HttpPost("{id}/confirm")]
|
|
|
|
|
|
public async Task Confirm(string orgId, string id, [FromBody]OrganizationUserConfirmRequestModel model)
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = await _organizationService.ConfirmUserAsync(new Guid(id), model.Key);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-09 23:58:43 -05:00
|
|
|
|
[HttpPut("{id}")]
|
|
|
|
|
|
[HttpPost("{id}")]
|
|
|
|
|
|
public async Task Put(string id, [FromBody]OrganizationUserUpdateRequestModel model)
|
|
|
|
|
|
{
|
|
|
|
|
|
var organizationUser = await _organizationUserRepository.GetByIdAsync(new Guid(id));
|
|
|
|
|
|
if(organizationUser == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-13 22:54:24 -04:00
|
|
|
|
await _organizationService.SaveUserAsync(organizationUser, model.Subvaults?.Select(s => s.ToSubvaultUser()));
|
2017-03-09 23:58:43 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-04 21:28:41 -05:00
|
|
|
|
[HttpDelete("{id}")]
|
|
|
|
|
|
[HttpPost("{id}/delete")]
|
|
|
|
|
|
public async Task Delete(string orgId, string id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var organization = await _organizationRepository.GetByIdAsync(new Guid(id),
|
|
|
|
|
|
_userService.GetProperUserId(User).Value);
|
|
|
|
|
|
if(organization == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await _organizationRepository.DeleteAsync(organization);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|