# Design ## Context - **Create today:** `AdminUserService.create` (mobile-api) saves the user through services-system `/user/save` with `login = N`. It then calls `/user/resetAdminPassword`, which generates a password, stores it and emails it. If the email is not sent, create returns 502 `TEMP_PASSWORD_NOT_SENT`. - **Emailed reset:** `POST /api/admin/users/{id}/resetPassword` uses `loadManaged`, which gives 404 for an unknown user and 403 for a super admin when the caller is not one. It then calls `/user/resetAdminPassword`, which only acts on an enabled type-`A` user. - **`/user/save` never writes the password.** For an existing user it copies the stored hash back. A new user is saved without one. - **Password rules:** services-system `/user/firstTimeLogin` checks the new password against the `ADMIN_PWD_FORMAT` rules (`PasswordRuleProperty.validatePasswordRoles`). `/user/validatePasswordFromRulesForMember` does the same check against the member rules. - **Sign-in:** `login = N` makes mobile-api answer every admin call with 403 `PASSWORD_RESET_REQUIRED` until `/api/admin/auth/first-login`. The portal then sends the user to the change-password page. - **Generator:** `ValidateUtil.getRandomPasswordString` uses `java.util.Random`. - **Legacy:** in the Tastefully backoffice, `newUser.html` has `password` and `matchingPassword`. `UserController` creates the account with the typed password and sets `LoginStatus.N`. ## Goals / Non-Goals **Goals:** - Onboard and unlock staff without email, the legacy way. - Make every generated temporary password unpredictable. **Non-Goals:** - Configuring email. - Ending the user's existing sessions when an admin sets a password. The emailed reset does not do this today either. - Showing or generating a password for the admin. ## Decisions 1. **SecureRandom at the shared generator.** `getRandomPasswordString` draws from one static `SecureRandom`, with the same alphabet. 2. **services-system: two internal endpoints next to `resetAdminPassword`.** - `POST /user/validatePasswordFromRulesForAdmin {password}` mirrors the member endpoint with `ADMIN_PWD_FORMAT`. It returns the rules that fail; an empty list means the password passes. - `POST /user/setAdminPassword {id, password}`: - acts only on an existing, enabled type-`A` user; - re-checks the admin rules itself; - stores the bcrypt hash with `login = N` through the existing `userService.resetPassword`; - returns rows updated: 0 means refused. - Both endpoints are reachable only inside the cluster, through the gateway `/user/**` route, like the existing ones. - Alternative: let `/user/save` accept a password. Rejected: save copies the stored hash on purpose, and it serves every user update. 3. **mobile-api create** (`AdminUserRequest` gains `password` and `confirmPassword`): - Missing password: 400 `REQUIRED_FIELD`. - No match: 400 `PASSWORD_MISMATCH`. - Fails the rules: 400 `PASSWORD_POLICY`. - All three checks run before the user is saved, so a refused password creates no user. - After the save it calls `setAdminPassword`. If that returns 0, create answers 500 `SAVE_FAILED`. The user then exists without a usable password, and the admin can fix it with "Set temporary password". - The `resetAdminPassword` call and `TEMP_PASSWORD_NOT_SENT` are removed. 4. **mobile-api `POST /api/admin/users/{id}/password`** (`{password, confirmPassword}`) in `AdminUserController`, under the class-level `BO_USERS`: - Own account: 400 `SELF_NOT_ALLOWED`. - `loadManaged` gives 404 for an unknown user, and 403 for a super admin when the caller is not one. - Disabled user: 409 `USER_DISABLED`. - Then the same `REQUIRED_FIELD`, `PASSWORD_MISMATCH` and `PASSWORD_POLICY` checks as create. - On success: 204. - It logs one audit line, `temporary password set by for user `, and never the password. 5. **Portal:** - `/users/new` gets Password and Confirm Password inputs (`type="password"`, `autocomplete="new-password"`). The client checks that they match before submitting. - `/users/[id]` gets a "Set temporary password" form beside the emailed reset, with the same two inputs. - It is hidden on the caller's own account. - It goes through a new proxy route. - On success it clears both fields and shows "Temporary password set. The user must change it at next sign-in." - The refusal codes map to i18n messages. - The password lives only in form state. It never goes into the URL, storage or logs. 6. **Handover:** `docs/deployment.md` section 2 (step 6) and section 10 say the admin types each staff member's first password and passes it on in person. ## Risks / Trade-offs - **The admin knows the password.** The user must change it at first sign-in before any other call works. The legacy backoffice worked the same way. - **The user's existing sessions survive a password set.** The emailed reset has the same gap. Recorded with the existing "refreshed token stays valid" follow-up. - **Breaking API change:** create without `password` now fails with 400. The only caller is the portal, which ships in the same release. - **Create is two calls** (save, then set password). A failure between them leaves a user without a password. That is the same exposure as today's emailed create, and "Set temporary password" fixes it. ## Migration Plan No data change. Deploy services-system, mobile-api and the portal together; the core-libs change ships inside each service jar. Rollback is the previous images.