Files
xenia-pos-local/manager_dashboard/src/pages/staff/permissionsLogic.js
T
bonaminandClaude Opus 5.5 92bc1b0113 feat(manager): staff permission editor, signed-in sessions, approvals log (platform-v2 2.6e)
- Staff: the 13 perm_* tick columns become one Permissions column (count,
  "changed from role") opening an editor grouped by module, from
  /api/waiters/permission-catalog: switches labelled per key, keys you don't
  hold are disabled with the reason, reset to role defaults (names what it
  can't give), save the whole set; access.cannot_grant explained by label.
- "Signed in now" (/management/sessions): who, where (app or device), since,
  last active, your own session marked; end session; show ended + reason.
- "Manager approvals" (/management/approvals): the override audit with
  action label, asked by, approved by, device, used/not used, order link.
- Backend: sessions list adds device_name/device_app/current/expires_at;
  overrides list adds device_name/expires_at.
- Permission labels for every key in modules.json (en/el), guarded by
  test_module_texts; manager unit tests for the editor logic; e2e 66 checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 08:10:57 +02:00

89 lines
3.6 KiB
JavaScript

// Pure logic of the staff permission editor, the sessions list and the approvals log
// (plan step 2.6e). No React here, so tests/platform.test.mjs can run it with node --test.
/** The module a permission key belongs to: its first segment ("ordering.orders.cancel" → "ordering"). */
export const moduleOf = key => key.split('.', 1)[0]
/** i18n key of a permission's label: modules:<module>.permissions.<rest>, nested by the dots. */
export const permLabelKey = key => `modules:${moduleOf(key)}.permissions.${key.slice(moduleOf(key).length + 1)}`
/**
* The groups the editor shows.
* catalog GET /api/waiters/permission-catalog → .modules
* modules GET /api/platform/modules → .modules (entitled / effective per module)
* A module that isn't licensed is left out (its keys are kept as they are when saving).
* Keys no role has (core.system.admin) are only shown to a superadmin.
* Order: core, then the modules that are on, then those turned off (catalogue order within).
*/
export function editorGroups(catalog, modules, { actorIsSuperadmin = false } = {}) {
const byKey = Object.fromEntries((modules || []).map(m => [m.key, m]))
return catalog
.filter(m => byKey[m.key]?.entitled !== false)
.map(m => ({
key: m.key,
on: byKey[m.key] ? !!byKey[m.key].effective : true,
permissions: m.permissions.filter(p => actorIsSuperadmin || p.roles.length > 0),
}))
.filter(g => g.permissions.length > 0)
.map((g, i) => ({ g, rank: (g.key === 'core' ? 0 : g.on ? 1 : 2) * 1000 + i }))
.sort((a, b) => a.rank - b.rank)
.map(x => x.g)
}
/** Keys added and removed by the draft, compared with what the user has now. */
export function diff(draft, original) {
return {
added: [...draft].filter(k => !original.has(k)).sort(),
removed: [...original].filter(k => !draft.has(k)).sort(),
}
}
/**
* May the editor switch this key ON? Removing is always allowed. Giving is allowed for a key
* the user already had (it isn't new) or one the editor holds; `grantable` null = superadmin.
* Same rule as the backend's access.cannot_grant.
*/
export function canGive(key, original, grantable) {
return grantable == null || original.has(key) || grantable.has(key)
}
/**
* "Reset to role defaults": the role's template, minus the keys the editor may not give.
* Returns { keys, skipped } so the editor can say what it couldn't add.
*/
export function resetToRole(template, original, grantable) {
const keys = new Set()
const skipped = []
for (const k of template) {
if (canGive(k, original, grantable)) keys.add(k)
else skipped.push(k)
}
return { keys, skipped: skipped.sort() }
}
/** Does this user's key set differ from their role's template? */
export function differsFromRole(keys, template) {
const a = new Set(keys)
const b = new Set(template)
return a.size !== b.size || [...a].some(k => !b.has(k))
}
/** An approval's outcome: used / expired (never used) / waiting (still valid). */
export function approvalState(row, { now = Date.now() } = {}) {
if (row.used_at) return 'used'
return Date.parse(row.expires_at) <= now ? 'expired' : 'waiting'
}
/** An approval's target as something to show: { order: id } or { text } or null. */
export function approvalTarget(target) {
if (!target) return null
const m = /^order:(\d+)$/.exec(target)
return m ? { order: Number(m[1]) } : { text: target }
}
/** May this manager end this session? Not their own (that's "sign out"), not support unless superadmin. */
export function canEndSession(session, { actorIsSuperadmin = false } = {}) {
if (session.ended_at || session.current) return false
return session.client !== 'support' || actorIsSuperadmin
}