Files
co-lite/openspec/changes/add-company-scoping/design.md
T

7.6 KiB

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.