2016-03-20 00:31:42 +08:00
|
|
|
|
|
|
|
import {UserManager} from "../model/UserManager";
|
|
|
|
import {NextFunction, Request, Response} from "express";
|
|
|
|
import {Error, ErrorCodes} from "../../common/entities/Error";
|
|
|
|
import {UserRoles} from "../../common/entities/User";
|
|
|
|
|
2016-03-26 18:19:10 +08:00
|
|
|
export class UserRequestConstrainsMWs {
|
2016-03-20 00:31:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
public static forceSelfRequest(req:Request, res:Response, next:NextFunction){
|
|
|
|
if ((typeof req.params === 'undefined') || (typeof req.params.id === 'undefined')) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
if(req.session.user.id !== req.params.id){
|
2016-03-26 18:19:10 +08:00
|
|
|
return next(new Error(ErrorCodes.NOT_AUTHORISED));
|
2016-03-20 00:31:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static notSelfRequest(req:Request, res:Response, next:NextFunction){
|
|
|
|
if ((typeof req.params === 'undefined') || (typeof req.params.id === 'undefined')) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(req.session.user.id === req.params.id){
|
2016-03-26 18:19:10 +08:00
|
|
|
return next(new Error(ErrorCodes.NOT_AUTHORISED));
|
2016-03-20 00:31:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static notSelfRequestOr2Admins(req:Request, res:Response, next:NextFunction){
|
|
|
|
if ((typeof req.params === 'undefined') || (typeof req.params.id === 'undefined')) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(req.session.user.id !== req.params.id){
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
UserManager.find({minRole:UserRoles.Admin}, (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
|
|
|
}
|
|
|
|
if(result.length <= 1) {
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|