108 lines
4.8 KiB
C#
108 lines
4.8 KiB
C#
using System.Security.Claims;
|
|||
|
|
using Heimdall.Application.DTOs;
|
||
|
|
using Heimdall.Application.Services;
|
||
|
|
using Heimdall.Infrastructure.Security;
|
||
|
|
using Microsoft.AspNetCore.Authorization;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using Microsoft.AspNetCore.RateLimiting;
|
||
|
|
|
||
|
|
namespace Heimdall.Api.Controllers;
|
||
|
|
|
||
|
|
[ApiController]
|
||
|
|
[Route("api/sites")]
|
||
|
|
[EnableRateLimiting("Api")]
|
||
|
|
public class SitesController : ControllerBase
|
||
|
|
{
|
||
|
|
private readonly ISiteService _siteService;
|
||
|
|
|
||
|
|
public SitesController(ISiteService siteService) => _siteService = siteService;
|
||
|
|
|
||
|
|
/// <summary>Create a new site (Admin only).</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.SiteWrite)]
|
||
|
|
[HttpPost]
|
||
|
|
[ProducesResponseType(typeof(SiteWithTokenResponse), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
||
|
|
public async Task<IActionResult> Create(
|
||
|
|
[FromBody] CreateSiteRequest request, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var userIdClaim = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||
|
|
if (string.IsNullOrEmpty(userIdClaim) || !Guid.TryParse(userIdClaim, out var userId))
|
||
|
|
return Unauthorized(new { error = "Invalid user identity" });
|
||
|
|
|
||
|
|
var result = await _siteService.CreateAsync(request, userId, ct);
|
||
|
|
return result.IsSuccess
|
||
|
|
? Ok(result.Data)
|
||
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>List all sites.</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.SiteRead)]
|
||
|
|
[HttpGet]
|
||
|
|
[ProducesResponseType(typeof(PagedResult<SiteResponse>), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||
|
|
public async Task<IActionResult> List(
|
||
|
|
[FromQuery] int page = 1, [FromQuery] int pageSize = 20, CancellationToken ct = default)
|
||
|
|
{
|
||
|
|
var result = await _siteService.ListAsync(page, pageSize, ct);
|
||
|
|
if (!result.IsSuccess)
|
||
|
|
return StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
return Ok(result.Data);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Get a site by ID.</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.SiteRead)]
|
||
|
|
[HttpGet("{id:guid}")]
|
||
|
|
[ProducesResponseType(typeof(SiteResponse), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> GetById(Guid id, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _siteService.GetByIdAsync(id, ct);
|
||
|
|
return result.IsSuccess
|
||
|
|
? Ok(result.Data)
|
||
|
|
: NotFound(new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Update a site (Admin only).</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.SiteWrite)]
|
||
|
|
[HttpPut("{id:guid}")]
|
||
|
|
[ProducesResponseType(typeof(SiteResponse), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> Update(
|
||
|
|
Guid id, [FromBody] UpdateSiteRequest request, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _siteService.UpdateAsync(id, request, ct);
|
||
|
|
return result.IsSuccess
|
||
|
|
? Ok(result.Data)
|
||
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Delete a site (Admin only).</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.SiteDelete)]
|
||
|
|
[HttpDelete("{id:guid}")]
|
||
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _siteService.DeleteAsync(id, ct);
|
||
|
|
return result.IsSuccess
|
||
|
|
? NoContent()
|
||
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Generate a new agent registration token (Admin only).</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.SiteWrite)]
|
||
|
|
[HttpPost("{id:guid}/tokens")]
|
||
|
|
[ProducesResponseType(typeof(RegistrationTokenResponse), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> GenerateToken(Guid id, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _siteService.GenerateTokenAsync(id, ct);
|
||
|
|
return result.IsSuccess
|
||
|
|
? Ok(result.Data)
|
||
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
}
|
||
|
|
}
|