| | | 1 | | using ArturRios.Heimdall.Command.Input; |
| | | 2 | | using ArturRios.Heimdall.Command.Output; |
| | | 3 | | using ArturRios.Heimdall.Domain.Enums; |
| | | 4 | | using ArturRios.Heimdall.Query.Input; |
| | | 5 | | using ArturRios.Heimdall.Query.Output; |
| | | 6 | | using ArturRios.Heimdall.Shared.Messages; |
| | | 7 | | using ArturRios.Heimdall.WebApi.Security; |
| | | 8 | | using ArturRios.Mediator.Command; |
| | | 9 | | using ArturRios.Mediator.Query; |
| | | 10 | | using ArturRios.Output; |
| | | 11 | | using ArturRios.Util.WebApi.AspNetCore; |
| | | 12 | | using ArturRios.Util.WebApi.Security.Attributes; |
| | | 13 | | using Microsoft.AspNetCore.Mvc; |
| | | 14 | | |
| | | 15 | | namespace ArturRios.Heimdall.WebApi.Controllers; |
| | | 16 | | |
| | | 17 | | [Route("api/scopes/{scopeId:guid}/google-users")] |
| | 33 | 18 | | public class GoogleUserController(CommandMediator commandMediator, QueryMediator queryMediator) : Controller |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Lists the Google Users of a scope, with pagination and optional name/email filters (UC-27, |
| | | 22 | | /// FR-GO-14). A System Admin or an owner of the scope may call it; the ownership check |
| | | 23 | | /// (AF-27b) is enforced by the handler from the acting caller. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <remarks> |
| | | 26 | | /// Unlike <see cref="GetById" /> this does carry a <c>RoleRequirement</c>: the authorization |
| | | 27 | | /// matrix grants a Google User a read of themselves, never a listing, so every <c>User</c> is |
| | | 28 | | /// refused here before the handler runs. |
| | | 29 | | /// </remarks> |
| | | 30 | | [HttpGet] |
| | | 31 | | [RoleRequirement((int)Roles.SystemAdmin, (int)Roles.ScopeAdmin)] |
| | | 32 | | public async Task<ActionResult<PaginatedOutput<GoogleUserOutput>>> List( |
| | | 33 | | Guid scopeId, [FromQuery] ListScopeGoogleUsersQuery query) |
| | | 34 | | { |
| | 8 | 35 | | query.ScopeId = scopeId; |
| | 8 | 36 | | HttpContext.ApplyActor(query); |
| | | 37 | | |
| | 8 | 38 | | var result = await queryMediator |
| | 8 | 39 | | .ExecutePaginatedQueryAsync<ListScopeGoogleUsersQuery, GoogleUserOutput>(query); |
| | | 40 | | |
| | 8 | 41 | | return result.ToActionResult(statusMap: GoogleUserMessageMap.StatusCodes); |
| | 8 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Retrieves a single Google User by its public identifier within a scope (UC-27, FR-GO-14). |
| | | 46 | | /// </summary> |
| | | 47 | | /// <remarks> |
| | | 48 | | /// Open to any authenticated actor, deliberately. UC-27 names a Google User as one of its |
| | | 49 | | /// three actors — they may read their own record — and a Google User's token carries the |
| | | 50 | | /// <c>User</c> role (FR-GO-04), so any attribute strong enough to keep other Users out would |
| | | 51 | | /// lock out the actor the use case grants. The per-actor rule (AF-27b) is data-dependent and |
| | | 52 | | /// is therefore the handler's, as UC-07's by-id read is. |
| | | 53 | | /// </remarks> |
| | | 54 | | [HttpGet("{id:guid}")] |
| | | 55 | | public async Task<ActionResult<DataOutput<GoogleUserOutput?>>> GetById( |
| | | 56 | | Guid scopeId, Guid id, [FromQuery] bool includeDeleted = false) |
| | | 57 | | { |
| | 10 | 58 | | var query = new GetGoogleUserByIdQuery { ScopeId = scopeId, Id = id, IncludeDeleted = includeDeleted }; |
| | 10 | 59 | | HttpContext.ApplyActor(query); |
| | | 60 | | |
| | 10 | 61 | | var result = await queryMediator.ExecuteQueryAsync<GetGoogleUserByIdQuery, GoogleUserOutput>(query); |
| | | 62 | | |
| | 10 | 63 | | return result.ToActionResult(statusMap: GoogleUserMessageMap.StatusCodes); |
| | 10 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Logically deletes a Google User by setting <c>IsDeleted = true</c> (UC-28, FR-GO-15). The |
| | | 68 | | /// attribute keeps every <c>User</c> out — the authorization matrix withholds this from them, |
| | | 69 | | /// Google or password alike — while the rule that depends on data it cannot see is the |
| | | 70 | | /// handler's: whether the acting Scope Admin owns the Google User's scope (AF-28c). |
| | | 71 | | /// </summary> |
| | | 72 | | /// <remarks> |
| | | 73 | | /// Unlike <see cref="GetById" /> there is no actor a role attribute would wrongly exclude |
| | | 74 | | /// here, which is why this one carries the attribute the by-id read cannot. |
| | | 75 | | /// </remarks> |
| | | 76 | | [HttpDelete("{id:guid}")] |
| | | 77 | | [RoleRequirement((int)Roles.SystemAdmin, (int)Roles.ScopeAdmin)] |
| | | 78 | | public async Task<ActionResult<DataOutput<DeleteGoogleUserCommandOutput?>>> Delete(Guid scopeId, Guid id) |
| | | 79 | | { |
| | 8 | 80 | | var command = new DeleteGoogleUserCommand { ScopeId = scopeId, Id = id }; |
| | 8 | 81 | | HttpContext.ApplyActor(command); |
| | | 82 | | |
| | 8 | 83 | | var result = await commandMediator |
| | 8 | 84 | | .ExecuteCommandAsync<DeleteGoogleUserCommand, DeleteGoogleUserCommandOutput>(command); |
| | | 85 | | |
| | 8 | 86 | | return result.ToActionResult(statusMap: GoogleUserMessageMap.StatusCodes); |
| | 8 | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Permanently (hard) deletes a Google User, removing the record for good (UC-29, FR-GO-16). |
| | | 91 | | /// Restricted to System Admins — the authorization matrix withholds this even from an owning |
| | | 92 | | /// Scope Admin, who may only delete logically (UC-28). |
| | | 93 | | /// </summary> |
| | | 94 | | /// <remarks> |
| | | 95 | | /// The one Google User endpoint where the attribute is the whole authorization rule: UC-29 |
| | | 96 | | /// names a single actor and nothing about the decision depends on data, so the handler applies |
| | | 97 | | /// none and the command carries no acting person. Nothing cascades either — a Google User owns |
| | | 98 | | /// no dependent row. |
| | | 99 | | /// </remarks> |
| | | 100 | | [HttpDelete("{id:guid}/hard")] |
| | | 101 | | [RoleRequirement((int)Roles.SystemAdmin)] |
| | | 102 | | public async Task<ActionResult<DataOutput<HardDeleteGoogleUserCommandOutput?>>> HardDelete( |
| | | 103 | | Guid scopeId, Guid id) |
| | | 104 | | { |
| | 7 | 105 | | var result = await commandMediator |
| | 7 | 106 | | .ExecuteCommandAsync<HardDeleteGoogleUserCommand, HardDeleteGoogleUserCommandOutput>( |
| | 7 | 107 | | new HardDeleteGoogleUserCommand { ScopeId = scopeId, Id = id }); |
| | | 108 | | |
| | 7 | 109 | | return result.ToActionResult(statusMap: GoogleUserMessageMap.StatusCodes); |
| | 7 | 110 | | } |
| | | 111 | | } |