# Licence and administrator privilege security update

## What was wrong

The old `User::isSuperAdmin` trusted either user ID 1 or the editable role code SUPER_ADMIN. This bypassed permission checkboxes. Role creation/rename could therefore become a privilege-escalation route. RSA licence generation already required an enabled issuing installation and its private signing key; seeing an activation link alone does not prove that a client could sign valid licences.

## What changed

- User ID 1 no longer gets automatic privileges.
- A role name/code never grants unrestricted access. An optional trusted system-administrator role is pinned by its original numeric ID in server configuration. If not configured, all roles use their explicit permissions.
- New roles cannot claim the reserved Super Admin identity. The protected role cannot be modified or assigned through Users/HR by ordinary administrators.
- Licence management requires the configured owner user ID AND an enabled issuing installation with the private signing key. Even a trusted business administrator or a directly inserted licence permission does not pass this gate.
- The owner account cannot be reset, reassigned, disabled or deleted by ordinary administrators through Users or HR.
- Role assignment cannot be used to grant permissions/modules the assigning user lacks. Public request/apply-signed-key forms on login remain available to clients; these do not generate keys.

## Apply to each deployed client

This update has been applied only to the development project. Existing offline installations and the hosted ERP are NOT changed remotely.

1. Back up that client's files and database. Deploy the updated project files compatible with that client's database version. This security change itself adds no database columns or migration. Do not replace the live client's database with a development database.
2. Keep the client's own `.env`. Do not copy the owner's `.env`, `bootstrap/cache/config.php`, private.pem, database exports or storage contents to clients.
3. In each CLIENT `.env`, set these values (remove duplicate definitions):

```dotenv
JEVATECH_LICENSE_ENFORCE=true
JEVATECH_LICENSE_ALLOW_LEGACY_KEYS=false
JEVATECH_LICENSE_ISSUER=false
JEVATECH_LICENSE_OWNER_USER_ID=0
JEVATECH_LICENSE_PRIVATE_KEY=
JEVATECH_LICENSE_PRIVATE_KEY_PATH=
JEVAPOS_SYSTEM_ADMIN_ROLE_ID=0
```

`JEVAPOS_SYSTEM_ADMIN_ROLE_ID=0` means nobody bypasses role checkboxes. Assign the client's business administrator appropriate modules/actions via an ordinary Admin role. If the original system-admin role must retain its existing bypass for business operations, pin its verified original numeric ID instead of 0. It still cannot access licence issuing on a client installation. This development database's verified original role ID is 45; other databases can differ. Never automatically trust a newly created role because its name/code says Super Admin.

4. In the project folder, using that computer's XAMPP PHP executable, run:

```text
php artisan optimize:clear
php artisan security:check-license-access --client
```

All checks should pass. Restart Apache if opcode caching is enabled. Log out and log back in. Verify ordinary sales still work, adding/renaming Super Admin is rejected, and `/customer-activations` returns 403 for client users.

5. Review users/roles created since deployment, including unexpected administrator accounts. Reset potentially shared vendor/admin credentials. Do not delete historical business records or licences during that review.

## Owner ERP configuration

Only the owner's protected issuing server should have `JEVATECH_LICENSE_ISSUER=true`, the private signing key stored outside web-accessible directories, and `JEVATECH_LICENSE_OWNER_USER_ID` set to the exact owner login's user ID. The development owner's verified ID is 3, trusted role ID 45. Determine the actual IDs if a different database is used. Other ERP users must not be able to reset that owner's credentials or assign the protected role.

Run `php artisan security:check-license-access` on the owner server. Existing RSA-signed licences remain valid; no key rotation is performed by this update.

## Important limitations

Application permissions cannot make editable PHP/source files or a database tamper-proof against someone with full operating-system/database administrator access. They can alter local enforcement code. Keeping the private signing key exclusively on the owner server prevents client installations from producing authentic signatures accepted by unmodified systems. Encoding, OS access restrictions and a controlled update process are additional layers, not guarantees.

If private.pem or an inline private signing key was ever copied to a client or exposed in an archive, treat the signing key as compromised. Stop distributing it and plan key rotation, distribution of the replacement public key, and licence reissuance. Do not rotate blindly: that can invalidate all deployed clients' existing licences.
