diff --git a/database/entity/0001-init_db/ServerUser.ts b/database/entity/0001-init_db/ServerUser.ts index e776093ac..42b5816ae 100644 --- a/database/entity/0001-init_db/ServerUser.ts +++ b/database/entity/0001-init_db/ServerUser.ts @@ -8,8 +8,8 @@ export class ServerUser extends BaseEntity { @Column({ length: 50 }) username: string - @Column({ type: 'bigint', unsigned: true }) - password: BigInt + @Column({ length: 255 }) + password: string @Column({ length: 50, unique: true }) email: string @@ -23,9 +23,9 @@ export class ServerUser extends BaseEntity { @Column({ name: 'last_login', default: null, nullable: true }) lastLogin: Date - @Column({ name: 'created', default: () => 'CURRENT_TIMESTAMP' }) + @Column({ default: () => 'CURRENT_TIMESTAMP' }) created: Date - @Column({ name: 'created', default: () => 'CURRENT_TIMESTAMP' }) + @Column({ default: () => 'CURRENT_TIMESTAMP' }) modified: Date } diff --git a/database/entity/0003-login_server_tables/LoginUserRoles.ts b/database/entity/0003-login_server_tables/LoginUserRoles.ts new file mode 100644 index 000000000..33553fea4 --- /dev/null +++ b/database/entity/0003-login_server_tables/LoginUserRoles.ts @@ -0,0 +1,13 @@ +import { BaseEntity, Entity, PrimaryGeneratedColumn, Column } from 'typeorm' + +@Entity('login_user_roles') +export class LoginUserRoles extends BaseEntity { + @PrimaryGeneratedColumn('increment', { unsigned: true }) + id: number + + @Column({ name: 'user_id' }) + userId: number + + @Column({ name: 'role_id' }) + roleId: number +} diff --git a/database/entity/LoginUserRoles.ts b/database/entity/LoginUserRoles.ts new file mode 100644 index 000000000..1efa81caf --- /dev/null +++ b/database/entity/LoginUserRoles.ts @@ -0,0 +1 @@ +export { LoginUserRoles } from './0003-login_server_tables/LoginUserRoles' diff --git a/database/entity/index.ts b/database/entity/index.ts index 53a6a14bf..901b4ab0d 100644 --- a/database/entity/index.ts +++ b/database/entity/index.ts @@ -2,6 +2,7 @@ import { Balance } from './Balance' import { LoginElopageBuys } from './LoginElopageBuys' import { LoginEmailOptIn } from './LoginEmailOptIn' import { LoginUser } from './LoginUser' +import { LoginUserRoles } from './LoginUserRoles' import { LoginUserBackup } from './LoginUserBackup' import { Migration } from './Migration' import { ServerUser } from './ServerUser' @@ -17,6 +18,7 @@ export const entities = [ LoginElopageBuys, LoginEmailOptIn, LoginUser, + LoginUserRoles, LoginUserBackup, Migration, ServerUser, diff --git a/database/src/factories/login-user-backup.factory.ts b/database/src/factories/login-user-backup.factory.ts index c8f047681..054e1bca8 100644 --- a/database/src/factories/login-user-backup.factory.ts +++ b/database/src/factories/login-user-backup.factory.ts @@ -10,7 +10,7 @@ interface LoginUserBackupContext { define(LoginUserBackup, (faker: typeof Faker, context?: LoginUserBackupContext) => { if (!context) context = {} - if (!context.userId) throw new Error('LoginUserBackup: No iserId present!') + if (!context.userId) throw new Error('LoginUserBackup: No userId present!') const userBackup = new LoginUserBackup() // TODO: Get the real passphrase diff --git a/database/src/factories/login-user-roles.factory.ts b/database/src/factories/login-user-roles.factory.ts new file mode 100644 index 000000000..2d73564f1 --- /dev/null +++ b/database/src/factories/login-user-roles.factory.ts @@ -0,0 +1,20 @@ +import Faker from 'faker' +import { define } from 'typeorm-seeding' +import { LoginUserRoles } from '../../entity/LoginUserRoles' + +interface LoginUserRolesContext { + userId?: number + roleId?: number +} + +define(LoginUserRoles, (faker: typeof Faker, context?: LoginUserRolesContext) => { + if (!context) context = {} + if (!context.userId) throw new Error('LoginUserRoles: No userId present!') + if (!context.roleId) throw new Error('LoginUserRoles: No roleId present!') + + const userRoles = new LoginUserRoles() + userRoles.userId = context.userId + userRoles.roleId = context.roleId + + return userRoles +}) diff --git a/database/src/factories/server-user.factory.ts b/database/src/factories/server-user.factory.ts index ab3dca811..5db99f374 100644 --- a/database/src/factories/server-user.factory.ts +++ b/database/src/factories/server-user.factory.ts @@ -4,7 +4,7 @@ import { ServerUser } from '../../entity/ServerUser' interface ServerUserContext { username?: string - password?: BigInt + password?: string email?: string role?: string activated?: number @@ -18,7 +18,7 @@ define(ServerUser, (faker: typeof Faker, context?: ServerUserContext) => { const user = new ServerUser() user.username = context.username ? context.username : faker.internet.userName() - user.password = context.password ? context.password : BigInt(0) + user.password = context.password ? context.password : faker.internet.password() user.email = context.email ? context.email : faker.internet.email() user.role = context.role ? context.role : 'admin' user.activated = context.activated ? context.activated : 0 diff --git a/database/src/seeds/users/peter-lustig.admin.seed.ts b/database/src/seeds/users/peter-lustig.admin.seed.ts index 85c7f09e8..53c7be395 100644 --- a/database/src/seeds/users/peter-lustig.admin.seed.ts +++ b/database/src/seeds/users/peter-lustig.admin.seed.ts @@ -2,7 +2,8 @@ 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' +import { ServerUser } from '../../../entity/ServerUser' +import { LoginUserRoles } from '../../../entity/LoginUserRoles' const peterLustig = { email: 'peter@lustig.de', @@ -17,7 +18,7 @@ const peterLustig = { 'hex', ), emailHash: Buffer.from('9f700e6f6ec351a140b674c0edd4479509697b023bd8bee8826915ef6c2af036', 'hex'), - createdAt: new Date('2021-11-25T10:48:43'), + createdAt: new Date('2020-11-25T10:48:43'), emailChecked: true, passphraseShown: false, language: 'de', @@ -28,9 +29,10 @@ const peterLustig = { '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, + serverUserPassword: '$2y$10$TzIWLeZoKs251gwrhSQmHeKhKI/EQ4EV5ClfAT8Ufnb4lcUXPa5X.', + activated: 1, lastLogin: new Date('2021-10-27T12:25:57'), - modified: new Date('2021-10-27T12:25:57'), + modified: new Date('2021-09-27T12:25:57'), isAdmin: true, } @@ -69,5 +71,21 @@ export class CreatePeterLustigSeed implements Seeder { 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() } }