@autofleet/zehut 1.4.9-alpha.8 → 1.4.9-beta-1
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.
- package/lib/check-permission.d.ts +3 -0
- package/lib/check-permission.js +19 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +8 -3
- package/lib/tracer.d.ts +15 -0
- package/lib/tracer.js +55 -0
- package/lib/user/index.js +3 -3
- package/package.json +2 -2
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.checkDemandSourcePermission = exports.checkBusinessModelPermission = exports.checkFleetPermission = void 0;
|
|
4
|
+
const tracer_1 = require("./tracer");
|
|
5
|
+
exports.checkFleetPermission = fleetId => {
|
|
6
|
+
const { context } = tracer_1.getCurrentTrace();
|
|
7
|
+
const user = context === null || context === void 0 ? void 0 : context.get('userObject');
|
|
8
|
+
return user === null || user === void 0 ? void 0 : user.fleets.includes(fleetId);
|
|
9
|
+
};
|
|
10
|
+
exports.checkBusinessModelPermission = businessModelId => {
|
|
11
|
+
const { context } = tracer_1.getCurrentTrace();
|
|
12
|
+
const user = context === null || context === void 0 ? void 0 : context.get('userObject');
|
|
13
|
+
return user === null || user === void 0 ? void 0 : user.businessModels.includes(businessModelId);
|
|
14
|
+
};
|
|
15
|
+
exports.checkDemandSourcePermission = demandSourceId => {
|
|
16
|
+
const { context } = tracer_1.getCurrentTrace();
|
|
17
|
+
const user = context === null || context === void 0 ? void 0 : context.get('userObject');
|
|
18
|
+
return user === null || user === void 0 ? void 0 : user.demandSources.includes(demandSourceId);
|
|
19
|
+
};
|
package/lib/index.d.ts
CHANGED
|
@@ -12,5 +12,8 @@ declare const _default: {
|
|
|
12
12
|
eagerLoadPermissionsMiddleware: (req: any, res: any, next: any) => Promise<any>;
|
|
13
13
|
getCurrentPayload: () => any;
|
|
14
14
|
getDecodedBearer: (req: any) => any;
|
|
15
|
+
checkFleetPermission: (fleetId: any) => any;
|
|
16
|
+
checkBusinessModelPermission: (businessModelId: any) => any;
|
|
17
|
+
checkDemandSourcePermission: (demandSourceId: any) => any;
|
|
15
18
|
};
|
|
16
19
|
export default _default;
|
package/lib/index.js
CHANGED
|
@@ -20,12 +20,17 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
20
20
|
};
|
|
21
21
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
22
|
const user_1 = __importStar(require("./user"));
|
|
23
|
-
const
|
|
23
|
+
const tracer_1 = require("./tracer");
|
|
24
|
+
const check_permission_1 = require("./check-permission");
|
|
25
|
+
tracer_1.enable();
|
|
24
26
|
exports.default = {
|
|
25
27
|
User: user_1.default,
|
|
26
28
|
middleware: user_1.middleware,
|
|
27
29
|
middlewareWithDecode: user_1.middlewareWithDecode,
|
|
28
30
|
eagerLoadPermissionsMiddleware: user_1.eagerLoadPermissionsMiddleware,
|
|
29
|
-
getCurrentPayload:
|
|
30
|
-
getDecodedBearer: user_1.getDecodedBearer
|
|
31
|
+
getCurrentPayload: tracer_1.getCurrentTrace,
|
|
32
|
+
getDecodedBearer: user_1.getDecodedBearer,
|
|
33
|
+
checkFleetPermission: check_permission_1.checkFleetPermission,
|
|
34
|
+
checkBusinessModelPermission: check_permission_1.checkBusinessModelPermission,
|
|
35
|
+
checkDemandSourcePermission: check_permission_1.checkDemandSourcePermission,
|
|
31
36
|
};
|
package/lib/tracer.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import asyncHooks from 'async_hooks';
|
|
2
|
+
declare const tracer: {
|
|
3
|
+
currentTrace: any;
|
|
4
|
+
traces: {};
|
|
5
|
+
};
|
|
6
|
+
declare class Trace {
|
|
7
|
+
id: string;
|
|
8
|
+
type: string;
|
|
9
|
+
context: Map<string, any>;
|
|
10
|
+
constructor(type: any);
|
|
11
|
+
}
|
|
12
|
+
export declare const newTrace: (type: any) => Trace;
|
|
13
|
+
export declare const enable: () => asyncHooks.AsyncHook;
|
|
14
|
+
export declare const getCurrentTrace: () => any;
|
|
15
|
+
export default tracer;
|
package/lib/tracer.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getCurrentTrace = exports.enable = exports.newTrace = void 0;
|
|
7
|
+
const async_hooks_1 = __importDefault(require("async_hooks"));
|
|
8
|
+
const uuid_1 = require("uuid");
|
|
9
|
+
const prevStates = {};
|
|
10
|
+
const tracer = {
|
|
11
|
+
currentTrace: null,
|
|
12
|
+
traces: {},
|
|
13
|
+
};
|
|
14
|
+
function init(asyncId, type, triggerAsyncId) {
|
|
15
|
+
if (tracer.traces[triggerAsyncId]) {
|
|
16
|
+
tracer.traces[asyncId] = tracer.traces[triggerAsyncId];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function before(asyncId) {
|
|
20
|
+
if (!tracer.traces[asyncId]) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
prevStates[asyncId] = tracer.currentTrace;
|
|
24
|
+
tracer.currentTrace = tracer.traces[asyncId];
|
|
25
|
+
}
|
|
26
|
+
function after(asyncId) {
|
|
27
|
+
if (!tracer.traces[asyncId]) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
tracer.currentTrace = prevStates[asyncId];
|
|
31
|
+
}
|
|
32
|
+
function destroy(asyncId) {
|
|
33
|
+
if (tracer.traces[asyncId]) {
|
|
34
|
+
delete tracer.traces[asyncId];
|
|
35
|
+
delete prevStates[asyncId];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const hook = async_hooks_1.default.createHook({
|
|
39
|
+
init, before, after, destroy,
|
|
40
|
+
});
|
|
41
|
+
class Trace {
|
|
42
|
+
constructor(type) {
|
|
43
|
+
this.id = uuid_1.v1();
|
|
44
|
+
this.type = type;
|
|
45
|
+
this.context = new Map();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.newTrace = (type) => {
|
|
49
|
+
tracer.currentTrace = new Trace(type);
|
|
50
|
+
tracer.traces[async_hooks_1.default.executionAsyncId()] = tracer.currentTrace;
|
|
51
|
+
return tracer.currentTrace;
|
|
52
|
+
};
|
|
53
|
+
exports.enable = () => hook.enable();
|
|
54
|
+
exports.getCurrentTrace = () => tracer.currentTrace || {};
|
|
55
|
+
exports.default = tracer;
|
package/lib/user/index.js
CHANGED
|
@@ -14,11 +14,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.getDecodedBearer = exports.eagerLoadPermissionsMiddleware = exports.middlewareWithDecode = exports.middleware = void 0;
|
|
16
16
|
const ApiUser_1 = __importDefault(require("./ApiUser"));
|
|
17
|
-
const outbreak_1 = require("@autofleet/outbreak");
|
|
18
17
|
const utils_1 = require("../utils");
|
|
18
|
+
const tracer_1 = require("../tracer");
|
|
19
19
|
exports.middleware = (options = {}) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
20
20
|
const userId = req.headers['x-af-user-id'];
|
|
21
|
-
const trace =
|
|
21
|
+
const trace = tracer_1.newTrace('userPayload');
|
|
22
22
|
if (!userId) {
|
|
23
23
|
trace.context.set('userObject', {});
|
|
24
24
|
return next();
|
|
@@ -38,7 +38,7 @@ exports.middleware = (options = {}) => (req, res, next) => __awaiter(void 0, voi
|
|
|
38
38
|
});
|
|
39
39
|
exports.middlewareWithDecode = (options = {}) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
40
40
|
var _a;
|
|
41
|
-
const trace =
|
|
41
|
+
const trace = tracer_1.newTrace('userPayload');
|
|
42
42
|
if (req.headers.authorization) {
|
|
43
43
|
const decoded = utils_1.decodeBearer(req.headers.authorization);
|
|
44
44
|
const userId = (_a = decoded === null || decoded === void 0 ? void 0 : decoded.user) === null || _a === void 0 ? void 0 : _a.id;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/zehut",
|
|
3
|
-
"version": "1.4.9-
|
|
3
|
+
"version": "1.4.9-beta-1",
|
|
4
4
|
"description": "manage user's identity",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"homepage": "https://github.com/Autofleet/zehut",
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"@autofleet/network": "^1.1.4",
|
|
31
|
-
"@autofleet/outbreak": "0.0.
|
|
31
|
+
"@autofleet/outbreak": "0.0.7",
|
|
32
32
|
"axios": "^0.19.2",
|
|
33
33
|
"jsonwebtoken": "^8.5.1",
|
|
34
34
|
"uuid": "^8.3.2"
|