Repository navigation
Description
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 usersB. 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
-
POST /api/staff(Create Staff):- Role creation guard: If
req.user.role === 'manager'androleis NOT one of['cashier', 'waiter', 'chef'], return403 Forbidden({ error: 'Managers can only create operational staff accounts (cashier, waiter, chef)' }). - PIN role restriction: If
roleis in['cashier', 'waiter', 'chef']and a non-emptypinis passed, return400 Bad Request({ error: 'PINs are only permitted for owner and manager roles' }). - PIN format validation: If
pinis provided for owner/manager, validate format/^\d{4,6}$/. If invalid, return400 Bad Request({ error: 'PIN must be between 4 and 6 numeric digits' }).
- Role creation guard: If
-
PUT /api/staff/:id(Update Staff):- Lockdown
is_active: Ifreq.body.is_active !== undefined, return400 Bad Request({ error: 'Use /deactivate or /reactivate endpoints to change account status' }). - Target authorization: Fetch target member. If
!member, return404. If!canModifyTargetStaff(req.user.role, member.role), return403 Forbidden({ error: 'Managers cannot modify owner or manager accounts' }). - Role change authorization: Changing role requires
req.user.role === 'owner'. (Non-owners attempting role change return403 Only owners can change roles). - Role demotion PIN cleanup: If role is updated to
cashier,waiter, orchef, automatically setpin_hash = NULL. - Operational PIN rejection: If target role (new or existing) is operational and a non-empty
pinis passed, return400 Bad Request. - PIN preservation: Omitting
pin(pin === undefined) preserves existingpin_hash. - PIN format: Validate
/^\d{4,6}$/ifpinis passed for owner/manager.
- Lockdown
-
POST /api/staff/:id/deactivate&POST /api/staff/:id/reactivate:- Fetch target member. If
!member, return404. - Check target authorization: If
!canModifyTargetStaff(req.user.role, member.role), return403 Forbidden({ error: 'Managers cannot deactivate or reactivate owner or manager accounts' }). - Last-owner protection: Prevent deactivating the last active owner (
ownerCount <= 1).
- Fetch target member. If
2. Frontend Specifications (frontend/src/app/(dashboard)/staff/page.tsx & frontend/src/lib/types.ts)
-
TypeScript Types (
frontend/src/lib/types.ts):- Add
has_pin?: boolean | number;toStaffinterface.
- Add
-
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'. Clearform.pin = ''if role is switched to operational. - Password Reveal (Eye Icons):
- Add interactive show/hide toggle buttons using
EyeandEyeOfffromlucide-react. - Use
type="button"andaria-label="Toggle password visibility"(andaria-label="Toggle PIN visibility"). - Reset visibility states (
false) on modal open/close.
- Add interactive show/hide toggle buttons using
- Confirm Password Fields:
- Add
confirmPasswordto Add/Edit form and Reset Password modal. - Validate
password === confirmPasswordon submit; if mismatch, show toast error and prevent API submission.
- Add
- PIN Badge: Render
PIN setbadge whenBoolean(s.has_pin)istrue.
- Role-Based PIN Input: Only render PIN input container when
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:
- Manager cannot edit owner account details or password (403)
- Manager cannot edit manager account details or password (403)
- Manager cannot deactivate owner or manager account (403)
- Manager cannot reactivate owner or manager account (403)
- Manager cannot use PUT
is_activeto disable accounts (400) - Manager cannot create owner or manager account (403)
- Manager CAN create cashier, waiter, or chef account (201)
- Manager CAN edit cashier, waiter, or chef account (200)
- Operational roles reject non-null PINs (400)
- PIN format must be 4 to 6 numeric digits (400 for letters or invalid lengths)
- Demoting manager to waiter clears
pin_hash(has_pinbecomes 0) - Omitting PIN on update preserves existing
pin_hash - Last-owner deactivation protection enforced (400)
- Owner can manage all permitted roles (200/201)
4. Audit Tracking Architecture Note
orders.user_idpreserves staff attribution because users are soft-deactivated (is_active = 0). Identity fields (name,email) reside on theuserstable, 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
itsbkm commentedon Jul 26, 2026
Implemented in commit 790ab0b (
fix(staff): harden authorization and PIN policies).Completed:
has_pinAPI field without exposingpin_hashValidated with
npm run lint,npm run build,npm run build:frontend, and the fullnpm testsuite.fix(deps): resolve security alerts #145-#150 (#1433)