1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/backend/middlewares/user/UserMWs.ts

104 lines
3.7 KiB
TypeScript
Raw Normal View History

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