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:
@@ -1387,6 +1387,77 @@ def bulk_set_project(body: BulkProjectIn, actor: models.User = Depends(require_u
|
||||
return run_bulk(db, actor, body.user_ids, "project_" + body.op, apply)
|
||||
|
||||
|
||||
class BulkResolveIn(BaseModel):
|
||||
text: str = Field(default="", max_length=200_000)
|
||||
|
||||
|
||||
_CSV_HEADER_WORDS = ("username", "user", "login", "email", "user name")
|
||||
|
||||
|
||||
@app.post("/api/auth/bulk-users/resolve")
|
||||
def bulk_resolve(body: BulkResolveIn, actor: models.User = Depends(require_user_manager), db: Session = Depends(get_db)):
|
||||
"""CR-020 (T12.4): turn an uploaded list of usernames into the account ids a
|
||||
bulk action will run on - a second way IN to the same four bulk routes, not a
|
||||
third copy of their rules. The CSV holds usernames only (decided 2026-10-06);
|
||||
the action is chosen in the console and applies to every accepted row.
|
||||
|
||||
Validate and report, the CR-005 pattern: every line is either accepted or
|
||||
rejected WITH ITS LINE NUMBER and a reason - never silently skipped. A line is
|
||||
rejected when it names nobody this caller can see (the same 'not found' a
|
||||
hidden account gets everywhere else), an account they may not change (with
|
||||
the same reason the single route would give), their own account, or a
|
||||
username already listed on an earlier line. Nothing is changed here; the
|
||||
bulk route the console calls next re-checks every row anyway."""
|
||||
rows = []
|
||||
seen: dict[str, int] = {}
|
||||
visible = visible_user_ids(db, actor)
|
||||
cache: dict = {}
|
||||
for line_no, raw in enumerate((body.text or "").splitlines(), start=1):
|
||||
line = raw.strip().lstrip("")
|
||||
if not line:
|
||||
continue
|
||||
cell = re.split(r"[,;\t]", line, maxsplit=1)[0].strip().strip('"').strip()
|
||||
if not cell:
|
||||
rows.append({"line": line_no, "text": line[:120], "status": "rejected",
|
||||
"reason": "no username in the first column"})
|
||||
continue
|
||||
if line_no == 1 and cell.lower() in _CSV_HEADER_WORDS:
|
||||
continue # header row
|
||||
row = {"line": line_no, "text": cell[:120], "status": "rejected", "reason": "",
|
||||
"user_id": None, "username": ""}
|
||||
if any(ord(ch) < 32 for ch in cell) or len(cell) > 120:
|
||||
row["reason"] = "not a valid username"
|
||||
rows.append(row)
|
||||
continue
|
||||
key = cell.lower()
|
||||
if key in seen:
|
||||
row["reason"] = f"duplicate of line {seen[key]}"
|
||||
rows.append(row)
|
||||
continue
|
||||
seen[key] = line_no
|
||||
u = db.scalars(select(models.User).where(func.lower(models.User.username) == key)).first()
|
||||
if u is None:
|
||||
# An offboarding list usually carries email addresses. Accepted only
|
||||
# when exactly one account has that address, so a shared or reused
|
||||
# address can never pick the wrong person.
|
||||
by_mail = db.scalars(select(models.User).where(func.lower(models.User.email) == key)).all()
|
||||
u = by_mail[0] if len(by_mail) == 1 else None
|
||||
if u is None or (visible is not None and u.id not in visible):
|
||||
row["reason"] = "User not found"
|
||||
elif u.id == actor.id:
|
||||
row["reason"] = "your own account - change it one setting at a time"
|
||||
else:
|
||||
problem = manage_user_problem(db, actor, u, cache)
|
||||
if problem:
|
||||
row["reason"] = problem
|
||||
else:
|
||||
row.update(status="ok", user_id=u.id, username=u.username)
|
||||
rows.append(row)
|
||||
accepted = [r for r in rows if r["status"] == "ok"]
|
||||
return {"read": len(rows), "accepted": len(accepted), "rejected": len(rows) - len(accepted),
|
||||
"user_ids": [r["user_id"] for r in accepted], "rows": rows}
|
||||
|
||||
|
||||
@app.post("/api/auth/bulk-users/delete")
|
||||
def bulk_delete(body: BulkUsersIn, actor: models.User = Depends(require_user_manager), db: Session = Depends(get_db)):
|
||||
"""Hard delete, the same as DELETE /api/auth/users/{id} (decided 2026-09-17:
|
||||
|
||||
Reference in New Issue
Block a user