Merge pull request #1516 from gradido/users_soft_delete

feature: Soft-Delete for users (database only)
This commit is contained in:
Ulf Gebhardt 2022-02-18 16:01:01 +01:00 committed by GitHub
commit c81baf2722
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 108 additions and 7 deletions

View File

@ -4,7 +4,7 @@ import dotenv from 'dotenv'
dotenv.config()
const constants = {
DB_VERSION: '0022-delete_decay_start_block',
DB_VERSION: '0023-users_disabled_soft_delete',
DECAY_START_TIME: new Date('2021-05-13 17:46:31'), // GMT+0
}

View File

@ -367,7 +367,7 @@ export class TransactionResolver {
const recipiantUser = await userRepository.findByPubkeyHex(recipiantPublicKey)
if (!recipiantUser) {
throw new Error('Cannot find recipiant user by local send coins transaction')
} else if (recipiantUser.disabled) {
} else if (recipiantUser.deletedAt) {
throw new Error('recipiant user account is disabled')
}

View File

@ -0,0 +1,75 @@
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: '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[]
}

View File

@ -1 +1 @@
export { User } from './0020-rename_and_clean_state_users/User'
export { User } from './0023-users_disabled_soft_delete/User'

View File

@ -0,0 +1,26 @@
/* MIGRATION TO IMPLEMENT SOFT DELETE ON THE USERS TABLE
*
* Replace the `disabled` column with `deletedAt` containing
* a date as it is standard for soft delete fields
*/
export async function upgrade(queryFn: (query: string, values?: any[]) => Promise<Array<any>>) {
// Create new `deletedAt` column
await queryFn(
'ALTER TABLE `users` ADD COLUMN `deletedAt` datetime DEFAULT NULL AFTER `disabled`;',
)
// Insert a 1.1.2022 as date for those users with `disabled=1`
await queryFn('UPDATE `users` SET `deletedAt` = "2022-01-01 00:00:00" WHERE `disabled` = 1;')
// Delete `disabled` column
await queryFn('ALTER TABLE `users` DROP COLUMN `disabled`;')
}
export async function downgrade(queryFn: (query: string, values?: any[]) => Promise<Array<any>>) {
await queryFn(
'ALTER TABLE `users` ADD COLUMN `disabled` tinyint(4) NOT NULL DEFAULT 0 AFTER `deletedAt`;',
)
await queryFn('UPDATE `users` SET `disabled` = 1 WHERE `deletedAt` IS NOT NULL;')
await queryFn('ALTER TABLE `users` DROP COLUMN `deletedAt`;')
}

View File

@ -12,7 +12,7 @@ define(User, (faker: typeof Faker, context?: UserContext) => {
user.email = context.email ? context.email : faker.internet.email()
user.firstName = context.firstName ? context.firstName : faker.name.firstName()
user.lastName = context.lastName ? context.lastName : faker.name.lastName()
user.disabled = context.disabled ? context.disabled : false
user.deletedAt = context.deletedAt ? context.deletedAt : null
// TODO Create real password and keys/hash
user.password = context.password ? context.password : BigInt(0)
user.privKey = context.privKey ? context.privKey : randomBytes(80)

View File

@ -3,7 +3,7 @@ export interface UserContext {
email?: string
firstName?: string
lastName?: string
disabled?: boolean
deletedAt?: Date
password?: BigInt
privKey?: Buffer
emailHash?: Buffer

View File

@ -10,7 +10,7 @@ export interface UserInterface {
createdAt?: Date
emailChecked?: boolean
language?: string
disabled?: boolean
deletedAt?: Date
groupId?: number
publisherId?: number
passphrase?: string

View File

@ -42,7 +42,7 @@ const createUserContext = (context: UserInterface): UserContext => {
email: context.email,
firstName: context.firstName,
lastName: context.lastName,
disabled: context.disabled,
deletedAt: context.deletedAt,
password: context.password,
privKey: context.privKey,
emailHash: context.emailHash,