parent
cc5a30980a
commit
efecffb72d
@ -1,4 +1,4 @@
|
|||||||
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put } from 'routing-controllers';
|
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers';
|
||||||
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
||||||
import { getConnectionManager, Repository } from 'typeorm';
|
import { getConnectionManager, Repository } from 'typeorm';
|
||||||
import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
|
import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
|
||||||
@ -7,6 +7,7 @@ import { UserGroupNotFoundError } from '../errors/UserGroupErrors';
|
|||||||
import { CreateUser } from '../models/actions/CreateUser';
|
import { CreateUser } from '../models/actions/CreateUser';
|
||||||
import { User } from '../models/entities/User';
|
import { User } from '../models/entities/User';
|
||||||
import { ResponseEmpty } from '../models/responses/ResponseEmpty';
|
import { ResponseEmpty } from '../models/responses/ResponseEmpty';
|
||||||
|
import { ResponseUser } from '../models/responses/ResponseUser';
|
||||||
|
|
||||||
|
|
||||||
@JsonController('/users')
|
@JsonController('/users')
|
||||||
@ -23,8 +24,13 @@ export class UserController {
|
|||||||
@Get()
|
@Get()
|
||||||
@ResponseSchema(User, { isArray: true })
|
@ResponseSchema(User, { isArray: true })
|
||||||
@OpenAPI({ description: 'Lists all users.' })
|
@OpenAPI({ description: 'Lists all users.' })
|
||||||
getAll() {
|
async getAll() {
|
||||||
return this.userRepository.find();
|
let responseUsers: ResponseUser[] = new Array<ResponseUser>();
|
||||||
|
const users = await this.userRepository.find({ relations: ['permissions', 'groups'] });
|
||||||
|
users.forEach(user => {
|
||||||
|
responseUsers.push(new ResponseUser(user));
|
||||||
|
});
|
||||||
|
return responseUsers;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get('/:id')
|
@Get('/:id')
|
||||||
@ -32,8 +38,10 @@ export class UserController {
|
|||||||
@ResponseSchema(UserNotFoundError, { statusCode: 404 })
|
@ResponseSchema(UserNotFoundError, { statusCode: 404 })
|
||||||
@OnUndefined(UserNotFoundError)
|
@OnUndefined(UserNotFoundError)
|
||||||
@OpenAPI({ description: 'Returns a user of a specified id (if it exists)' })
|
@OpenAPI({ description: 'Returns a user of a specified id (if it exists)' })
|
||||||
getOne(@Param('id') id: number) {
|
async getOne(@Param('id') id: number) {
|
||||||
return this.userRepository.findOne({ id: id });
|
let user = await this.userRepository.findOne({ id: id }, { relations: ['permissions', 'groups'] })
|
||||||
|
if (!user) { throw new UserNotFoundError(); }
|
||||||
|
return new ResponseUser(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
@ -48,7 +56,8 @@ export class UserController {
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.userRepository.save(user);
|
user = await this.userRepository.save(user)
|
||||||
|
return new ResponseUser(await this.userRepository.findOne(user, { relations: ['permissions', 'groups'] }));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Put('/:id')
|
@Put('/:id')
|
||||||
@ -57,7 +66,7 @@ export class UserController {
|
|||||||
@ResponseSchema(UserIdsNotMatchingError, { statusCode: 406 })
|
@ResponseSchema(UserIdsNotMatchingError, { statusCode: 406 })
|
||||||
@OpenAPI({ description: "Update a user object (id can't be changed)." })
|
@OpenAPI({ description: "Update a user object (id can't be changed)." })
|
||||||
async put(@Param('id') id: number, @EntityFromBody() user: User) {
|
async put(@Param('id') id: number, @EntityFromBody() user: User) {
|
||||||
let oldUser = await this.userRepository.findOne({ id: id });
|
let oldUser = await this.userRepository.findOne({ id: id }, { relations: ['permissions', 'groups'] });
|
||||||
|
|
||||||
if (!oldUser) {
|
if (!oldUser) {
|
||||||
throw new UserNotFoundError();
|
throw new UserNotFoundError();
|
||||||
@ -68,7 +77,7 @@ export class UserController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await this.userRepository.update(oldUser, user);
|
await this.userRepository.update(oldUser, user);
|
||||||
return user;
|
return new ResponseUser(await this.userRepository.findOne({ id: id }, { relations: ['permissions', 'groups'] }));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete('/:id')
|
@Delete('/:id')
|
||||||
@ -76,13 +85,16 @@ export class UserController {
|
|||||||
@ResponseSchema(ResponseEmpty, { statusCode: 204 })
|
@ResponseSchema(ResponseEmpty, { statusCode: 204 })
|
||||||
@OnUndefined(204)
|
@OnUndefined(204)
|
||||||
@OpenAPI({ description: 'Delete a specified runner (if it exists).' })
|
@OpenAPI({ description: 'Delete a specified runner (if it exists).' })
|
||||||
async remove(@Param("id") id: number) {
|
async remove(@Param("id") id: number, @QueryParam("force") force: boolean) {
|
||||||
let user = await this.userRepository.findOne({ id: id });
|
let runner = await this.userRepository.findOne({ id: id });
|
||||||
if (!user) {
|
if (!runner) { return null; }
|
||||||
return null;
|
const responseUser = await this.userRepository.findOne(runner, { relations: ['permissions', 'groups'] });
|
||||||
|
|
||||||
|
if (!runner) {
|
||||||
|
throw new UserNotFoundError();
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.userRepository.delete(user);
|
await this.userRepository.delete(runner);
|
||||||
return user;
|
return new ResponseUser(responseUser);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
94
src/models/responses/ResponseUser.ts
Normal file
94
src/models/responses/ResponseUser.ts
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
import {
|
||||||
|
IsArray,
|
||||||
|
IsBoolean,
|
||||||
|
IsInt,
|
||||||
|
IsOptional,
|
||||||
|
IsString
|
||||||
|
} from "class-validator";
|
||||||
|
import { Permission } from '../entities/Permission';
|
||||||
|
import { User } from '../entities/User';
|
||||||
|
import { UserGroup } from '../entities/UserGroup';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines a user response.
|
||||||
|
*/
|
||||||
|
export class ResponseUser {
|
||||||
|
/**
|
||||||
|
* Autogenerated unique id (primary key).
|
||||||
|
*/
|
||||||
|
@IsInt()
|
||||||
|
id: number;;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The user's first name.
|
||||||
|
*/
|
||||||
|
@IsString()
|
||||||
|
firstname: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The user's middle name.
|
||||||
|
* Optional.
|
||||||
|
*/
|
||||||
|
@IsString()
|
||||||
|
middlename?: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The user's last name.
|
||||||
|
*/
|
||||||
|
@IsString()
|
||||||
|
lastname: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The user's phone number.
|
||||||
|
* Optional.
|
||||||
|
*/
|
||||||
|
@IsString()
|
||||||
|
phone?: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The user's e-mail address.
|
||||||
|
* Optional.
|
||||||
|
*/
|
||||||
|
@IsString()
|
||||||
|
email?: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* is user enabled?
|
||||||
|
*/
|
||||||
|
@IsBoolean()
|
||||||
|
enabled: boolean = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* profilepic
|
||||||
|
*/
|
||||||
|
@IsString()
|
||||||
|
@IsOptional()
|
||||||
|
profilePic?: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Groups
|
||||||
|
*/
|
||||||
|
@IsArray()
|
||||||
|
@IsOptional()
|
||||||
|
groups: UserGroup[];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* permissions
|
||||||
|
*/
|
||||||
|
@IsArray()
|
||||||
|
@IsOptional()
|
||||||
|
permissions: Permission[];
|
||||||
|
|
||||||
|
public constructor(user: User) {
|
||||||
|
this.id = user.id;
|
||||||
|
this.firstname = user.firstname;
|
||||||
|
this.middlename = user.middlename;
|
||||||
|
this.lastname = user.lastname;
|
||||||
|
this.phone = user.phone;
|
||||||
|
this.email = user.email;
|
||||||
|
this.enabled = user.enabled;
|
||||||
|
this.profilePic = user.profilePic;
|
||||||
|
this.groups = user.groups;
|
||||||
|
this.permissions = user.permissions;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user