import {
    IsArray,


    IsOptional
} from "class-validator";
import { User } from '../entities/User';
import { ResponseObjectType } from '../enums/ResponseObjectType';
import { IResponse } from './IResponse';
import { ResponsePermission } from './ResponsePermission';

/**
 * Defines the user permission response (get /api/users/:id/permissions).
*/
export class ResponseUserPermissions implements IResponse {
    /**
    * The responseType.
    * This contains the type of class/entity this response contains.
    */
    responseType: ResponseObjectType = ResponseObjectType.USERPERMISSIONS;

    /**
     * The permissions directly granted to the user.
     */
    @IsArray()
    @IsOptional()
    directlyGranted: ResponsePermission[] = new Array<ResponsePermission>();

    /**
     * The permissions directly inherited the user.
     */
    @IsArray()
    @IsOptional()
    inherited: ResponsePermission[] = new Array<ResponsePermission>();

    /**
     * Creates a ResponseUserPermissions object from a user.
     * @param user The user the response shall be build for.
     */
    public constructor(user: User) {
        for (let permission of user.permissions) {
            this.directlyGranted.push(permission.toResponse());
        }
        for (let permission of user.inheritedPermissions) {
            this.inherited.push(permission.toResponse());
        }
    }
}
