@szegedsw/lib-node
Version:
A little framework published by Szeged Software Zrt. in order to enhance api endpoint security and create reuseable code. Email module, Logging system, and much more. Further improvements are expected.
43 lines • 2.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkRights = void 0;
const config_1 = require("../config/config");
const try_1 = require("../functions/try");
function checkRights(rights, status = config_1.HttpCodes.unauthorized) {
// const env = new Env();
return async (req, res, next) => {
await try_1._try(res, async () => {
var _a;
const routes = req.originalUrl.split("/");
// függvény annak meghatározására, hogy ez az elérési út beleesik-e a megadott utak egyikébe
const pathIncl = (paths) => Array.isArray(paths) &&
paths.some((path) => {
// csinálunk egy tömböt, amelyben minden elem az adott part helyét mutatja a route-ok között
const idx = path.split("/").map((part) => routes.indexOf(part));
// ha a tömb összes elemére igaz, hogy nemnegatívak, és szigorúan egymás után következnek, akkor igaz
return idx.every((id, i) => id >= 0 && (i === 0 || idx[i - 1] === id - 1) && (i === idx.length - 1 || idx[i + 1] === id + 1));
});
// ennek használatával a public utak keresése
// if (pathIncl(env.publicPaths())) {
// next();
// return;
// }
// szabad elérésű (al)utak, függetlenül a szülőre megadott korlátozástól
// const freeAccessPaths = env.freeAccessPaths();
const freeAccessPaths = {};
// saját jogosultságok, amelyekkel rendelkezve elérhetőek a child-routes
// tslint:disable-next-line: no-string-literal
const ownRights = (_a = req.session) === null || _a === void 0 ? void 0 : _a.rights;
// ha a saját jogon elérhető, vagy szabad elérésű (al)útként van megadva
if (rights.every((right) => (ownRights === null || ownRights === void 0 ? void 0 : ownRights.includes(right)) || pathIncl(freeAccessPaths[right.toString()]))) {
next();
return;
}
// ha nem sikerült a route elérése, alapértelmezetten unauthorized hibát dobunk
// tslint:disable-next-line: no-string-literal
res.status(status).json(req.session).end();
});
};
}
exports.checkRights = checkRights;
//# sourceMappingURL=check.rights.js.map