admin) manage local login users
Some checks failed
PR Checks / lint-test-and-docker-build (pull_request) Failing after 1m2s
Some checks failed
PR Checks / lint-test-and-docker-build (pull_request) Failing after 1m2s
This commit is contained in:
@@ -2,8 +2,10 @@ import { beforeEach, afterEach, describe, expect, it } from 'vitest';
|
||||
import { setupTestDb } from '../../tests/helpers.js';
|
||||
import type { Database } from './db/types.js';
|
||||
import {
|
||||
createLocalUser,
|
||||
deleteUser,
|
||||
listUsers,
|
||||
setLocalPasswordForUser,
|
||||
updateUser,
|
||||
upsertUserFromAuth,
|
||||
upsertUserProfile
|
||||
@@ -100,3 +102,50 @@ describe('deleteUser', () => {
|
||||
expect(listUsers().find((item) => item.id === 'u4')).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('createLocalUser', () => {
|
||||
it('creates a local user with credentials', async () => {
|
||||
const created = await createLocalUser({
|
||||
username: 'local-user',
|
||||
fullName: 'Local User',
|
||||
email: 'local@example.com',
|
||||
password: 'averysecurepassword'
|
||||
});
|
||||
expect(created.username).toBe('local-user');
|
||||
expect(created.auth_source).toBe('Local');
|
||||
const row = database.get<{ user_id: string; password_hash: string }>(
|
||||
'SELECT user_id, password_hash FROM local_credentials WHERE user_id = ?',
|
||||
[created.id]
|
||||
);
|
||||
expect(row?.user_id).toBe(created.id);
|
||||
expect(row?.password_hash).toBeTruthy();
|
||||
});
|
||||
|
||||
it('rejects duplicate usernames', async () => {
|
||||
await createLocalUser({
|
||||
username: 'dup-user',
|
||||
fullName: 'Dup User',
|
||||
password: 'averysecurepassword'
|
||||
});
|
||||
await expect(
|
||||
createLocalUser({
|
||||
username: 'dup-user',
|
||||
fullName: 'Dup User Two',
|
||||
password: 'averysecurepassword'
|
||||
})
|
||||
).rejects.toThrow('Username already exists');
|
||||
});
|
||||
});
|
||||
|
||||
describe('setLocalPasswordForUser', () => {
|
||||
it('sets local credentials for an existing user', async () => {
|
||||
upsertUserFromAuth({ id: 'u5', username: 'sarah', fullName: 'Sarah Lee' });
|
||||
await setLocalPasswordForUser('u5', 'averysecurepassword');
|
||||
const row = database.get<{ user_id: string; password_hash: string }>(
|
||||
'SELECT user_id, password_hash FROM local_credentials WHERE user_id = ?',
|
||||
['u5']
|
||||
);
|
||||
expect(row?.user_id).toBe('u5');
|
||||
expect(row?.password_hash).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { randomUUID } from 'crypto';
|
||||
import { db } from './db/index.js';
|
||||
import { setLocalCredentialPassword } from './local-credentials.js';
|
||||
|
||||
export interface AppUser {
|
||||
id: string;
|
||||
@@ -11,6 +13,7 @@ export interface AppUser {
|
||||
}
|
||||
|
||||
const DEFAULT_AUTH_SOURCE = 'OIDC - Synology';
|
||||
const LOCAL_AUTH_SOURCE = 'Local';
|
||||
|
||||
const normalizeOptional = (value?: string | null): string | undefined => {
|
||||
const trimmed = value?.trim();
|
||||
@@ -112,6 +115,56 @@ export function deleteUser(id: string): void {
|
||||
db.run('DELETE FROM users WHERE id = ?', [id]);
|
||||
}
|
||||
|
||||
export async function createLocalUser(input: {
|
||||
username: string;
|
||||
fullName: string;
|
||||
email?: string | null;
|
||||
password: string;
|
||||
}): Promise<AppUser> {
|
||||
const username = input.username?.trim() ?? '';
|
||||
const fullName = input.fullName?.trim() ?? '';
|
||||
if (!username || !fullName) {
|
||||
throw new Error('Username and full name are required');
|
||||
}
|
||||
const email = normalizeEmail(input.email);
|
||||
const existingUsername = db.get<{ id: string }>(
|
||||
'SELECT id FROM users WHERE lower(username) = lower(?)',
|
||||
[username]
|
||||
);
|
||||
if (existingUsername) {
|
||||
throw new Error('Username already exists');
|
||||
}
|
||||
if (email) {
|
||||
const existingEmail = db.get<{ id: string }>(
|
||||
'SELECT id FROM users WHERE lower(email) = lower(?)',
|
||||
[email]
|
||||
);
|
||||
if (existingEmail) {
|
||||
throw new Error('Email already exists');
|
||||
}
|
||||
}
|
||||
const id = randomUUID();
|
||||
db.run(
|
||||
`INSERT INTO users (id, username, full_name, email, auth_source)
|
||||
VALUES (?, ?, ?, ?, ?)`,
|
||||
[id, username, fullName, email ?? null, LOCAL_AUTH_SOURCE]
|
||||
);
|
||||
await setLocalCredentialPassword(id, input.password);
|
||||
return db.get<AppUser>('SELECT * FROM users WHERE id = ?', [id])!;
|
||||
}
|
||||
|
||||
export async function setLocalPasswordForUser(userId: string, password: string): Promise<void> {
|
||||
const id = userId?.trim() ?? '';
|
||||
if (!id) {
|
||||
throw new Error('User id required');
|
||||
}
|
||||
const existing = db.get<{ id: string }>('SELECT id FROM users WHERE id = ?', [id]);
|
||||
if (!existing) {
|
||||
throw new Error('User not found');
|
||||
}
|
||||
await setLocalCredentialPassword(id, password);
|
||||
}
|
||||
|
||||
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`,
|
||||
|
||||
Reference in New Issue
Block a user