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

132 lines
5.0 KiB
C#
Raw Normal View History

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.Context;
2020-01-20 08:53:09 -05:00
using Bit.Core.Enums;
using Bit.Core.Utilities;
using Bit.Core.Settings;
using Microsoft.AspNetCore.DataProtection;
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 IOrganizationUserRepository _organizationUserRepository;
2020-02-19 14:56:16 -05:00
private readonly IUserService _userService;
private readonly ICurrentContext _currentContext;
private readonly GlobalSettings _globalSettings;
private readonly IDataProtector _organizationServiceDataProtector;
2020-01-15 08:35:53 -05:00
public PoliciesController(
IPolicyRepository policyRepository,
2020-01-15 09:19:49 -05:00
IPolicyService policyService,
2020-02-19 14:56:16 -05:00
IOrganizationService organizationService,
IOrganizationUserRepository organizationUserRepository,
2020-02-19 14:56:16 -05:00
IUserService userService,
ICurrentContext currentContext,
GlobalSettings globalSettings,
IDataProtectionProvider dataProtectionProvider)
2020-01-15 08:35:53 -05:00
{
_policyRepository = policyRepository;
2020-01-15 09:19:49 -05:00
_policyService = policyService;
2020-02-19 14:56:16 -05:00
_organizationService = organizationService;
_organizationUserRepository = organizationUserRepository;
2020-02-19 14:56:16 -05:00
_userService = userService;
2020-01-15 08:35:53 -05:00
_currentContext = currentContext;
_globalSettings = globalSettings;
_organizationServiceDataProtector = dataProtectionProvider.CreateProtector(
"OrganizationServiceDataProtector");
2020-01-15 08:35:53 -05:00
}
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 (!await _currentContext.ManagePolicies(orgIdGuid))
2020-01-20 08:53:09 -05:00
{
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 (!await _currentContext.ManagePolicies(orgIdGuid))
2020-01-15 08:35:53 -05:00
{
throw new NotFoundException();
}
var policies = await _policyRepository.GetManyByOrganizationIdAsync(orgIdGuid);
var responses = policies.Select(p => new PolicyResponseModel(p));
return new ListResponseModel<PolicyResponseModel>(responses);
}
[AllowAnonymous]
[HttpGet("token")]
public async Task<ListResponseModel<PolicyResponseModel>> GetByToken(string orgId, [FromQuery]string email,
[FromQuery]string token, [FromQuery]string organizationUserId)
{
var orgUserId = new Guid(organizationUserId);
var tokenValid = CoreHelpers.UserInviteTokenIsValid(_organizationServiceDataProtector, token,
email, orgUserId, _globalSettings);
if (!tokenValid)
{
throw new NotFoundException();
}
var orgIdGuid = new Guid(orgId);
var orgUser = await _organizationUserRepository.GetByIdAsync(orgUserId);
if (orgUser == null || orgUser.OrganizationId != orgIdGuid)
{
throw new NotFoundException();
}
var policies = await _policyRepository.GetManyByOrganizationIdAsync(orgIdGuid);
2020-03-02 11:30:44 -05:00
var responses = policies.Where(p => p.Enabled).Select(p => new PolicyResponseModel(p));
2020-01-15 08:35:53 -05:00
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 (!await _currentContext.ManagePolicies(orgIdGuid))
2020-01-15 08:35:53 -05:00
{
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
}
}
}