import { IsArray, IsBoolean, IsOptional, IsString } from "class-validator"; import { User } from '../entities/User'; import { ResponseObjectType } from '../enums/ResponseObjectType'; import { IResponse } from './IResponse'; import { ResponsePrincipal } from './ResponsePrincipal'; import { ResponseUserGroup } from './ResponseUserGroup'; /** * Defines the user response. */ export class ResponseUser extends ResponsePrincipal implements IResponse { /** * The responseType. * This contains the type of class/entity this response contains. */ responseType: ResponseObjectType = ResponseObjectType.USER; /** * The user's first name. */ @IsString() firstname: string; /** * The user's middle name. */ @IsString() middlename?: string; /** * The user's last name. */ @IsString() lastname: string; /** * The user's phone number. */ @IsString() phone?: string; /** * The user's e-mail address. */ @IsString() email?: string; /** * The user's username. */ @IsString() username?: string; /** * Is user enabled? */ @IsBoolean() enabled: boolean = true; /** * The user's profile pic (or rather a url pointing to it). */ @IsString() profilePic: string; /** * The groups that the user is a part of. */ @IsArray() @IsOptional() groups: ResponseUserGroup[]; /** * The user's permissions. * Directly granted or inherited converted to their string form and deduplicated. */ @IsArray() @IsOptional() permissions: string[]; /** * Creates a ResponseUser object from a user. * @param user The user the response shall be build for. */ public constructor(user: User) { super(user); this.firstname = user.firstname; this.middlename = user.middlename; this.lastname = user.lastname; this.phone = user.phone; this.email = user.email; this.username = user.username; this.enabled = user.enabled; this.profilePic = user.profilePic; this.groups = new Array(); this.permissions = user.allPermissions; if (user.groups) { for (let group of user.groups) { delete group.permissions; this.groups.push(group.toResponse()); } } } }