@steedos/service-objectql 2.5.0-beta.30 → 2.5.0-beta.35
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/package.json +3 -3
- package/package.service.js +56 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@steedos/service-objectql",
|
|
3
|
-
"version": "2.5.0-beta.
|
|
3
|
+
"version": "2.5.0-beta.35",
|
|
4
4
|
"main": "package.service.js",
|
|
5
5
|
"private": false,
|
|
6
6
|
"publishConfig": {
|
|
@@ -13,8 +13,8 @@
|
|
|
13
13
|
"description": "steedos package",
|
|
14
14
|
"repository": {},
|
|
15
15
|
"license": "MIT",
|
|
16
|
-
"gitHead": "
|
|
16
|
+
"gitHead": "6588ef59b6d98ce127d43d677d6315aa866ad47b",
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@steedos/objectql": "2.5.0-beta.
|
|
18
|
+
"@steedos/objectql": "2.5.0-beta.35"
|
|
19
19
|
}
|
|
20
20
|
}
|
package/package.service.js
CHANGED
|
@@ -2,13 +2,14 @@
|
|
|
2
2
|
* @Author: sunhaolin@hotoa.com
|
|
3
3
|
* @Date: 2023-03-23 15:12:14
|
|
4
4
|
* @LastEditors: baozhoutao@steedos.com
|
|
5
|
-
* @LastEditTime: 2023-
|
|
5
|
+
* @LastEditTime: 2023-05-18 14:33:09
|
|
6
6
|
* @Description:
|
|
7
7
|
*/
|
|
8
8
|
"use strict";
|
|
9
9
|
// @ts-check
|
|
10
|
+
const objectql = require('@steedos/objectql');
|
|
11
|
+
const { getObject } = objectql;
|
|
10
12
|
|
|
11
|
-
const { getObject } = require('@steedos/objectql');
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* @typedef {import('moleculer').Context} Context Moleculer's Context
|
|
@@ -482,6 +483,16 @@ module.exports = {
|
|
|
482
483
|
const obj = getObject(objectName)
|
|
483
484
|
return await obj.allowDelete(userSession);
|
|
484
485
|
}
|
|
486
|
+
},
|
|
487
|
+
encryptFieldValue: {
|
|
488
|
+
params: {
|
|
489
|
+
objectName: { type: "string" },
|
|
490
|
+
doc: { type: "object", optional: true },
|
|
491
|
+
},
|
|
492
|
+
async handler(ctx) {
|
|
493
|
+
const { objectName, doc } = ctx.params;
|
|
494
|
+
return await this.encryptFieldValue(objectName, doc);
|
|
495
|
+
}
|
|
485
496
|
}
|
|
486
497
|
|
|
487
498
|
},
|
|
@@ -497,8 +508,50 @@ module.exports = {
|
|
|
497
508
|
* Methods
|
|
498
509
|
*/
|
|
499
510
|
methods: {
|
|
511
|
+
/**
|
|
512
|
+
* 获取需要加密的字段
|
|
513
|
+
* @param {*} objectName
|
|
514
|
+
* @returns
|
|
515
|
+
*/
|
|
516
|
+
getRequireEncryptionFields: async function (objectName) {
|
|
517
|
+
const objFields = await objectql.getObject(objectName).getFields();
|
|
518
|
+
const requireEncryptionFields = {};
|
|
519
|
+
// 遍历所有字段,整理出要求加密的字段
|
|
520
|
+
for (const key in objFields) {
|
|
521
|
+
if (Object.hasOwnProperty.call(objFields, key)) {
|
|
522
|
+
const field = objFields[key];
|
|
523
|
+
if (field.enable_encryption) {
|
|
524
|
+
requireEncryptionFields[key] = field;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
return requireEncryptionFields;
|
|
529
|
+
},
|
|
500
530
|
|
|
501
|
-
|
|
531
|
+
/**
|
|
532
|
+
* 加密字段值
|
|
533
|
+
* @param {*} objectName
|
|
534
|
+
* @param {*} doc
|
|
535
|
+
*/
|
|
536
|
+
encryptFieldValue: async function (objectName, doc) {
|
|
537
|
+
try {
|
|
538
|
+
const datasource = objectql.getDataSource('default');
|
|
539
|
+
const requireEncryptionFields = await this.getRequireEncryptionFields(objectName);
|
|
540
|
+
for (const key in requireEncryptionFields) {
|
|
541
|
+
if (Object.hasOwnProperty.call(requireEncryptionFields, key)) {
|
|
542
|
+
const field = requireEncryptionFields[key];
|
|
543
|
+
// 判断是加密字段并且值不为空,且还未加密过
|
|
544
|
+
if (field.enable_encryption && _.has(doc, key) && doc[key] && !doc[key].buffer && !doc[key].sub_type) {
|
|
545
|
+
doc[key] = await datasource.adapter.encryptValue(doc[key]);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
} catch (error) {
|
|
550
|
+
console.error('encryptFieldValue error', objectName, doc)
|
|
551
|
+
console.error(`[字段级加密] 对象${objectName}中字段加密失败:`, error);
|
|
552
|
+
}
|
|
553
|
+
return doc;
|
|
554
|
+
}
|
|
502
555
|
},
|
|
503
556
|
|
|
504
557
|
/**
|