jsm-core
Version:
Core library for JSM project
94 lines (93 loc) • 4.46 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const typedi_1 = require("typedi");
const jsm_exceptions_1 = __importDefault(require("jsm-exceptions"));
const cache_manager_class_1 = require("../../managers/cache-manager/cache-manager.class");
const context_1 = require("../../../context");
/**
* Check if user has permission
* - If the request is identified as a service request, always pass
* - If user is master, always pass
* - If the permission is not passed, pass
* - If user has the permission, pass
* - If the passed permissions is an array, check if user has one of the permissions
* - If the passed permissions is an array of arrays, check if user has all the permissions in one of the arrays
* @param {*} req Express req Object
* @param {*} res Express res Object
* @param {*} next Express next Function
*/
const userHasPermission = (permission) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b, _c;
try {
const cache = typedi_1.Container.get(cache_manager_class_1.CacheManager);
/**
* Always pass if another service is authenticated
*/
if (req.attached_entities.service)
return next();
/**
* Handle JWT token expired
*/
if (req.jwt_expired)
throw new jsm_exceptions_1.default.JWTTokenExpired("JWT token expired");
/**
* Masters always have all the role groups
*/
if (!permission || !permission.length) {
return next();
}
/**
* The condition logic
*/
if (!req.attached_entities.user ||
!req.attached_entities.user.permissions ||
!req.attached_entities.user.permissions.length)
throw new jsm_exceptions_1.default.UnauthorizedException("You must be logged in to access this resource");
const permissionObjects = yield ((_b = (_a = (0, context_1.getRegistry)().cache) === null || _a === void 0 ? void 0 : _a.permissions) === null || _b === void 0 ? void 0 : _b.list());
const userPermissionNames = (((_c = req.attached_entities.user) === null || _c === void 0 ? void 0 : _c.permissions) || []).map((p) => { var _a; return (_a = permissionObjects.find((po) => po._id === p)) === null || _a === void 0 ? void 0 : _a.name; });
if (typeof permission === "string" &&
userPermissionNames.indexOf(permission) > -1) {
return next();
}
let granted = false;
if (typeof permission !== "string" && permission instanceof Array) {
permission.forEach((perm) => {
if (typeof perm === "string") {
return (granted =
granted ||
userPermissionNames.indexOf(perm) > -1 ||
perm.length === 0);
}
if (typeof perm !== "string" && perm instanceof Array) {
let _granted = true;
perm.forEach((p) => {
_granted =
_granted &&
(userPermissionNames.indexOf(p) > -1 || p.length === 0);
});
granted = granted || _granted;
}
});
}
if (granted) {
return next();
}
return next(new jsm_exceptions_1.default.UnauthorizedException(`You don't have permission to access this resource`));
}
catch (error) {
return next(error);
}
});
exports.default = userHasPermission;