2016-03-18 22:36:58 +02:00
|
|
|
import {NextFunction, Request, Response} from "express";
|
2016-05-25 20:17:42 +02:00
|
|
|
import {Error, ErrorCodes} from "../../../common/entities/Error";
|
|
|
|
import {ObjectManagerRepository} from "../../model/ObjectManagerRepository";
|
2016-12-27 17:09:47 +02:00
|
|
|
import {UserDTO} from "../../../common/entities/UserDTO";
|
2016-07-07 12:26:36 +02:00
|
|
|
import {Config} from "../../config/Config";
|
2016-07-09 15:08:36 +02:00
|
|
|
import {Utils} from "../../../common/Utils";
|
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
|
|
|
|
2016-05-09 17:04:56 +02:00
|
|
|
public static changePassword(req:Request, res:Response, next:NextFunction) {
|
2016-07-07 12:26:36 +02:00
|
|
|
if (Config.Client.authenticationRequired === false) {
|
|
|
|
return next(new Error(ErrorCodes.USER_MANAGEMENT_DISABLED));
|
|
|
|
}
|
2016-03-19 18:31:42 +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();
|
2016-03-18 22:36:58 +02:00
|
|
|
}
|
2016-04-22 13:23:44 +02:00
|
|
|
|
2016-05-09 17:04:56 +02:00
|
|
|
ObjectManagerRepository.getInstance().getUserManager().changePassword(req.body.userModReq, (err, result) => {
|
2016-03-19 18:31:42 +02:00
|
|
|
if ((err) || (!result)) {
|
2016-03-26 12:19:10 +02:00
|
|
|
return next(new Error(ErrorCodes.GENERAL_ERROR));
|
2016-03-19 18:31:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return next();
|
|
|
|
});
|
2016-03-18 22:36:58 +02:00
|
|
|
}
|
2016-03-19 18:31:42 +02:00
|
|
|
|
2016-05-09 17:04:56 +02:00
|
|
|
|
|
|
|
public static createUser(req:Request, res:Response, next:NextFunction) {
|
2016-07-07 12:26:36 +02:00
|
|
|
if (Config.Client.authenticationRequired === false) {
|
|
|
|
return next(new Error(ErrorCodes.USER_MANAGEMENT_DISABLED));
|
|
|
|
}
|
2016-03-19 18:31:42 +02:00
|
|
|
if ((typeof req.body === 'undefined') || (typeof req.body.newUser === 'undefined')) {
|
|
|
|
return next();
|
2016-03-18 22:36:58 +02:00
|
|
|
}
|
2016-03-19 18:31:42 +02:00
|
|
|
|
2016-05-09 17:04:56 +02:00
|
|
|
ObjectManagerRepository.getInstance().getUserManager().createUser(req.body.newUser, (err, result) => {
|
2016-03-19 18:31:42 +02:00
|
|
|
if ((err) || (!result)) {
|
2016-03-26 12:19:10 +02:00
|
|
|
return next(new Error(ErrorCodes.USER_CREATION_ERROR));
|
2016-03-19 18:31:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
2016-03-18 22:36:58 +02:00
|
|
|
}
|
2016-03-19 18:31:42 +02:00
|
|
|
|
2016-05-09 17:04:56 +02:00
|
|
|
public static deleteUser(req:Request, res:Response, next:NextFunction) {
|
2016-07-07 12:26:36 +02:00
|
|
|
if (Config.Client.authenticationRequired === false) {
|
|
|
|
return next(new Error(ErrorCodes.USER_MANAGEMENT_DISABLED));
|
|
|
|
}
|
2016-05-03 14:29:24 +02:00
|
|
|
if ((typeof req.params === 'undefined') || (typeof req.params.id === 'undefined')) {
|
2016-03-18 22:36:58 +02:00
|
|
|
return next();
|
2016-03-19 18:31:42 +02:00
|
|
|
}
|
2016-03-18 22:36:58 +02:00
|
|
|
|
2016-05-09 17:04:56 +02:00
|
|
|
ObjectManagerRepository.getInstance().getUserManager().deleteUser(req.params.id, (err, result) => {
|
2016-03-18 22:36:58 +02:00
|
|
|
if ((err) || (!result)) {
|
2016-03-26 12:19:10 +02:00
|
|
|
return next(new Error(ErrorCodes.GENERAL_ERROR));
|
2016-03-18 22:36:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-19 18:31:42 +02:00
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-09 17:04:56 +02:00
|
|
|
public static changeRole(req:Request, res:Response, next:NextFunction) {
|
2016-07-07 12:26:36 +02:00
|
|
|
if (Config.Client.authenticationRequired === false) {
|
|
|
|
return next(new Error(ErrorCodes.USER_MANAGEMENT_DISABLED));
|
|
|
|
}
|
2016-05-03 14:29:24 +02:00
|
|
|
if ((typeof req.params === 'undefined') || (typeof req.params.id === 'undefined')
|
|
|
|
|| (typeof req.body === 'undefined') || (typeof req.body.newRole === 'undefined')) {
|
2016-03-19 18:31:42 +02:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2016-05-09 17:04:56 +02:00
|
|
|
ObjectManagerRepository.getInstance().getUserManager().changeRole(req.params.id, req.body.newRole, (err) => {
|
2016-04-22 13:23:44 +02:00
|
|
|
if (err) {
|
2016-03-26 12:19:10 +02:00
|
|
|
return next(new Error(ErrorCodes.GENERAL_ERROR));
|
2016-03-19 18:31:42 +02:00
|
|
|
}
|
2016-03-18 22:36:58 +02:00
|
|
|
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-09 17:04:56 +02:00
|
|
|
public static listUsers(req:Request, res:Response, next:NextFunction) {
|
2016-07-07 12:26:36 +02:00
|
|
|
if (Config.Client.authenticationRequired === false) {
|
|
|
|
return next(new Error(ErrorCodes.USER_MANAGEMENT_DISABLED));
|
|
|
|
}
|
2016-12-27 17:09:47 +02:00
|
|
|
ObjectManagerRepository.getInstance().getUserManager().find({}, (err, result: Array<UserDTO>) => {
|
2016-03-19 18:31:42 +02:00
|
|
|
if ((err) || (!result)) {
|
2016-03-26 12:19:10 +02:00
|
|
|
return next(new Error(ErrorCodes.GENERAL_ERROR));
|
2016-03-19 18:31:42 +02:00
|
|
|
}
|
2016-07-09 15:08:36 +02:00
|
|
|
result = Utils.clone(result);
|
2016-05-09 17:04:56 +02:00
|
|
|
for (let i = 0; i < result.length; i++) {
|
2016-05-03 14:29:24 +02:00
|
|
|
result[i].password = "";
|
|
|
|
}
|
2016-05-09 17:04:56 +02:00
|
|
|
|
2016-03-26 12:19:10 +02:00
|
|
|
req.resultPipe = result;
|
|
|
|
return next();
|
2016-03-19 18:31:42 +02:00
|
|
|
});
|
2016-03-18 22:36:58 +02:00
|
|
|
}
|
2016-03-19 18:31:42 +02:00
|
|
|
|
|
|
|
|
2016-03-18 22:36:58 +02:00
|
|
|
}
|