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

55 lines
1.7 KiB
TypeScript
Raw Normal View History

import {NextFunction, Request, Response} from "express";
import {Error, ErrorCodes} from "../../common/entities/Error";
import {UserRoles} from "../../common/entities/User";
2016-04-22 19:23:44 +08:00
import {ObjectManagerRepository} from "../model/ObjectManagerRepository";
export class UserRequestConstrainsMWs {
2016-05-09 23:04:56 +08:00
public static forceSelfRequest(req:Request, res:Response, next:NextFunction) {
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) {
return next(new Error(ErrorCodes.NOT_AUTHORISED));
}
2016-05-09 23:04:56 +08:00
return next();
}
2016-05-09 23:04:56 +08:00
public static notSelfRequest(req:Request, res:Response, next:NextFunction) {
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) {
return next(new Error(ErrorCodes.NOT_AUTHORISED));
}
return next();
}
2016-05-09 23:04:56 +08:00
public static notSelfRequestOr2Admins(req:Request, res:Response, next:NextFunction) {
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) {
return next();
}
//TODO: fix it!
2016-05-09 23:04:56 +08:00
ObjectManagerRepository.getInstance().getUserManager().find({minRole: UserRoles.Admin}, (err, result) => {
if ((err) || (!result)) {
return next(new Error(ErrorCodes.GENERAL_ERROR));
}
2016-05-09 23:04:56 +08:00
if (result.length <= 1) {
return next(new Error(ErrorCodes.GENERAL_ERROR));
}
2016-05-09 23:04:56 +08:00
});
2016-05-09 23:04:56 +08:00
return next();
}
}