docs(openspec): propose add-company-scoping

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
ngqileandClaude Opus 5.5 committed 2026-11-30 18:23:08 +08:00
1 parent 628d885261
commit 4243a2d6cd
10 files changed
+606

No files matched your search

@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-11-30
@@ -0,0 +1,101 @@
# Design
Full design with approaches and the user's approved calls: `docs/superpowers/specs/2026-11-30-company-scoping-design.md`.
## Context
- **Identity is a string everywhere:**
- services-auth issues the JWT with the user's `username` as subject.
- It resolves a token back to a user through services-system `/user/findByUsernameWithPassword/{username}`.
- mobile-api resolves the caller from the token about 40 times. It also uses `caller.getUsername()` for `doneBy`, the lockout key, the `login_audit` username and the audit actor.
- `user.username` is globally unique (`username_UNIQUE`). Member users live in the same table.
- **Already scoped by company:** leads, event leads, event salespersons and interests are queried by the caller's company. mobile-api passes `caller.getCompanyId()` to services-counter.
- **Not scoped:**
- the users search (services-system);
- events (the Tastefully `event` table, services-counter, no `company_id`);
- emails (service-email, no `company_id`);
- login history and locked users (services-system; `login_audit.company_id` exists but is unused);
- the audit log (services-log; `audit_event.company_id` exists but is unused).
- **Super admin today:** holds role id 1. Checked in `AppAccessAspect`, `AdminUserService`, `AdminRoleService`, `AdminAclService` and `AdminEmailService`.
- **Company today:** `company` is `(id, name, create_date)`, with one row (id 1, "Co-Lite"). No entity or API.
- **Emails:** mobile-api `EmailService` publishes emails to `email-save-request-topic`, and service-email saves them.
## Goals / Non-Goals
**Goals:**
- Every scoped admin read and write carries the caller's company; super admins see all or a picked company.
- Company sign-in without changing services-auth, the token format or member sign-in.
- Adding a company needs no code, deploy or database work.
**Non-Goals:**
- Members and the customer portal.
- Company admins with their own roles or settings.
- Stamping a super admin's audit row with the changed record's company.
- Deleting companies.
## Decisions
1. **The stored username of a staff user is the login name `local@code`.**
- The portal sends `company` and `username`. mobile-api builds the login name: `username@company`, lowercased code; a blank company means the plain username, which is a super admin.
- services-system stores `john@ins` in `user.username`. The existing unique key then gives per-company uniqueness. services-auth, tokens, `findByUsername*`, `login_audit` and the audit actor all keep working on that one string unchanged.
- Screens show the local part (`john`) next to the company. A username input SHALL NOT contain `@`.
- Alternative rejected: a new `login_name` column, or a lookup split across company and username. Either would touch services-auth and every token lookup for the same outcome.
- The user's choice of approach B is preserved: per-company usernames, a separate Company field and a renamable code.
2. **Company code change:**
- A services-system `@Transactional` endpoint renames the code and rewrites `username = CONCAT(local, '@', newCode)` for that company's admin users.
- Old tokens no longer resolve to a user, so those sessions end on their next request. The username column is the session's identity, so nothing else is needed.
- Login history keeps the old names.
3. **Company table and API:**
- `company` gains `code varchar(20) NOT NULL UNIQUE` and `status char(1) NOT NULL DEFAULT 'A'`.
- services-system gets a `Company` entity, repository and `/company/*` endpoints: list, find, save, rename-code.
- mobile-api gets `AdminCompanyController` (`/api/admin/companies`), super admin only.
4. **Super admin check, in one place:**
- `AdminCompanyScope` (mobile-api) defines `isSuper(caller)` as "no company AND holds role 1". It replaces the role-only checks.
- `scope(caller, requestedCompanyId)` returns the company to filter by:
- staff: their own company, ignoring any request value;
- super admin: the requested company, or null for all.
- `requireCompanyForCreate(caller, requested)`: a super admin must choose one.
- `requireSuper(caller)` answers 403.
- Ported from babyboo's `AdminCountryScope` pattern.
5. **Inactive company:**
- `AdminSessionGuard`, which already loads the caller on every admin request, refuses a staff user whose company is inactive, with the same 401 as a dead session.
- `AdminAuthController.login` refuses one with `BAD_LOGIN`.
- The company status comes from services-system with the user, by adding `companyStatus` to the user lookup. That avoids an extra call per request.
6. **Scoped queries:**
- Each backend list or search endpoint takes an optional `companyId`; null means all companies, and only mobile-api decides when null is allowed.
- Detail and edit endpoints resolve the record, then compare its company with the scope; a mismatch answers 404.
- Where a services-counter query takes a mandatory `companyId` today, it becomes optional for lists.
- Every write still takes a concrete company.
7. **Events and emails get `company_id`:**
- `event.company_id bigint NULL`, set from the scope on create and never changed by edit.
- `email.company_id bigint NULL`, set by mobile-api `EmailService` from the user the email is for (forgot password, temporary password) and stored by service-email.
- Public form emails and other system emails have no company and are visible to super admins only.
8. **Interests become shared:**
- The unique key `(company_id, name)` becomes `(name)`; queries stop filtering by company.
- `interest.company_id` stays, unused and nullable, so no data is lost.
- Writes are super admin only.
9. **Login history and audit:**
- `login_audit.company_id` is filled from the user that the login name matches; it is already done this way.
- The audit actor's company is already stored.
- The searches take the scope; NULL-company rows match only when the scope is "all".
10. **Portal:**
- `AdminAuthContext` gains `companyId`, `companyCode` and `isSuper` from the login response.
- Super admins get a header `CompanyPicker`, persisted per tab. It feeds `companyId` into the list queries and the default of create forms.
- A `Company` column appears on scoped lists when "All companies" is selected.
- The login and forgot-password pages get a Company field.
- `/companies` is gated by `isSuper`, not by a module.
11. **One-time data step** (change folder SQL plus seed):
- Set company 1 to `INS`/`ins`; insert `CV`/`cv`.
- Set `admin` to company NULL.
- Rewrite the other admin usernames to `username@ins`. First check that none contains `@`; stop if one does.
- Set existing events and emails to company 1.
- Move the interest unique key.
## Risks / Trade-offs
- **Member usernames share the column.** A member whose username is exactly `john@ins` would block creating staff `john` in INS. Members do not exist in Co-Lite at go-live; the users API reports such a clash as "username taken".
- **The login name is visible in history and audit.** Login history and the audit log show `john@ins`, which is clear for staff and super admins alike.
- **A code change ends sessions.** This is intentional, and the Companies screen warns before saving.
- **Every scoped endpoint must use the scope.** A missed endpoint is a cross-company leak.
- Mitigation: one helper, a test per endpoint for another company's record, and a whole-branch review that checks every `/api/admin/**` handler.
- **Tokens in flight at deploy:** their subjects (for example `sales1`) no longer match the rewritten usernames, so every session ends. Co-Lite is not deployed yet, so nobody is affected.
@@ -0,0 +1,39 @@
# Proposal
## Why
Co-Lite goes live on 2027-01-15 with two test-run companies, INS and CV, and more are planned. Each company's staff must see only their own company's data, and one or more super admins must see and manage everything, including creating companies. Everything built so far assumes a single company: users, events, emails, login history and the audit log are unscoped, interests are per company, and there is no way to create a company or tell companies apart at sign-in. Approved design: `docs/superpowers/specs/2026-11-30-company-scoping-design.md`.
## What Changes
- **Companies:** `company` gains a unique `code` and an active/inactive `status`. A new super-admin-only Companies screen lists, creates and edits companies. An inactive company's staff cannot sign in.
- **Sign-in:** the admin login page asks for Company, Username and Password; a super admin leaves Company empty. Usernames and emails are unique within a company. Forgot password asks for the same company and username plus the email.
- **Super admins:** a super admin is a company-less admin user holding the super admin role. Company staff can never hold that role. There can be several super admins.
- **Scoping:** every admin read, write, export and detail lookup is limited to the caller's company: users, events, event leads, leads, emails, login history, locked users and the audit log. Another company's record answers 404. Super admins see all companies, can narrow to one, and pick the company when creating a company-owned record.
- **Shared data:** roles, interests, lockout settings and password rules are shared by all companies, and only super admins edit them. Interests become one shared list.
- **Portal:** a header company picker and a Company column for super admins; Company fields on the login, forgot-password, user and event forms; the Companies screen.
- **BREAKING (sign-in):** company staff must enter their company code to sign in. The deploy signs everyone out.
- **Deploy SQL:** company code/status, `company_id` on `event` and `email`, the interest unique key, staff login names, INS and CV setup.
## Capabilities
### New Capabilities
- `company-scoping`: companies, the Companies screen, company sign-in identity, super admins, and the rule that limits every admin view and action to the caller's company.
### Modified Capabilities
- `admin-access`: staff login and forgot password take a company.
- `admin-users`: company membership, per-company username and email uniqueness, super admin creation.
- `admin-roles`: editing roles becomes super admin only.
- `lead-interests`: one shared interest list, edited by super admins only.
- `admin-login-security`: unlock is limited to the caller's company; settings become super admin only.
## Impact
- **services-system:** company entity, repository and endpoints; user lookup, save and uniqueness by login name; user search by company; login-history and locked-user filters by company.
- **services-counter:** `company_id` on events; event queries by company; interest queries become shared; queries accept "all companies" for super admins.
- **service-email:** `company_id` on emails, set when an email is saved for a user; search by company.
- **services-log:** audit search by company.
- **mobile-api:** login and forgot password with company; one company-scope helper used by every admin service; Companies API; super-admin-only guards on roles, interests and login-security settings.
- **admin portal:** login, forgot-password, header picker, Company column and fields, Companies screen, super-admin-only screens.
- **Database:** additive SQL in the change folder plus a data step for existing users, events and emails; mirrored in `local/seed/03-colite.sql`.
- **Unchanged:** services-auth, token format, members and the customer portal.
@@ -0,0 +1,53 @@
## MODIFIED 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.
#### 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: 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: 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
@@ -0,0 +1,43 @@
## MODIFIED Requirements
### Requirement: Unlock
The system SHALL let an admin with `BO_LOGIN_SECURITY` list the usernames of their own company that are locked now, with their failure count, last failure time and when the lock ends, and SHALL let that admin unlock one of them. A super admin SHALL see and unlock locked usernames of every company, including attempts that match no company. Unlocking SHALL save an `UNLOCK_RESET` record naming the admin who unlocked.
#### Scenario: Unlock a locked username
- **GIVEN** INS user `sales1` is locked
- **WHEN** an INS admin with `BO_LOGIN_SECURITY` unlocks `sales1`
- **THEN** `sales1` no longer appears in the locked list
- **AND** `sales1` can sign in with the right password
- **AND** an `UNLOCK_RESET` record for `sales1` names the admin
#### Scenario: Another company's locked user
- **GIVEN** CV user `sales2` is locked
- **WHEN** an INS admin with `BO_LOGIN_SECURITY` lists locked users or unlocks `sales2` of CV
- **THEN** `sales2` is not listed and the unlock answers 404
### Requirement: Lockout and password-rule settings
The system SHALL let an admin with `BO_LOGIN_SECURITY` read the lockout settings and the admin password rules, and SHALL let only a super admin change them, because they apply to every company. The system SHALL reject a maximum below 1 or above 1000, a period below 1 or above 10000, and a time unit other than S, M, H or D. Password rules SHALL only be edited, never added or removed. Every change SHALL write a log line naming the admin.
#### Scenario: Change the lockout settings
- **WHEN** a super admin sets the maximum to 3 and the period to 30 minutes
- **THEN** the next lock decision in every company uses 3 attempts in 30 minutes
#### Scenario: Invalid setting
- **WHEN** a super admin sets the maximum to 0
- **THEN** the change is refused with a validation error and the settings stay as they were
#### Scenario: Change a password rule
- **WHEN** a super admin sets the minimum length rule to 10
- **THEN** a new password of 9 characters is refused by every admin password check
#### Scenario: Company user changes settings
- **WHEN** an INS user with `BO_LOGIN_SECURITY` changes a lockout setting or a password rule
- **THEN** the change is refused with 403 and the settings stay as they were
@@ -0,0 +1,38 @@
## MODIFIED Requirements
### Requirement: Role list and edit
The system SHALL let any admin with `BO_ROLES` list admin roles, and SHALL let only a super admin create and rename them, because roles are shared by every company. Role names SHALL be unique. The member role and `ROLE_ADMIN` SHALL NOT appear in the list (`ROLE_ADMIN` is attached to every admin automatically and grants no modules).
#### Scenario: Create role
- **WHEN** a super admin creates role `ROLE_SALES`
- **THEN** it appears in the role list and can be assigned to users of any company
#### Scenario: Duplicate name
- **GIVEN** role `ROLE_SALES` exists
- **WHEN** a super admin creates another role named `ROLE_SALES`
- **THEN** the save is refused with a message
#### Scenario: Company user edits a role
- **WHEN** an INS user with `BO_ROLES` creates or renames a role
- **THEN** the request is refused with 403 and nothing changes
### Requirement: Role permissions
The system SHALL let only a super admin choose, for a role, which back-office modules it grants, shown as the module tree. Saving SHALL replace the role's module grants with the chosen set. The grants of the super admin role and of `ROLE_ADMIN` SHALL NOT be editable. Admins with `BO_ROLES` who are not super admins SHALL see the grants read-only.
#### Scenario: Grant Users module
- **GIVEN** role `ROLE_SALES` without `BO_USERS`
- **WHEN** a super admin grants `BO_USERS` to `ROLE_SALES` and saves
- **THEN** users with `ROLE_SALES` in every company see Users in the sidebar after their next login or session refresh
- **AND** `GET /api/admin/users` responds 200 for them
#### Scenario: Own role
- **GIVEN** an INS user who is not super admin and holds `ROLE_SALES` and `BO_ROLES`
- **WHEN** they change the module grants of `ROLE_SALES`, or of any other role
- **THEN** the save is refused with 403 and the grants stay as they were
@@ -0,0 +1,44 @@
## MODIFIED Requirements
### Requirement: Company membership
Every admin user other than a super admin SHALL belong to exactly one company. A company user's new user SHALL belong to the creating user's company, whatever the request says. A super admin SHALL choose the company of a new user, or no company to create another super admin.
#### Scenario: Company in the request is ignored
- **WHEN** an INS user's create-user request carries the CV company id
- **THEN** the new user belongs to INS
#### Scenario: Super admin chooses the company
- **WHEN** a super admin creates a user and chooses CV
- **THEN** the new user belongs to CV
### Requirement: Create user
The system SHALL let an admin with `BO_USERS` create an admin user with name, username, email, one or more roles, and an initial password with its confirmation. Username and email SHALL be unique within the user's company, so the same username or email may exist in another company. A username SHALL NOT contain `@`. The password SHALL match its confirmation and pass the admin password rules. The new user SHALL be saved with a pending password change and SHALL NOT be sent an email.
#### Scenario: New user
- **WHEN** an INS admin creates user `sales1` with email `sales1@example.com`, role Sales and a valid initial password
- **THEN** the user is saved as active in INS with a pending password change
- **AND** `sales1` can sign in with company `ins` and that password and must choose a new password before anything else
- **AND** no email is sent
#### Scenario: Duplicate username
- **GIVEN** a user `sales1` exists in INS
- **WHEN** an admin creates another INS user with username `sales1`
- **THEN** the save is refused and the form shows that the username is taken
#### Scenario: Same username in another company
- **GIVEN** a user `sales1` exists in INS
- **WHEN** a CV admin creates user `sales1` in CV
- **THEN** the user is created
#### Scenario: Weak or mismatched initial password
- **WHEN** an admin creates a user with a password that fails the admin password rules, or that does not match its confirmation
- **THEN** the save is refused with a message on the form
- **AND** no user is created
@@ -0,0 +1,128 @@
## ADDED Requirements
### Requirement: Companies
The system SHALL let a super admin list, create and edit companies, each with a name, a unique code and an active or inactive status. A code SHALL be 2 to 20 characters of lowercase letters, digits and hyphens, compared ignoring case. Companies SHALL NOT be deletable. The Companies screen and its endpoints SHALL be available to super admins only.
#### Scenario: Create a company
- **WHEN** a super admin creates company `CV` with code `cv`
- **THEN** it is listed as active
- **AND** staff can be created in it
#### Scenario: Duplicate code
- **GIVEN** a company with code `ins`
- **WHEN** a super admin creates or renames another company to code `INS`
- **THEN** the save is refused and the code is reported as taken
#### Scenario: Not a super admin
- **WHEN** company staff open the Companies page or call a companies endpoint
- **THEN** the page shows not-authorised and the endpoint responds 403
### Requirement: Company sign-in
The system SHALL sign company staff in with company code, username and password, and SHALL sign super admins in with username and password and no company. The answer SHALL be the same for a wrong or unknown company, an inactive company, an unknown user and a wrong password. A user's sessions SHALL end when their company is deactivated or its code is changed.
#### Scenario: Staff sign-in
- **GIVEN** an active user `john` in active company `ins`
- **WHEN** they sign in with company `ins`, username `john` and the right password
- **THEN** they are signed in as `john` of INS
#### Scenario: Same username in two companies
- **GIVEN** a user `john` in INS and a different user `john` in CV
- **WHEN** someone signs in with company `cv`, username `john` and CV John's password
- **THEN** they are signed in as CV's John and see only CV data
#### Scenario: Wrong company
- **WHEN** INS's `john` signs in with company `cv` or a code that does not exist
- **THEN** login is refused with the same invalid-credentials answer as a wrong password
#### Scenario: Inactive company
- **GIVEN** company CV is deactivated
- **WHEN** a CV user signs in with the right password, or uses an existing session
- **THEN** the sign-in is refused with the invalid-credentials answer and the session ends
#### Scenario: Code changed
- **GIVEN** a signed-in user of the company with code `ins`
- **WHEN** a super admin changes that code to `ins-my`
- **THEN** the user's session ends
- **AND** they sign in again with company `ins-my`
### Requirement: Super admins
A super admin SHALL be an admin user who belongs to no company and holds the super admin role. Only a super admin SHALL create another super admin. The super admin role SHALL NOT be granted to a user who belongs to a company, and every admin user who is not a super admin SHALL belong to a company.
#### Scenario: Create a second super admin
- **WHEN** a super admin creates a user with no company
- **THEN** the user is created as a super admin and signs in without a company
#### Scenario: Super admin role for company staff
- **WHEN** anyone assigns the super admin role to a user of INS
- **THEN** the save is refused with a message
### Requirement: Company data is separated
The system SHALL limit every admin list, search, export, detail, create, edit and action to the caller's company for: users, events, event teams, event leads, leads, emails, login history, locked users and audit events. A record of another company SHALL answer 404, as if it did not exist.
#### Scenario: Lists show own company only
- **GIVEN** leads, events and users in INS and in CV
- **WHEN** an INS user with the matching modules opens Leads, Events or Users
- **THEN** only INS records are listed
- **AND** an export contains only INS leads
#### Scenario: Another company's record by id
- **GIVEN** a CV lead, event, user or email
- **WHEN** an INS user requests it, edits it or acts on it by its id
- **THEN** the answer is 404 and nothing changes
#### Scenario: Same phone in two companies
- **GIVEN** INS has a lead with mobile `+60123456789`
- **WHEN** a CV user captures a lead with the same mobile at a CV event
- **THEN** a new CV lead is created and flagged New
- **AND** the INS lead is unchanged
### Requirement: Super admin sees all companies
The system SHALL show super admins every company's records by default, SHALL let them narrow any scoped list to one company, and SHALL show each record's company in mixed lists. Creating a company-owned record (user, event, event lead) SHALL require a super admin to choose the company.
#### Scenario: Mixed list
- **WHEN** a super admin opens Leads with all companies selected
- **THEN** INS and CV leads are listed together with a Company column
#### Scenario: Pick a company
- **WHEN** a super admin picks CV in the company picker
- **THEN** scoped lists show CV records only
#### Scenario: Create without a company
- **WHEN** a super admin creates an event without choosing a company
- **THEN** the save is refused and the form asks for a company
### Requirement: Company of login and audit records
An audit event and a login-history record SHALL belong to the company of the user who acted, or of the user who tried to sign in when the given company and username match a user. Records with no company SHALL be visible to super admins only.
#### Scenario: Super admin edits a company record
- **WHEN** a super admin edits an INS lead
- **THEN** the audit event has no company
- **AND** INS staff with Audit log do not see it, and super admins do
#### Scenario: Failed sign-in with an unknown company
- **WHEN** someone fails to sign in with a company code that does not exist
- **THEN** the login-history record has no company and only super admins see it
@@ -0,0 +1,34 @@
## MODIFIED Requirements
### Requirement: Interest list
The system SHALL keep one interest list shared by all companies. Admins with `BO_INTERESTS` SHALL see the list, and only a super admin SHALL add, rename, reorder, deactivate and reactivate interests. An interest name SHALL be required, at most 100 characters, and unique ignoring case. Interests SHALL NOT be deletable.
#### Scenario: Add interest
- **WHEN** a super admin adds `Kitchen appliances`
- **THEN** it is listed as active
- **AND** staff of every company can tag leads with it
#### Scenario: Duplicate name
- **GIVEN** an interest `Kitchen appliances`
- **WHEN** a super admin adds `kitchen APPLIANCES`
- **THEN** the save is refused and the name is reported as a duplicate
#### Scenario: Deactivate
- **GIVEN** an active interest `Cookware`
- **WHEN** a super admin deactivates it
- **THEN** it is still listed, marked inactive
- **AND** it is no longer offered when capturing a lead in any company
#### Scenario: Company user edits
- **WHEN** an INS user with `BO_INTERESTS` adds, renames or deactivates an interest
- **THEN** the request is refused with 403 and the list is unchanged
#### Scenario: No module
- **WHEN** an admin without `BO_INTERESTS` opens the Interests page or calls an interests endpoint
- **THEN** the page shows not-authorised and the endpoint responds 403
@@ -0,0 +1,124 @@
# Tasks
Design: `design.md`. All tasks run serially, ordered so that sign-in keeps working after each one. Every scoped endpoint gets a test where another company's record answers 404 (or is absent from the list).
## 1. Data
- [ ] 1.1 SQL and local seed (design decisions 3, 7, 8 and 11).
- Files in `sql/`:
- `01-company-code-status.sql`: add `code` and `status`; set company 1 to `INS`/`ins`; insert `CV`/`cv`.
- `02-company-columns.sql`: add `company_id` to `event` and `email`; set existing rows to company 1; move the interest unique key to `(name)`.
- `03-admin-login-names.sql`: stop if any admin username contains `@`; set `admin` to company NULL; rewrite the other admin usernames to `username@ins`.
- `99-rollback.sql`.
- Mirror them in `local/seed/03-colite.sql`, apply them locally, and confirm `admin` still signs in.
- Scope: `local/**`, `openspec/changes/add-company-scoping/sql/**`
- Verify: `select` shows the codes, the NULL-company `admin`, the rewritten usernames and the new columns.
## 2. Backend services
- [ ] 2.1 services-system: companies, users and login security (decisions 1, 2, 3, 5 and 9).
- Tests first:
- company list, create, edit; duplicate code (ignoring case) refused; code format validated;
- rename-code rewrites only that company's admin usernames, in one transaction;
- user lookups return the company code and status;
- the user search filters by company when given;
- email uniqueness is per company;
- role 1 cannot be saved on a user with a company;
- login history and locked users filter by company, and NULL-company rows appear only for "all".
- Scope: `solvway-services-system/src/**`
- Verify: `mvn -T 1C -B -q -pl solvway-services-system -am verify`
- [ ] 2.2 services-counter: events, leads, event leads and interests (decisions 6, 7 and 8).
- Tests first:
- an event is saved with its company and listed by company, or for all when none is given;
- lead and event-lead lists accept "all companies";
- detail lookups return the record's company;
- interests are one list, unique by name ignoring case.
- Scope: `solvway-services-counter/src/**`
- Verify: `mvn -T 1C -B -q -pl solvway-services-counter -am verify`
- [ ] 2.3 service-email and services-log (decisions 7 and 9).
- Tests first:
- an email saved with a company keeps it, and the email search filters by company;
- the audit search filters by company, and NULL-company rows appear only for "all".
- Scope: `solvway-services-email/src/**`, `solvway-services-log/src/**`
- Verify: `mvn -T 1C -B -q -pl solvway-services-email,solvway-services-log -am verify`
## 3. mobile-api
- [ ] 3.1 Sign-in, company scope and Companies API (decisions 1, 3, 4 and 5).
- Tests first:
- login and forgot-password build the login name from company and username; a blank company means a super admin;
- a wrong company, an inactive company and an unknown user all give `BAD_LOGIN`;
- the session guard ends an inactive company's sessions;
- `isSuper` needs both no company and role 1;
- `scope` ignores a staff request's company;
- the Companies API is super admin only;
- the login response carries the company and `isSuper`.
- Scope: `solvway-mobile-api/src/**`
- Verify: `mvn -T 1C -B -q -pl solvway-mobile-api -am verify`
- [ ] 3.2 Users, roles, interests and login-security settings (decisions 1, 4 and 8).
- Tests first:
- users are listed, created and edited within the scope, and another company's user answers 404;
- a username with `@` is refused;
- a super admin must choose a company or create a super admin;
- role 1 is refused for company users;
- role create, rename and grants, interest writes and lockout or password-rule changes answer 403 for company staff;
- unlock works only within the caller's company.
- Scope: `solvway-mobile-api/src/**`
- Verify: `mvn -T 1C -B -q -pl solvway-mobile-api -am verify`
- [ ] 3.3 Events, leads, event leads and exports (decision 6).
- Tests first:
- each list and export is limited to the scope;
- another company's event, lead or event lead answers 404 on detail, edit, capture and export;
- a super admin capture or event create without a company is refused;
- the event team only offers users of the event's company.
- Scope: `solvway-mobile-api/src/**`
- Verify: `mvn -T 1C -B -q -pl solvway-mobile-api -am verify`
- [ ] 3.4 Emails, login history and audit log (decisions 7 and 9).
- Tests first:
- each is limited to the scope;
- emails for a user carry that user's company;
- another company's email or audit event answers 404.
- Scope: `solvway-mobile-api/src/**`
- Verify: `mvn -T 1C -B -q -pl solvway-mobile-api -am verify`
- Live check on the local stack:
- INS and CV users each see only their own data;
- `admin` sees both;
- a CV record by id answers 404 to INS.
## 4. Portal
- [ ] 4.1 Sign-in, session and Companies screen (decision 10).
- Tests first:
- the login and forgot-password pages send company, username and password, and a blank company is allowed;
- the auth context holds `isSuper` and the company;
- `/companies` lists, creates and edits companies, warns before a code change, and is refused for non-super users.
- Scope: `solvway-admin-portal/app/**`
- Verify: `cd solvway-admin-portal && npx vitest run && npx tsc --noEmit && npm run lint && npm run build`
- [ ] 4.2 Company picker, Company column, company fields and super-admin-only screens (decision 10).
- Tests first:
- the picker is shown only to super admins and feeds the list queries;
- the Company column appears for "All companies";
- user, event and capture forms show Company for super admins only;
- Roles, Interests and the Login security Settings tab are read-only or hidden for company staff.
- Scope: `solvway-admin-portal/app/**`
- Verify: `cd solvway-admin-portal && npx vitest run && npx tsc --noEmit && npm run lint && npm run build`
## 5. Check and docs
- [ ] 5.1 Browser check and handover.
- Playwright on the local stack:
- `admin` creates CV users and a CV event, then switches the picker between all, INS and CV;
- an INS user and a CV user with the same username each sign in with their company and see only their own leads, events, users, emails, login history and audit log;
- an INS user opening a CV record by URL sees not-found;
- renaming CV's code ends the CV user's session;
- deactivating CV blocks its sign-in;
- check at 1280 px and 360 px.
- Docs, in `docs/deployment.md`:
- section 3: the new SQL, and that it ends every session;
- section 10: company sign-in and super admins.
- Scope: `docs/deployment.md`
- Verify: screenshots, and the steps above observed.
## Workflow follow-up
- Whole-branch review, with every `/api/admin/**` handler checked for scoping. Then push, PR to main and merge, and archive on its own branch.