Files
slp-modular-cms/aidlc-docs/bugs/invitation-delete-and-lifetime/bug-fix.md
T

63 lines
6.1 KiB
Markdown

# Bug Fix: pending invitations cannot be removed; invitation lifetime not configurable
- **Slug**: invitation-delete-and-lifetime
- **Status**: Complete (pending ASF test approval by Bryan)
- **Issue**: None (relayed by session "CMS & API" from Bryan, 2026-11-08)
- **Branch**: feature/andreaslop-site-hosting
- **Started**: 2026-11-08
- **Documentation Language**: English
- **Conversation Language**: Dutch (user language)
## Report
A. An invited but not yet activated user cannot be deleted.
B. The activation lifetime is too short; wanted: 24 hours, configurable (Identity option), no second place with another duration.
C. "Nieuwe activatiecode aanvragen" does nothing, no mails are sent (analysis only, nothing built without Bryan's go).
## Root Cause
**A.** The delete path itself works (`DELETE Users/{id}` falls back to `DeleteInvitationByIdAsync`, the row is removed and the token stops working). Two causes made it unreachable:
1. `GetAllPendingInvitationsAsync` listed only unexpired invitations. An invitation that was not activated in time disappeared from the list: invisible, so impossible to remove.
2. The Delete action was Owner-only in the UI. An Administrator (who can invite) saw no Delete action on a pending invitation.
The cause could not be confirmed against Bryan's actual session; both are real and fixed.
**B.** One place, hardcoded: `InvitationService.CreateInvitationAsync` used `AddDays(1)` (already 24 hours). No other duration exists in backend or frontend texts (no text names the duration). If Bryan saw a shorter effective time, the likely suspects are the unconfigurable value and the "expired" screen being shown for withdrawn links too; a database time-zone effect on the `datetime` column (DateTimeOffset stored without offset) cannot be excluded from here and needs a look at a real row on the test host.
**C.** See the analysis sent to the peer: there is no mail system anywhere (no SMTP/provider/sender code or config); the button on the invite-error page only navigates to `/`.
## The Fix
- `InvitationSettings` (`Identity:Invitation:ExpiryHours`, default 24, validated 1..720, `ValidateOnStart`); `InvitationService` uses it.
- `GetAllPendingInvitationsAsync` includes expired invitations (`PendingInvitationInfo.IsExpired`); `UserDto.InvitationExpired`; expired rows have no invite link. UI: status "Uitnodiging verlopen", no copy-link action, still removable.
- UI: an Administrator may withdraw a pending invitation for a plain User (same reach as assigning that role); account deletion stays Owner-only.
- Invite-error page: neutral title "Link ongeldig of verlopen" (it used the "system setup required" text) and text that covers expired, withdrawn and unknown links.
**Left unchanged, reported**: `DELETE Users/{id}` has no role hierarchy: an Administrator calling the API directly can delete an Owner or another Administrator (only the last-Owner rule protects). Pre-existing; needs a decision before changing.
## Verification
**Path used**: Automated test framework
- Revocation really invalidates: delete -> `ValidateInvitationAsync` false and `CompleteInvitationAsync` throws Unauthorized, no user created.
- Expired invitation is listed with `IsExpired` and can be deleted; controller returns `InvitationExpired` and no link for it.
- Lifetime: default 24 h, configured 72 h, config binding default/48, out-of-range values (0, -5, 721) rejected.
- Frontend: Owner and Administrator delete rules, expired row (label, no copy link, deletable), neutral invalid-link text.
- Core.Tests 379, Identity 58, Availability 82, Master 57, Offerings 57; frontend 322; tsc and eslint clean.
## Impact on SlpSoftware
No migration. Behaviour: expired invitations now appear in the users list (removable), lifetime configurable (default unchanged at 24 h; an invalid value fails startup), Administrator can withdraw User invitations, invite-error text changed.
## Follow-up decisions (Bryan, relayed 2026-11-08)
1. **Role hierarchy closed in the API** (the gap reported above). `UserManagementRules` (Core): an Owner manages everyone; an Administrator only plain Users (delete, deactivate, change role, withdraw/regenerate an invitation) and may never hand out the Owner role (also not by invitation); an Administrator may still step down to User themselves; the last-Owner rule is unchanged. The requester's role is read from the database (not the possibly stale token); an unresolvable caller is treated as Administrator. Enforced in `UsersController` (Invite, ChangeRole, SetUserActive, DeleteUser, RegenerateInvite); the UI mirrors it (Administrators see delete/deactivate/regenerate for Users only, nothing for Administrators and Owners). Tests: `UsersControllerRoleHierarchyTests` (hierarchy rules, each action, invitations, last Owner).
2. **Invite-error card**: the button that suggested requesting a new link (it only navigated away, there is no mail) is now "Naar inloggen"; the text already says to ask the administrator.
3. **"Nieuwe uitnodigingslink maken"** (small, kept): `POST Users/{id}/regenerate-invite` gives an open or expired invitation a new token and a new lifetime; the old link stops working. The UI action copies the new link. It stays useful as a fallback once e-mail exists. Tests: service (old token invalid, new valid, expired revived, unknown/accepted -> null), controller (hierarchy, 404), UI.
4. **Lifetime shorter than 24 h?** Round trip `DateTimeOffset` -> MariaDB `datetime` -> back was proven against a real MariaDB (server and client in CET, offsets +0/+1/+2/-5 written): the provider stores the UTC clock value and reads it back as UTC, so the remaining lifetime stays 24.00 h. `DateTimeOffsetRoundTripTests` (runs when `ConnectionStrings__DefaultConnection` points at a MariaDB, as in CI; uses its own scratch database; does nothing otherwise). So a time-zone shortening cannot be demonstrated; stored seconds are truncated (sub-second only). To rule out anything on the test host, a read-only look at `AndreaSlopFotografieTest.Invitations` would show `CreatedAt`, `ExpiryDate`, the difference in hours and the server `UTC_TIMESTAMP()`.
## Result
- **PR(s)**: None
- **Commit(s)**: see branch history
- **Completed**: 2026-11-08