This migration updates the ScopesJson for the ApiClient with Id 91000000-0000-0000-0000-000000000001 to include additional authorization scopes for improved API access control. The Down method reverts the changes if necessary.
212 lines
7.0 KiB
C#
212 lines
7.0 KiB
C#
using AutoMapper;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.SelfService.Portal.Core.API.Authorization;
|
|
using Microsoft.SelfService.Portal.Core.API.Dto.Target.Add;
|
|
using Microsoft.SelfService.Portal.Core.API.Dto.Target.Edit;
|
|
using Microsoft.SelfService.Portal.Core.API.Dto.Target.Get;
|
|
using Microsoft.SelfService.Portal.Core.API.Interfaces;
|
|
using Microsoft.SelfService.Portal.Core.API.Models;
|
|
|
|
namespace Microsoft.SelfService.Portal.Core.API.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
[Authorize(Policy = ApiPolicies.ConfigurationRead)]
|
|
public class TargetController : Controller
|
|
{
|
|
private readonly ITargetInterface _targetInterface;
|
|
private readonly IDomainInterface _domainInterface;
|
|
private readonly IMapper _mapper;
|
|
|
|
public TargetController(ITargetInterface targetInterface, IDomainInterface domainInterface, IMapper mapper)
|
|
{
|
|
_targetInterface = targetInterface;
|
|
_domainInterface = domainInterface;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
[HttpGet]
|
|
[ProducesResponseType(200, Type = typeof(IEnumerable<TargetModel>))]
|
|
public IActionResult GetTargets()
|
|
{
|
|
var targets = _mapper.Map<List<GetTargetDto>>(_targetInterface.GetTargets());
|
|
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
return Ok(targets);
|
|
}
|
|
|
|
[HttpGet("{Id}")]
|
|
[ProducesResponseType(200, Type = typeof(TargetModel))]
|
|
[ProducesResponseType(400)]
|
|
public IActionResult GetTargetById(Guid Id)
|
|
{
|
|
if (!_targetInterface.CheckTargetById(Id))
|
|
return NotFound();
|
|
|
|
var target = _mapper.Map<GetTargetDetailsDto>(_targetInterface.GetTargetById(Id));
|
|
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
return Ok(target);
|
|
}
|
|
|
|
[HttpPost]
|
|
[Authorize(Policy = ApiPolicies.ConfigurationWrite)]
|
|
[ProducesResponseType(200)]
|
|
[ProducesResponseType(400)]
|
|
public IActionResult AddTargetById([FromBody] AddTargetDto target)
|
|
{
|
|
if (target == null)
|
|
return BadRequest(ModelState);
|
|
|
|
if (_targetInterface.CheckTargetByName(target.Name))
|
|
{
|
|
ModelState.AddModelError("", "Target already exists");
|
|
return StatusCode(422, ModelState);
|
|
}
|
|
|
|
if (target.DomainID.HasValue && !_domainInterface.CheckDomainById(target.DomainID.Value))
|
|
{
|
|
ModelState.AddModelError("", "Domain does not exist");
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
var targetMap = _mapper.Map<TargetModel>(target);
|
|
|
|
if (!_targetInterface.AddTargetById(targetMap))
|
|
{
|
|
ModelState.AddModelError("", "Something went wrong while saving");
|
|
return StatusCode(500, ModelState);
|
|
}
|
|
|
|
return Ok(targetMap.Id);
|
|
}
|
|
|
|
[HttpPut("{Id}")]
|
|
[Authorize(Policy = ApiPolicies.ConfigurationWrite)]
|
|
[ProducesResponseType(204)]
|
|
[ProducesResponseType(400)]
|
|
[ProducesResponseType(404)]
|
|
public IActionResult EditTargetById(Guid Id, [FromBody] EditTargetDto target)
|
|
{
|
|
if (target == null)
|
|
return BadRequest(ModelState);
|
|
|
|
if (!_targetInterface.CheckTargetById(Id))
|
|
return NotFound();
|
|
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
var existingTarget = _targetInterface.GetTargetById(Id);
|
|
if (existingTarget == null)
|
|
return NotFound();
|
|
|
|
existingTarget.Name = target.Name;
|
|
existingTarget.TargetType = target.TargetType;
|
|
existingTarget.ProviderType = target.ProviderType;
|
|
existingTarget.ExternalId = target.ExternalId;
|
|
existingTarget.MetadataJson = target.MetadataJson;
|
|
|
|
if (!_targetInterface.EditTargetById(existingTarget))
|
|
{
|
|
ModelState.AddModelError("", "Something went wrong");
|
|
return StatusCode(500, ModelState);
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
[HttpPost("{Id}/Domain/{domainId}")]
|
|
[Authorize(Policy = ApiPolicies.ConfigurationWrite)]
|
|
[ProducesResponseType(204)]
|
|
[ProducesResponseType(400)]
|
|
[ProducesResponseType(404)]
|
|
[ProducesResponseType(409)]
|
|
public IActionResult LinkTargetToDomain(Guid Id, Guid domainId)
|
|
{
|
|
if (!_targetInterface.CheckTargetById(Id))
|
|
return NotFound();
|
|
|
|
if (!_domainInterface.CheckDomainById(domainId))
|
|
return NotFound();
|
|
|
|
var existingTarget = _targetInterface.GetTargetById(Id);
|
|
if (existingTarget == null)
|
|
return NotFound();
|
|
|
|
if (existingTarget.DomainID.HasValue)
|
|
return Conflict("Target is already linked to a domain. Unlink it first.");
|
|
|
|
existingTarget.DomainID = domainId;
|
|
|
|
if (!_targetInterface.EditTargetById(existingTarget))
|
|
{
|
|
ModelState.AddModelError("", "Something went wrong");
|
|
return StatusCode(500, ModelState);
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
[HttpDelete("{Id}/Domain")]
|
|
[Authorize(Policy = ApiPolicies.ConfigurationWrite)]
|
|
[ProducesResponseType(204)]
|
|
[ProducesResponseType(404)]
|
|
public IActionResult UnlinkTargetFromDomain(Guid Id)
|
|
{
|
|
if (!_targetInterface.CheckTargetById(Id))
|
|
return NotFound();
|
|
|
|
var existingTarget = _targetInterface.GetTargetById(Id);
|
|
if (existingTarget == null)
|
|
return NotFound();
|
|
|
|
if (!existingTarget.DomainID.HasValue)
|
|
return NoContent();
|
|
|
|
existingTarget.DomainID = null;
|
|
|
|
if (!_targetInterface.EditTargetById(existingTarget))
|
|
{
|
|
ModelState.AddModelError("", "Something went wrong");
|
|
return StatusCode(500, ModelState);
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
[HttpDelete("{Id}")]
|
|
[Authorize(Policy = ApiPolicies.ConfigurationWrite)]
|
|
[ProducesResponseType(204)]
|
|
[ProducesResponseType(400)]
|
|
[ProducesResponseType(404)]
|
|
public IActionResult DeleteTargetById(Guid Id)
|
|
{
|
|
if (!_targetInterface.CheckTargetById(Id))
|
|
return NotFound();
|
|
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
var target = _targetInterface.GetTargetById(Id);
|
|
|
|
if (!_targetInterface.DeleteTargetById(target))
|
|
{
|
|
ModelState.AddModelError("", "Something went wrong while deleting");
|
|
return StatusCode(500, ModelState);
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
}
|
|
}
|
|
|