T12.4: CR-020 CSV upload (usernames only)

POST /api/auth/bulk-users/resolve reads a list of usernames (first
column; comma, semicolon or tab; header skipped) and answers line by
line, the CR-005 validate-and-report pattern. A line is rejected, with
its number and reason, when it names nobody the caller can see ('User
not found', as everywhere else), an account they may not change (the
single route's own reason), their own account, or a username already
listed (duplicate of line N). An email address resolves only when
exactly one account has it. Nothing is changed by this route.

The console's 'Select from CSV' turns the accepted accounts into the
ordinary selection (filter reset so every selected row is visible) and
lists every rejected line. The action, confirm and per-row report are
the checkbox ones, through the same T12.1 routes, which re-check each
row: a CSV cannot reach an account a checkbox could not.
Usernames only, action in the toolbar (decided 2026-10-06). Files over
1 MB are refused before upload; FileReader fallback for browsers
without Blob.text().

bulk_users_check.py: 61/61. DOM harness: 40/40.
This commit is contained in:
2026-10-06 11:04:01 -07:00
parent 681048197a
commit 74d3bc4af7
4 changed files with 197 additions and 4 deletions

View File

@@ -184,6 +184,8 @@ document.addEventListener('change', e => {
// can only fail.
function renderBulkActions(){
const box = document.getElementById('bulk-actions');
const csvBtn = document.getElementById('csv-btn');
if(csvBtn) csvBtn.hidden = !manages();
if(!box || !manages()) return;
const grantable = PERM_ROLES.filter(r => (_scope.grantable_roles || []).indexOf(r) >= 0);
const projects = (_scope.managed_projects || []).slice()
@@ -290,6 +292,70 @@ function bulkProject(op){
: 'Remove '+people(n)+' from '+pname+'?\n\nTheir other projects stay as they are.'});
}
// ── CSV upload (CR-020, T12.4) ────────────────────────────────────────────────
// The file holds usernames only (decided 2026-10-06). The server reads it and
// answers line by line; the accepted accounts become the ordinary selection, so
// the action, its confirmation and its row-by-row report are the same ones the
// checkboxes use. A CSV cannot reach an account a checkbox could not: the bulk
// route re-checks every row.
const CSV_MAX_BYTES = 1024 * 1024;
// Blob.text() where the browser has it, FileReader where it does not (older
// tablet browsers).
function readFileText(file){
if(typeof file.text === 'function') return file.text();
return new Promise((resolve, reject) => {
const r = new FileReader();
r.onload = () => resolve(String(r.result || ''));
r.onerror = () => reject(r.error);
r.readAsText(file);
});
}
function pickCsv(){
const f = document.getElementById('csv-file');
if(f){ f.value = ''; f.click(); }
}
document.addEventListener('change', e => {
if(e.target && e.target.id === 'csv-file' && e.target.files && e.target.files[0]) loadCsv(e.target.files[0]);
});
async function loadCsv(file){
if(file.size > CSV_MAX_BYTES){
bannerHtml('csv-banner', 'bad', '✕ '+uesc(file.name)+' is larger than 1 MB. Split it and load the parts one at a time.');
return;
}
let text = '';
try { text = await readFileText(file); }
catch(_){ bannerHtml('csv-banner', 'bad', '✕ Could not read '+uesc(file.name)+'.'); return; }
bannerHtml('csv-banner', '', 'Reading '+uesc(file.name)+'…');
const { status, json } = await api('POST', '/api/auth/bulk-users/resolve', { text });
if(status !== 200 || !json || !Array.isArray(json.rows)){
bannerHtml('csv-banner', 'bad', '✕ '+uesc(apiError(status, json, 'Could not read the list')));
return;
}
// Show everyone, so every accepted account is a visible, checked row: a
// selection must never include a row the filter hides.
const s = document.getElementById('user-search'), f = document.getElementById('user-filter');
if(s) s.value = '';
if(f) f.value = '';
await loadUsers();
const want = new Set(json.user_ids || []);
_shownSelectable.forEach(id => { if(want.has(id)) _selected.add(id); });
updateSelectionUI();
const missed = [...want].filter(id => !_selected.has(id)).length;
const rejected = json.rows.filter(r => r.status !== 'ok');
const lead = (rejected.length || missed ? '⚠ ' : '✓ ')+uesc(file.name)+': '+json.read+' line'+(json.read===1?'':'s')+' read, '+
_selected.size+' selected'+(rejected.length ? ', '+rejected.length+' rejected' : '')+'.'+
(missed ? ' '+missed+' accepted account'+(missed===1?' is':'s are')+' no longer in the directory; reload and try again.' : '')+
(_selected.size ? ' Pick an action in the bulk bar to apply it to the selected accounts.' : '');
const list = rejected.length
? '<ul>'+rejected.map(r => '<li>Line '+r.line+' ('+uesc(r.text)+'): '+uesc(r.reason)+'</li>').join('')+'</ul>'
: '';
bannerHtml('csv-banner', (rejected.length || missed) ? 'warn' : 'ok', lead + list);
}
function selCell(u){
if(!selectable(u)){
const why = u.id === _meId ? 'Your own account: change it one setting at a time'