admin) User management admin page (#35)
All checks were successful
Build and Push Image / docker-build-and-push (push) Successful in 2m20s
All checks were successful
Build and Push Image / docker-build-and-push (push) Successful in 2m20s
Reviewed-on: #35 Reviewed-by: shaun <shaun@campbellwireless.net> Co-authored-by: AI Agent <ai-agent@campbellwireless.net> Co-committed-by: AI Agent <ai-agent@campbellwireless.net>
This commit was merged in pull request #35.
This commit is contained in:
138
src/lib/server/users.ts
Normal file
138
src/lib/server/users.ts
Normal file
@@ -0,0 +1,138 @@
|
||||
import { db } from './db/index.js';
|
||||
|
||||
export interface AppUser {
|
||||
id: string;
|
||||
username: string;
|
||||
full_name: string;
|
||||
email: string | null;
|
||||
auth_source: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
const DEFAULT_AUTH_SOURCE = 'OIDC - Synology';
|
||||
|
||||
const normalizeOptional = (value?: string | null): string | undefined => {
|
||||
const trimmed = value?.trim();
|
||||
return trimmed ? trimmed : undefined;
|
||||
};
|
||||
|
||||
const normalizeEmail = (value?: string | null): string | null | undefined => {
|
||||
if (value === undefined) return undefined;
|
||||
if (value === null) return null;
|
||||
const trimmed = value.trim();
|
||||
return trimmed ? trimmed : null;
|
||||
};
|
||||
|
||||
export function listUsers(): AppUser[] {
|
||||
return db.all<AppUser>(
|
||||
`SELECT id, username, full_name, email, auth_source, created_at, updated_at
|
||||
FROM users
|
||||
ORDER BY username COLLATE NOCASE`
|
||||
);
|
||||
}
|
||||
|
||||
export function upsertUserFromAuth(input: {
|
||||
id: string;
|
||||
username?: string | null;
|
||||
fullName?: string | null;
|
||||
email?: string | null;
|
||||
authSource?: string | null;
|
||||
}): AppUser {
|
||||
const existing = db.get<AppUser>('SELECT * FROM users WHERE id = ?', [input.id]);
|
||||
const username = normalizeOptional(input.username) ?? existing?.username ?? input.id;
|
||||
const fullName = normalizeOptional(input.fullName) ?? existing?.full_name ?? '';
|
||||
const email = normalizeOptional(input.email) ?? existing?.email ?? null;
|
||||
const authSource =
|
||||
normalizeOptional(input.authSource) ?? existing?.auth_source ?? DEFAULT_AUTH_SOURCE;
|
||||
|
||||
if (existing) {
|
||||
const setClauses: string[] = [];
|
||||
const values: Array<string | null> = [];
|
||||
if (username !== existing.username) {
|
||||
setClauses.push('username = ?');
|
||||
values.push(username);
|
||||
}
|
||||
if (fullName !== existing.full_name) {
|
||||
setClauses.push('full_name = ?');
|
||||
values.push(fullName);
|
||||
}
|
||||
if (email !== existing.email) {
|
||||
setClauses.push('email = ?');
|
||||
values.push(email);
|
||||
}
|
||||
if (authSource !== existing.auth_source) {
|
||||
setClauses.push('auth_source = ?');
|
||||
values.push(authSource);
|
||||
}
|
||||
if (setClauses.length > 0) {
|
||||
setClauses.push("updated_at = datetime('now')");
|
||||
values.push(input.id);
|
||||
db.run(`UPDATE users SET ${setClauses.join(', ')} WHERE id = ?`, values);
|
||||
}
|
||||
} else {
|
||||
db.run(
|
||||
`INSERT INTO users (id, username, full_name, email, auth_source)
|
||||
VALUES (?, ?, ?, ?, ?)`,
|
||||
[input.id, username, fullName, email, authSource]
|
||||
);
|
||||
}
|
||||
return db.get<AppUser>('SELECT * FROM users WHERE id = ?', [input.id])!;
|
||||
}
|
||||
|
||||
export function upsertUserProfile(
|
||||
userId: string,
|
||||
fullName: string,
|
||||
email?: string | null
|
||||
): AppUser {
|
||||
return upsertUserFromAuth({ id: userId, fullName, email, authSource: DEFAULT_AUTH_SOURCE });
|
||||
}
|
||||
|
||||
export function updateUser(input: {
|
||||
id: string;
|
||||
username: string;
|
||||
fullName: string;
|
||||
email?: string | null;
|
||||
}): void {
|
||||
const existing = db.get<{ email: string | null }>('SELECT email FROM users WHERE id = ?', [
|
||||
input.id
|
||||
]);
|
||||
const email = normalizeEmail(input.email);
|
||||
const nextEmail = email === undefined ? existing?.email ?? null : email;
|
||||
db.run(
|
||||
`UPDATE users
|
||||
SET username = ?, full_name = ?, email = ?, updated_at = datetime('now')
|
||||
WHERE id = ?`,
|
||||
[input.username.trim(), input.fullName.trim(), nextEmail, input.id]
|
||||
);
|
||||
syncSelfProfile(input.id, input.fullName, nextEmail);
|
||||
}
|
||||
|
||||
export function deleteUser(id: string): void {
|
||||
db.run('DELETE FROM users WHERE id = ?', [id]);
|
||||
}
|
||||
|
||||
function syncSelfProfile(userId: string, fullName: string, email?: string | null): void {
|
||||
const profile = db.get<{ id: string; first_name: string; last_name: string; email: string | null }>(
|
||||
`SELECT id, first_name, last_name, email FROM people WHERE user_id = ? AND is_self = 1 LIMIT 1`,
|
||||
[userId]
|
||||
);
|
||||
if (!profile) return;
|
||||
|
||||
const trimmed = fullName.trim();
|
||||
let firstName = profile.first_name;
|
||||
let lastName = profile.last_name;
|
||||
if (trimmed) {
|
||||
const parts = trimmed.split(/\s+/);
|
||||
firstName = parts[0] ?? profile.first_name;
|
||||
if (parts.length > 1) {
|
||||
lastName = parts.slice(1).join(' ');
|
||||
}
|
||||
}
|
||||
const nextEmail = email === undefined ? profile.email : email;
|
||||
db.run(
|
||||
`UPDATE people SET first_name = ?, last_name = ?, email = ?, updated_at = datetime('now')
|
||||
WHERE id = ?`,
|
||||
[firstName, lastName, nextEmail, profile.id]
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user