| | | 1 | | using ArturRios.Data.Relational.Core.Interfaces; |
| | | 2 | | using ArturRios.Heimdall.Command.Input; |
| | | 3 | | using ArturRios.Heimdall.Command.Output; |
| | | 4 | | using ArturRios.Heimdall.Domain.Entities; |
| | | 5 | | using ArturRios.Heimdall.Shared.Messages; |
| | | 6 | | using ArturRios.Heimdall.Shared.Services; |
| | | 7 | | using ArturRios.Mediator.Command.Interfaces; |
| | | 8 | | using ArturRios.Output; |
| | | 9 | | using Microsoft.EntityFrameworkCore; |
| | | 10 | | |
| | | 11 | | namespace ArturRios.Heimdall.Command.Handlers; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Handles <see cref="DeleteScopePermissionCommand" /> (UC-34, FR-SP-07): locates the permission |
| | | 15 | | /// inside the addressed scope (AF-34a), enforces the acting role's rule — a System Admin may |
| | | 16 | | /// delete any permission, anyone else only one whose scope they own (AF-34e) — serves an |
| | | 17 | | /// already-deleted permission as an idempotent no-op (AF-34b), and otherwise sets |
| | | 18 | | /// <c>IsDeleted = true</c> and stamps <c>UpdatedAt</c>. Nothing cascades: a scope permission owns |
| | | 19 | | /// no dependent row. All failures are returned as errors on the <see cref="DataOutput{T}" /> |
| | | 20 | | /// rather than thrown. |
| | | 21 | | /// </summary> |
| | 19 | 22 | | public class DeleteScopePermissionCommandHandler( |
| | 19 | 23 | | IAsyncReadOnlyRepository<ScopePermission, long> permissionReader, |
| | 19 | 24 | | IAsyncRepository<ScopePermission, long> permissionWriter, |
| | 19 | 25 | | IScopeOwnershipChecker scopeOwnership) |
| | | 26 | | : ICommandHandlerAsync<DeleteScopePermissionCommand, DeleteScopePermissionCommandOutput> |
| | | 27 | | { |
| | | 28 | | public async Task<DataOutput<DeleteScopePermissionCommandOutput?>> HandleAsync( |
| | | 29 | | DeleteScopePermissionCommand command, CancellationToken cancellationToken = default) |
| | | 30 | | { |
| | 19 | 31 | | var output = DataOutput<DeleteScopePermissionCommandOutput?>.New; |
| | | 32 | | |
| | | 33 | | // AF-34a: the permission must exist inside the addressed scope. The lookup deliberately omits |
| | | 34 | | // the !IsDeleted filter UC-33 applies, so an already-deleted permission is found and served |
| | | 35 | | // idempotently by AF-34b below rather than reported as not found. The route's scopeId |
| | | 36 | | // qualifies it: a permission that lives in another scope is not the resource this path |
| | | 37 | | // addresses. Checked before authorization so AF-34a and AF-34e both stay observable. |
| | 19 | 38 | | var permission = await permissionReader.Query() |
| | 19 | 39 | | .Include(x => x.Scope) |
| | 19 | 40 | | .FirstOrDefaultAsync(x => x.PublicId == command.Id && x.Scope.PublicId == command.ScopeId); |
| | | 41 | | |
| | 19 | 42 | | if (permission is null) |
| | | 43 | | { |
| | 5 | 44 | | return output.WithError(ScopePermissionMessages.ScopePermissionNotFound); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | // AF-34e: a System Admin may delete any scope's permission; anyone else must own the scope. |
| | | 48 | | // Runs before AF-34b so an already-deleted permission cannot be used to probe for scopes the |
| | | 49 | | // caller may not act on. |
| | 14 | 50 | | if (!await scopeOwnership.ActorMayManageScopeAsync( |
| | 14 | 51 | | command.ActingRole, command.ActingPersonId, permission.ScopeId)) |
| | | 52 | | { |
| | 3 | 53 | | return output.WithError(ScopePermissionMessages.NotScopeOwner); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | // AF-34b: already deleted by an earlier call, so there is nothing to write. It cannot have |
| | | 57 | | // arrived here from its scope: logically deleting a scope (UC-04) does not cascade to its |
| | | 58 | | // permissions. UpdatedAt is left alone — the row already carries the requested state, and |
| | | 59 | | // re-stamping it would misreport when the deletion happened. |
| | 11 | 60 | | if (permission.IsDeleted) |
| | | 61 | | { |
| | 5 | 62 | | return output |
| | 5 | 63 | | .WithData(new DeleteScopePermissionCommandOutput |
| | 5 | 64 | | { |
| | 5 | 65 | | Id = permission.PublicId, |
| | 5 | 66 | | AlreadyDeleted = true |
| | 5 | 67 | | }) |
| | 5 | 68 | | .WithMessage(ScopePermissionMessages.ScopePermissionDeletedSuccessfully); |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | // UC-34 step 3: flip the flag and stamp UpdatedAt (no DB trigger maintains it). |
| | 6 | 72 | | permission.IsDeleted = true; |
| | 6 | 73 | | permission.UpdatedAt = DateTime.UtcNow; |
| | | 74 | | |
| | 6 | 75 | | var update = await permissionWriter.UpdateAsync(permission); |
| | | 76 | | |
| | 6 | 77 | | if (!update.Success) |
| | | 78 | | { |
| | 0 | 79 | | return output.WithErrors(update.Errors); |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | // UC-34 step 4. |
| | 6 | 83 | | return output |
| | 6 | 84 | | .WithData(new DeleteScopePermissionCommandOutput |
| | 6 | 85 | | { |
| | 6 | 86 | | Id = permission.PublicId, |
| | 6 | 87 | | AlreadyDeleted = false |
| | 6 | 88 | | }) |
| | 6 | 89 | | .WithMessage(ScopePermissionMessages.ScopePermissionDeletedSuccessfully); |
| | 19 | 90 | | } |
| | | 91 | | } |