55 lines
1.4 KiB
TypeScript
55 lines
1.4 KiB
TypeScript
import {
|
|
IsInt,
|
|
IsNotEmpty
|
|
} from "class-validator";
|
|
import { getConnectionManager } from 'typeorm';
|
|
import { PrincipalNotFoundError } from '../../errors/PrincipalErrors';
|
|
import { Permission } from '../entities/Permission';
|
|
import { Principal } from '../entities/Principal';
|
|
import { PermissionAction } from '../enums/PermissionAction';
|
|
import { PermissionTarget } from '../enums/PermissionTargets';
|
|
|
|
/**
|
|
* Defines a track of given length.
|
|
*/
|
|
export class CreatePermission {
|
|
|
|
/**
|
|
* The permissions's principal's id.
|
|
*/
|
|
@IsInt()
|
|
@IsNotEmpty()
|
|
principal: number;
|
|
|
|
/**
|
|
* The permissions's target.
|
|
*/
|
|
@IsNotEmpty()
|
|
target: PermissionTarget;
|
|
|
|
/**
|
|
* The permissions's action.
|
|
*/
|
|
@IsNotEmpty()
|
|
action: PermissionAction;
|
|
|
|
/**
|
|
* Converts a Permission object based on this.
|
|
*/
|
|
public async toPermission(): Promise<Permission> {
|
|
let newPermission: Permission = new Permission();
|
|
|
|
newPermission.principal = await this.getPrincipal();
|
|
newPermission.target = this.target;
|
|
newPermission.action = this.action;
|
|
|
|
return newPermission;
|
|
}
|
|
|
|
public async getPrincipal(): Promise<Principal> {
|
|
let principal = await getConnectionManager().get().getRepository(Principal).findOne({ id: this.principal })
|
|
if (!principal) { throw new PrincipalNotFoundError(); }
|
|
return principal;
|
|
}
|
|
}
|