| | | 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 | | /// <summary> |
| | | 18 | | /// Scope owner invitations (FR-SC-14…FR-SC-18): what a scope's owners see and withdraw (UC-58), and what |
| | | 19 | | /// an invited administrator lists, accepts and declines (UC-59…UC-61). An invitation is opened by |
| | | 20 | | /// <c>POST /api/scopes/{scopeId}/owners/{personId}</c> (UC-21) when a Scope Admin asks. |
| | | 21 | | /// </summary> |
| | | 22 | | [Route("api")] |
| | 19 | 23 | | public class ScopeInvitationController(CommandMediator commandMediator, QueryMediator queryMediator) : Controller |
| | | 24 | | { |
| | | 25 | | /// <summary> |
| | | 26 | | /// Lists the scope's open owner invitations (UC-58, FR-SC-15), newest first: each one's id, the invitee's |
| | | 27 | | /// id, name and address, the inviter's name, and when it was made and expires. A System Admin or an |
| | | 28 | | /// owner of the scope may call it; the attribute keeps a <c>User</c> out, and the handler checks the |
| | | 29 | | /// scope (AF-58a) and the ownership (AF-58b). |
| | | 30 | | /// </summary> |
| | | 31 | | [HttpGet("scopes/{scopeId:guid}/owner-invitations")] |
| | | 32 | | [RoleRequirement((int)Roles.SystemAdmin, (int)Roles.ScopeAdmin)] |
| | | 33 | | public async Task<ActionResult<PaginatedOutput<ScopeOwnerInvitationOutput>>> ListForScope( |
| | | 34 | | Guid scopeId, [FromQuery] ListScopeOwnerInvitationsQuery query) |
| | | 35 | | { |
| | 4 | 36 | | query.ScopeId = scopeId; |
| | 4 | 37 | | HttpContext.ApplyActor(query); |
| | | 38 | | |
| | 4 | 39 | | var result = await queryMediator |
| | 4 | 40 | | .ExecutePaginatedQueryAsync<ListScopeOwnerInvitationsQuery, ScopeOwnerInvitationOutput>(query, HttpContext.R |
| | | 41 | | |
| | 4 | 42 | | return result.ToActionResult(statusMap: ScopeInvitationMessageMap.StatusCodes); |
| | 4 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Withdraws an open owner invitation of the scope (UC-58, FR-SC-15): 204 with no body. An ended, |
| | | 47 | | /// expired or unknown invitation, or another scope's, answers 404 "The invitation was not found or is |
| | | 48 | | /// no longer open." (AF-58c). Same actors as the listing. |
| | | 49 | | /// </summary> |
| | | 50 | | [HttpDelete("scopes/{scopeId:guid}/owner-invitations/{invitationId:guid}")] |
| | | 51 | | [RoleRequirement((int)Roles.SystemAdmin, (int)Roles.ScopeAdmin)] |
| | | 52 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 53 | | public async Task<IActionResult> Withdraw(Guid scopeId, Guid invitationId) |
| | | 54 | | { |
| | 5 | 55 | | var command = new WithdrawScopeOwnerInvitationCommand { ScopeId = scopeId, InvitationId = invitationId }; |
| | 5 | 56 | | HttpContext.ApplyActor(command); |
| | | 57 | | |
| | 5 | 58 | | var result = await commandMediator |
| | 5 | 59 | | .ExecuteCommandAsync<WithdrawScopeOwnerInvitationCommand, WithdrawScopeOwnerInvitationCommandOutput>(command |
| | | 60 | | |
| | 5 | 61 | | return result.Success |
| | 5 | 62 | | ? NoContent() |
| | 5 | 63 | | : result.ToActionResult(statusMap: ScopeInvitationMessageMap.StatusCodes).Result!; |
| | 5 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Lists the caller's own open scope owner invitations (UC-59, FR-SC-16), soonest to expire first: each |
| | | 68 | | /// one's id, the scope's id and name, the inviter's name, and when it was made and expires. Any signed-in |
| | | 69 | | /// Scope Admin or System Admin may call it; a <c>User</c> cannot be invited, so the attribute keeps them |
| | | 70 | | /// out. |
| | | 71 | | /// </summary> |
| | | 72 | | [HttpGet("persons/me/scope-invitations")] |
| | | 73 | | [RoleRequirement((int)Roles.SystemAdmin, (int)Roles.ScopeAdmin)] |
| | | 74 | | public async Task<ActionResult<PaginatedOutput<MyScopeInvitationOutput>>> ListMine( |
| | | 75 | | [FromQuery] ListMyScopeInvitationsQuery query) |
| | | 76 | | { |
| | 2 | 77 | | HttpContext.ApplyActor(query); |
| | | 78 | | |
| | 2 | 79 | | var result = await queryMediator |
| | 2 | 80 | | .ExecutePaginatedQueryAsync<ListMyScopeInvitationsQuery, MyScopeInvitationOutput>(query, HttpContext.Request |
| | | 81 | | |
| | 2 | 82 | | return result.ToActionResult(statusMap: ScopeInvitationMessageMap.StatusCodes); |
| | 2 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Accepts a scope owner invitation (UC-60, FR-SC-17): the invitation ends as accepted and the caller |
| | | 87 | | /// becomes an owner of the scope, together. Only the invitee may; everybody else, and an ended or expired |
| | | 88 | | /// invitation, gets 404 "The invitation was not found or is no longer open." (AF-60a). An invitation whose |
| | | 89 | | /// scope was deleted, or whose invitee is no longer an unrestricted administrator, is voided and answered |
| | | 90 | | /// the same (AF-60b). |
| | | 91 | | /// </summary> |
| | | 92 | | [HttpPost("scope-invitations/{invitationId:guid}/accept")] |
| | | 93 | | [RoleRequirement((int)Roles.SystemAdmin, (int)Roles.ScopeAdmin)] |
| | | 94 | | public async Task<ActionResult<DataOutput<AcceptScopeOwnerInvitationCommandOutput?>>> Accept(Guid invitationId) |
| | | 95 | | { |
| | 7 | 96 | | var command = new AcceptScopeOwnerInvitationCommand { InvitationId = invitationId }; |
| | 7 | 97 | | HttpContext.ApplyActor(command); |
| | | 98 | | |
| | 7 | 99 | | var result = await commandMediator |
| | 7 | 100 | | .ExecuteCommandAsync<AcceptScopeOwnerInvitationCommand, AcceptScopeOwnerInvitationCommandOutput>(command); |
| | | 101 | | |
| | 7 | 102 | | return result.ToActionResult(statusMap: ScopeInvitationMessageMap.StatusCodes); |
| | 7 | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Declines a scope owner invitation (UC-61, FR-SC-17). Same caller and refusals as UC-60 (AF-61a). |
| | | 107 | | /// </summary> |
| | | 108 | | [HttpPost("scope-invitations/{invitationId:guid}/decline")] |
| | | 109 | | [RoleRequirement((int)Roles.SystemAdmin, (int)Roles.ScopeAdmin)] |
| | | 110 | | public async Task<ActionResult<DataOutput<DeclineScopeOwnerInvitationCommandOutput?>>> Decline(Guid invitationId) |
| | | 111 | | { |
| | 1 | 112 | | var command = new DeclineScopeOwnerInvitationCommand { InvitationId = invitationId }; |
| | 1 | 113 | | HttpContext.ApplyActor(command); |
| | | 114 | | |
| | 1 | 115 | | var result = await commandMediator |
| | 1 | 116 | | .ExecuteCommandAsync<DeclineScopeOwnerInvitationCommand, DeclineScopeOwnerInvitationCommandOutput>(command); |
| | | 117 | | |
| | 1 | 118 | | return result.ToActionResult(statusMap: ScopeInvitationMessageMap.StatusCodes); |
| | 1 | 119 | | } |
| | | 120 | | } |