diff --git a/database/entity/0003-login_server_tables/LoginUser.ts b/database/entity/0003-login_server_tables/LoginUser.ts index 1b444b0e4..ebaf8ad53 100644 --- a/database/entity/0003-login_server_tables/LoginUser.ts +++ b/database/entity/0003-login_server_tables/LoginUser.ts @@ -1,4 +1,5 @@ -import { BaseEntity, Entity, PrimaryGeneratedColumn, Column } from 'typeorm' +import { BaseEntity, Entity, PrimaryGeneratedColumn, Column, OneToOne } from 'typeorm' +import { LoginUserBackup } from '../LoginUserBackup' // Moriz: I do not like the idea of having two user tables @Entity('login_users') @@ -53,4 +54,7 @@ export class LoginUser extends BaseEntity { @Column({ name: 'publisher_id', default: 0 }) publisherId: number + + @OneToOne(() => LoginUserBackup, (loginUserBackup) => loginUserBackup.loginUser) + loginUserBackup: LoginUserBackup } diff --git a/database/entity/0003-login_server_tables/LoginUserBackup.ts b/database/entity/0003-login_server_tables/LoginUserBackup.ts index 548a46408..7152e12e5 100644 --- a/database/entity/0003-login_server_tables/LoginUserBackup.ts +++ b/database/entity/0003-login_server_tables/LoginUserBackup.ts @@ -15,7 +15,7 @@ export class LoginUserBackup extends BaseEntity { @Column({ name: 'mnemonic_type', default: -1 }) mnemonicType: number - @OneToOne(() => LoginUser, { nullable: false }) + @OneToOne(() => LoginUser, (loginUser) => loginUser.loginUserBackup, { nullable: false }) @JoinColumn({ name: 'user_id' }) loginUser: LoginUser } diff --git a/database/src/factories/login-user-backup.factory.ts b/database/src/factories/login-user-backup.factory.ts index 35363263e..c8f047681 100644 --- a/database/src/factories/login-user-backup.factory.ts +++ b/database/src/factories/login-user-backup.factory.ts @@ -3,16 +3,20 @@ import { define } from 'typeorm-seeding' import { LoginUserBackup } from '../../entity/LoginUserBackup' interface LoginUserBackupContext { + userId?: number passphrase?: string mnemonicType?: number } define(LoginUserBackup, (faker: typeof Faker, context?: LoginUserBackupContext) => { if (!context) context = {} + if (!context.userId) throw new Error('LoginUserBackup: No iserId present!') const userBackup = new LoginUserBackup() + // TODO: Get the real passphrase userBackup.passphrase = context.passphrase ? context.passphrase : faker.random.words(24) userBackup.mnemonicType = context.mnemonicType ? context.mnemonicType : 2 + userBackup.userId = context.userId return userBackup }) diff --git a/database/src/factories/login-user.factory.ts b/database/src/factories/login-user.factory.ts index 5fd64f233..00a0a0ee0 100644 --- a/database/src/factories/login-user.factory.ts +++ b/database/src/factories/login-user.factory.ts @@ -31,6 +31,7 @@ define(LoginUser, (faker: typeof Faker, context?: LoginUserContext) => { user.lastName = context.lastName ? context.lastName : faker.name.lastName() user.username = context.username ? context.username : faker.internet.userName() user.description = context.description ? context.description : faker.random.words(4) + // TODO Create real password and keys/hash user.password = context.password ? context.password : BigInt(0) user.pubKey = context.pubKey ? context.pubKey : randomBytes(32) user.privKey = context.privKey ? context.privKey : randomBytes(80) diff --git a/database/src/index.ts b/database/src/index.ts index 9341b3d45..f781b09db 100644 --- a/database/src/index.ts +++ b/database/src/index.ts @@ -5,7 +5,7 @@ import CONFIG from './config' import prepare from './prepare' import connection from './typeorm/connection' import { useSeeding, runSeeder } from 'typeorm-seeding' -import { CreateUserSeed } from './seeds/create-user.seed' +import { CreatePeterLustigSeed } from './seeds/users/peter-lustig.admin.seed' const run = async (command: string) => { // Database actions not supported by our migration library @@ -54,7 +54,7 @@ const run = async (command: string) => { root: process.cwd(), configName: 'ormconfig.js', }) - await runSeeder(CreateUserSeed) + await runSeeder(CreatePeterLustigSeed) break default: throw new Error(`Unsupported command ${command}`) diff --git a/database/src/interface/UserContext.ts b/database/src/interface/UserContext.ts new file mode 100644 index 000000000..201a2c944 --- /dev/null +++ b/database/src/interface/UserContext.ts @@ -0,0 +1,29 @@ +export interface UserContext { + // from login user (contains state user) + email?: string + firstName?: string + lastName?: string + username?: string + description?: string + password?: BigInt + pubKey?: Buffer + privKey?: Buffer + emailHash?: Buffer + createdAt?: Date + emailChecked?: boolean + passphraseShown?: boolean + language?: string + disabled?: boolean + groupId?: number + publisherId?: number + // from login user backup + passphrase?: string + mnemonicType?: number + // from server user + role?: string + activated?: number + lastLogin?: Date + modified?: Date + // flag for admin + isAdmin?: boolean +} diff --git a/database/src/seeds/create-user.seed.ts b/database/src/seeds/create-user.seed.ts index b2f6be6ab..ca3a182c4 100644 --- a/database/src/seeds/create-user.seed.ts +++ b/database/src/seeds/create-user.seed.ts @@ -2,38 +2,6 @@ import { Factory, Seeder } from 'typeorm-seeding' import { User } from '../../entity/User' // import { LoginUser } from '../../entity/LoginUser' -/* -interface UserContext { - // from login user (contains state user) - email?: string - firstName?: string - lastName?: string - username?: string - description?: string - password?: BigInt - pubKey?: Buffer - privKey?: Buffer - emailHash?: Buffer - createdAt?: Date - emailChecked?: boolean - passphraseShown?: boolean - language?: string - disabled?: boolean - groupId?: number - publisherId?: number - // from login user backup - passphrase?: string - mnemonicType?: number - // from server user - role?: string - activated?: number - lastLogin?: Date - modified?: Date - // flag for admin - isAdmin?: boolean -} -*/ - export class CreateUserSeed implements Seeder { public async run(factory: Factory): Promise { // const loginUser = await factory(LoginUser)().make() diff --git a/database/src/seeds/users/peter-lustig.admin.seed.ts b/database/src/seeds/users/peter-lustig.admin.seed.ts new file mode 100644 index 000000000..85c7f09e8 --- /dev/null +++ b/database/src/seeds/users/peter-lustig.admin.seed.ts @@ -0,0 +1,73 @@ +import { Factory, Seeder } from 'typeorm-seeding' +import { User } from '../../../entity/User' +import { LoginUser } from '../../../entity/LoginUser' +import { LoginUserBackup } from '../../../entity/LoginUserBackup' +// import { ServerUser } from '../../../entity/ServerUser' + +const peterLustig = { + email: 'peter@lustig.de', + firstName: 'Peter', + lastName: 'Lustig', + username: 'peter', + description: 'Latzhose und Nickelbrille', + password: BigInt('3917921995996627700'), + pubKey: Buffer.from('7281e0ee3258b08801f3ec73e431b4519677f65c03b0382c63a913b5784ee770', 'hex'), + privKey: Buffer.from( + '3c7c0253033212ed983f6bb10ce73362a99f0bd01d4d1b21ca702d532ca32710ba36abf72a22a963b9026e764e954f441f4905b87a66861bd6b9d9689b7f8aefea66cc493e21da4244e85be81660b9c4', + 'hex', + ), + emailHash: Buffer.from('9f700e6f6ec351a140b674c0edd4479509697b023bd8bee8826915ef6c2af036', 'hex'), + createdAt: new Date('2021-11-25T10:48:43'), + emailChecked: true, + passphraseShown: false, + language: 'de', + disabled: false, + groupId: 1, + publisherId: null, + passphrase: + 'okay property choice naive calm present weird increase stuff royal vibrant frame attend wood one else tribe pull hedgehog woman kitchen hawk snack smart ', + mnemonicType: 2, + role: 'admin', + activated: 0, + lastLogin: new Date('2021-10-27T12:25:57'), + modified: new Date('2021-10-27T12:25:57'), + isAdmin: true, +} + +export class CreatePeterLustigSeed implements Seeder { + public async run(factory: Factory): Promise { + await factory(User)({ + pubkey: peterLustig.pubKey, + email: peterLustig.email, + firstName: peterLustig.firstName, + lastName: peterLustig.lastName, + username: peterLustig.username, + disabled: peterLustig.disabled, + }).create() + + const loginUser = await factory(LoginUser)({ + email: peterLustig.email, + firstName: peterLustig.firstName, + lastName: peterLustig.lastName, + username: peterLustig.username, + description: peterLustig.description, + password: peterLustig.password, + pubKey: peterLustig.pubKey, + privKey: peterLustig.privKey, + emailHash: peterLustig.emailHash, + createdAt: peterLustig.createdAt, + emailChecked: peterLustig.emailChecked, + passphraseShown: peterLustig.passphraseShown, + language: peterLustig.language, + disabled: peterLustig.disabled, + groupId: peterLustig.groupId, + publisherId: peterLustig.publisherId, + }).create() + + await factory(LoginUserBackup)({ + passphrase: peterLustig.passphrase, + mnemonicType: peterLustig.mnemonicType, + userId: loginUser.id, + }).create() + } +}