UNPKG

3.15 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.getGrantCache = getGrantCache;
9exports["default"] = void 0;
10
11var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
12
13var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
14
15var _err = _interopRequireDefault(require("@lskjs/err"));
16
17function getGrantCache(_x, _x2) {
18 return _getGrantCache.apply(this, arguments);
19}
20
21function _getGrantCache() {
22 _getGrantCache = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(req, rules) {
23 var grant, userId, grantCache;
24 return _regenerator["default"].wrap(function _callee2$(_context2) {
25 while (1) {
26 switch (_context2.prev = _context2.next) {
27 case 0:
28 _context2.next = 2;
29 return this.app.module('grant');
30
31 case 2:
32 grant = _context2.sent;
33 userId = req.user ? req.user._id : null;
34 _context2.next = 6;
35 return grant.getCache(rules, {
36 user: req.user,
37 userId: userId,
38 cache: grant.__cache.fork({
39 prefix: userId || 'guest'
40 })
41 });
42
43 case 6:
44 grantCache = _context2.sent;
45
46 grantCache.checkAccess = /*#__PURE__*/function () {
47 var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(action) {
48 var can;
49 return _regenerator["default"].wrap(function _callee$(_context) {
50 while (1) {
51 switch (_context.prev = _context.next) {
52 case 0:
53 _context.next = 2;
54 return this.can(action);
55
56 case 2:
57 can = _context.sent;
58
59 if (!can) {
60 _context.next = 5;
61 break;
62 }
63
64 return _context.abrupt("return");
65
66 case 5:
67 throw new _err["default"]('grant.access', {
68 status: 403,
69 data: {
70 action: action
71 }
72 });
73
74 case 6:
75 case "end":
76 return _context.stop();
77 }
78 }
79 }, _callee, this);
80 }));
81
82 return function (_x3) {
83 return _ref.apply(this, arguments);
84 };
85 }();
86
87 return _context2.abrupt("return", grantCache);
88
89 case 9:
90 case "end":
91 return _context2.stop();
92 }
93 }
94 }, _callee2, this);
95 }));
96 return _getGrantCache.apply(this, arguments);
97}
98
99var _default = getGrantCache;
100exports["default"] = _default;
101//# sourceMappingURL=getGrantCache.js.map
\No newline at end of file