# Roles, permissions and scope (foundation)

**Model.** A permission key is `module.resource.action`. A role grants permissions, each with a **scope**:
`own` (records assigned to / owned by the user), `team` (records of the user's teams; a user without a team sees only themself),
`company` (all records of the company). When several roles grant the same permission the widest scope wins. No grant means no
access (deny by default); there is **no** `Gate::before` bypass, so even administrators only hold what their role grants.

## Seeded permissions (Phase 1)
| Key | Meaning | Policy enforcement |
|---|---|---|
| `admin.user.view` / `admin.user.manage` | See / create, edit, deactivate users, assign roles, reset MFA | `UserPolicy` (own/team/company); create needs company scope |
| `admin.role.view` / `admin.role.manage` | See / create and edit roles and permission sets | `RolePolicy` (company scope for manage) |
| `admin.org.view` / `admin.org.manage` | See / manage branches, departments, teams | `OrganizationPolicy` |
| `admin.audit.view` | Read the audit log | `AuditLogPolicy` (company scope) |
| `admin.settings.manage` | System health details (more settings later) | Gate `view-health-details` |

## Seeded roles
| Role | Grants | Notes |
|---|---|---|
| `admin` (system) | all of the above, company scope | `requires_mfa`; cannot be deleted; must keep `admin.user.manage` and `admin.role.manage` |
| `auditor` (system) | `admin.user.view`, `admin.role.view`, `admin.org.view`, `admin.audit.view` | read-only |

## Request authorization order
1. `auth:sanctum` (401) → 2. `company.context` (binds the user's company; disabled account → 403) → 3. `mfa.enrolled`
(403 `mfa_enrollment_required` while policy demands MFA) → 4. class-level policy ability (403 **before any record lookup**, so
unprivileged users learn nothing about which ids exist) → 5. record lookup through the company scope (other company → 404)
→ 6. record-level policy ability (own/team/company scope) → 7. business guards (below).

## Anti-escalation rules (`PrivilegeGuard`, `AdministratorGuard`)
- You can only **grant** a permission you hold at the same or a wider scope (create role, change a role's permissions).
- You can only **modify or delete a role** that grants nothing beyond what you hold; system roles cannot be deleted.
- You can only **assign or remove** a role you could create; you can only **deactivate / re-role / MFA-reset** a user whose roles you could all handle.
- You cannot deactivate yourself; the **last active administrator** can never be deactivated or lose the `admin` role.
- Request bodies can never set `company_id`, `status`, passwords or ids; ULIDs for related records are validated inside the company.

## Adding permissions in later phases
Add the key to `PermissionSeeder` together with the feature, a policy method that applies the scope, a row in the route-level
tests, and an entry in this file. The route-inventory test fails if a new route is reachable without authentication or permission.
