2016-03-20 00:31:42 +08:00
|
|
|
import {NextFunction, Request, Response} from "express";
|
2016-05-26 02:17:42 +08:00
|
|
|
import {Error, ErrorCodes} from "../../../common/entities/Error";
|
2016-12-27 23:09:47 +08:00
|
|
|
import {UserRoles} from "../../../common/entities/UserDTO";
|
2016-05-26 02:17:42 +08:00
|
|
|
import {ObjectManagerRepository} from "../../model/ObjectManagerRepository";
|
2016-03-20 00:31:42 +08:00
|
|
|
|
2016-03-26 18:19:10 +08:00
|
|
|
export class UserRequestConstrainsMWs {
|
2016-03-20 00:31:42 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
public static forceSelfRequest(req:Request, res:Response, next:NextFunction) {
|
2016-03-20 00:31:42 +08:00
|
|
|
if ((typeof req.params === 'undefined') || (typeof req.params.id === 'undefined')) {
|
|
|
|
return next();
|
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
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
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
|
2016-03-20 00:31:42 +08:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
public static notSelfRequest(req:Request, res:Response, next:NextFunction) {
|
2016-03-20 00:31:42 +08:00
|
|
|
if ((typeof req.params === 'undefined') || (typeof req.params.id === 'undefined')) {
|
|
|
|
return next();
|
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
|
|
|
|
public static notSelfRequestOr2Admins(req:Request, res:Response, next:NextFunction) {
|
2016-03-20 00:31:42 +08:00
|
|
|
if ((typeof req.params === 'undefined') || (typeof req.params.id === 'undefined')) {
|
|
|
|
return next();
|
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
|
|
|
|
if (req.session.user.id !== req.params.id) {
|
2016-03-20 00:31:42 +08:00
|
|
|
return next();
|
|
|
|
}
|
2016-05-03 20:29:24 +08:00
|
|
|
//TODO: fix it!
|
2016-05-09 23:04:56 +08:00
|
|
|
ObjectManagerRepository.getInstance().getUserManager().find({minRole: UserRoles.Admin}, (err, result) => {
|
2016-03-20 00:31:42 +08:00
|
|
|
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-05-09 23:04:56 +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
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
|
2016-03-20 00:31:42 +08:00
|
|
|
});
|
2016-05-09 23:04:56 +08:00
|
|
|
|
2016-03-20 00:31:42 +08:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|