// FIXME: Update this file to be null safe and then delete the line below #nullable disable using System.Net; using Bit.Api.AdminConsole.Public.Models.Request; using Bit.Api.AdminConsole.Public.Models.Response; using Bit.Api.Models.Public.Response; using Bit.Core.AdminConsole.Enums; using Bit.Core.AdminConsole.OrganizationFeatures.Policies.PolicyUpdateEvents.Interfaces; using Bit.Core.AdminConsole.Repositories; using Bit.Core.Context; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace Bit.Api.AdminConsole.Public.Controllers; [Route("public/policies")] [Authorize("Organization")] public class PoliciesController : Controller { private readonly IPolicyRepository _policyRepository; private readonly ICurrentContext _currentContext; private readonly IVNextSavePolicyCommand _vNextSavePolicyCommand; public PoliciesController( IPolicyRepository policyRepository, ICurrentContext currentContext, IVNextSavePolicyCommand vNextSavePolicyCommand) { _policyRepository = policyRepository; _currentContext = currentContext; _vNextSavePolicyCommand = vNextSavePolicyCommand; } /// /// Retrieve a policy. /// /// /// Retrieves the details of a policy. /// /// The type of policy to be retrieved. [HttpGet("{type}")] [ProducesResponseType(typeof(PolicyResponseModel), (int)HttpStatusCode.OK)] [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task Get(PolicyType type) { var policy = await _policyRepository.GetByOrganizationIdTypeAsync(_currentContext.OrganizationId.Value, type); if (policy == null) { return new NotFoundResult(); } return new JsonResult(new PolicyResponseModel(policy)); } /// /// List all policies. /// /// /// Returns a list of your organization's policies. /// [HttpGet] [ProducesResponseType(typeof(ListResponseModel), (int)HttpStatusCode.OK)] public async Task List() { var policies = await _policyRepository.GetManyByOrganizationIdAsync(_currentContext.OrganizationId.Value); return new JsonResult(new ListResponseModel(policies.Select(p => new PolicyResponseModel(p)))); } /// /// Update a policy. /// /// /// Updates the specified policy. If a property is not provided, /// the value of the existing property will be reset. /// /// The type of policy to be updated. /// The request model. [HttpPut("{type}")] [ProducesResponseType(typeof(PolicyResponseModel), (int)HttpStatusCode.OK)] [ProducesResponseType(typeof(ErrorResponseModel), (int)HttpStatusCode.BadRequest)] [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task Put(PolicyType type, [FromBody] PolicyUpdateRequestModel model) { var savePolicyModel = model.ToSavePolicyModel(_currentContext.OrganizationId!.Value, type); var policy = await _vNextSavePolicyCommand.SaveAsync(savePolicyModel); var response = new PolicyResponseModel(policy); return new JsonResult(response); } }