2020-01-15 08:35:53 -05: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;
|
|
|
|
|
|
using Bit.Core;
|
2020-01-20 08:53:09 -05:00
|
|
|
|
using Bit.Core.Enums;
|
2020-01-15 08:35:53 -05:00
|
|
|
|
|
|
|
|
|
|
namespace Bit.Api.Controllers
|
|
|
|
|
|
{
|
|
|
|
|
|
[Route("organizations/{orgId}/policies")]
|
|
|
|
|
|
[Authorize("Application")]
|
|
|
|
|
|
public class PoliciesController : Controller
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IPolicyRepository _policyRepository;
|
2020-01-15 09:19:49 -05:00
|
|
|
|
private readonly IPolicyService _policyService;
|
2020-02-19 14:56:16 -05:00
|
|
|
|
private readonly IOrganizationService _organizationService;
|
|
|
|
|
|
private readonly IUserService _userService;
|
2020-01-15 08:35:53 -05:00
|
|
|
|
private readonly CurrentContext _currentContext;
|
|
|
|
|
|
|
|
|
|
|
|
public PoliciesController(
|
|
|
|
|
|
IPolicyRepository policyRepository,
|
2020-01-15 09:19:49 -05:00
|
|
|
|
IPolicyService policyService,
|
2020-02-19 14:56:16 -05:00
|
|
|
|
IOrganizationService organizationService,
|
|
|
|
|
|
IUserService userService,
|
2020-01-15 08:35:53 -05:00
|
|
|
|
CurrentContext currentContext)
|
|
|
|
|
|
{
|
|
|
|
|
|
_policyRepository = policyRepository;
|
2020-01-15 09:19:49 -05:00
|
|
|
|
_policyService = policyService;
|
2020-02-19 14:56:16 -05:00
|
|
|
|
_organizationService = organizationService;
|
|
|
|
|
|
_userService = userService;
|
2020-01-15 08:35:53 -05:00
|
|
|
|
_currentContext = currentContext;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-01-20 08:53:09 -05:00
|
|
|
|
[HttpGet("{type}")]
|
|
|
|
|
|
public async Task<PolicyResponseModel> Get(string orgId, int type)
|
2020-01-15 08:35:53 -05:00
|
|
|
|
{
|
2020-01-20 08:53:09 -05:00
|
|
|
|
var orgIdGuid = new Guid(orgId);
|
|
|
|
|
|
if(!_currentContext.OrganizationAdmin(orgIdGuid))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
var policy = await _policyRepository.GetByOrganizationIdTypeAsync(orgIdGuid, (PolicyType)type);
|
|
|
|
|
|
if(policy == null)
|
2020-01-15 08:35:53 -05:00
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new PolicyResponseModel(policy);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("")]
|
|
|
|
|
|
public async Task<ListResponseModel<PolicyResponseModel>> Get(string orgId)
|
|
|
|
|
|
{
|
|
|
|
|
|
var orgIdGuid = new Guid(orgId);
|
|
|
|
|
|
if(!_currentContext.OrganizationManager(orgIdGuid))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var policies = await _policyRepository.GetManyByOrganizationIdAsync(orgIdGuid);
|
|
|
|
|
|
var responses = policies.Select(p => new PolicyResponseModel(p));
|
|
|
|
|
|
return new ListResponseModel<PolicyResponseModel>(responses);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-01-20 08:53:09 -05:00
|
|
|
|
[HttpPut("{type}")]
|
|
|
|
|
|
public async Task<PolicyResponseModel> Put(string orgId, int type, [FromBody]PolicyRequestModel model)
|
2020-01-15 08:35:53 -05:00
|
|
|
|
{
|
|
|
|
|
|
var orgIdGuid = new Guid(orgId);
|
|
|
|
|
|
if(!_currentContext.OrganizationAdmin(orgIdGuid))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
2020-01-20 08:53:09 -05:00
|
|
|
|
var policy = await _policyRepository.GetByOrganizationIdTypeAsync(new Guid(orgId), (PolicyType)type);
|
|
|
|
|
|
if(policy == null)
|
2020-01-15 08:35:53 -05:00
|
|
|
|
{
|
2020-01-20 08:53:09 -05:00
|
|
|
|
policy = model.ToPolicy(orgIdGuid);
|
2020-01-15 08:35:53 -05:00
|
|
|
|
}
|
2020-01-20 08:53:09 -05:00
|
|
|
|
else
|
2020-01-15 08:35:53 -05:00
|
|
|
|
{
|
2020-01-20 08:53:09 -05:00
|
|
|
|
policy = model.ToPolicy(policy);
|
2020-01-15 08:35:53 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-19 14:56:16 -05:00
|
|
|
|
var userId = _userService.GetProperUserId(User);
|
|
|
|
|
|
await _policyService.SaveAsync(policy, _userService, _organizationService, userId);
|
2020-01-20 08:53:09 -05:00
|
|
|
|
return new PolicyResponseModel(policy);
|
2020-01-15 08:35:53 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|