digitaltwin-core
Version:
Minimalist framework to collect and handle data in a Digital Twin project
86 lines • 2.67 kB
TypeScript
import type { DatabaseAdapter } from '../database/database_adapter.js';
import type { AuthenticatedUser, UserRecord } from './types.js';
/**
* Service for managing users in the Digital Twin framework.
*
* This service handles the complete user lifecycle in a Digital Twin application
* with Keycloak authentication via Apache APISIX. It manages a normalized database
* schema with three tables:
*
* - `users`: Core user records linked to Keycloak IDs
* - `roles`: Master list of available roles
* - `user_roles`: Many-to-many relationship between users and roles
*
* Key features:
* - Automatic user creation on first authentication
* - Role synchronization with Keycloak
* - Optimized queries with proper indexing
* - Transaction-safe role updates
*
* @example
* ```typescript
* // Initialize in your Digital Twin engine
* const userService = new UserService(databaseAdapter)
* await userService.initializeTables()
*
* // Use in AssetsManager handlers
* const authUser = ApisixAuthParser.parseAuthHeaders(req.headers)
* const userRecord = await userService.findOrCreateUser(authUser!)
*
* // Link assets to users
* await this.uploadAsset({
* description: 'My file',
* source: 'upload',
* owner_id: userRecord.id!.toString(),
* filename: 'document.pdf',
* file: buffer
* })
* ```
*/
export declare class UserService {
private db;
private readonly usersTable;
private readonly rolesTable;
private readonly userRolesTable;
constructor(db: DatabaseAdapter);
/**
* Ensures all user-related tables exist in the database
*/
initializeTables(): Promise<void>;
/**
* Finds or creates a user and synchronizes their roles.
*
* When authentication is disabled, returns a mock user record
* without touching the database.
*/
findOrCreateUser(authUser: AuthenticatedUser): Promise<UserRecord>;
/**
* Gets a user by their database ID
*/
getUserById(id: number): Promise<UserRecord | undefined>;
/**
* Gets a user by their Keycloak ID with roles
*/
getUserByKeycloakId(keycloakId: string): Promise<UserRecord | undefined>;
/**
* Gets the underlying Knex instance from the database adapter
*/
private getKnex;
/**
* Finds a user by their Keycloak ID
*/
private findUserByKeycloakId;
/**
* Creates a new user record
*/
private createUser;
/**
* Synchronizes user roles with what's provided by Keycloak
*/
private syncUserRoles;
/**
* Gets a user with their roles populated
*/
private getUserWithRoles;
}
//# sourceMappingURL=user_service.d.ts.map