@izara_project/izara-market-library-service-schemas 1.0.74 → 1.0.75
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/index.js +10 -6
- package/package.json +9 -8
- package/src/CheckPermission/CheckPermission.js +140 -0
package/index.js
CHANGED
|
@@ -17,14 +17,18 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
17
17
|
|
|
18
18
|
'use strict';
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
20
|
module.exports = {
|
|
23
21
|
templateMgr: require('./src/TemplateManager'),
|
|
24
22
|
sourceMgr: require('./src/SourceManager'),
|
|
25
23
|
generateCodeLibs: require('./src/GenerateCodeLibs'),
|
|
26
24
|
utils: require('./src/MainLibs'),
|
|
27
|
-
generateSchemaWithTemplate:
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
25
|
+
generateSchemaWithTemplate:
|
|
26
|
+
require('./src/TemplateManager/src/GenerateSchema')
|
|
27
|
+
.generateSchemaWithTemplate,
|
|
28
|
+
generateCodeWithTemplate: require('./src/reStructure/GenerateCode')
|
|
29
|
+
.generateCodeWithTemplate,
|
|
30
|
+
generateSchema: require('./src/reStructure/GenerateSchema'),
|
|
31
|
+
|
|
32
|
+
// Libs
|
|
33
|
+
checkPermission: require('./src/CheckPermission')
|
|
34
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@izara_project/izara-market-library-service-schemas",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.75",
|
|
4
4
|
"description": "Schemas for Izara Market project",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -14,26 +14,27 @@
|
|
|
14
14
|
"license": "AGPL-3.0-or-later",
|
|
15
15
|
"homepage": "https://bitbucket.org/izara-market-libraries/izara-market-library-service-schemas/src/master/README.md",
|
|
16
16
|
"devDependencies": {
|
|
17
|
-
"jest": "^30.0
|
|
17
|
+
"jest": "^30.2.0"
|
|
18
18
|
},
|
|
19
19
|
"jest": {
|
|
20
20
|
"testEnvironment": "node"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@izara_project/izara-core-library-calling-flow": "^1.0.3",
|
|
24
|
-
"@izara_project/izara-core-library-core": "^1.0.
|
|
25
|
-
"@izara_project/izara-core-library-external-request": "^1.0.
|
|
24
|
+
"@izara_project/izara-core-library-core": "^1.0.27",
|
|
25
|
+
"@izara_project/izara-core-library-external-request": "^1.0.21",
|
|
26
|
+
"@izara_project/izara-core-library-lambda": "^1.0.5",
|
|
26
27
|
"@izara_project/izara-core-library-logger": "^1.0.7",
|
|
27
|
-
"@izara_project/izara-core-library-service-schemas": "^1.0.
|
|
28
|
+
"@izara_project/izara-core-library-service-schemas": "^1.0.100",
|
|
28
29
|
"@izara_project/izara-core-library-sns": "^1.0.6",
|
|
29
30
|
"@izara_project/izara-core-library-sqs": "^1.0.4",
|
|
30
31
|
"@izara_project/izara-shared": "^1.0.126",
|
|
31
|
-
"@izara_project/izara-shared-
|
|
32
|
-
"@izara_project/izara-shared-
|
|
32
|
+
"@izara_project/izara-shared-core": "^1.0.4",
|
|
33
|
+
"@izara_project/izara-shared-service-schemas": "^1.0.32",
|
|
33
34
|
"ejs": "^3.1.10",
|
|
34
35
|
"js-beautify": "^1.15.4",
|
|
35
36
|
"lodash": "^4.17.21",
|
|
36
37
|
"object-hash": "^3.0.0",
|
|
37
|
-
"yaml": "^2.8.
|
|
38
|
+
"yaml": "^2.8.1"
|
|
38
39
|
}
|
|
39
40
|
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (C) 2021 Sven Mason <http://izara.io>
|
|
3
|
+
|
|
4
|
+
This program is free software: you can redistribute it and/or modify
|
|
5
|
+
it under the terms of the GNU Affero General Public License as
|
|
6
|
+
published by the Free Software Foundation, either version 3 of the
|
|
7
|
+
License, or (at your option) any later version.
|
|
8
|
+
|
|
9
|
+
This program is distributed in the hope that it will be useful,
|
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
GNU Affero General Public License for more details.
|
|
13
|
+
|
|
14
|
+
You should have received a copy of the GNU Affero General Public License
|
|
15
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/*
|
|
19
|
+
Copyright (C) 2020 Sven Mason <http://izara.io>
|
|
20
|
+
|
|
21
|
+
This program is free software: you can redistribute it and/or modify
|
|
22
|
+
it under the terms of the GNU Affero General Public License as
|
|
23
|
+
published by the Free Software Foundation, either version 3 of the
|
|
24
|
+
License, or (at your option) any later version.
|
|
25
|
+
|
|
26
|
+
This program is distributed in the hope that it will be useful,
|
|
27
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
28
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
29
|
+
GNU Affero General Public License for more details.
|
|
30
|
+
|
|
31
|
+
You should have received a copy of the GNU Affero General Public License
|
|
32
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
'use strict';
|
|
36
|
+
|
|
37
|
+
const {
|
|
38
|
+
lambdaFunctionName
|
|
39
|
+
} = require('@izara_project/izara-core-library-lambda');
|
|
40
|
+
const {
|
|
41
|
+
lambda
|
|
42
|
+
} = require('@izara_project/izara-core-library-external-request');
|
|
43
|
+
const { consts } = require('@izara_project/izara-core-library-core');
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Checks a user's permission by invoking another Lambda function.
|
|
47
|
+
* Adheres to Izara.io backend syntax guidelines.
|
|
48
|
+
*
|
|
49
|
+
* @param {object} _izContext - The context object containing logger, credentials, etc.
|
|
50
|
+
* @param {object} payload - The data payload for the permission check.
|
|
51
|
+
* @param {string} [payload.objectType] - The type of the object (e.g., 'INVOICE'). Must be used with `action`.
|
|
52
|
+
* @param {string} [payload.action] - The action to perform (e.g., 'READ', 'APPROVE'). Must be used with `objectType`.
|
|
53
|
+
* @param {string} [payload.flowTag] - The tag for a flow-based permission check. Used instead of `objectType`/`action`.
|
|
54
|
+
* @param {string} [payload.serviceName] - The name of the calling service (optional).
|
|
55
|
+
* @returns {Promise<void>} - Resolves on successful invocation, rejects on error.
|
|
56
|
+
* @throws {Error} If the payload data is invalid or if the Lambda invocation fails.
|
|
57
|
+
*/
|
|
58
|
+
async function checkPermission(_izContext, payload) {
|
|
59
|
+
// Validate that the payload object itself is provided.
|
|
60
|
+
if (!payload || typeof payload !== 'object') {
|
|
61
|
+
const error = new Error('Payload object is required.');
|
|
62
|
+
_izContext.logger.error(error.message);
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const userId = _izContext.correlationIds.get(consts.BASE_USER_ID);
|
|
67
|
+
const targetId = _izContext.correlationIds.get(consts.TARGET_ID);
|
|
68
|
+
|
|
69
|
+
// Validate the required 'userId' field.
|
|
70
|
+
if (!userId || typeof userId !== 'string' || userId.trim() === '') {
|
|
71
|
+
const error = new Error(
|
|
72
|
+
'userId is required and must be a non-empty string.'
|
|
73
|
+
);
|
|
74
|
+
_izContext.logger.error(error.message);
|
|
75
|
+
throw error;
|
|
76
|
+
} else {
|
|
77
|
+
payload.userId = userId;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (targetId) {
|
|
81
|
+
payload.targetId = targetId;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Define validation flags for business rules.
|
|
85
|
+
const hasObjectParams = payload.objectType || payload.action;
|
|
86
|
+
const hasFlowTag = payload.flowTag;
|
|
87
|
+
|
|
88
|
+
// Rule: Cannot mix object-based and flow-based parameters.
|
|
89
|
+
if (hasObjectParams && hasFlowTag) {
|
|
90
|
+
const error = new Error(
|
|
91
|
+
'Invalid payload: Cannot provide flowTag together with objectType or action.'
|
|
92
|
+
);
|
|
93
|
+
_izContext.logger.error(error.message, { payload });
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Rule: If using object-based, both objectType and action are required.
|
|
98
|
+
if (hasObjectParams && (!payload.objectType || !payload.action)) {
|
|
99
|
+
const error = new Error(
|
|
100
|
+
'Invalid payload: Both objectType and action must be provided together.'
|
|
101
|
+
);
|
|
102
|
+
_izContext.logger.error(error.message, { payload });
|
|
103
|
+
throw error;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Rule: Must provide at least one of the two valid schemas.
|
|
107
|
+
if (!hasObjectParams && !hasFlowTag) {
|
|
108
|
+
const error = new Error(
|
|
109
|
+
'Invalid payload: Must provide either (objectType and action) or flowTag.'
|
|
110
|
+
);
|
|
111
|
+
_izContext.logger.error(error.message, { payload });
|
|
112
|
+
throw error;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Log the payload for debugging before invoking the next service.
|
|
116
|
+
_izContext.logger.debug('Checking permission with payload:', payload);
|
|
117
|
+
|
|
118
|
+
try {
|
|
119
|
+
const lambdaName = await lambdaFunctionName(
|
|
120
|
+
_izContext,
|
|
121
|
+
'CheckPermissionHdrInv',
|
|
122
|
+
'UserAccount'
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
await lambda.invokeSync(_izContext, lambdaName, payload);
|
|
126
|
+
|
|
127
|
+
_izContext.logger.info('Permission check invoked successfully.');
|
|
128
|
+
} catch (err) {
|
|
129
|
+
// Log the full error object for better traceability.
|
|
130
|
+
_izContext.logger.error(
|
|
131
|
+
'Error invoking CheckPermissionHdrInv Lambda: ',
|
|
132
|
+
err
|
|
133
|
+
);
|
|
134
|
+
throw err;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
module.exports = {
|
|
139
|
+
checkPermission
|
|
140
|
+
};
|