Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
7.6 KiB
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
usernameas 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()fordoneBy, the lockout key, thelogin_auditusername and the audit actor. user.usernameis globally unique (username_UNIQUE). Member users live in the same table.
- services-auth issues the JWT with the user's
- 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
eventtable, services-counter, nocompany_id); - emails (service-email, no
company_id); - login history and locked users (services-system;
login_audit.company_idexists but is unused); - the audit log (services-log;
audit_event.company_idexists but is unused).
- Super admin today: holds role id 1. Checked in
AppAccessAspect,AdminUserService,AdminRoleService,AdminAclServiceandAdminEmailService. - Company today:
companyis(id, name, create_date), with one row (id 1, "Co-Lite"). No entity or API. - Emails: mobile-api
EmailServicepublishes emails toemail-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
- The stored username of a staff user is the login name
local@code.- The portal sends
companyandusername. 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@insinuser.username. The existing unique key then gives per-company uniqueness. services-auth, tokens,findByUsername*,login_auditand 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_namecolumn, 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.
- The portal sends
- Company code change:
- A services-system
@Transactionalendpoint renames the code and rewritesusername = 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.
- A services-system
- Company table and API:
companygainscode varchar(20) NOT NULL UNIQUEandstatus char(1) NOT NULL DEFAULT 'A'.- services-system gets a
Companyentity, repository and/company/*endpoints: list, find, save, rename-code. - mobile-api gets
AdminCompanyController(/api/admin/companies), super admin only.
- Super admin check, in one place:
AdminCompanyScope(mobile-api) definesisSuper(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
AdminCountryScopepattern.
- 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.loginrefuses one withBAD_LOGIN.- The company status comes from services-system with the user, by adding
companyStatusto the user lookup. That avoids an extra call per request.
- 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
companyIdtoday, it becomes optional for lists. - Every write still takes a concrete company.
- Each backend list or search endpoint takes an optional
- 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-apiEmailServicefrom 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.
- Interests become shared:
- The unique key
(company_id, name)becomes(name); queries stop filtering by company. interest.company_idstays, unused and nullable, so no data is lost.- Writes are super admin only.
- The unique key
- Login history and audit:
login_audit.company_idis 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".
- Portal:
AdminAuthContextgainscompanyId,companyCodeandisSuperfrom the login response.- Super admins get a header
CompanyPicker, persisted per tab. It feedscompanyIdinto the list queries and the default of create forms. - A
Companycolumn appears on scoped lists when "All companies" is selected. - The login and forgot-password pages get a Company field.
/companiesis gated byisSuper, not by a module.
- One-time data step (change folder SQL plus seed):
- Set company 1 to
INS/ins; insertCV/cv. - Set
adminto 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.
- Set company 1 to
Risks / Trade-offs
- Member usernames share the column. A member whose username is exactly
john@inswould block creating staffjohnin 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.
- Mitigation: one helper, a test per endpoint for another company's record, and a whole-branch review that checks every
- 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.