Skip to content

Security: Hardening Staff Management Authorization, Server-Side PIN Policies, & UI Enhancements #145

Closed

Description

@khaira777

Overview & Purpose

This issue contains the complete, unified implementation specification for hardening staff management authorization, enforcing server-side PIN restrictions, adding automated security regression tests, and enhancing the Staff Settings UI.


1. Backend Specifications (main/routes/staff.ts)

A. Data Retrieval & SQL Updates

Update all SELECT queries across GET /api/staff, GET /api/staff/:id, POST /api/staff, PUT /api/staff/:id, POST /:id/deactivate, and POST /:id/reactivate to include has_pin (and never return pin_hash):

SELECT id, name, email, role, (pin_hash IS NOT NULL) AS has_pin, is_active, created_at, updated_at FROM users

B. Target-Role Authorization Guard

Add a target-role authorization helper:

function canModifyTargetStaff(requesterRole: string, targetRole: string): boolean {
  if (requesterRole === 'owner') return true;
  if (requesterRole === 'manager') {
    return !['owner', 'manager'].includes(targetRole);
  }
  return false;
}

C. Endpoint Rules & Validations

  1. POST /api/staff (Create Staff):

    • Role creation guard: If req.user.role === 'manager' and role is NOT one of ['cashier', 'waiter', 'chef'], return 403 Forbidden ({ error: 'Managers can only create operational staff accounts (cashier, waiter, chef)' }).
    • PIN role restriction: If role is in ['cashier', 'waiter', 'chef'] and a non-empty pin is passed, return 400 Bad Request ({ error: 'PINs are only permitted for owner and manager roles' }).
    • PIN format validation: If pin is provided for owner/manager, validate format /^\d{4,6}$/. If invalid, return 400 Bad Request ({ error: 'PIN must be between 4 and 6 numeric digits' }).
  2. PUT /api/staff/:id (Update Staff):

    • Lockdown is_active: If req.body.is_active !== undefined, return 400 Bad Request ({ error: 'Use /deactivate or /reactivate endpoints to change account status' }).
    • Target authorization: Fetch target member. If !member, return 404. If !canModifyTargetStaff(req.user.role, member.role), return 403 Forbidden ({ error: 'Managers cannot modify owner or manager accounts' }).
    • Role change authorization: Changing role requires req.user.role === 'owner'. (Non-owners attempting role change return 403 Only owners can change roles).
    • Role demotion PIN cleanup: If role is updated to cashier, waiter, or chef, automatically set pin_hash = NULL.
    • Operational PIN rejection: If target role (new or existing) is operational and a non-empty pin is passed, return 400 Bad Request.
    • PIN preservation: Omitting pin (pin === undefined) preserves existing pin_hash.
    • PIN format: Validate /^\d{4,6}$/ if pin is passed for owner/manager.
  3. POST /api/staff/:id/deactivate & POST /api/staff/:id/reactivate:

    • Fetch target member. If !member, return 404.
    • Check target authorization: If !canModifyTargetStaff(req.user.role, member.role), return 403 Forbidden ({ error: 'Managers cannot deactivate or reactivate owner or manager accounts' }).
    • Last-owner protection: Prevent deactivating the last active owner (ownerCount <= 1).

2. Frontend Specifications (frontend/src/app/(dashboard)/staff/page.tsx & frontend/src/lib/types.ts)

  1. TypeScript Types (frontend/src/lib/types.ts):

    • Add has_pin?: boolean | number; to Staff interface.
  2. Staff Settings UI (frontend/src/app/(dashboard)/staff/page.tsx):

    • Role-Based PIN Input: Only render PIN input container when form.role === 'owner' || form.role === 'manager'. Clear form.pin = '' if role is switched to operational.
    • Password Reveal (Eye Icons):
      • Add interactive show/hide toggle buttons using Eye and EyeOff from lucide-react.
      • Use type="button" and aria-label="Toggle password visibility" (and aria-label="Toggle PIN visibility").
      • Reset visibility states (false) on modal open/close.
    • Confirm Password Fields:
      • Add confirmPassword to Add/Edit form and Reset Password modal.
      • Validate password === confirmPassword on submit; if mismatch, show toast error and prevent API submission.
    • PIN Badge: Render PIN set badge when Boolean(s.has_pin) is true.

3. Security Regression Suite (tests/staff-authz.test.ts)

Create a dedicated integration test file tests/staff-authz.test.ts and wire it into package.json (test:staff-authz script, included in npm test). Must verify:

  1. Manager cannot edit owner account details or password (403)
  2. Manager cannot edit manager account details or password (403)
  3. Manager cannot deactivate owner or manager account (403)
  4. Manager cannot reactivate owner or manager account (403)
  5. Manager cannot use PUT is_active to disable accounts (400)
  6. Manager cannot create owner or manager account (403)
  7. Manager CAN create cashier, waiter, or chef account (201)
  8. Manager CAN edit cashier, waiter, or chef account (200)
  9. Operational roles reject non-null PINs (400)
  10. PIN format must be 4 to 6 numeric digits (400 for letters or invalid lengths)
  11. Demoting manager to waiter clears pin_hash (has_pin becomes 0)
  12. Omitting PIN on update preserves existing pin_hash
  13. Last-owner deactivation protection enforced (400)
  14. Owner can manage all permitted roles (200/201)

4. Audit Tracking Architecture Note

  • orders.user_id preserves staff attribution because users are soft-deactivated (is_active = 0). Identity fields (name, email) reside on the users table, so updating a user's name updates display across past order lists. Relational links are preserved, but user identity fields are not immutable snapshots.

Activity

  1. itsbkm commented on Jul 26, 2026

    @itsbkm
    Contributor

    Implemented in commit 790ab0b (fix(staff): harden authorization and PIN policies).

    Completed:

    • Manager-to-owner/manager modification, activation, and creation escalation guards
    • Server-side PIN role and 4-6 digit validation, PIN preservation, and demotion cleanup
    • Safe has_pin API field without exposing pin_hash
    • Staff UI password/PIN visibility controls, confirmation fields, and role-aware PIN input
    • Dedicated staff authorization regression coverage

    Validated with npm run lint, npm run build, npm run build:frontend, and the full npm test suite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions