Symptôme : après F5 sur une page authentifiée, les appels API des pages (api/stats, api/agents…) renvoyaient 401 jusqu'à la connexion du circuit Blazor. Cause : au rendu initial, AuthState est peuplé depuis le cookie (AccessToken null → pas de Bearer) et le HttpClient serveur n'envoie pas le cookie navigateur ; les contrôleurs n'acceptaient que JwtBearer. Correctif : - SameOriginHttpMessageHandler : propage le cookie d'authentification du HttpContext courant vers la requête interne quand aucun Bearer n'est posé (le Bearer du circuit prend le relais ensuite). - Contrôleurs API humains : AuthenticationSchemes = 'JwtBearer,Cookies' (77 occurrences) — le cookie porte les mêmes claims de rôle/permission que le JWT. - Vérifié en réel : F5 /dashboard → plus de snackbar 401, console propre, api/agents validé et 200 côté serveur. 182 tests Web verts.
134 lines
5.9 KiB
C#
134 lines
5.9 KiB
C#
using Heimdall.Application.DTOs;
|
|
using Heimdall.Application.Groups.Commands.CreateGroup;
|
|
using Heimdall.Application.Groups.Commands.UpdateGroup;
|
|
using Heimdall.Application.Groups.Commands.DeleteGroup;
|
|
using Heimdall.Application.Groups.Commands.AddUserToGroup;
|
|
using Heimdall.Application.Groups.Commands.RemoveUserFromGroup;
|
|
using Heimdall.Application.Groups.Queries.GetGroups;
|
|
using Heimdall.Application.Groups.Queries.GetGroup;
|
|
using Heimdall.Application.Groups.Queries.GetGroupMembers;
|
|
using Heimdall.Infrastructure.Security;
|
|
using MediatR;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.RateLimiting;
|
|
|
|
namespace Heimdall.Api.Controllers;
|
|
|
|
/// <summary>
|
|
/// CRUD management for user groups and their members (Admin only).
|
|
/// </summary>
|
|
[ApiController]
|
|
[Route("api/groups")]
|
|
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.AdminUsers)]
|
|
[EnableRateLimiting("Api")]
|
|
public class GroupsController : ControllerBase
|
|
{
|
|
private readonly IMediator _mediator;
|
|
|
|
public GroupsController(IMediator mediator) => _mediator = mediator;
|
|
|
|
/// <summary>List all groups (Admin only).</summary>
|
|
[HttpGet]
|
|
[ProducesResponseType(typeof(PagedResult<GroupResponse>), 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 _mediator.Send(new GetGroupsQuery(page, pageSize), ct);
|
|
if (!result.IsSuccess)
|
|
return StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
|
return Ok(result.Data);
|
|
}
|
|
|
|
/// <summary>Get a group by ID.</summary>
|
|
[HttpGet("{id:guid}")]
|
|
[ProducesResponseType(typeof(GroupResponse), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
|
public async Task<IActionResult> GetById(Guid id, CancellationToken ct)
|
|
{
|
|
var result = await _mediator.Send(new GetGroupQuery(id), ct);
|
|
return result.IsSuccess
|
|
? Ok(result.Data)
|
|
: NotFound(new { error = result.Error });
|
|
}
|
|
|
|
/// <summary>Create a new group (Admin only).</summary>
|
|
[HttpPost]
|
|
[ProducesResponseType(typeof(GroupResponse), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
|
public async Task<IActionResult> Create([FromBody] CreateGroupRequest request, CancellationToken ct)
|
|
{
|
|
var command = new CreateGroupCommand(request.Name, request.Description);
|
|
var result = await _mediator.Send(command, ct);
|
|
return result.IsSuccess
|
|
? Ok(result.Data)
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
|
}
|
|
|
|
/// <summary>Update a group (Admin only).</summary>
|
|
[HttpPut("{id:guid}")]
|
|
[ProducesResponseType(typeof(GroupResponse), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
|
public async Task<IActionResult> Update(
|
|
Guid id, [FromBody] UpdateGroupRequest request, CancellationToken ct)
|
|
{
|
|
var command = new UpdateGroupCommand(id, request.Name, request.Description);
|
|
var result = await _mediator.Send(command, ct);
|
|
return result.IsSuccess
|
|
? Ok(result.Data)
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
|
}
|
|
|
|
/// <summary>Delete a group (soft-delete, Admin only).</summary>
|
|
[HttpDelete("{id:guid}")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
|
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
|
{
|
|
var result = await _mediator.Send(new DeleteGroupCommand(id), ct);
|
|
return result.IsSuccess
|
|
? Ok()
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
|
}
|
|
|
|
/// <summary>Add a user to a group (Admin only).</summary>
|
|
[HttpPost("{groupId:guid}/users/{userId:guid}")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
|
public async Task<IActionResult> AddUser(Guid groupId, Guid userId, CancellationToken ct)
|
|
{
|
|
var result = await _mediator.Send(new AddUserToGroupCommand(groupId, userId), ct);
|
|
return result.IsSuccess
|
|
? Ok()
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
|
}
|
|
|
|
/// <summary>Remove a user from a group (Admin only).</summary>
|
|
[HttpDelete("{groupId:guid}/users/{userId:guid}")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
|
public async Task<IActionResult> RemoveUser(Guid groupId, Guid userId, CancellationToken ct)
|
|
{
|
|
var result = await _mediator.Send(new RemoveUserFromGroupCommand(groupId, userId), ct);
|
|
return result.IsSuccess
|
|
? Ok()
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
|
}
|
|
|
|
/// <summary>Get all members of a group (Admin only).</summary>
|
|
[HttpGet("{groupId:guid}/users")]
|
|
[ProducesResponseType(typeof(IReadOnlyList<GroupMemberResponse>), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
|
public async Task<IActionResult> GetMembers(Guid groupId, CancellationToken ct)
|
|
{
|
|
var result = await _mediator.Send(new GetGroupMembersQuery(groupId), ct);
|
|
return result.IsSuccess
|
|
? Ok(result.Data)
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
|
}
|
|
}
|