2016-03-18 22:36:58 +02:00
|
|
|
import {NextFunction, Request, Response} from "express";
|
2017-07-15 12:47:11 +02:00
|
|
|
import {ErrorCodes, ErrorDTO} from "../../../common/entities/Error";
|
2016-05-25 20:17:42 +02:00
|
|
|
import {ObjectManagerRepository} from "../../model/ObjectManagerRepository";
|
2016-07-09 15:08:36 +02:00
|
|
|
import {Utils} from "../../../common/Utils";
|
2017-06-04 15:25:08 +02:00
|
|
|
import {Config} from "../../../common/config/private/Config";
|
2016-03-18 22:36:58 +02:00
|
|
|
|
2016-03-26 12:19:10 +02:00
|
|
|
export class UserMWs {
|
2016-03-18 22:36:58 +02:00
|
|
|
|
2017-07-03 19:17:49 +02:00
|
|
|
public static async changePassword(req: Request, res: Response, next: NextFunction) {
|
|
|
|
if (Config.Client.authenticationRequired === false) {
|
2017-07-15 12:47:11 +02:00
|
|
|
return next(new ErrorDTO(ErrorCodes.USER_MANAGEMENT_DISABLED));
|
2016-03-18 22:36:58 +02:00
|
|
|
}
|
2017-07-03 19:17:49 +02:00
|
|
|
if ((typeof req.body === 'undefined') || (typeof req.body.userModReq === 'undefined')
|
|
|
|
|| (typeof req.body.userModReq.id === 'undefined')
|
|
|
|
|| (typeof req.body.userModReq.oldPassword === 'undefined')
|
|
|
|
|| (typeof req.body.userModReq.newPassword === 'undefined')) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await ObjectManagerRepository.getInstance().UserManager.changePassword(req.body.userModReq);
|
|
|
|
return next();
|
2016-03-19 18:31:42 +02:00
|
|
|
|
2017-07-03 19:17:49 +02:00
|
|
|
} catch (err) {
|
2017-07-15 12:47:11 +02:00
|
|
|
return next(new ErrorDTO(ErrorCodes.GENERAL_ERROR, null, err));
|
2017-07-03 19:17:49 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-09 17:04:56 +02:00
|
|
|
|
2016-03-19 18:31:42 +02:00
|
|
|
|
2017-07-03 19:17:49 +02:00
|
|
|
public static async createUser(req: Request, res: Response, next: NextFunction) {
|
|
|
|
if (Config.Client.authenticationRequired === false) {
|
2017-07-15 12:47:11 +02:00
|
|
|
return next(new ErrorDTO(ErrorCodes.USER_MANAGEMENT_DISABLED));
|
2017-07-03 19:17:49 +02:00
|
|
|
}
|
|
|
|
if ((typeof req.body === 'undefined') || (typeof req.body.newUser === 'undefined')) {
|
|
|
|
return next();
|
|
|
|
}
|
2016-03-19 18:31:42 +02:00
|
|
|
|
2017-07-03 19:17:49 +02:00
|
|
|
try {
|
|
|
|
await ObjectManagerRepository.getInstance().UserManager.createUser(req.body.newUser);
|
|
|
|
return next();
|
2016-03-19 18:31:42 +02:00
|
|
|
|
2017-07-03 19:17:49 +02:00
|
|
|
} catch (err) {
|
2017-07-15 12:47:11 +02:00
|
|
|
return next(new ErrorDTO(ErrorCodes.USER_CREATION_ERROR, null, err));
|
2016-03-18 22:36:58 +02:00
|
|
|
}
|
2016-03-19 18:31:42 +02:00
|
|
|
|
2016-03-18 22:36:58 +02:00
|
|
|
|
2017-07-03 19:17:49 +02:00
|
|
|
}
|
2016-03-18 22:36:58 +02:00
|
|
|
|
2017-07-03 19:17:49 +02:00
|
|
|
public static async deleteUser(req: Request, res: Response, next: NextFunction) {
|
|
|
|
if (Config.Client.authenticationRequired === false) {
|
2017-07-15 12:47:11 +02:00
|
|
|
return next(new ErrorDTO(ErrorCodes.USER_MANAGEMENT_DISABLED));
|
2017-07-03 19:17:49 +02:00
|
|
|
}
|
|
|
|
if ((typeof req.params === 'undefined') || (typeof req.params.id === 'undefined')) {
|
|
|
|
return next();
|
|
|
|
}
|
2016-03-18 22:36:58 +02:00
|
|
|
|
2016-03-19 18:31:42 +02:00
|
|
|
|
2017-07-03 19:17:49 +02:00
|
|
|
try {
|
|
|
|
await ObjectManagerRepository.getInstance().UserManager.deleteUser(req.params.id);
|
|
|
|
return next();
|
|
|
|
|
|
|
|
} catch (err) {
|
2017-07-15 12:47:11 +02:00
|
|
|
return next(new ErrorDTO(ErrorCodes.GENERAL_ERROR, null, err));
|
2016-03-19 18:31:42 +02:00
|
|
|
}
|
|
|
|
|
2017-07-03 19:17:49 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async changeRole(req: Request, res: Response, next: NextFunction) {
|
|
|
|
if (Config.Client.authenticationRequired === false) {
|
2017-07-15 12:47:11 +02:00
|
|
|
return next(new ErrorDTO(ErrorCodes.USER_MANAGEMENT_DISABLED));
|
2017-07-03 19:17:49 +02:00
|
|
|
}
|
|
|
|
if ((typeof req.params === 'undefined') || (typeof req.params.id === 'undefined')
|
|
|
|
|| (typeof req.body === 'undefined') || (typeof req.body.newRole === 'undefined')) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await ObjectManagerRepository.getInstance().UserManager.changeRole(req.params.id, req.body.newRole);
|
|
|
|
return next();
|
|
|
|
|
|
|
|
} catch (err) {
|
2017-07-15 12:47:11 +02:00
|
|
|
return next(new ErrorDTO(ErrorCodes.GENERAL_ERROR, null, err));
|
2017-07-03 19:17:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static async listUsers(req: Request, res: Response, next: NextFunction) {
|
|
|
|
if (Config.Client.authenticationRequired === false) {
|
2017-07-15 12:47:11 +02:00
|
|
|
return next(new ErrorDTO(ErrorCodes.USER_MANAGEMENT_DISABLED));
|
2016-03-18 22:36:58 +02:00
|
|
|
}
|
|
|
|
|
2017-07-03 19:17:49 +02:00
|
|
|
try {
|
|
|
|
let result = await ObjectManagerRepository.getInstance().UserManager.find({});
|
|
|
|
result = Utils.clone(result);
|
|
|
|
for (let i = 0; i < result.length; i++) {
|
|
|
|
result[i].password = "";
|
|
|
|
}
|
|
|
|
req.resultPipe = result;
|
2017-07-08 12:43:42 +02:00
|
|
|
next();
|
2017-07-03 19:17:49 +02:00
|
|
|
} catch (err) {
|
2017-07-15 12:47:11 +02:00
|
|
|
return next(new ErrorDTO(ErrorCodes.GENERAL_ERROR, null, err));
|
2016-03-18 22:36:58 +02:00
|
|
|
}
|
2017-07-03 19:17:49 +02:00
|
|
|
}
|
2016-03-19 18:31:42 +02:00
|
|
|
|
|
|
|
|
2017-07-03 19:17:49 +02:00
|
|
|
}
|