backend/src/models/responses/ResponseUser.ts
Nicolai Ort b01e1eb8a1
All checks were successful
continuous-integration/drone/pr Build is passing
Added a new endpoint that returns a users permissions as objects sorted into two arrays
ref #93
2021-01-13 18:19:59 +01:00

98 lines
2.0 KiB
TypeScript

import {
IsArray,
IsBoolean,
IsOptional,
IsString
} from "class-validator";
import { User } from '../entities/User';
import { UserGroup } from '../entities/UserGroup';
import { ResponsePrincipal } from './ResponsePrincipal';
/**
* Defines the user response.
*/
export class ResponseUser extends ResponsePrincipal {
/**
* 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: UserGroup[];
/**
* 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 = user.groups;
this.permissions = user.allPermissions;
this.groups.forEach(function (g) { delete g.permissions });
}
}