diff --git a/database/src/factories/login-user-backup.factory.ts b/database/src/factories/login-user-backup.factory.ts index 054e1bca8..40b82de9b 100644 --- a/database/src/factories/login-user-backup.factory.ts +++ b/database/src/factories/login-user-backup.factory.ts @@ -1,12 +1,7 @@ import Faker from 'faker' import { define } from 'typeorm-seeding' import { LoginUserBackup } from '../../entity/LoginUserBackup' - -interface LoginUserBackupContext { - userId?: number - passphrase?: string - mnemonicType?: number -} +import { LoginUserBackupContext } from '../interface/UserContext' define(LoginUserBackup, (faker: typeof Faker, context?: LoginUserBackupContext) => { if (!context) context = {} diff --git a/database/src/factories/login-user-roles.factory.ts b/database/src/factories/login-user-roles.factory.ts index 2d73564f1..5b7ba9c9d 100644 --- a/database/src/factories/login-user-roles.factory.ts +++ b/database/src/factories/login-user-roles.factory.ts @@ -1,11 +1,7 @@ import Faker from 'faker' import { define } from 'typeorm-seeding' import { LoginUserRoles } from '../../entity/LoginUserRoles' - -interface LoginUserRolesContext { - userId?: number - roleId?: number -} +import { LoginUserRolesContext } from '../interface/UserContext' define(LoginUserRoles, (faker: typeof Faker, context?: LoginUserRolesContext) => { if (!context) context = {} diff --git a/database/src/factories/login-user.factory.ts b/database/src/factories/login-user.factory.ts index 00a0a0ee0..a5a28943e 100644 --- a/database/src/factories/login-user.factory.ts +++ b/database/src/factories/login-user.factory.ts @@ -2,25 +2,7 @@ import Faker from 'faker' import { define } from 'typeorm-seeding' import { LoginUser } from '../../entity/LoginUser' import { randomBytes } from 'crypto' - -interface LoginUserContext { - 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 -} +import { LoginUserContext } from '../interface/UserContext' define(LoginUser, (faker: typeof Faker, context?: LoginUserContext) => { if (!context) context = {} diff --git a/database/src/factories/server-user.factory.ts b/database/src/factories/server-user.factory.ts index 5db99f374..06ff17c35 100644 --- a/database/src/factories/server-user.factory.ts +++ b/database/src/factories/server-user.factory.ts @@ -1,17 +1,7 @@ import Faker from 'faker' import { define } from 'typeorm-seeding' import { ServerUser } from '../../entity/ServerUser' - -interface ServerUserContext { - username?: string - password?: string - email?: string - role?: string - activated?: number - lastLogin?: Date - created?: Date - modified?: Date -} +import { ServerUserContext } from '../interface/UserContext' define(ServerUser, (faker: typeof Faker, context?: ServerUserContext) => { if (!context) context = {} diff --git a/database/src/factories/user.factory.ts b/database/src/factories/user.factory.ts index ffa32e815..1f684f23f 100644 --- a/database/src/factories/user.factory.ts +++ b/database/src/factories/user.factory.ts @@ -2,15 +2,7 @@ import Faker from 'faker' import { define } from 'typeorm-seeding' import { User } from '../../entity/User' import { randomBytes } from 'crypto' - -interface UserContext { - pubkey?: Buffer - email?: string - firstName?: string - lastName?: string - username?: string - disabled?: boolean -} +import { UserContext } from '../interface/UserContext' define(User, (faker: typeof Faker, context?: UserContext) => { if (!context) context = {} diff --git a/database/src/interface/UserContext.ts b/database/src/interface/UserContext.ts index 201a2c944..4e188904c 100644 --- a/database/src/interface/UserContext.ts +++ b/database/src/interface/UserContext.ts @@ -1,5 +1,13 @@ export interface UserContext { - // from login user (contains state user) + pubkey?: Buffer + email?: string + firstName?: string + lastName?: string + username?: string + disabled?: boolean +} + +export interface LoginUserContext { email?: string firstName?: string lastName?: string @@ -15,15 +23,27 @@ export interface UserContext { language?: string disabled?: boolean groupId?: number - publisherId?: number - // from login user backup + publisherId?: number | null +} + +export interface LoginUserBackupContext { + userId?: number passphrase?: string mnemonicType?: number - // from server user +} + +export interface ServerUserContext { + username?: string + password?: string + email?: string role?: string activated?: number lastLogin?: Date + created?: Date modified?: Date - // flag for admin - isAdmin?: boolean +} + +export interface LoginUserRolesContext { + userId?: number + roleId?: number } diff --git a/database/src/interface/UserInterface.ts b/database/src/interface/UserInterface.ts new file mode 100644 index 000000000..942158593 --- /dev/null +++ b/database/src/interface/UserInterface.ts @@ -0,0 +1,30 @@ +export interface UserInterface { + // 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 | null + // from login user backup + passphrase?: string + mnemonicType?: number + // from server user + serverUserPassword?: string + role?: string + activated?: number + lastLogin?: Date + modified?: Date + // flag for admin + isAdmin?: boolean +} diff --git a/database/src/seeds/helpers/user-helpers.ts b/database/src/seeds/helpers/user-helpers.ts new file mode 100644 index 000000000..2474b1241 --- /dev/null +++ b/database/src/seeds/helpers/user-helpers.ts @@ -0,0 +1,72 @@ +import { + UserContext, + LoginUserContext, + LoginUserBackupContext, + ServerUserContext, + LoginUserRolesContext, +} from '../../interface/UserContext' +import { UserInterface } from '../../interface/UserInterface' +import { LoginUser } from '../../../entity/LoginUser' + +export const createUserContext = (context: UserInterface): UserContext => { + return { + pubkey: context.pubKey, + email: context.email, + firstName: context.firstName, + lastName: context.lastName, + username: context.username, + disabled: context.disabled, + } +} + +export const createLoginUserContext = (context: UserInterface): LoginUserContext => { + return { + email: context.email, + firstName: context.firstName, + lastName: context.lastName, + username: context.username, + description: context.description, + password: context.password, + pubKey: context.pubKey, + privKey: context.privKey, + emailHash: context.emailHash, + createdAt: context.createdAt, + emailChecked: context.emailChecked, + passphraseShown: context.passphraseShown, + language: context.language, + disabled: context.disabled, + groupId: context.groupId, + publisherId: context.publisherId, + } +} + +export const createLoginUserBackupContext = ( + context: UserInterface, + loginUser: LoginUser, +): LoginUserBackupContext => { + return { + passphrase: context.passphrase, + mnemonicType: context.mnemonicType, + userId: loginUser.id, + } +} + +export const createServerUserContext = (context: UserInterface): ServerUserContext => { + return { + role: context.role, + username: context.username, + password: context.serverUserPassword, + email: context.email, + activated: context.activated, + created: context.createdAt, + lastLogin: context.lastLogin, + modified: context.modified, + } +} + +export const createLoginUserRolesContext = (loginUser: LoginUser): LoginUserRolesContext => { + return { + userId: loginUser.id, + roleId: 1, + } +} diff --git a/database/src/seeds/users/peter-lustig.admin.seed.ts b/database/src/seeds/users/peter-lustig.admin.seed.ts index 53c7be395..a8d17a6e4 100644 --- a/database/src/seeds/users/peter-lustig.admin.seed.ts +++ b/database/src/seeds/users/peter-lustig.admin.seed.ts @@ -4,88 +4,29 @@ import { LoginUser } from '../../../entity/LoginUser' import { LoginUserBackup } from '../../../entity/LoginUserBackup' import { ServerUser } from '../../../entity/ServerUser' import { LoginUserRoles } from '../../../entity/LoginUserRoles' +import { + createUserContext, + createLoginUserContext, + createLoginUserBackupContext, + createServerUserContext, + createLoginUserRolesContext, +} from '../helpers/user-helpers' +import { peterLustig } from './peter-lustig' -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('2020-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', - serverUserPassword: '$2y$10$TzIWLeZoKs251gwrhSQmHeKhKI/EQ4EV5ClfAT8Ufnb4lcUXPa5X.', - activated: 1, - lastLogin: new Date('2021-10-27T12:25:57'), - modified: new Date('2021-09-27T12:25:57'), - isAdmin: true, -} +const userData = peterLustig 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() + await factory(User)(createUserContext(userData)).create() + const loginUser = await factory(LoginUser)(createLoginUserContext(userData)).create() + await factory(LoginUserBackup)(createLoginUserBackupContext(userData, loginUser)).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() + if (userData.isAdmin) { + await factory(ServerUser)(createServerUserContext(userData)).create() - await factory(LoginUserBackup)({ - passphrase: peterLustig.passphrase, - mnemonicType: peterLustig.mnemonicType, - userId: loginUser.id, - }).create() - - await factory(ServerUser)({ - role: peterLustig.role, - username: peterLustig.username, - password: peterLustig.serverUserPassword, - email: peterLustig.email, - activated: peterLustig.activated, - created: peterLustig.createdAt, - lastLogin: peterLustig.lastLogin, - modified: peterLustig.modified, - }).create() - - await factory(LoginUserRoles)({ - userId: loginUser.id, - roleId: 1, - }).create() + // This is crazy: we just need the relation to roleId but no role at all + // It works with LoginRoles empty!! + await factory(LoginUserRoles)(createLoginUserRolesContext(loginUser)).create() + } } } diff --git a/database/src/seeds/users/peter-lustig.ts b/database/src/seeds/users/peter-lustig.ts new file mode 100644 index 000000000..63caf55f6 --- /dev/null +++ b/database/src/seeds/users/peter-lustig.ts @@ -0,0 +1,30 @@ +export 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('2020-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', + serverUserPassword: '$2y$10$TzIWLeZoKs251gwrhSQmHeKhKI/EQ4EV5ClfAT8Ufnb4lcUXPa5X.', + activated: 1, + lastLogin: new Date('2021-10-27T12:25:57'), + modified: new Date('2021-09-27T12:25:57'), + isAdmin: true, +}