parent
ff3a5b4545
commit
882065470a
|
@ -1,12 +1,14 @@
|
|||
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, QueryParam } from 'routing-controllers';
|
||||
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers';
|
||||
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
||||
import { getConnectionManager, Repository } from 'typeorm';
|
||||
import { PermissionNotFoundError } from '../errors/PermissionErrors';
|
||||
import { PrincipalNotFoundError } from '../errors/PrincipalsErrors';
|
||||
import { PermissionIdsNotMatchingError, PermissionNeedsPrincipalError, PermissionNotFoundError } from '../errors/PermissionErrors';
|
||||
import { PrincipalNotFoundError } from '../errors/PrincipalErrors';
|
||||
import { CreatePermission } from '../models/actions/CreatePermission';
|
||||
import { UpdatePermission } from '../models/actions/UpdatePermission';
|
||||
import { Permission } from '../models/entities/Permission';
|
||||
import { ResponseEmpty } from '../models/responses/ResponseEmpty';
|
||||
import { ResponsePermission } from '../models/responses/ResponsePermission';
|
||||
import { ResponsePrincipal } from '../models/responses/ResponsePrincipal';
|
||||
|
||||
|
||||
@JsonController('/permissions')
|
||||
|
@ -64,13 +66,15 @@ export class PermissionController {
|
|||
return new ResponsePermission(permission);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@Put('/:id')
|
||||
@ResponseSchema(ResponsePrincipal)
|
||||
@ResponseSchema(PermissionNotFoundError, { statusCode: 404 })
|
||||
@ResponseSchema(PrincipalNotFoundError, { statusCode: 404 })
|
||||
@ResponseSchema(PermissionIdsNotMatchingError, { statusCode: 406 })
|
||||
@OpenAPI({ description: "Update a runnerTeam object (id can't be changed)." })
|
||||
async put(@Param('id') id: number, @EntityFromBody() permission: Permission) {
|
||||
@ResponseSchema(PermissionNeedsPrincipalError, { statusCode: 406 })
|
||||
@OpenAPI({ description: "Update a permission object (id can't be changed)." })
|
||||
async put(@Param('id') id: number, @Body({ validate: true }) permission: UpdatePermission) {
|
||||
let oldPermission = await this.permissionRepository.findOne({ id: id }, { relations: ['principal'] });
|
||||
|
||||
if (!oldPermission) {
|
||||
|
@ -81,10 +85,10 @@ export class PermissionController {
|
|||
throw new PermissionIdsNotMatchingError();
|
||||
}
|
||||
|
||||
await this.permissionRepository.update(oldPermission, permission);
|
||||
await this.permissionRepository.update(oldPermission, await permission.toPermission());
|
||||
|
||||
return new ResponsePermission(await this.permissionRepository.findOne({ id: permission.id }, { relations: ['principal'] }));
|
||||
}*/
|
||||
}
|
||||
|
||||
@Delete('/:id')
|
||||
@ResponseSchema(ResponsePermission)
|
||||
|
|
|
@ -22,4 +22,15 @@ export class PermissionIdsNotMatchingError extends NotAcceptableError {
|
|||
|
||||
@IsString()
|
||||
message = "The id's don't match!! \n And if you wanted to change a permission's id: This isn't allowed"
|
||||
}
|
||||
|
||||
/**
|
||||
* Error to throw when a permission get's provided without a principal.
|
||||
*/
|
||||
export class PermissionNeedsPrincipalError extends NotAcceptableError {
|
||||
@IsString()
|
||||
name = "PermissionNeedsPrincipalError"
|
||||
|
||||
@IsString()
|
||||
message = "You provided no principal for this permission."
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
import { IsString } from 'class-validator';
|
||||
import { NotAcceptableError, NotFoundError } from 'routing-controllers';
|
||||
|
||||
/**
|
||||
* Error to throw when a user couldn't be found.
|
||||
*/
|
||||
export class PrincipalNotFoundError extends NotFoundError {
|
||||
@IsString()
|
||||
name = "PrincipalNotFoundError"
|
||||
|
||||
@IsString()
|
||||
message = "Principal not found!"
|
||||
}
|
||||
|
||||
/**
|
||||
* Error to throw, when a provided runnerOrganisation doesn't belong to the accepted types.
|
||||
*/
|
||||
export class PrincipalWrongTypeError extends NotAcceptableError {
|
||||
@IsString()
|
||||
name = "PrincipalWrongTypeError"
|
||||
|
||||
@IsString()
|
||||
message = "The princial must have an existing principal's id. \n You provided a object of another type."
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
import { IsString } from 'class-validator';
|
||||
import { NotFoundError } from 'routing-controllers';
|
||||
|
||||
/**
|
||||
* Error to throw when a user couldn't be found.
|
||||
*/
|
||||
export class PrincipalNotFoundError extends NotFoundError {
|
||||
@IsString()
|
||||
name = "PrincipalNotFoundError"
|
||||
|
||||
@IsString()
|
||||
message = "Principal not found!"
|
||||
}
|
|
@ -3,7 +3,7 @@ import {
|
|||
IsNotEmpty
|
||||
} from "class-validator";
|
||||
import { getConnectionManager } from 'typeorm';
|
||||
import { PrincipalNotFoundError } from '../../errors/PrincipalsErrors';
|
||||
import { PrincipalNotFoundError } from '../../errors/PrincipalErrors';
|
||||
import { Permission } from '../entities/Permission';
|
||||
import { Principal } from '../entities/Principal';
|
||||
import { PermissionAction } from '../enums/PermissionAction';
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
import { IsInt, IsNotEmpty, IsObject } from 'class-validator';
|
||||
import { getConnectionManager } from 'typeorm';
|
||||
import { PermissionNeedsPrincipalError } from '../../errors/PermissionErrors';
|
||||
import { PrincipalNotFoundError, PrincipalWrongTypeError } from '../../errors/PrincipalErrors';
|
||||
import { Permission } from '../entities/Permission';
|
||||
import { Principal } from '../entities/Principal';
|
||||
import { PermissionAction } from '../enums/PermissionAction';
|
||||
import { PermissionTarget } from '../enums/PermissionTargets';
|
||||
|
||||
export class UpdatePermission {
|
||||
|
||||
/**
|
||||
* The updated runner's id.
|
||||
*/
|
||||
@IsInt()
|
||||
id: number;
|
||||
|
||||
/**
|
||||
* The permissions's principal's id.
|
||||
*/
|
||||
@IsObject()
|
||||
@IsNotEmpty()
|
||||
principal: Principal;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Manages all the different ways a group can be provided.
|
||||
*/
|
||||
public async getPrincipal(): Promise<Principal> {
|
||||
if (this.principal === undefined) {
|
||||
throw new PermissionNeedsPrincipalError();
|
||||
}
|
||||
if (!isNaN(this.principal.id)) {
|
||||
let principal = await getConnectionManager().get().getRepository(Principal).findOne({ id: this.principal.id });
|
||||
if (!principal) { throw new PrincipalNotFoundError(); }
|
||||
return principal;
|
||||
}
|
||||
|
||||
throw new PrincipalWrongTypeError();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue