1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2024-12-14 11:23:17 +02:00
pigallery2/backend/middlewares/UserMWs.ts

92 lines
3.0 KiB
TypeScript
Raw Normal View History

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