parent
ebb0c5faca
commit
dc485c02ea
@ -1,7 +1,8 @@
|
|||||||
import { Get, JsonController, OnUndefined, Param } from 'routing-controllers';
|
import { Body, Get, JsonController, OnUndefined, Param, Post } from 'routing-controllers';
|
||||||
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
||||||
import { getConnectionManager, Repository } from 'typeorm';
|
import { getConnectionManager, Repository } from 'typeorm';
|
||||||
import { PermissionNotFoundError } from '../errors/PermissionErrors';
|
import { PermissionNotFoundError } from '../errors/PermissionErrors';
|
||||||
|
import { CreatePermission } from '../models/actions/CreatePermission';
|
||||||
import { Permission } from '../models/entities/Permission';
|
import { Permission } from '../models/entities/Permission';
|
||||||
import { ResponsePermission } from '../models/responses/ResponsePermission';
|
import { ResponsePermission } from '../models/responses/ResponsePermission';
|
||||||
|
|
||||||
@ -9,13 +10,13 @@ import { ResponsePermission } from '../models/responses/ResponsePermission';
|
|||||||
@JsonController('/permissions')
|
@JsonController('/permissions')
|
||||||
//@Authorized('RUNNERS:read')
|
//@Authorized('RUNNERS:read')
|
||||||
export class PermissionController {
|
export class PermissionController {
|
||||||
private permissionController: Repository<Permission>;
|
private permissionRepository: Repository<Permission>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the repository of this controller's model/entity.
|
* Gets the repository of this controller's model/entity.
|
||||||
*/
|
*/
|
||||||
constructor() {
|
constructor() {
|
||||||
this.permissionController = getConnectionManager().get().getRepository(Permission);
|
this.permissionRepository = getConnectionManager().get().getRepository(Permission);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
@ -23,7 +24,7 @@ export class PermissionController {
|
|||||||
@OpenAPI({ description: 'Lists all permissions.' })
|
@OpenAPI({ description: 'Lists all permissions.' })
|
||||||
async getAll() {
|
async getAll() {
|
||||||
let responsePermissions: ResponsePermission[] = new Array<ResponsePermission>();
|
let responsePermissions: ResponsePermission[] = new Array<ResponsePermission>();
|
||||||
const permissions = await this.permissionController.find({ relations: ['principal'] });
|
const permissions = await this.permissionRepository.find({ relations: ['principal'] });
|
||||||
permissions.forEach(permission => {
|
permissions.forEach(permission => {
|
||||||
responsePermissions.push(new ResponsePermission(permission));
|
responsePermissions.push(new ResponsePermission(permission));
|
||||||
});
|
});
|
||||||
@ -37,27 +38,27 @@ export class PermissionController {
|
|||||||
@OnUndefined(PermissionNotFoundError)
|
@OnUndefined(PermissionNotFoundError)
|
||||||
@OpenAPI({ description: 'Returns a permissions of a specified id (if it exists)' })
|
@OpenAPI({ description: 'Returns a permissions of a specified id (if it exists)' })
|
||||||
async getOne(@Param('id') id: number) {
|
async getOne(@Param('id') id: number) {
|
||||||
let permission = await this.permissionController.findOne({ id: id }, { relations: ['principal'] });
|
let permission = await this.permissionRepository.findOne({ id: id }, { relations: ['principal'] });
|
||||||
if (!permission) { throw new PermissionNotFoundError(); }
|
if (!permission) { throw new PermissionNotFoundError(); }
|
||||||
return new ResponsePermission(permission);
|
return new ResponsePermission(permission);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
@Post()
|
@Post()
|
||||||
@ResponseSchema(ResponseRunnerTeam)
|
@ResponseSchema(ResponsePermission)
|
||||||
@OpenAPI({ description: 'Create a new runnerTeam object (id will be generated automagicly).' })
|
@OpenAPI({ description: 'Create a new runnerTeam object (id will be generated automagicly).' })
|
||||||
async post(@Body({ validate: true }) createRunnerTeam: CreateRunnerTeam) {
|
async post(@Body({ validate: true }) createPermission: CreatePermission) {
|
||||||
let runnerTeam;
|
let permission;
|
||||||
try {
|
try {
|
||||||
runnerTeam = await createRunnerTeam.toRunnerTeam();
|
permission = await createPermission.toPermission();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
runnerTeam = await this.runnerTeamRepository.save(runnerTeam);
|
permission = await this.permissionRepository.save(permission);
|
||||||
runnerTeam = await this.runnerTeamRepository.findOne(runnerTeam, { relations: ['parentGroup', 'contact'] });
|
permission = await this.permissionRepository.findOne(permission, { relations: ['principal'] });
|
||||||
|
|
||||||
return new ResponseRunnerTeam(runnerTeam);
|
return new ResponsePermission(permission);
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
@Put('/:id')
|
@Put('/:id')
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user