2016-03-19 04:36:58 +08:00
|
|
|
|
|
|
|
import {UserManager} from "../model/UserManager";
|
|
|
|
import {NextFunction, Request, Response} from "express";
|
2016-03-19 16:58:27 +08:00
|
|
|
import {Error, ErrorCodes} from "../../common/entities/Error";
|
2016-03-19 04:36:58 +08:00
|
|
|
|
2016-03-26 18:19:10 +08:00
|
|
|
export class UserMWs {
|
2016-03-19 04:36:58 +08:00
|
|
|
|
2016-03-20 00:31:42 +08:00
|
|
|
public static changePassword(req:Request, res:Response, next:NextFunction){
|
|
|
|
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-19 04:36:58 +08:00
|
|
|
}
|
2016-03-20 00:31:42 +08:00
|
|
|
|
|
|
|
UserManager.changePassword(req.body.userModReq, (err, result) =>{
|
|
|
|
if ((err) || (!result)) {
|
2016-03-26 18:19:10 +08:00
|
|
|
return next(new Error(ErrorCodes.GENERAL_ERROR));
|
2016-03-20 00:31:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return next();
|
|
|
|
});
|
2016-03-19 04:36:58 +08:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:31:42 +08:00
|
|
|
|
|
|
|
public static createUser(req:Request, res:Response, next:NextFunction){
|
|
|
|
if ((typeof req.body === 'undefined') || (typeof req.body.newUser === 'undefined')) {
|
|
|
|
return next();
|
2016-03-19 04:36:58 +08:00
|
|
|
}
|
2016-03-20 00:31:42 +08:00
|
|
|
|
|
|
|
UserManager.createUser(req.body.newUser, (err, result) =>{
|
|
|
|
if ((err) || (!result)) {
|
2016-03-26 18:19:10 +08:00
|
|
|
return next(new Error(ErrorCodes.USER_CREATION_ERROR));
|
2016-03-20 00:31:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
2016-03-19 04:36:58 +08:00
|
|
|
}
|
2016-03-20 00:31:42 +08:00
|
|
|
|
|
|
|
public static deleteUser(req:Request, res:Response, next:NextFunction){
|
|
|
|
if ((typeof req.body === 'undefined') || (typeof req.body.newUser === 'undefined')
|
|
|
|
|| (typeof req.body.userModReq.id === 'undefined')) {
|
2016-03-19 04:36:58 +08:00
|
|
|
return next();
|
2016-03-20 00:31:42 +08:00
|
|
|
}
|
2016-03-19 04:36:58 +08:00
|
|
|
|
2016-03-20 00:31:42 +08:00
|
|
|
UserManager.deleteUser(req.body.userModReq.id, (err, result) =>{
|
2016-03-19 04:36:58 +08:00
|
|
|
if ((err) || (!result)) {
|
2016-03-26 18:19:10 +08:00
|
|
|
return next(new Error(ErrorCodes.GENERAL_ERROR));
|
2016-03-19 04:36:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-20 00:31:42 +08:00
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static changeRole(req:Request, res:Response, next:NextFunction){
|
|
|
|
if ((typeof req.body === 'undefined') || (typeof req.body.userModReq === 'undefined')
|
|
|
|
|| (typeof req.body.userModReq.id === 'undefined')
|
|
|
|
|| (typeof req.body.userModReq.newRole === 'undefined')) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
UserManager.changeRole(req.body.userModReq, (err, result) =>{
|
|
|
|
if ((err) || (!result)) {
|
2016-03-26 18:19:10 +08:00
|
|
|
return next(new Error(ErrorCodes.GENERAL_ERROR));
|
2016-03-20 00:31:42 +08:00
|
|
|
}
|
2016-03-19 04:36:58 +08:00
|
|
|
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-20 00:31:42 +08:00
|
|
|
public static listUsers(req:Request, res:Response, next:NextFunction){
|
|
|
|
UserManager.find({}, (err, result) =>{
|
|
|
|
if ((err) || (!result)) {
|
2016-03-26 18:19:10 +08:00
|
|
|
return next(new Error(ErrorCodes.GENERAL_ERROR));
|
2016-03-20 00:31:42 +08:00
|
|
|
}
|
|
|
|
|
2016-03-26 18:19:10 +08:00
|
|
|
req.resultPipe = result;
|
|
|
|
return next();
|
2016-03-20 00:31:42 +08:00
|
|
|
});
|
2016-03-19 04:36:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-20 00:31:42 +08:00
|
|
|
|
|
|
|
|
2016-03-19 04:36:58 +08:00
|
|
|
}
|