mirror of
https://github.com/IT4Change/gradido.git
synced 2026-02-06 09:56:05 +00:00
Merge pull request #1808 from gradido/eliminate-server-user-table
refactor: Drop Server User Table
This commit is contained in:
commit
fef49cafea
@ -10,7 +10,7 @@ Decimal.set({
|
|||||||
})
|
})
|
||||||
|
|
||||||
const constants = {
|
const constants = {
|
||||||
DB_VERSION: '0033-add_referrer_id',
|
DB_VERSION: '0034-drop_server_user_table',
|
||||||
DECAY_START_TIME: new Date('2021-05-13 17:46:31'), // GMT+0
|
DECAY_START_TIME: new Date('2021-05-13 17:46:31'), // GMT+0
|
||||||
CONFIG_VERSION: {
|
CONFIG_VERSION: {
|
||||||
DEFAULT: 'DEFAULT',
|
DEFAULT: 'DEFAULT',
|
||||||
|
|||||||
@ -8,7 +8,6 @@ import { RIGHTS } from '@/auth/RIGHTS'
|
|||||||
import { getCustomRepository } from '@dbTools/typeorm'
|
import { getCustomRepository } from '@dbTools/typeorm'
|
||||||
import { UserRepository } from '@repository/User'
|
import { UserRepository } from '@repository/User'
|
||||||
import { INALIENABLE_RIGHTS } from '@/auth/INALIENABLE_RIGHTS'
|
import { INALIENABLE_RIGHTS } from '@/auth/INALIENABLE_RIGHTS'
|
||||||
import { ServerUser } from '@entity/ServerUser'
|
|
||||||
|
|
||||||
const isAuthorized: AuthChecker<any> = async ({ context }, rights) => {
|
const isAuthorized: AuthChecker<any> = async ({ context }, rights) => {
|
||||||
context.role = ROLE_UNAUTHORIZED // unauthorized user
|
context.role = ROLE_UNAUTHORIZED // unauthorized user
|
||||||
@ -36,8 +35,7 @@ const isAuthorized: AuthChecker<any> = async ({ context }, rights) => {
|
|||||||
try {
|
try {
|
||||||
const user = await userRepository.findByPubkeyHex(context.pubKey)
|
const user = await userRepository.findByPubkeyHex(context.pubKey)
|
||||||
context.user = user
|
context.user = user
|
||||||
const countServerUsers = await ServerUser.count({ email: user.email })
|
context.role = user.isAdmin ? ROLE_ADMIN : ROLE_USER
|
||||||
context.role = countServerUsers > 0 ? ROLE_ADMIN : ROLE_USER
|
|
||||||
} catch {
|
} catch {
|
||||||
// in case the database query fails (user deleted)
|
// in case the database query fails (user deleted)
|
||||||
throw new Error('401 Unauthorized')
|
throw new Error('401 Unauthorized')
|
||||||
|
|||||||
@ -14,8 +14,8 @@ export class User {
|
|||||||
this.emailChecked = user.emailChecked
|
this.emailChecked = user.emailChecked
|
||||||
this.language = user.language
|
this.language = user.language
|
||||||
this.publisherId = user.publisherId
|
this.publisherId = user.publisherId
|
||||||
|
this.isAdmin = user.isAdmin
|
||||||
// TODO
|
// TODO
|
||||||
this.isAdmin = null
|
|
||||||
this.coinanimation = null
|
this.coinanimation = null
|
||||||
this.klickTipp = null
|
this.klickTipp = null
|
||||||
this.hasElopage = null
|
this.hasElopage = null
|
||||||
@ -58,11 +58,11 @@ export class User {
|
|||||||
|
|
||||||
// `passphrase` text COLLATE utf8mb4_unicode_ci DEFAULT NULL,
|
// `passphrase` text COLLATE utf8mb4_unicode_ci DEFAULT NULL,
|
||||||
|
|
||||||
|
@Field(() => Date, { nullable: true })
|
||||||
|
isAdmin: Date | null
|
||||||
|
|
||||||
// TODO this is a bit inconsistent with what we query from the database
|
// TODO this is a bit inconsistent with what we query from the database
|
||||||
// therefore all those fields are now nullable with default value null
|
// therefore all those fields are now nullable with default value null
|
||||||
@Field(() => Boolean, { nullable: true })
|
|
||||||
isAdmin: boolean | null
|
|
||||||
|
|
||||||
@Field(() => Boolean, { nullable: true })
|
@Field(() => Boolean, { nullable: true })
|
||||||
coinanimation: boolean | null
|
coinanimation: boolean | null
|
||||||
|
|
||||||
|
|||||||
@ -100,6 +100,7 @@ describe('UserResolver', () => {
|
|||||||
emailChecked: false,
|
emailChecked: false,
|
||||||
passphrase: expect.any(String),
|
passphrase: expect.any(String),
|
||||||
language: 'de',
|
language: 'de',
|
||||||
|
isAdmin: null,
|
||||||
deletedAt: null,
|
deletedAt: null,
|
||||||
publisherId: 1234,
|
publisherId: 1234,
|
||||||
referrerId: null,
|
referrerId: null,
|
||||||
@ -336,7 +337,7 @@ describe('UserResolver', () => {
|
|||||||
firstName: 'Bibi',
|
firstName: 'Bibi',
|
||||||
hasElopage: false,
|
hasElopage: false,
|
||||||
id: expect.any(Number),
|
id: expect.any(Number),
|
||||||
isAdmin: false,
|
isAdmin: null,
|
||||||
klickTipp: {
|
klickTipp: {
|
||||||
newsletterState: false,
|
newsletterState: false,
|
||||||
},
|
},
|
||||||
|
|||||||
@ -19,9 +19,7 @@ import { sendResetPasswordEmail as sendResetPasswordEmailMailer } from '@/mailer
|
|||||||
import { sendAccountActivationEmail } from '@/mailer/sendAccountActivationEmail'
|
import { sendAccountActivationEmail } from '@/mailer/sendAccountActivationEmail'
|
||||||
import { klicktippSignIn } from '@/apis/KlicktippController'
|
import { klicktippSignIn } from '@/apis/KlicktippController'
|
||||||
import { RIGHTS } from '@/auth/RIGHTS'
|
import { RIGHTS } from '@/auth/RIGHTS'
|
||||||
import { ROLE_ADMIN } from '@/auth/ROLES'
|
|
||||||
import { hasElopageBuys } from '@/util/hasElopageBuys'
|
import { hasElopageBuys } from '@/util/hasElopageBuys'
|
||||||
import { ServerUser } from '@entity/ServerUser'
|
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||||
const sodium = require('sodium-native')
|
const sodium = require('sodium-native')
|
||||||
@ -207,7 +205,6 @@ export class UserResolver {
|
|||||||
})
|
})
|
||||||
user.coinanimation = coinanimation
|
user.coinanimation = coinanimation
|
||||||
|
|
||||||
user.isAdmin = context.role === ROLE_ADMIN
|
|
||||||
return user
|
return user
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -243,9 +240,6 @@ export class UserResolver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const user = new User(dbUser)
|
const user = new User(dbUser)
|
||||||
// user.email = email
|
|
||||||
// user.pubkey = dbUser.pubKey.toString('hex')
|
|
||||||
user.language = dbUser.language
|
|
||||||
|
|
||||||
// Elopage Status & Stored PublisherId
|
// Elopage Status & Stored PublisherId
|
||||||
user.hasElopage = await this.hasElopage({ ...context, user: dbUser })
|
user.hasElopage = await this.hasElopage({ ...context, user: dbUser })
|
||||||
@ -266,10 +260,6 @@ export class UserResolver {
|
|||||||
})
|
})
|
||||||
user.coinanimation = coinanimation
|
user.coinanimation = coinanimation
|
||||||
|
|
||||||
// context.role is not set to the actual role yet on login
|
|
||||||
const countServerUsers = await ServerUser.count({ email: user.email })
|
|
||||||
user.isAdmin = countServerUsers > 0
|
|
||||||
|
|
||||||
context.setHeaders.push({
|
context.setHeaders.push({
|
||||||
key: 'token',
|
key: 'token',
|
||||||
value: encode(dbUser.pubKey),
|
value: encode(dbUser.pubKey),
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
import { createUser, setPassword } from '@/seeds/graphql/mutations'
|
import { createUser, setPassword } from '@/seeds/graphql/mutations'
|
||||||
import { User } from '@entity/User'
|
import { User } from '@entity/User'
|
||||||
import { LoginEmailOptIn } from '@entity/LoginEmailOptIn'
|
import { LoginEmailOptIn } from '@entity/LoginEmailOptIn'
|
||||||
import { ServerUser } from '@entity/ServerUser'
|
|
||||||
import { UserInterface } from '@/seeds/users/UserInterface'
|
import { UserInterface } from '@/seeds/users/UserInterface'
|
||||||
import { ApolloServerTestClient } from 'apollo-server-testing'
|
import { ApolloServerTestClient } from 'apollo-server-testing'
|
||||||
|
|
||||||
@ -29,23 +28,9 @@ export const userFactory = async (
|
|||||||
// get user from database
|
// get user from database
|
||||||
const dbUser = await User.findOneOrFail({ id })
|
const dbUser = await User.findOneOrFail({ id })
|
||||||
|
|
||||||
if (user.createdAt || user.deletedAt) {
|
if (user.createdAt) dbUser.createdAt = user.createdAt
|
||||||
if (user.createdAt) dbUser.createdAt = user.createdAt
|
if (user.deletedAt) dbUser.deletedAt = user.deletedAt
|
||||||
if (user.deletedAt) dbUser.deletedAt = user.deletedAt
|
if (user.isAdmin) dbUser.isAdmin = new Date()
|
||||||
await dbUser.save()
|
await dbUser.save()
|
||||||
}
|
|
||||||
|
|
||||||
if (user.isAdmin) {
|
|
||||||
const admin = new ServerUser()
|
|
||||||
admin.username = dbUser.firstName
|
|
||||||
admin.password = 'please_refactor'
|
|
||||||
admin.email = dbUser.email
|
|
||||||
admin.role = 'admin'
|
|
||||||
admin.activated = 1
|
|
||||||
admin.lastLogin = new Date()
|
|
||||||
admin.created = dbUser.createdAt
|
|
||||||
admin.modified = dbUser.createdAt
|
|
||||||
await admin.save()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,6 +17,7 @@ const communityDbUser: dbUser = {
|
|||||||
createdAt: new Date(),
|
createdAt: new Date(),
|
||||||
emailChecked: false,
|
emailChecked: false,
|
||||||
language: '',
|
language: '',
|
||||||
|
isAdmin: null,
|
||||||
publisherId: 0,
|
publisherId: 0,
|
||||||
passphrase: '',
|
passphrase: '',
|
||||||
settings: [],
|
settings: [],
|
||||||
|
|||||||
@ -30,4 +30,3 @@ yarn dev_down
|
|||||||
yarn dev_reset
|
yarn dev_reset
|
||||||
```
|
```
|
||||||
Runs all down migrations and after this all up migrations.
|
Runs all down migrations and after this all up migrations.
|
||||||
|
|
||||||
|
|||||||
81
database/entity/0034-drop_server_user_table/User.ts
Normal file
81
database/entity/0034-drop_server_user_table/User.ts
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
import {
|
||||||
|
BaseEntity,
|
||||||
|
Entity,
|
||||||
|
PrimaryGeneratedColumn,
|
||||||
|
Column,
|
||||||
|
OneToMany,
|
||||||
|
DeleteDateColumn,
|
||||||
|
} from 'typeorm'
|
||||||
|
import { UserSetting } from '../UserSetting'
|
||||||
|
|
||||||
|
@Entity('users', { engine: 'InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci' })
|
||||||
|
export class User extends BaseEntity {
|
||||||
|
@PrimaryGeneratedColumn('increment', { unsigned: true })
|
||||||
|
id: number
|
||||||
|
|
||||||
|
@Column({ name: 'public_key', type: 'binary', length: 32, default: null, nullable: true })
|
||||||
|
pubKey: Buffer
|
||||||
|
|
||||||
|
@Column({ name: 'privkey', type: 'binary', length: 80, default: null, nullable: true })
|
||||||
|
privKey: Buffer
|
||||||
|
|
||||||
|
@Column({ length: 255, unique: true, nullable: false, collation: 'utf8mb4_unicode_ci' })
|
||||||
|
email: string
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
name: 'first_name',
|
||||||
|
length: 255,
|
||||||
|
nullable: true,
|
||||||
|
default: null,
|
||||||
|
collation: 'utf8mb4_unicode_ci',
|
||||||
|
})
|
||||||
|
firstName: string
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
name: 'last_name',
|
||||||
|
length: 255,
|
||||||
|
nullable: true,
|
||||||
|
default: null,
|
||||||
|
collation: 'utf8mb4_unicode_ci',
|
||||||
|
})
|
||||||
|
lastName: string
|
||||||
|
|
||||||
|
@DeleteDateColumn()
|
||||||
|
deletedAt: Date | null
|
||||||
|
|
||||||
|
@Column({ type: 'bigint', default: 0, unsigned: true })
|
||||||
|
password: BigInt
|
||||||
|
|
||||||
|
@Column({ name: 'email_hash', type: 'binary', length: 32, default: null, nullable: true })
|
||||||
|
emailHash: Buffer
|
||||||
|
|
||||||
|
@Column({ name: 'created', default: () => 'CURRENT_TIMESTAMP', nullable: false })
|
||||||
|
createdAt: Date
|
||||||
|
|
||||||
|
@Column({ name: 'email_checked', type: 'bool', nullable: false, default: false })
|
||||||
|
emailChecked: boolean
|
||||||
|
|
||||||
|
@Column({ length: 4, default: 'de', collation: 'utf8mb4_unicode_ci', nullable: false })
|
||||||
|
language: string
|
||||||
|
|
||||||
|
@Column({ name: 'is_admin', type: 'datetime', nullable: true, default: null })
|
||||||
|
isAdmin: Date | null
|
||||||
|
|
||||||
|
@Column({ name: 'referrer_id', type: 'int', unsigned: true, nullable: true, default: null })
|
||||||
|
referrerId?: number | null
|
||||||
|
|
||||||
|
@Column({ name: 'publisher_id', default: 0 })
|
||||||
|
publisherId: number
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
type: 'text',
|
||||||
|
name: 'passphrase',
|
||||||
|
collation: 'utf8mb4_unicode_ci',
|
||||||
|
nullable: true,
|
||||||
|
default: null,
|
||||||
|
})
|
||||||
|
passphrase: string
|
||||||
|
|
||||||
|
@OneToMany(() => UserSetting, (userSetting) => userSetting.user)
|
||||||
|
settings: UserSetting[]
|
||||||
|
}
|
||||||
@ -1 +0,0 @@
|
|||||||
export { ServerUser } from './0001-init_db/ServerUser'
|
|
||||||
@ -1 +1 @@
|
|||||||
export { User } from './0033-add_referrer_id/User'
|
export { User } from './0034-drop_server_user_table/User'
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
import { LoginElopageBuys } from './LoginElopageBuys'
|
import { LoginElopageBuys } from './LoginElopageBuys'
|
||||||
import { LoginEmailOptIn } from './LoginEmailOptIn'
|
import { LoginEmailOptIn } from './LoginEmailOptIn'
|
||||||
import { Migration } from './Migration'
|
import { Migration } from './Migration'
|
||||||
import { ServerUser } from './ServerUser'
|
|
||||||
import { Transaction } from './Transaction'
|
import { Transaction } from './Transaction'
|
||||||
import { TransactionLink } from './TransactionLink'
|
import { TransactionLink } from './TransactionLink'
|
||||||
import { User } from './User'
|
import { User } from './User'
|
||||||
@ -13,7 +12,6 @@ export const entities = [
|
|||||||
LoginElopageBuys,
|
LoginElopageBuys,
|
||||||
LoginEmailOptIn,
|
LoginEmailOptIn,
|
||||||
Migration,
|
Migration,
|
||||||
ServerUser,
|
|
||||||
Transaction,
|
Transaction,
|
||||||
TransactionLink,
|
TransactionLink,
|
||||||
User,
|
User,
|
||||||
|
|||||||
37
database/migrations/0034-drop_server_user_table.ts
Normal file
37
database/migrations/0034-drop_server_user_table.ts
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/* MIGRATION DROP server_users TABLE
|
||||||
|
add isAdmin COLUMN to users TABLE */
|
||||||
|
|
||||||
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
||||||
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
|
|
||||||
|
export async function upgrade(queryFn: (query: string, values?: any[]) => Promise<Array<any>>) {
|
||||||
|
await queryFn('ALTER TABLE `users` ADD COLUMN `is_admin` datetime DEFAULT NULL AFTER `language`;')
|
||||||
|
|
||||||
|
await queryFn(
|
||||||
|
'UPDATE users AS users INNER JOIN server_users AS server_users ON users.email = server_users.email SET users.is_admin = server_users.modified WHERE users.email IN (SELECT email from server_users);',
|
||||||
|
)
|
||||||
|
|
||||||
|
await queryFn('DROP TABLE `server_users`;')
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function downgrade(queryFn: (query: string, values?: any[]) => Promise<Array<any>>) {
|
||||||
|
await queryFn(`
|
||||||
|
CREATE TABLE IF NOT EXISTS \`server_users\` (
|
||||||
|
\`id\` int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||||
|
\`username\` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
|
||||||
|
\`password\` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
|
||||||
|
\`email\` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
|
||||||
|
\`role\` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'admin',
|
||||||
|
\`activated\` tinyint(4) NOT NULL DEFAULT '0',
|
||||||
|
\`last_login\` datetime DEFAULT NULL,
|
||||||
|
\`created\` datetime NOT NULL,
|
||||||
|
\`modified\` datetime NOT NULL,
|
||||||
|
PRIMARY KEY (\`id\`)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;`)
|
||||||
|
|
||||||
|
await queryFn(
|
||||||
|
'INSERT INTO `server_users` (`email`, `username`, `password`, `created`, `modified`) SELECT `email`, `first_name`, `password`, `is_admin`, `is_admin` FROM `users` WHERE `is_admin` IS NOT NULL;',
|
||||||
|
)
|
||||||
|
|
||||||
|
await queryFn('ALTER TABLE `users` DROP COLUMN `is_admin`;')
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user