136 lines
2.6 KiB
TypeScript
136 lines
2.6 KiB
TypeScript
import { IsBoolean, IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString, IsUUID } from "class-validator";
|
|
import { ChildEntity, Column, JoinTable, ManyToMany, OneToMany } from "typeorm";
|
|
import { config } from '../../config';
|
|
import { ResponsePrincipal } from '../responses/ResponsePrincipal';
|
|
import { ResponseUser } from '../responses/ResponseUser';
|
|
import { Permission } from './Permission';
|
|
import { Principal } from './Principal';
|
|
import { UserAction } from './UserAction';
|
|
import { UserGroup } from './UserGroup';
|
|
|
|
/**
|
|
* Defines a admin user.
|
|
*/
|
|
@ChildEntity()
|
|
export class User extends Principal {
|
|
/**
|
|
* uuid
|
|
*/
|
|
@Column({ unique: true })
|
|
@IsUUID(4)
|
|
uuid: string;
|
|
|
|
/**
|
|
* user email
|
|
*/
|
|
@Column({ nullable: true, unique: true })
|
|
@IsEmail()
|
|
email?: string;
|
|
|
|
/**
|
|
* user phone
|
|
*/
|
|
@Column({ nullable: true })
|
|
@IsOptional()
|
|
@IsPhoneNumber(config.phone_validation_countrycode)
|
|
phone?: string;
|
|
|
|
/**
|
|
* username
|
|
*/
|
|
@Column({ nullable: true, unique: true })
|
|
@IsString()
|
|
username?: string;
|
|
|
|
/**
|
|
* firstname
|
|
*/
|
|
@Column()
|
|
@IsString()
|
|
@IsNotEmpty()
|
|
firstname: string;
|
|
|
|
/**
|
|
* middlename
|
|
*/
|
|
@Column({ nullable: true })
|
|
@IsString()
|
|
@IsOptional()
|
|
middlename?: string;
|
|
|
|
/**
|
|
* lastname
|
|
*/
|
|
@Column()
|
|
@IsString()
|
|
@IsNotEmpty()
|
|
lastname: string;
|
|
|
|
/**
|
|
* password
|
|
*/
|
|
@Column()
|
|
@IsString()
|
|
@IsNotEmpty()
|
|
password: string;
|
|
|
|
/**
|
|
* groups
|
|
*/
|
|
@IsOptional()
|
|
@ManyToMany(() => UserGroup, { nullable: true })
|
|
@JoinTable()
|
|
groups: UserGroup[];
|
|
|
|
/**
|
|
* is user enabled?
|
|
*/
|
|
@Column()
|
|
@IsBoolean()
|
|
enabled: boolean = true;
|
|
|
|
/**
|
|
* jwt refresh count
|
|
*/
|
|
@IsInt()
|
|
@Column({ default: 1 })
|
|
refreshTokenCount?: number;
|
|
|
|
/**
|
|
* profilepic
|
|
*/
|
|
@Column({ nullable: true, unique: true })
|
|
@IsString()
|
|
@IsOptional()
|
|
profilePic?: string;
|
|
|
|
/**
|
|
* actions
|
|
*/
|
|
@IsOptional()
|
|
@OneToMany(() => UserAction, action => action.user, { nullable: true })
|
|
actions: UserAction[]
|
|
|
|
/**
|
|
* calculate all permissions
|
|
*/
|
|
public get calc_permissions(): Permission[] {
|
|
let final_permissions = []
|
|
this.groups.forEach((permission) => {
|
|
if (!final_permissions.includes(permission)) {
|
|
final_permissions.push(permission)
|
|
}
|
|
})
|
|
this.permissions.forEach((permission) => {
|
|
if (!final_permissions.includes(permission)) {
|
|
final_permissions.push(permission)
|
|
}
|
|
})
|
|
return final_permissions
|
|
}
|
|
|
|
public toResponse(): ResponsePrincipal {
|
|
return new ResponseUser(this);
|
|
}
|
|
}
|