From 681048197aecd5a0e5e0a5b819d9d8a3a8598361 Mon Sep 17 00:00:00 2001 From: Matt Mabrey Date: Tue, 6 Oct 2026 11:02:09 -0700 Subject: [PATCH] T12.3: CR-020 bulk-action toolbar Role change, enable/disable, and project add/remove on the selected rows, wired to the T12.1 endpoints. Delete is T12.5. - The toolbar offers only what the caller may do: grantable roles, managed projects (archived last, marked), grantable per-project roles. - Every action confirms through wpConfirmDialog naming the exact count (and for projects, the project and role) before anything is sent. - The result lists every failed row with the server's reason, plus the changed / already-set counts; a partial failure is a warning, an all-failed batch an alert, a server error says nothing was changed. Never a blanket success. Announced through the live-region banner. - The directory reloads afterwards, which clears the selection. DOM harness (jsdom, real page and scripts, mocked API): 30/30 with T12.2. --- html/users.js | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/html/users.js b/html/users.js index 064d6d0..4edd95b 100644 --- a/html/users.js +++ b/html/users.js @@ -44,6 +44,7 @@ async function boot(){ renderScope(); } renderCreateForm(); + renderBulkActions(); loadUsers(); } @@ -176,6 +177,119 @@ document.addEventListener('change', e => { } }); +// ── bulk actions (CR-020, T12.3) ────────────────────────────────────────────── +// The toolbar offers only what this caller may do: the roles they may grant, the +// projects they administer, the per-project roles they may hand out. The server +// re-checks every row anyway (T12.1); this is so nobody is offered a choice that +// can only fail. +function renderBulkActions(){ + const box = document.getElementById('bulk-actions'); + if(!box || !manages()) return; + const grantable = PERM_ROLES.filter(r => (_scope.grantable_roles || []).indexOf(r) >= 0); + const projects = (_scope.managed_projects || []).slice() + .sort((a,b) => (a.archived?1:0) - (b.archived?1:0)); + const projRoles = PROJECT_SCOPED_ROLES.filter(r => (_scope.grantable_project_roles || []).indexOf(r) >= 0); + const group = (label, inner) => '
'+ + ''+uesc(label)+''+inner+'
'; + box.innerHTML = + group('Permissions', + ''+ + '') + + group('Sign-in', + ''+ + '') + + (projects.length ? group('Project access', + ''+ + ''+ + ''+ + '') : ''); +} + +function selectedIds(){ return _users.filter(u => _selected.has(u.id)).map(u => u.id); } +function people(n){ return n + ' user' + (n === 1 ? '' : 's'); } +function selVal(id){ return (document.getElementById(id) || {}).value || ''; } +function selText(id){ + const s = document.getElementById(id); + return (s && s.selectedIndex >= 0) ? s.options[s.selectedIndex].text : ''; +} + +// One path for every bulk action: confirm through the dialog kit with the exact +// count, send, report row by row, reload. A reload clears the selection, which is +// right: the rows may have changed underneath it. +async function runBulk(path, body, ask){ + const ids = selectedIds(); + if(!ids.length) return; + if(!(await wpConfirmDialog({title: ask.title, okLabel: ask.ok, message: ask.message(ids.length)}))) return; + bannerHtml('bulk-banner', '', uesc(ask.title)+': working on '+uesc(people(ids.length))+'…'); + const { status, json } = await api('POST', path, Object.assign({ user_ids: ids }, body)); + if(status !== 200 || !json || !Array.isArray(json.results)){ + bannerHtml('bulk-banner', 'bad', '✕ '+uesc(apiError(status, json, ask.title+' failed'))+' Nothing was changed.'); + return; + } + showBulkResult(json, ask.title); + loadUsers(); +} + +// Every row is accounted for: changed, already so, or failed with the server's +// reason. A batch that partly failed never reads as a success (CR-020). +function showBulkResult(json, title){ + const failed = json.results.filter(r => r.status === 'failed'); + const parts = [json.ok+' changed']; + if(json.unchanged) parts.push(json.unchanged+' already set'); + if(failed.length) parts.push(failed.length+' failed'); + const kind = !failed.length ? 'ok' : (json.ok + json.unchanged ? 'warn' : 'bad'); + const lead = (failed.length ? (kind === 'bad' ? '✕ ' : '⚠ ') : '✓ ')+uesc(title)+': '+parts.join(', ')+'.'; + const list = failed.length + ? '' + : ''; + bannerHtml('bulk-banner', kind, lead + list); +} + +function bannerHtml(id, kind, html){ + const el = document.getElementById(id); + if(!el) return; + el.className = 'banner' + (kind ? ' '+kind : ''); + el.innerHTML = html; +} + +function bulkRole(){ + const role = selVal('bulk-role'); + if(!role) return; + return runBulk('/api/auth/bulk-users/role', { role }, { + title: 'Set permissions', ok: 'Change permissions', + message: n => 'Set permissions to '+(PERM_LABELS[role] || role)+' for '+people(n)+'?\n\n'+ + 'Each account is checked on its own. Any that cannot be changed are listed afterwards.'}); +} + +function bulkActive(on){ + return runBulk('/api/auth/bulk-users/active', { is_active: !!on }, { + title: on ? 'Enable accounts' : 'Disable accounts', ok: on ? 'Enable' : 'Disable', + message: n => (on ? 'Enable sign-in for ' : 'Disable sign-in for ')+people(n)+'?'+ + (on ? '' : '\n\nA disabled account cannot sign in. Nothing else about it changes.')}); +} + +function bulkProject(op){ + const pid = selVal('bulk-project'); + if(!pid) return; + const role = op === 'add' ? selVal('bulk-project-role') : ''; + const pname = selText('bulk-project'); + return runBulk('/api/auth/bulk-users/projects', { project_id: pid, op, role }, { + title: op === 'add' ? 'Add to project' : 'Remove from project', + ok: op === 'add' ? 'Add' : 'Remove', + message: n => op === 'add' + ? 'Add '+people(n)+' to '+pname+' with '+(role ? PERM_LABELS[role]+' on that project' : 'their account role')+'?\n\n'+ + 'Their other projects, and their roles there, stay as they are.' + : 'Remove '+people(n)+' from '+pname+'?\n\nTheir other projects stay as they are.'}); +} + function selCell(u){ if(!selectable(u)){ const why = u.id === _meId ? 'Your own account: change it one setting at a time'