# PEOPLEFLOW — Role-Based Access Control (RBAC) & Data Isolation

## 1. Three-Layer Authorization Model

Every server-side request (Page Server Component, Server Action, and `/api/v1/*` Route Handler) passes through **three authorization checks**:

1. **Functional Permission (RBAC)**: Does at least one of the user's assigned roles hold the required permission code (e.g., `offboarding.emergency_revoke`)?
2. **Company Data Scope**: Is the target record's `companyId` within the user's `allowedCompanyIds` (or does the user hold `hasGroupWideAccess`)?
3. **Relationship & Field-Level Policy**:
   - **Managers** may only approve requests and view direct/dotted-line/department reports.
   - **Employees** may only read their own onboarding status, assigned tasks, and personal documents.
   - **Restricted PII** (`dateOfBirth`, `personalEmail`, `contactNumber`, `separationReason`) is stripped from DTOs unless the user holds `employee.pii.read` or `offboarding.reason.read`.

> **Security Rule:** Hiding a button or sidebar link in the UI is purely a UX convenience. Authorization is **always enforced inside the service layer**.

---

## 2. Role & Permission Matrix

Legend: **Full** = Create/Read/Update/Action | **Scoped** = Own company / department / assigned items only | **Read** = View only | **PII** = Access to restricted sensitive fields | **—** = Denied

| Module / Capability | Sys Admin | HR Admin | HR Staff | IT Admin | IT Staff | Dept Head / Mgr | Admin / Facilities | Mgmt / Exec | Employee |
| :--- | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| **Management Dashboard** | Full (Group) | Scoped (Co) | Scoped (Co) | IT KPIs | Own Tasks | Dept KPIs | Asset KPIs | Consolidated Read | Personal Home |
| **Employee Directory (Basic)** | Full | Full (Co) | Create/Update (Co) | Read (Co) | Read (Assigned) | Read (Dept) | Read (Co) | Read (Group/Co) | Self Only |
| **Employee Sensitive PII (DOB, Contact)** | Read (Audited) | Full (Co) + PII | Scoped + PII | — | — | — | — | — | Self Only |
| **Company / Dept / Position Setup** | Full | Read / Dept Mgmt | Read | Read | Read | Read | Read | Read | — |
| **Onboarding: Initiate & Edit** | Full | Full (Co) | Create/Track (Co) | Read (Co) | Read (IT Tasks) | Read (Dept) | Read (Co) | Read | Self Status |
| **Onboarding: Dept Approval** | Override | Validate | — | — | — | Approve (Dept) | — | — | — |
| **Offboarding: Initiate** | Full | Full (Co) | Submit (Co) | Read (Co) | Read (IT Tasks) | Review (Dept) | Read (Co) | Read | — |
| **Offboarding: Separation Reason (PII)** | Audited Read | Full (Co) | Scoped (Co) | — | — | — | — | Summary Only | — |
| **Offboarding: Emergency Revocation** | Execute | Request | — | Execute | — | — | — | — | — |
| **Employee Movement / Transfer** | Full | Full (Co) | Submit (Co) | Review Access | IT Tasks | Approve (Src/Dst) | Asset Tasks | Read | — |
| **Workflow Builder (Edit/Publish)** | Full | Read / Propose | — | Read | — | — | — | — | — |
| **Tasks: View & Update** | All | Company All | HR Tasks | IT All | Assigned IT | Dept Tasks | Facilities Tasks | Read | Own Tasks |
| **Tasks: Reassign** | All | Company HR | — | IT Tasks | — | Dept Tasks | Facilities Tasks | — | — |
| **Assets: Inventory & Assignment** | Full | Read | Read | IT Assets | IT Tasks | Read (Dept) | Full (Physical/Co) | Read | Own Assigned |
| **Documents: Upload & Download** | Full (Audited) | HR Docs (Co) | HR Docs (Co) | IT Evidence | IT Evidence | Dept Handover | Asset Forms | Reports Only | Own Docs |
| **Integrations Config & Secrets** | Full | — | — | Configure / Retry | — | — | — | — | — |
| **Reports & Exports** | Consolidated | Company HR | Limited HR | IT SLA Reports | — | Dept Reports | Asset Reports | Consolidated | — |
| **Audit Logs** | Full (Group) | Company HR Logs | — | IT / Integration Logs | — | — | — | Compliance Read | — |
| **Users, Roles & Permissions** | Full | — | — | — | — | — | — | — | — |

---

## 3. Granular Permission Codes (Seeded Baseline)

- **Dashboard**: `dashboard.view`, `dashboard.view_consolidated`
- **Organization**: `company.manage`, `department.manage`, `position.manage`
- **Users & RBAC**: `user.manage`, `role.manage`, `company_access.manage`
- **Employees**: `employee.read`, `employee.read_self`, `employee.create`, `employee.update`, `employee.pii.read`, `employee.status.update`
- **Onboarding**: `onboarding.read`, `onboarding.create`, `onboarding.validate`, `onboarding.approve`, `onboarding.complete`, `onboarding.cancel`
- **Offboarding**: `offboarding.read`, `offboarding.create`, `offboarding.reason.read`, `offboarding.manager_review`, `offboarding.emergency_revoke`, `offboarding.complete`
- **Movements**: `movement.read`, `movement.create`, `movement.approve_source`, `movement.approve_target`, `movement.validate`, `movement.complete`
- **Workflows**: `workflow.read`, `workflow.manage`, `workflow.publish`
- **Tasks**: `task.read_own`, `task.read_department`, `task.read_company`, `task.update_own`, `task.reassign`, `task.verify_external`
- **Assets**: `asset.read`, `asset.manage`, `asset.assign`, `asset.return`
- **Documents**: `document.read`, `document.upload`, `document.download`, `document.delete`
- **Integrations**: `integration.read`, `integration.manage`, `integration.execute`, `integration.retry`
- **Reports & Audit**: `report.view`, `report.export`, `audit.read`

---

## 4. Authentication Architecture

1. **Production SSO**: Microsoft Entra ID via OIDC (`Auth.js` Microsoft Entra ID provider).
   - Maps Entra `oid` (`entraObjectId`) and verified corporate email to `User`.
   - Enforces secure `HttpOnly`, `SameSite=Lax`, `Secure` session cookies with short idle timeout and forced session invalidation on deactivation.
2. **Development Authentication**:
   - Email/password credentials provider with seeded test accounts for all 9 roles across ECC, NCC, SCII, and Group Admin.
   - Strictly disabled when `NODE_ENV === "production"` unless explicitly enabled in a non-prod staging flag.
