Files
co-lite/openspec/specs/admin-access/spec.md

140 lines
6.0 KiB
Markdown

# admin-access Specification
## Purpose
Staff log in to the Co-Lite admin portal, and every admin page and `/api/admin/**` endpoint admits only active staff holding the module it needs.
## Requirements
### Requirement: Staff login
The system SHALL let an active admin user log in through the admin portal with company code, username and password, or with username and password and no company for a super admin, and SHALL return the user's name, roles, company, menu and whether a password change is required. When a login fails and the company field was left empty, the login page SHALL also say that staff sign in with their company code, decided from the form alone so that it never reveals whether the username exists.
#### Scenario: Correct credentials
- **GIVEN** an active admin user `sales1` of active company `ins`
- **WHEN** they submit company `ins`, username `sales1` and their password on the login page
- **THEN** they are signed in and land on the portal home
- **AND** the sidebar shows only the menu entries their roles grant
#### Scenario: Super admin
- **GIVEN** the super admin `admin`, who belongs to no company
- **WHEN** they submit username `admin` and their password with the company left empty
- **THEN** they are signed in
#### Scenario: Wrong password
- **WHEN** a user submits a wrong password
- **THEN** `/api/admin/auth/login` responds 401
- **AND** the login page shows an invalid-credentials message without saying whether the company or username exists
#### Scenario: Staff user without company
- **GIVEN** an active admin user `sales1` of company `ins`
- **WHEN** they submit username `sales1` and their correct password with the company left empty
- **THEN** login is refused with a 401
- **AND** the page shows the invalid-credentials message and that staff sign in with their company code
#### Scenario: Unknown username without company
- **WHEN** someone submits a username that does not exist with the company left empty
- **THEN** the page shows exactly the same messages as for `sales1` without a company
#### Scenario: Deactivated user
- **GIVEN** an admin user whose account is deactivated
- **WHEN** they submit their correct password
- **THEN** login is refused with a 401 and the page shows the same invalid-credentials message
#### Scenario: Member account
- **WHEN** a member (non-admin) account submits its correct credentials to the admin login
- **THEN** login is refused with a 401
### Requirement: First-login password change
The system SHALL require a user whose password is temporary (new user or reset) to set a new password before using any other admin page or endpoint.
#### Scenario: Pending change confines the session
- **GIVEN** a user logged in with a temporary password
- **WHEN** they open any portal page other than the password-change page
- **THEN** they are sent to the password-change page
- **AND** any `/api/admin/**` call other than first-login and logout responds 403
#### Scenario: Password changed
- **GIVEN** a user on the password-change page
- **WHEN** they submit a new password that meets the password rules
- **THEN** the password is saved, the temporary flag clears, and they can use the portal
### Requirement: Forgot password
The system SHALL let a user request a temporary password by entering their company code, username and email on a public forgot-password page; a super admin leaves the company empty. The response SHALL be the same whether or not the details match an active admin user.
#### Scenario: Matching username and email
- **GIVEN** an active admin user `sales1` of company `ins` with email `sales1@example.com`
- **WHEN** someone submits company `ins`, username `sales1` and `sales1@example.com`
- **THEN** a temporary password is emailed to `sales1@example.com`
- **AND** the user must change it at next login
- **AND** the page shows "If the details match an account, we have emailed a temporary password."
#### Scenario: Non-matching details
- **WHEN** someone submits a company, username and email that do not match an active admin user
- **THEN** no password changes and no email is sent
- **AND** the page shows the same message as for a match
### Requirement: Logout
The system SHALL end the session on logout, and SHALL reject the logged-out token on every later `/api/admin/**` call even before it expires.
#### Scenario: Token used after logout
- **GIVEN** a user who logged out
- **WHEN** a request reuses their old token on `/api/admin/me`
- **THEN** it responds 401
### Requirement: Admin endpoint guard
Every `/api/admin/**` endpoint except login and forgot password SHALL require a valid admin token, and SHALL require at least one of the back-office modules the endpoint declares, granted to one of the caller's roles. A user with the super admin role SHALL pass every module check. An endpoint that declares no module SHALL be denied to everyone except super admin.
#### Scenario: No token
- **WHEN** `/api/admin/users` is called without a token
- **THEN** it responds 401
#### Scenario: Member token on an admin path
- **WHEN** a valid member token calls `/api/admin/me`
- **THEN** it responds 403
#### Scenario: Missing module
- **GIVEN** an admin user whose roles do not grant `BO_USERS`
- **WHEN** they call `GET /api/admin/users`
- **THEN** it responds 403
#### Scenario: Granted module
- **GIVEN** an admin user whose role grants `BO_USERS`
- **WHEN** they call `GET /api/admin/users`
- **THEN** it responds 200
### Requirement: Page guard
The admin portal SHALL show a not-authorised page when a signed-in user opens a page whose module their roles do not grant, and SHALL send a signed-out visitor to the login page for every page except login and forgot password.
#### Scenario: Signed-out visitor
- **WHEN** a signed-out visitor opens `/users`
- **THEN** they are sent to the login page and return to `/users` after logging in
#### Scenario: Page without the module
- **GIVEN** a signed-in user without `BO_ROLES`
- **WHEN** they open `/roles`
- **THEN** the not-authorised page is shown and no role data is loaded