Sysop Permissions

This commit is contained in:
Brian Fertig 2026-11-18 18:41:57 -07:00
parent 602fe38258
commit 3139f7723e
6 changed files with 58 additions and 8 deletions

4
.dockerignore Normal file
View File

@ -0,0 +1,4 @@
node_modules
dist
.env
.git

19
.env.example Normal file
View File

@ -0,0 +1,19 @@
# Copy to .env and change the password before deploying.
POSTGRES_USER=bbs
POSTGRES_PASSWORD=change-me
POSTGRES_DB=bbs
# Host port the BBS web UI is published on
PORT=8080
# Simulated modem speed for server output (bps). 0 = no throttling.
BAUD=14400
# Number of simultaneous callers ("nodes")
MAX_NODES=7
# Hang up on idle callers after this many minutes
IDLE_TIMEOUT_MIN=10
# Timezone for BBS clocks / caller logs
TZ=America/Denver

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
node_modules
dist
.env

View File

@ -1,7 +1,7 @@
import type { Session } from '../session/Session'; import type { Session } from '../session/Session';
import { esc, padRight, center } from '../session/ansi'; import { esc, padRight, center } from '../session/ansi';
import { nodes } from '../nodes'; import { nodes } from '../nodes';
import { lastCallers } from '../users/userRepo'; import { lastCallers, SYSOP_LEVEL } from '../users/userRepo';
import { config } from '../config'; import { config } from '../config';
const W = 78; // inner box width const W = 78; // inner box width
@ -20,7 +20,7 @@ function minutesOn(s: Session): number {
} }
function securityLabel(level: number): string { function securityLabel(level: number): string {
if (level >= 255) return 'Sysop'; if (level >= SYSOP_LEVEL) return 'Sysop';
if (level >= 100) return 'Co-Sysop'; if (level >= 100) return 'Co-Sysop';
if (level >= 50) return 'Elite'; if (level >= 50) return 'Elite';
if (level >= 20) return 'Validated'; if (level >= 20) return 'Validated';

View File

@ -1,5 +1,5 @@
import type { Session } from '../session/Session'; import type { Session } from '../session/Session';
import { createUser, handleExists, HandleTakenError, type User } from '../users/userRepo'; import { createUser, handleExists, HandleTakenError, isSysop, type User } from '../users/userRepo';
import { esc } from '../session/ansi'; import { esc } from '../session/ansi';
const HANDLE_RE = /^[A-Za-z0-9][A-Za-z0-9 _.\-]{1,18}[A-Za-z0-9_.\-]$/; const HANDLE_RE = /^[A-Za-z0-9][A-Za-z0-9 _.\-]{1,18}[A-Za-z0-9_.\-]$/;
@ -89,7 +89,12 @@ export async function newUser(s: Session): Promise<User | null> {
try { try {
const user = await createUser({ handle, password, realName, location, email }); const user = await createUser({ handle, password, realName, location, email });
if (isSysop(user)) {
await s.println('\r\n |10Account created! |14You are the first user on this system,');
await s.println(' |14so you have been made the |15SYSOP|14. |07The board is yours.');
} else {
await s.println('\r\n |10Account created! |07Your access level is |15New User|07.'); await s.println('\r\n |10Account created! |07Your access level is |15New User|07.');
}
return user; return user;
} catch (err) { } catch (err) {
if (err instanceof HandleTakenError) { if (err instanceof HandleTakenError) {

View File

@ -24,6 +24,10 @@ export interface NewUser {
export class HandleTakenError extends Error {} export class HandleTakenError extends Error {}
/** Security levels. Sysop-only features should check isSysop(). */
export const NEW_USER_LEVEL = 10;
export const SYSOP_LEVEL = 255;
// Used so a login attempt for an unknown handle costs the same as a real one. // Used so a login attempt for an unknown handle costs the same as a real one.
const DUMMY_HASH = bcrypt.hashSync('not-a-real-password', 10); const DUMMY_HASH = bcrypt.hashSync('not-a-real-password', 10);
@ -42,21 +46,36 @@ export async function verifyLogin(handle: string, password: string): Promise<Use
return user && ok ? user : null; return user && ok ? user : null;
} }
/** The very first account created on the system becomes the Sysop. */
export async function createUser(u: NewUser): Promise<User> { export async function createUser(u: NewUser): Promise<User> {
const hash = await bcrypt.hash(u.password, 10); const hash = await bcrypt.hash(u.password, 10);
const client = await pool.connect();
try { try {
const { rows } = await pool.query<User>( await client.query('BEGIN');
`INSERT INTO users (handle, password_hash, real_name, location, email) // Serialise signups so two simultaneous "first" users can't both become Sysop.
VALUES ($1, $2, $3, $4, $5) RETURNING *`, await client.query('SELECT pg_advisory_xact_lock(727275)');
[u.handle, hash, u.realName, u.location, u.email], const { rows } = await client.query<User>(
`INSERT INTO users (handle, password_hash, real_name, location, email, security_level)
VALUES ($1, $2, $3, $4, $5,
CASE WHEN EXISTS (SELECT 1 FROM users) THEN $6::int ELSE $7::int END)
RETURNING *`,
[u.handle, hash, u.realName, u.location, u.email, NEW_USER_LEVEL, SYSOP_LEVEL],
); );
await client.query('COMMIT');
return rows[0]; return rows[0];
} catch (err: unknown) { } catch (err: unknown) {
await client.query('ROLLBACK');
if ((err as { code?: string }).code === '23505') throw new HandleTakenError(); if ((err as { code?: string }).code === '23505') throw new HandleTakenError();
throw err; throw err;
} finally {
client.release();
} }
} }
export function isSysop(user: User): boolean {
return user.security_level >= SYSOP_LEVEL;
}
/** Records a successful logon. Returns the system caller number and the refreshed user row. */ /** Records a successful logon. Returns the system caller number and the refreshed user row. */
export async function recordCall(userId: number, node: number): Promise<{ callerNo: number; user: User }> { export async function recordCall(userId: number, node: number): Promise<{ callerNo: number; user: User }> {
const client = await pool.connect(); const client = await pool.connect();