Merge branch 'dev' into feature/13-runner_controllers

This commit is contained in:
Nicolai Ort 2020-12-04 22:43:29 +01:00
commit 48484f04c9
9 changed files with 496 additions and 136 deletions

View File

@ -22,6 +22,7 @@
],
"license": "CC-BY-NC-SA-4.0",
"dependencies": {
"argon2": "^0.27.0",
"body-parser": "^1.19.0",
"class-transformer": "^0.3.1",
"class-validator": "^0.12.2",
@ -39,7 +40,8 @@
"routing-controllers-openapi": "^2.1.0",
"swagger-ui-express": "^4.1.5",
"typeorm": "^0.2.29",
"typeorm-routing-controllers-extensions": "^0.2.0"
"typeorm-routing-controllers-extensions": "^0.2.0",
"uuid": "^8.3.1"
},
"devDependencies": {
"@types/cors": "^2.8.8",
@ -49,6 +51,7 @@
"@types/multer": "^1.4.4",
"@types/node": "^14.14.9",
"@types/swagger-ui-express": "^4.1.2",
"@types/uuid": "^8.3.0",
"dotenv-safe": "^8.2.0",
"nodemon": "^2.0.6",
"sqlite3": "^5.0.0",

View File

@ -0,0 +1,86 @@
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm';
import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
import { UserGroupNotFoundError, UserIdsNotMatchingError, UserNotFoundError } from '../errors/UserErrors';
import { CreateUser } from '../models/creation/CreateUser';
import { User } from '../models/entities/User';
@JsonController('/users')
export class UserController {
private userRepository: Repository<User>;
/**
* Gets the repository of this controller's model/entity.
*/
constructor() {
this.userRepository = getConnectionManager().get().getRepository(User);
}
@Get()
@ResponseSchema(User, { isArray: true })
@OpenAPI({ description: 'Lists all users.' })
getAll() {
return this.userRepository.find();
}
@Get('/:id')
@ResponseSchema(User)
@ResponseSchema(UserNotFoundError, { statusCode: 404 })
@OnUndefined(UserNotFoundError)
@OpenAPI({ description: 'Returns a user of a specified id (if it exists)' })
getOne(@Param('id') id: number) {
return this.userRepository.findOne({ id: id });
}
@Post()
@ResponseSchema(User)
@ResponseSchema(UserGroupNotFoundError)
@OpenAPI({ description: 'Create a new user object (id will be generated automagicly).' })
async post(@Body({ validate: true }) createUser: CreateUser) {
let user;
try {
user = await createUser.toUser();
} catch (error) {
return error;
}
return this.userRepository.save(user);
}
@Put('/:id')
@ResponseSchema(User)
@ResponseSchema(UserNotFoundError, { statusCode: 404 })
@ResponseSchema(UserIdsNotMatchingError, { statusCode: 406 })
@OpenAPI({ description: "Update a user object (id can't be changed)." })
async put(@Param('id') id: number, @EntityFromBody() user: User) {
let oldUser = await this.userRepository.findOne({ id: id });
if (!oldUser) {
throw new UserNotFoundError();
}
if (oldUser.id != user.id) {
throw new UserIdsNotMatchingError();
}
await this.userRepository.update(oldUser, user);
return user;
}
@Delete('/:id')
@ResponseSchema(User)
@ResponseSchema(UserNotFoundError, { statusCode: 404 })
@OpenAPI({ description: 'Delete a specified runner (if it exists).' })
async remove(@Param('id') id: number) {
let runner = await this.userRepository.findOne({ id: id });
if (!runner) {
throw new UserNotFoundError();
}
await this.userRepository.delete(runner);
return runner;
}
}

View File

@ -0,0 +1,86 @@
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm';
import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
import { UserGroupIdsNotMatchingError, UserGroupNotFoundError } from '../errors/UserGroupErrors';
import { CreateUserGroup } from '../models/creation/CreateUserGroup';
import { UserGroup } from '../models/entities/UserGroup';
@JsonController('/usergroups')
export class UserGroupController {
private userGroupsRepository: Repository<UserGroup>;
/**
* Gets the repository of this controller's model/entity.
*/
constructor() {
this.userGroupsRepository = getConnectionManager().get().getRepository(UserGroup);
}
@Get()
@ResponseSchema(UserGroup, { isArray: true })
@OpenAPI({ description: 'Lists all usergroups.' })
getAll() {
return this.userGroupsRepository.find();
}
@Get('/:id')
@ResponseSchema(UserGroup)
@ResponseSchema(UserGroupNotFoundError, { statusCode: 404 })
@OnUndefined(UserGroupNotFoundError)
@OpenAPI({ description: 'Returns a usergroup of a specified id (if it exists)' })
getOne(@Param('id') id: number) {
return this.userGroupsRepository.findOne({ id: id });
}
@Post()
@ResponseSchema(UserGroup)
@ResponseSchema(UserGroupNotFoundError)
@OpenAPI({ description: 'Create a new usergroup object (id will be generated automagicly).' })
async post(@Body({ validate: true }) createUserGroup: CreateUserGroup) {
let userGroup;
try {
userGroup = await createUserGroup.toUserGroup();
} catch (error) {
return error;
}
return this.userGroupsRepository.save(userGroup);
}
@Put('/:id')
@ResponseSchema(UserGroup)
@ResponseSchema(UserGroupNotFoundError, { statusCode: 404 })
@ResponseSchema(UserGroupIdsNotMatchingError, { statusCode: 406 })
@OpenAPI({ description: "Update a usergroup object (id can't be changed)." })
async put(@Param('id') id: number, @EntityFromBody() userGroup: UserGroup) {
let oldUserGroup = await this.userGroupsRepository.findOne({ id: id });
if (!oldUserGroup) {
throw new UserGroupNotFoundError()
}
if (oldUserGroup.id != userGroup.id) {
throw new UserGroupIdsNotMatchingError();
}
await this.userGroupsRepository.update(oldUserGroup, userGroup);
return userGroup;
}
@Delete('/:id')
@ResponseSchema(UserGroup)
@ResponseSchema(UserGroupNotFoundError, { statusCode: 404 })
@OpenAPI({ description: 'Delete a specified usergroup (if it exists).' })
async remove(@Param('id') id: number) {
let userGroup = await this.userGroupsRepository.findOne({ id: id });
if (!userGroup) {
throw new UserGroupNotFoundError();
}
await this.userGroupsRepository.delete(userGroup);
return userGroup;
}
}

47
src/errors/UserErrors.ts Normal file
View File

@ -0,0 +1,47 @@
import { IsString } from 'class-validator';
import { NotAcceptableError, NotFoundError } from 'routing-controllers';
/**
* Error to throw when a usergroup couldn't be found.
*/
export class UserGroupNotFoundError extends NotFoundError {
@IsString()
name = "UserGroupNotFoundError"
@IsString()
message = "User Group not found!"
}
/**
* Error to throw when no username or email is set
*/
export class UsernameOrEmailNeededError extends NotFoundError {
@IsString()
name = "UsernameOrEmailNeededError"
@IsString()
message = "no username or email is set!"
}
/**
* Error to throw when a user couldn't be found.
*/
export class UserNotFoundError extends NotFoundError {
@IsString()
name = "UserNotFoundError"
@IsString()
message = "User not found!"
}
/**
* Error to throw when two users' ids don't match.
* Usually occurs when a user tries to change a user's id.
*/
export class UserIdsNotMatchingError extends NotAcceptableError {
@IsString()
name = "UserIdsNotMatchingError"
@IsString()
message = "The id's don't match!! \n And if you wanted to change a user's id: This isn't allowed"
}

View File

@ -0,0 +1,36 @@
import { IsString } from 'class-validator';
import { NotAcceptableError, NotFoundError } from 'routing-controllers';
/**
* Error to throw when no groupname is set
*/
export class GroupNameNeededError extends NotFoundError {
@IsString()
name = "GroupNameNeededError"
@IsString()
message = "no groupname is set!"
}
/**
* Error to throw when a usergroup couldn't be found.
*/
export class UserGroupNotFoundError extends NotFoundError {
@IsString()
name = "UserGroupNotFoundError"
@IsString()
message = "User Group not found!"
}
/**
* Error to throw when two usergroups' ids don't match.
* Usually occurs when a user tries to change a usergroups's id.
*/
export class UserGroupIdsNotMatchingError extends NotAcceptableError {
@IsString()
name = "UserGroupIdsNotMatchingError"
@IsString()
message = "The id's don't match!! \n If you wanted to change a usergroup's id: This isn't allowed"
}

View File

@ -0,0 +1,76 @@
import * as argon2 from "argon2";
import { IsEmail, IsOptional, IsPhoneNumber, IsString, IsUUID } from 'class-validator';
import { getConnectionManager } from 'typeorm';
import * as uuid from 'uuid';
import { UserGroupNotFoundError, UsernameOrEmailNeededError } from '../../errors/UserErrors';
import { User } from '../entities/User';
import { UserGroup } from '../entities/UserGroup';
export class CreateUser {
@IsString()
firstname: string;
@IsString()
middlename?: string;
@IsOptional()
@IsString()
username?: string;
@IsPhoneNumber("ZZ")
@IsOptional()
phone?: string;
@IsString()
password: string;
@IsString()
lastname: string;
@IsEmail()
@IsString()
email?: string;
@IsOptional()
groupId?: number[] | number
@IsUUID("4")
uuid: string;
public async toUser(): Promise<User> {
let newUser: User = new User();
if (this.email === undefined && this.username === undefined) {
throw new UsernameOrEmailNeededError();
}
if (this.groupId) {
if (!Array.isArray(this.groupId)) {
this.groupId = [this.groupId]
}
const groupIDs: number[] = this.groupId
let errors = 0
const validateusergroups = async () => {
let foundgroups = []
for (const g of groupIDs) {
const found = await getConnectionManager().get().getRepository(UserGroup).find({ id: g });
if (found.length === 0) {
errors++
} else {
foundgroups.push(found[0])
}
}
newUser.groups = foundgroups
}
await validateusergroups()
if (errors !== 0) {
throw new UserGroupNotFoundError();
}
}
const new_uuid = uuid.v4()
newUser.email = this.email
newUser.username = this.username
newUser.firstname = this.firstname
newUser.middlename = this.middlename
newUser.lastname = this.lastname
newUser.uuid = new_uuid
newUser.password = await argon2.hash(this.password + new_uuid);
console.log(newUser)
return newUser;
}
}

View File

@ -0,0 +1,28 @@
import { IsOptional, IsString } from 'class-validator';
import { GroupNameNeededError } from '../../errors/UserGroupErrors';
import { UserGroup } from '../entities/UserGroup';
export class CreateUserGroup {
@IsOptional()
@IsString()
name: string;
@IsOptional()
@IsString()
description?: string;
public async toUserGroup(): Promise<UserGroup> {
let newUserGroup: UserGroup = new UserGroup();
if (this.name === undefined) {
throw new GroupNameNeededError();
}
newUserGroup.name = this.name
if (this.description) {
newUserGroup.description = this.description
}
console.log(newUserGroup)
return newUserGroup;
}
}

View File

@ -1,8 +1,8 @@
import { Entity, Column, OneToMany, ManyToOne, PrimaryGeneratedColumn, Generated, Unique, JoinTable, ManyToMany } from "typeorm";
import { IsBoolean, IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString, isUUID, } from "class-validator";
import { UserGroup } from './UserGroup';
import { IsBoolean, IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString, IsUUID } from "class-validator";
import { Column, Entity, JoinTable, ManyToMany, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { Permission } from './Permission';
import { UserAction } from './UserAction';
import { UserGroup } from './UserGroup';
/**
* Defines a admin user.
@ -18,11 +18,9 @@ export class User {
id: number;
/**
* autogenerated uuid
* uuid
*/
@IsOptional()
@IsInt()
@Generated("uuid")
@IsUUID("4")
uuid: string;
/**

View File

@ -1,17 +1,17 @@
import { PrimaryGeneratedColumn, Column, OneToMany, Entity, ManyToOne } from "typeorm";
import {
IsInt,
IsNotEmpty,
IsOptional,
IsString,
IsString
} from "class-validator";
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import { Permission } from "./Permission";
/**
* Defines the UserGroup interface.
*/
@Entity()
export abstract class UserGroup {
export class UserGroup {
/**
* Autogenerated unique id (primary key).
*/