54
src/models/actions/CreatePermission.ts
Normal file
54
src/models/actions/CreatePermission.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import {
|
||||
IsInt,
|
||||
IsNotEmpty
|
||||
} from "class-validator";
|
||||
import { getConnectionManager } from 'typeorm';
|
||||
import { PrincipalNotFoundError } from '../../errors/PrincipalsErrors';
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user