Implemented permission getting

ref #6
This commit is contained in:
Nicolai Ort 2020-12-18 15:12:06 +01:00
parent 388fc6ba6a
commit d89fcb84a2
5 changed files with 180 additions and 10 deletions

View File

@ -0,0 +1,109 @@
import { Get, JsonController } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm';
import { Permission } from '../models/entities/Permission';
import { ResponsePermission } from '../models/responses/ResponsePermission';
import { ResponseRunnerTeam } from '../models/responses/ResponseRunnerTeam';
@JsonController('/permissions')
//@Authorized('RUNNERS:read')
export class PermissionController {
private permissionController: Repository<Permission>;
/**
* Gets the repository of this controller's model/entity.
*/
constructor() {
this.permissionController = getConnectionManager().get().getRepository(Permission);
}
@Get()
@ResponseSchema(ResponseRunnerTeam, { isArray: true })
@OpenAPI({ description: 'Lists all runnerTeams.' })
async getAll() {
let responsePermissions: ResponsePermission[] = new Array<ResponsePermission>();
const permissions = await this.permissionController.find({ relations: ['principal'] });
permissions.forEach(permission => {
responsePermissions.push(new ResponsePermission(permission));
});
return responsePermissions;
}
/*
@Get('/:id')
@ResponseSchema(ResponseRunnerTeam)
@ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 })
@OnUndefined(RunnerTeamNotFoundError)
@OpenAPI({ description: 'Returns a runnerTeam of a specified id (if it exists)' })
async getOne(@Param('id') id: number) {
let runnerTeam = await this.runnerTeamRepository.findOne({ id: id }, { relations: ['parentGroup', 'contact'] });
if (!runnerTeam) { throw new RunnerTeamNotFoundError(); }
return new ResponseRunnerTeam(runnerTeam);
}
@Post()
@ResponseSchema(ResponseRunnerTeam)
@OpenAPI({ description: 'Create a new runnerTeam object (id will be generated automagicly).' })
async post(@Body({ validate: true }) createRunnerTeam: CreateRunnerTeam) {
let runnerTeam;
try {
runnerTeam = await createRunnerTeam.toRunnerTeam();
} catch (error) {
throw error;
}
runnerTeam = await this.runnerTeamRepository.save(runnerTeam);
runnerTeam = await this.runnerTeamRepository.findOne(runnerTeam, { relations: ['parentGroup', 'contact'] });
return new ResponseRunnerTeam(runnerTeam);
}
@Put('/:id')
@ResponseSchema(ResponseRunnerTeam)
@ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 })
@ResponseSchema(RunnerTeamIdsNotMatchingError, { statusCode: 406 })
@OpenAPI({ description: "Update a runnerTeam object (id can't be changed)." })
async put(@Param('id') id: number, @Body({ validate: true }) runnerTeam: UpdateRunnerTeam) {
let oldRunnerTeam = await this.runnerTeamRepository.findOne({ id: id }, { relations: ['parentGroup', 'contact'] });
if (!oldRunnerTeam) {
throw new RunnerTeamNotFoundError();
}
if (oldRunnerTeam.id != runnerTeam.id) {
throw new RunnerTeamIdsNotMatchingError();
}
await this.runnerTeamRepository.update(oldRunnerTeam, await runnerTeam.toRunnerTeam());
return new ResponseRunnerTeam(await this.runnerTeamRepository.findOne({ id: runnerTeam.id }, { relations: ['parentGroup', 'contact'] }));
}
@Delete('/:id')
@ResponseSchema(ResponseRunnerTeam)
@ResponseSchema(ResponseEmpty, { statusCode: 204 })
@ResponseSchema(RunnerTeamHasRunnersError, { statusCode: 406 })
@OnUndefined(204)
@OpenAPI({ description: 'Delete a specified runnerTeam (if it exists).' })
async remove(@Param("id") id: number, @QueryParam("force") force: boolean) {
let team = await this.runnerTeamRepository.findOne({ id: id });
if (!team) { return null; }
let runnerTeam = await this.runnerTeamRepository.findOne(team, { relations: ['parentGroup', 'contact', 'runners'] });
if (!force) {
if (runnerTeam.runners.length != 0) {
throw new RunnerTeamHasRunnersError();
}
}
const runnerController = new RunnerController()
for (let runner of runnerTeam.runners) {
await runnerController.remove(runner.id, true);
}
const responseTeam = new ResponseRunnerTeam(runnerTeam);
await this.runnerTeamRepository.delete(team);
return responseTeam;
}
*/
}

View File

@ -10,7 +10,7 @@ import { Principal } from './Principal';
* Defines the Permission interface.
*/
@Entity()
export abstract class Permission {
export class Permission {
/**
* Autogenerated unique id (primary key).
*/

View File

@ -0,0 +1,46 @@
import {
IsInt,
IsNotEmpty,
IsObject
} from "class-validator";
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 ResponsePermission {
/**
* Autogenerated unique id (primary key).
*/
@IsInt()
id: number;;
/**
* The permissions's principal.
*/
@IsObject()
@IsNotEmpty()
principal: Principal;
/**
* The permissions's target.
*/
@IsNotEmpty()
target: PermissionTarget;
/**
* The permissions's action.
*/
@IsNotEmpty()
action: PermissionAction;
public constructor(permission: Permission) {
this.id = permission.id;
this.principal = permission.principal;
this.target = permission.target;
this.action = permission.action;
}
}

View File

@ -0,0 +1,20 @@
import {
IsInt
} from "class-validator";
import { Principal } from '../entities/Principal';
/**
* Defines Principal's response class.
*/
export abstract class ResponsePrincipal {
/**
* Autogenerated unique id (primary key).
*/
@IsInt()
id: number;
public constructor(principal: Principal) {
this.id = principal.id;
}
}

View File

@ -1,24 +1,19 @@
import {
IsArray,
IsBoolean,
IsInt,
IsOptional,
IsString
} from "class-validator";
import { Permission } from '../entities/Permission';
import { User } from '../entities/User';
import { UserGroup } from '../entities/UserGroup';
import { ResponsePrincipal } from './ResponsePrincipal';
/**
* Defines a user response.
*/
export class ResponseUser {
/**
* Autogenerated unique id (primary key).
*/
@IsInt()
id: number;;
export class ResponseUser extends ResponsePrincipal {
/**
* The user's first name.
*/
@ -80,7 +75,7 @@ export class ResponseUser {
permissions: Permission[];
public constructor(user: User) {
this.id = user.id;
super(user);
this.firstname = user.firstname;
this.middlename = user.middlename;
this.lastname = user.lastname;