# Creating the first production administrator securely

No production credential is ever seeded or committed. `php artisan db:seed` creates the company, permission catalogue and
roles only (the development administrator is created **only** when `APP_ENV=local`). Follow these steps on the server.

## 0. Preconditions
- HTTPS is working and `.env` is set: `APP_ENV=production`, `APP_DEBUG=false`, `SESSION_SECURE_COOKIE=true`,
  `SANCTUM_STATEFUL_DOMAINS=<your public host>`, `CRM_ENFORCE_MFA=true`, `APP_URL=https://<your host>`, working `MAIL_*` settings.
- `.env` is readable only by the application user (`chmod 600 .env`) and is not in the web root.
- The database user used by the application has **no `UPDATE`/`DELETE` on `audit_logs`** (see step 6).

## 1. Migrate and seed reference data
```bash
php artisan migrate --force
php artisan db:seed --force        # company, permissions, roles. Creates no users in production.
```

## 2. Create the administrator (pick one)
**Interactive (recommended).** Run it over SSH from a terminal; the password is read with hidden input and never appears in
the process list or shell history:
```bash
php artisan crm:create-admin owner@yourcompany.com "Owner Name"
# Password (min 12 characters): ********
```
**Non-interactive** (provisioning tools). Pass the password through an environment variable that exists only for this
command, never as an argument:
```bash
read -rs CRM_ADMIN_PASSWORD && export CRM_ADMIN_PASSWORD
php artisan crm:create-admin owner@yourcompany.com "Owner Name" --password-env=CRM_ADMIN_PASSWORD
unset CRM_ADMIN_PASSWORD
```
Use a long, unique password (a passphrase manager-generated value of 16+ characters). The command validates length, refuses
duplicates, writes a `user.created` audit entry (actor: system, source: console) and never prints the password.

## 3. First sign-in and MFA
1. Sign in at `https://<your host>`. Because the `admin` role requires two-factor authentication, every admin API answers
   `403 mfa_enrollment_required` and the app sends you to **Account security**.
2. Add the setup key to an authenticator app, enter the 6-digit code, and **store the 10 recovery codes offline** (they are shown once).
3. Create a **second administrator** with their own MFA right away: the system refuses to deactivate or demote the last
   active administrator, but a lost device with no second admin still requires CLI recovery (below).

## 4. Verify the installation
```bash
php artisan crm:check-security     # exits non-zero on any FAIL: debug off, secure cookies, MFA enforced, no dev admin, audit chain intact
php artisan audit:verify           # verifies the audit hash chain of every company
curl -I https://<your host>/.env   # must be 403/404
```

## 5. Day-to-day rules
- Give people the least privilege they need; use the **Roles & permissions** screen. Nobody can grant more than they hold.
- New users receive an invitation e-mail and choose their own password; administrators never see or set it.
- Review the **Audit log** screen after role/permission changes; schedule `php artisan audit:verify` (e.g. daily) and alert on a non-zero exit.

## 6. Make the audit log tamper-resistant at the database level
The application never updates or deletes audit rows, and every row is hash-chained. Add a database-level guard as well:
```sql
-- as a MySQL admin, after migrations; 'crm_app' is the application's database user
REVOKE UPDATE, DELETE ON crm_platform.audit_logs FROM 'crm_app'@'localhost';
```
Run migrations with a **separate** privileged user so the application user can stay restricted.

## Lost administrator access (CLI recovery)
If the only administrator loses their authenticator **and** recovery codes, someone with server access can create a new
administrator (`crm:create-admin`) and then reset the old account's MFA from the Users screen. Both actions are audited.
