v1.5 - history and timeline as well as many datastructure updates added, polished, fixes
This commit is contained in:
parent
1f0aa3078f
commit
1d84400aef
63 changed files with 7927 additions and 208 deletions
|
|
@ -9,22 +9,45 @@
|
|||
DB_PATH=/var/lib/ngu/ngu.db node src/admin-cli.js add you@ngu.org
|
||||
DB_PATH=/var/lib/ngu/ngu.db node src/admin-cli.js list
|
||||
DB_PATH=/var/lib/ngu/ngu.db node src/admin-cli.js passwd you@ngu.org
|
||||
DB_PATH=/var/lib/ngu/ngu.db node src/admin-cli.js role them@ngu.org editor
|
||||
DB_PATH=/var/lib/ngu/ngu.db node src/admin-cli.js disable them@ngu.org
|
||||
DB_PATH=/var/lib/ngu/ngu.db node src/admin-cli.js enable them@ngu.org
|
||||
|
||||
add takes --role=viewer for read-only, --name="Full Name".
|
||||
Press enter at the password prompt and it generates one and
|
||||
prints it once.
|
||||
Roles, low to high. Each one can do everything the one above it
|
||||
in this list can:
|
||||
|
||||
Changing or disabling a password also drops that person's live
|
||||
sessions, so "disable" takes effect now rather than in 30 days.
|
||||
viewer read the CMS, change nothing
|
||||
editor + create and update records
|
||||
admin + delete records
|
||||
superadmin + accounts, roles and sessions, via /admin/panel
|
||||
|
||||
add takes --role=editor, --name="Full Name". It defaults to
|
||||
admin. Press enter at the password prompt and it generates one
|
||||
and prints it once.
|
||||
|
||||
The list comes from auth.js rather than being repeated here, so
|
||||
the CLI can't drift from what requireRole will actually accept.
|
||||
|
||||
This is also how the first superadmin is made — there's no
|
||||
bootstrap path in the web interface, on purpose:
|
||||
|
||||
node src/admin-cli.js role you@ngu.org superadmin
|
||||
|
||||
Changing a password, a role, or disabling an account drops that
|
||||
person's live sessions, so it takes effect now rather than in
|
||||
30 days.
|
||||
|
||||
Nothing here refuses to demote or disable the last superadmin.
|
||||
The panel does, because a misclick there locks everyone out;
|
||||
here you're already root on the box holding the database, and a
|
||||
recovery tool that argues with you isn't one.
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
import { createInterface } from "node:readline";
|
||||
import { randomBytes } from "node:crypto";
|
||||
|
||||
import { openDatabase, migrate } from "./db.js";
|
||||
import { hashPassword, destroyAllSessionsFor } from "./auth.js";
|
||||
import { hashPassword, destroyAllSessionsFor, ROLES } from "./auth.js";
|
||||
|
||||
const MIN_PASSWORD = 12;
|
||||
|
||||
|
|
@ -86,11 +109,37 @@ function findUser(db, email) {
|
|||
.get(email);
|
||||
}
|
||||
|
||||
function checkRole(role) {
|
||||
if (!ROLES.includes(role)) {
|
||||
fail(`Role must be one of: ${ROLES.join(", ")}.`);
|
||||
}
|
||||
return role;
|
||||
}
|
||||
|
||||
/* Printed, never enforced — see the note at the top of the file.
|
||||
Worth saying out loud, because the person doing it is usually
|
||||
tidying up accounts rather than thinking about lockouts. */
|
||||
function warnIfLastSuper(db, user) {
|
||||
if (user.role !== "superadmin" || user.is_active !== 1) return;
|
||||
|
||||
const { n } = db
|
||||
.prepare(
|
||||
"SELECT COUNT(*) AS n FROM admin_users WHERE role = 'superadmin' AND is_active = 1",
|
||||
)
|
||||
.get();
|
||||
|
||||
if (n <= 1) {
|
||||
console.warn(
|
||||
"⚠ That's the last active superadmin. Nobody will be able to manage\n" +
|
||||
" accounts from /admin/panel until you promote someone here.",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function add(db, email, flags) {
|
||||
if (findUser(db, email)) fail(`${email} already exists. Use passwd to change it.`);
|
||||
|
||||
const role = flags.role ?? "admin";
|
||||
if (!["admin", "viewer"].includes(role)) fail("Role must be admin or viewer.");
|
||||
const role = checkRole(flags.role ?? "admin");
|
||||
|
||||
const password = await readPassword();
|
||||
|
||||
|
|
@ -117,10 +166,36 @@ async function passwd(db, email) {
|
|||
console.log(`✓ password changed for ${email}, existing sessions ended`);
|
||||
}
|
||||
|
||||
function setRole(db, email, role) {
|
||||
const user = findUser(db, email);
|
||||
if (!user) fail(`No account for ${email}.`);
|
||||
|
||||
checkRole(role);
|
||||
|
||||
if (user.role === role) {
|
||||
console.log(`· ${email} is already ${role}, nothing to do`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Only a demotion can strand the account list.
|
||||
if (role !== "superadmin") warnIfLastSuper(db, user);
|
||||
|
||||
db.prepare("UPDATE admin_users SET role = ? WHERE id = ?").run(role, user.id);
|
||||
|
||||
// The session they're holding was issued against the old role.
|
||||
// Every check reads the row fresh, so it isn't a security hole —
|
||||
// but their open tab would keep drawing buttons that now 403.
|
||||
destroyAllSessionsFor(db, user.id);
|
||||
|
||||
console.log(`✓ ${email} is now ${role} (was ${user.role}), sessions ended`);
|
||||
}
|
||||
|
||||
function setActive(db, email, active) {
|
||||
const user = findUser(db, email);
|
||||
if (!user) fail(`No account for ${email}.`);
|
||||
|
||||
if (!active) warnIfLastSuper(db, user);
|
||||
|
||||
db.prepare("UPDATE admin_users SET is_active = ? WHERE id = ?").run(
|
||||
active ? 1 : 0,
|
||||
user.id,
|
||||
|
|
@ -147,10 +222,13 @@ function list(db) {
|
|||
}
|
||||
|
||||
for (const r of rows) {
|
||||
const state = r.is_active ? r.role : "disabled";
|
||||
// Role and state are separate facts now. The old single column
|
||||
// printed "disabled" over the top of the role, which hid what
|
||||
// the account would go back to on enable.
|
||||
const state = r.is_active ? r.role : `${r.role} (disabled)`;
|
||||
const seen = r.last_login_at ?? "never";
|
||||
console.log(
|
||||
`${r.email.padEnd(32)} ${state.padEnd(9)} last login ${seen.padEnd(20)} ${r.sessions} session(s)`,
|
||||
`${r.email.padEnd(32)} ${state.padEnd(22)} last login ${seen.padEnd(20)} ${r.sessions} session(s)`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -175,24 +253,38 @@ migrate(db); // so a fresh database gets the tables before we use them
|
|||
try {
|
||||
switch (command) {
|
||||
case "add":
|
||||
if (!email) fail("Usage: admin-cli.js add email@example.com");
|
||||
if (!email) fail("Usage: admin-cli.js add email@example.com [--role=editor]");
|
||||
await add(db, email, flags);
|
||||
break;
|
||||
case "passwd":
|
||||
if (!email) fail("Usage: admin-cli.js passwd email@example.com");
|
||||
await passwd(db, email);
|
||||
break;
|
||||
case "role": {
|
||||
// Positional reads better for a two-argument command, but
|
||||
// --role= is what `add` takes, so accept both rather than
|
||||
// making people remember which is which.
|
||||
const role = positional[1]?.trim().toLowerCase() ?? flags.role;
|
||||
if (!email || !role) {
|
||||
fail(`Usage: admin-cli.js role email@example.com <${ROLES.join("|")}>`);
|
||||
}
|
||||
setRole(db, email, role);
|
||||
break;
|
||||
}
|
||||
case "disable":
|
||||
if (!email) fail("Usage: admin-cli.js disable email@example.com");
|
||||
setActive(db, email, false);
|
||||
break;
|
||||
case "enable":
|
||||
if (!email) fail("Usage: admin-cli.js enable email@example.com");
|
||||
setActive(db, email, true);
|
||||
break;
|
||||
case "list":
|
||||
list(db);
|
||||
break;
|
||||
default:
|
||||
console.log("Commands: add, passwd, disable, enable, list");
|
||||
console.log("Commands: add, passwd, role, disable, enable, list");
|
||||
console.log(`Roles: ${ROLES.join(", ")}`);
|
||||
process.exit(command ? 1 : 0);
|
||||
}
|
||||
} finally {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue