| | | 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}/applications")] |
| | 64 | 18 | | public class ApplicationController(CommandMediator commandMediator, QueryMediator queryMediator) : Controller |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Registers an application within a scope (UC-16, FR-AP-01/02/03). Only a System Admin or a |
| | | 22 | | /// Scope Admin may call it: FR-AP-03 restricts ownership to an administrator who owns the |
| | | 23 | | /// scope, so a <c>User</c> has nothing to create here and the attribute refuses them. The |
| | | 24 | | /// remaining rules depend on data the attribute cannot see and are enforced by the handler — |
| | | 25 | | /// the acting Scope Admin must own the scope (AF-16e) and may only name themself (AF-16c), |
| | | 26 | | /// and the named owner must satisfy FR-AP-03 (AF-16b). |
| | | 27 | | /// </summary> |
| | | 28 | | [HttpPost] |
| | | 29 | | [RoleRequirement((int)Roles.SystemAdmin, (int)Roles.ScopeAdmin)] |
| | | 30 | | public async Task<ActionResult<DataOutput<CreateApplicationCommandOutput?>>> Create( |
| | | 31 | | Guid scopeId, [FromBody] CreateApplicationCommand command) |
| | | 32 | | { |
| | 16 | 33 | | command.ScopeId = scopeId; |
| | 16 | 34 | | HttpContext.ApplyActor(command); |
| | | 35 | | |
| | 16 | 36 | | var result = await commandMediator |
| | 16 | 37 | | .ExecuteCommandAsync<CreateApplicationCommand, CreateApplicationCommandOutput>(command); |
| | | 38 | | |
| | 16 | 39 | | return result.ToActionResult(statusMap: ApplicationMessageMap.StatusCodes); |
| | 16 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Retrieves a single application by its public identifier within the scope (UC-17, |
| | | 44 | | /// FR-AP-04/09). A <c>User</c> can own no application (FR-AP-03) and so is refused by the |
| | | 45 | | /// attribute; among the remaining actors the rule is data-dependent and lives in the handler — |
| | | 46 | | /// a System Admin sees any application, a Scope Admin only the ones they own (AF-17b). |
| | | 47 | | /// </summary> |
| | | 48 | | [HttpGet("{id:guid}")] |
| | | 49 | | [RoleRequirement((int)Roles.SystemAdmin, (int)Roles.ScopeAdmin)] |
| | | 50 | | public async Task<ActionResult<DataOutput<ApplicationOutput?>>> GetById( |
| | | 51 | | Guid scopeId, Guid id, [FromQuery] bool includeDeleted = false) |
| | | 52 | | { |
| | 8 | 53 | | var query = new GetApplicationByIdQuery { ScopeId = scopeId, Id = id, IncludeDeleted = includeDeleted }; |
| | 8 | 54 | | HttpContext.ApplyActor(query); |
| | | 55 | | |
| | 8 | 56 | | var result = await queryMediator.ExecuteQueryAsync<GetApplicationByIdQuery, ApplicationOutput>(query); |
| | | 57 | | |
| | 8 | 58 | | return result.ToActionResult(statusMap: ApplicationMessageMap.StatusCodes); |
| | 8 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Lists the applications of a scope (UC-17, FR-AP-05/09). A System Admin sees every |
| | | 63 | | /// application in the scope; a Scope Admin must own the scope (AF-17b) and sees only the |
| | | 64 | | /// applications they own. Both narrowings are enforced by the handler from the acting user. |
| | | 65 | | /// </summary> |
| | | 66 | | [HttpGet] |
| | | 67 | | [RoleRequirement((int)Roles.SystemAdmin, (int)Roles.ScopeAdmin)] |
| | | 68 | | public async Task<ActionResult<PaginatedOutput<ApplicationOutput>>> List( |
| | | 69 | | Guid scopeId, [FromQuery] ListScopeApplicationsQuery query) |
| | | 70 | | { |
| | 11 | 71 | | query.ScopeId = scopeId; |
| | 11 | 72 | | HttpContext.ApplyActor(query); |
| | | 73 | | |
| | 11 | 74 | | var result = await queryMediator |
| | 11 | 75 | | .ExecutePaginatedQueryAsync<ListScopeApplicationsQuery, ApplicationOutput>(query); |
| | | 76 | | |
| | 11 | 77 | | return result.ToActionResult(statusMap: ApplicationMessageMap.StatusCodes); |
| | 11 | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Updates an application's name and owner (UC-18, FR-AP-06). A <c>User</c> can own no |
| | | 82 | | /// application (FR-AP-03) and so is refused by the attribute; among the remaining actors the |
| | | 83 | | /// rule is data-dependent and lives in the handler — a System Admin updates any application, a |
| | | 84 | | /// Scope Admin only the ones they own (AF-18c). The handler also resolves the application |
| | | 85 | | /// inside the addressed scope (AF-18a) and, when the owner changes, checks the new owner |
| | | 86 | | /// against FR-AP-03 (AF-18b). |
| | | 87 | | /// </summary> |
| | | 88 | | [HttpPut("{id:guid}")] |
| | | 89 | | [RoleRequirement((int)Roles.SystemAdmin, (int)Roles.ScopeAdmin)] |
| | | 90 | | public async Task<ActionResult<DataOutput<UpdateApplicationCommandOutput?>>> Update( |
| | | 91 | | Guid scopeId, Guid id, [FromBody] UpdateApplicationCommand command) |
| | | 92 | | { |
| | 12 | 93 | | command.ScopeId = scopeId; |
| | 12 | 94 | | command.Id = id; |
| | 12 | 95 | | HttpContext.ApplyActor(command); |
| | | 96 | | |
| | 12 | 97 | | var result = await commandMediator |
| | 12 | 98 | | .ExecuteCommandAsync<UpdateApplicationCommand, UpdateApplicationCommandOutput>(command); |
| | | 99 | | |
| | 12 | 100 | | return result.ToActionResult(statusMap: ApplicationMessageMap.StatusCodes); |
| | 12 | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Logically deletes an application by setting <c>IsDeleted = true</c> (UC-19, FR-AP-07). A |
| | | 105 | | /// <c>User</c> can own no application (FR-AP-03) and so is refused by the attribute; among the |
| | | 106 | | /// remaining actors the rule is data-dependent and lives in the handler — a System Admin |
| | | 107 | | /// deletes any application, a Scope Admin only the ones they own (AF-19c). The handler also |
| | | 108 | | /// resolves the application inside the addressed scope (AF-19a) and answers an already-deleted |
| | | 109 | | /// application idempotently (AF-19b). |
| | | 110 | | /// </summary> |
| | | 111 | | [HttpDelete("{id:guid}")] |
| | | 112 | | [RoleRequirement((int)Roles.SystemAdmin, (int)Roles.ScopeAdmin)] |
| | | 113 | | public async Task<ActionResult<DataOutput<DeleteApplicationCommandOutput?>>> Delete(Guid scopeId, Guid id) |
| | | 114 | | { |
| | 10 | 115 | | var command = new DeleteApplicationCommand { ScopeId = scopeId, Id = id }; |
| | 10 | 116 | | HttpContext.ApplyActor(command); |
| | | 117 | | |
| | 10 | 118 | | var result = await commandMediator |
| | 10 | 119 | | .ExecuteCommandAsync<DeleteApplicationCommand, DeleteApplicationCommandOutput>(command); |
| | | 120 | | |
| | 10 | 121 | | return result.ToActionResult(statusMap: ApplicationMessageMap.StatusCodes); |
| | 10 | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Permanently (hard) deletes an application, removing the record from the database (UC-20, |
| | | 126 | | /// FR-AP-08). Restricted to System Admins: a Scope Admin may logically delete an application |
| | | 127 | | /// they own (UC-19), but never purge it, so the attribute settles authorization on its own and |
| | | 128 | | /// the handler applies no further rule. The handler resolves the application inside the |
| | | 129 | | /// addressed scope in any deletion state and reports AF-20a when it does not exist — which |
| | | 130 | | /// includes a repeated call, as the removal leaves nothing to find. The application's |
| | | 131 | | /// credentials (UC-53) go with it, by cascade; its scope and owner are untouched. |
| | | 132 | | /// </summary> |
| | | 133 | | [HttpDelete("{id:guid}/hard")] |
| | | 134 | | [RoleRequirement((int)Roles.SystemAdmin)] |
| | | 135 | | public async Task<ActionResult<DataOutput<HardDeleteApplicationCommandOutput?>>> HardDelete( |
| | | 136 | | Guid scopeId, Guid id) |
| | | 137 | | { |
| | 7 | 138 | | var result = await commandMediator |
| | 7 | 139 | | .ExecuteCommandAsync<HardDeleteApplicationCommand, HardDeleteApplicationCommandOutput>( |
| | 7 | 140 | | new HardDeleteApplicationCommand { ScopeId = scopeId, Id = id }); |
| | | 141 | | |
| | 7 | 142 | | return result.ToActionResult(statusMap: ApplicationMessageMap.StatusCodes); |
| | 7 | 143 | | } |
| | | 144 | | } |