@rufous/aem 1.1.21 → 1.1.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ import { CustomError } from './custom-error';
2
+ export declare class PermissionDeniedError extends CustomError {
3
+ statusCode: number;
4
+ constructor();
5
+ serializeErrors(): {
6
+ message: string;
7
+ }[];
8
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ extendStatics(d, b);
11
+ function __() { this.constructor = d; }
12
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
+ };
14
+ })();
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.PermissionDeniedError = void 0;
17
+ var custom_error_1 = require("./custom-error");
18
+ var PermissionDeniedError = /** @class */ (function (_super) {
19
+ __extends(PermissionDeniedError, _super);
20
+ function PermissionDeniedError() {
21
+ var _this = _super.call(this, "Sorry, you don't have access.") || this;
22
+ _this.statusCode = 403;
23
+ Object.setPrototypeOf(_this, PermissionDeniedError.prototype);
24
+ return _this;
25
+ }
26
+ PermissionDeniedError.prototype.serializeErrors = function () {
27
+ return [{ message: "Sorry, you don't have access." }];
28
+ };
29
+ return PermissionDeniedError;
30
+ }(custom_error_1.CustomError));
31
+ exports.PermissionDeniedError = PermissionDeniedError;
package/build/index.d.ts CHANGED
@@ -5,6 +5,7 @@ export * from './errors/internal-server-error';
5
5
  export * from './errors/not-authorized-error';
6
6
  export * from './errors/not-found-error';
7
7
  export * from './errors/not-verified-error';
8
+ export * from './errors/permission-denied-error';
8
9
  export * from './errors/request-validation-error';
9
10
  export * from './errors/security-error';
10
11
  export * from './middlewares/current-user';
package/build/index.js CHANGED
@@ -18,6 +18,7 @@ __exportStar(require("./errors/internal-server-error"), exports);
18
18
  __exportStar(require("./errors/not-authorized-error"), exports);
19
19
  __exportStar(require("./errors/not-found-error"), exports);
20
20
  __exportStar(require("./errors/not-verified-error"), exports);
21
+ __exportStar(require("./errors/permission-denied-error"), exports);
21
22
  __exportStar(require("./errors/request-validation-error"), exports);
22
23
  __exportStar(require("./errors/security-error"), exports);
23
24
  __exportStar(require("./middlewares/current-user"), exports);
@@ -1,2 +1,2 @@
1
1
  import { Request, Response, NextFunction } from "express";
2
- export declare const requirePermissions: (accessType: string | undefined, permissionType: string[] | undefined, userModel: any, permissionsModel: any, req: Request, res: Response, next: NextFunction) => Response<any> | undefined;
2
+ export declare const requirePermissions: (accessType: "read", permissionType: string[], userModel: any, permissionsModel: any) => (req: Request, res: Response, next: NextFunction) => void;
@@ -7,38 +7,37 @@ exports.requirePermissions = void 0;
7
7
  var not_authorized_error_1 = require("../errors/not-authorized-error");
8
8
  var user_status_1 = require("../types/user-status");
9
9
  var mongoose_1 = __importDefault(require("mongoose"));
10
- exports.requirePermissions = function (accessType, permissionType, userModel, permissionsModel, req, res, next) {
11
- if (accessType === void 0) { accessType = "read"; }
12
- if (permissionType === void 0) { permissionType = []; }
13
- if (!req.currentUser) {
14
- throw new not_authorized_error_1.NotAuthorizedError();
15
- }
16
- if (req.currentUser.ust === user_status_1.UserStatus.Suspended) {
17
- throw new not_authorized_error_1.NotAuthorizedError();
18
- }
19
- var user = req.currentUser;
20
- var userDetails = mongoose_1.default.model(userModel).findById(user.id);
21
- var ispermExist = false;
22
- if (!userDetails) {
23
- throw new not_authorized_error_1.NotAuthorizedError();
24
- }
25
- else if (userDetails.roles) {
26
- var permissions = mongoose_1.default
27
- .model(permissionsModel)
28
- .find({ roles: { $in: userDetails.roles } });
29
- var permissionsArr = permissions
30
- .filter(function (perm) { return perm[accessType]; })
31
- .map(function (perm) { return perm.name; });
32
- ispermExist = permissionsArr.some(function (perm) {
33
- return permissionType.includes(perm);
34
- });
35
- }
36
- if (!ispermExist) {
37
- return res.status(403).json({
38
- error: "Sorry, you don't have access.",
39
- });
40
- }
41
- else {
10
+ var permission_denied_error_1 = require("../errors/permission-denied-error");
11
+ exports.requirePermissions = function (accessType, permissionType, userModel, permissionsModel) {
12
+ return function (req, res, next) {
13
+ if (!req.currentUser) {
14
+ throw new not_authorized_error_1.NotAuthorizedError();
15
+ }
16
+ if (req.currentUser.ust === user_status_1.UserStatus.Suspended) {
17
+ throw new not_authorized_error_1.NotAuthorizedError();
18
+ }
19
+ var user = req.currentUser;
20
+ var userDetails = mongoose_1.default.model(userModel).findById(user.id);
21
+ var ispermExist = false;
22
+ console.log("userDetails ", userDetails);
23
+ if (!userDetails) {
24
+ throw new not_authorized_error_1.NotAuthorizedError();
25
+ }
26
+ else if (userDetails.roles || userDetails.userRoles) {
27
+ var roles = userDetails.roles ? userDetails.roles : userDetails.userRoles;
28
+ var permissions = mongoose_1.default
29
+ .model(permissionsModel)
30
+ .find({ entityId: { $in: roles } });
31
+ var permissionsArr = permissions
32
+ .filter(function (perm) { return perm[accessType]; })
33
+ .map(function (perm) { return perm.name; });
34
+ ispermExist = permissionsArr.some(function (perm) {
35
+ return permissionType.includes(perm);
36
+ });
37
+ }
38
+ if (!ispermExist) {
39
+ throw new permission_denied_error_1.PermissionDeniedError();
40
+ }
42
41
  next();
43
- }
42
+ };
44
43
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rufous/aem",
3
- "version": "1.1.21",
3
+ "version": "1.1.23",
4
4
  "main": "./build/index.js",
5
5
  "types": "./build/index.d.ts",
6
6
  "files": [