# company-scoping Specification ## Purpose Separate the data of each company using Co-Lite (INS and CV at go-live): staff sign in with their company code and see only their own company's records, while company-less super admins manage companies and see every company. ## 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