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