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.
This commit is contained in:
2026-10-06 11:02:09 -07:00
parent cf46b3ce42
commit 681048197a

View File

@@ -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) => '<div class="bulkgroup" role="group" aria-label="'+uesc(label)+'">'+
'<span class="bulklabel">'+uesc(label)+'</span>'+inner+'</div>';
box.innerHTML =
group('Permissions',
'<select id="bulk-role" aria-label="New permissions role">'+
grantable.map(r => '<option value="'+r+'"'+(r==='project_user'?' selected':'')+'>'+uesc(PERM_LABELS[r])+'</option>').join('')+
'</select>'+
'<button type="button" onclick="bulkRole()">Set permissions</button>') +
group('Sign-in',
'<button type="button" onclick="bulkActive(true)">Enable</button>'+
'<button type="button" onclick="bulkActive(false)">Disable</button>') +
(projects.length ? group('Project access',
'<select id="bulk-project" aria-label="Project">'+
projects.map(p => '<option value="'+uesc(p.id)+'">'+uesc((p.name||'(unnamed)')+(p.number?' '+p.number:'')+
(p.archived?' (archived)':''))+'</option>').join('')+
'</select>'+
'<select id="bulk-project-role" aria-label="Role on that project, for Add">'+
'<option value="">Role: same as account</option>'+
projRoles.map(r => '<option value="'+r+'">Role: '+uesc(PERM_LABELS[r])+' here</option>').join('')+
'</select>'+
'<button type="button" onclick="bulkProject(\'add\')">Add to project</button>'+
'<button type="button" onclick="bulkProject(\'remove\')">Remove from project</button>') : '');
}
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
? '<ul>'+failed.map(r => '<li><strong>'+uesc(r.username || r.user_id)+'</strong>: '+uesc(r.reason)+'</li>').join('')+'</ul>'
: '';
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'