113 lines
5.2 KiB
TypeScript
113 lines
5.2 KiB
TypeScript
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 { 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')
|
|
//@Authorized('RUNNERS:read')
|
|
export class PermissionController {
|
|
private permissionRepository: Repository<Permission>;
|
|
|
|
/**
|
|
* Gets the repository of this controller's model/entity.
|
|
*/
|
|
constructor() {
|
|
this.permissionRepository = getConnectionManager().get().getRepository(Permission);
|
|
}
|
|
|
|
@Get()
|
|
@ResponseSchema(ResponsePermission, { isArray: true })
|
|
@OpenAPI({ description: 'Lists all permissions.' })
|
|
async getAll() {
|
|
let responsePermissions: ResponsePermission[] = new Array<ResponsePermission>();
|
|
const permissions = await this.permissionRepository.find({ relations: ['principal'] });
|
|
permissions.forEach(permission => {
|
|
responsePermissions.push(new ResponsePermission(permission));
|
|
});
|
|
return responsePermissions;
|
|
}
|
|
|
|
|
|
@Get('/:id')
|
|
@ResponseSchema(ResponsePermission)
|
|
@ResponseSchema(PermissionNotFoundError, { statusCode: 404 })
|
|
@OnUndefined(PermissionNotFoundError)
|
|
@OpenAPI({ description: 'Returns a permissions of a specified id (if it exists)' })
|
|
async getOne(@Param('id') id: number) {
|
|
let permission = await this.permissionRepository.findOne({ id: id }, { relations: ['principal'] });
|
|
if (!permission) { throw new PermissionNotFoundError(); }
|
|
return new ResponsePermission(permission);
|
|
}
|
|
|
|
|
|
@Post()
|
|
@ResponseSchema(ResponsePermission)
|
|
@ResponseSchema(PrincipalNotFoundError, { statusCode: 404 })
|
|
@OpenAPI({ description: 'Create a new runnerTeam object (id will be generated automagicly).' })
|
|
async post(@Body({ validate: true }) createPermission: CreatePermission) {
|
|
let permission;
|
|
try {
|
|
permission = await createPermission.toPermission();
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
let existingPermission = await this.permissionRepository.findOne({ target: permission.target, action: permission.action, principal: permission.principal }, { relations: ['principal'] });
|
|
if (existingPermission) { return new ResponsePermission(existingPermission); }
|
|
|
|
permission = await this.permissionRepository.save(permission);
|
|
permission = await this.permissionRepository.findOne(permission, { relations: ['principal'] });
|
|
|
|
return new ResponsePermission(permission);
|
|
}
|
|
|
|
|
|
@Put('/:id')
|
|
@ResponseSchema(ResponsePrincipal)
|
|
@ResponseSchema(PermissionNotFoundError, { statusCode: 404 })
|
|
@ResponseSchema(PrincipalNotFoundError, { statusCode: 404 })
|
|
@ResponseSchema(PermissionIdsNotMatchingError, { statusCode: 406 })
|
|
@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) {
|
|
throw new PermissionNotFoundError();
|
|
}
|
|
|
|
if (oldPermission.id != permission.id) {
|
|
throw new PermissionIdsNotMatchingError();
|
|
}
|
|
let existingPermission = await this.permissionRepository.findOne({ target: permission.target, action: permission.action, principal: permission.principal }, { relations: ['principal'] });
|
|
if (existingPermission) {
|
|
await this.remove(permission.id, true);
|
|
return new ResponsePermission(existingPermission);
|
|
}
|
|
|
|
await this.permissionRepository.update(oldPermission, await permission.toPermission());
|
|
|
|
return new ResponsePermission(await this.permissionRepository.findOne({ id: permission.id }, { relations: ['principal'] }));
|
|
}
|
|
|
|
@Delete('/:id')
|
|
@ResponseSchema(ResponsePermission)
|
|
@ResponseSchema(ResponseEmpty, { statusCode: 204 })
|
|
@OnUndefined(204)
|
|
@OpenAPI({ description: 'Delete a specified permission (if it exists).' })
|
|
async remove(@Param("id") id: number, @QueryParam("force") force: boolean) {
|
|
let permission = await this.permissionRepository.findOne({ id: id }, { relations: ['principal'] });
|
|
if (!permission) { return null; }
|
|
|
|
const responsePermission = new ResponsePermission(permission);
|
|
await this.permissionRepository.delete(permission);
|
|
return responsePermission;
|
|
}
|
|
} |