@growy/strapi-plugin-encrypted-field 1.6.0 → 1.7.0
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 +1 -1
- package/server/bootstrap.js +17 -48
package/package.json
CHANGED
package/server/bootstrap.js
CHANGED
|
@@ -1,55 +1,23 @@
|
|
|
1
1
|
const { encrypt, decrypt, validateValue, isEncryptedField } = require('./utils/crypto');
|
|
2
2
|
|
|
3
3
|
module.exports = ({ strapi }) => {
|
|
4
|
-
// Registrar
|
|
5
|
-
strapi.
|
|
6
|
-
|
|
4
|
+
// Registrar lifecycles para cada content type que tenga campos cifrados
|
|
5
|
+
const models = Object.values(strapi.contentTypes);
|
|
6
|
+
|
|
7
|
+
models.forEach((model) => {
|
|
8
|
+
const hasEncryptedFields = Object.values(model.attributes || {}).some(isEncryptedField);
|
|
9
|
+
|
|
10
|
+
if (!hasEncryptedFields) return;
|
|
7
11
|
|
|
8
|
-
|
|
12
|
+
const uid = model.uid;
|
|
13
|
+
|
|
14
|
+
strapi.log.info(`Registrando lifecycles de cifrado para ${uid}`);
|
|
9
15
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
? `api::${ctx.state.route.info.apiName}.${ctx.state.route.info.apiName}`
|
|
16
|
-
: null;
|
|
17
|
-
|
|
18
|
-
if (!uid) return;
|
|
19
|
-
|
|
20
|
-
let model;
|
|
21
|
-
try {
|
|
22
|
-
model = strapi.getModel(uid);
|
|
23
|
-
} catch (e) {
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (!model?.attributes) return;
|
|
28
|
-
|
|
29
|
-
for (const [key, attribute] of Object.entries(model.attributes)) {
|
|
30
|
-
if (!isEncryptedField(attribute)) continue;
|
|
31
|
-
|
|
32
|
-
if (data[key] && typeof data[key] === 'string') {
|
|
33
|
-
try {
|
|
34
|
-
const decrypted = decrypt(data[key], strapi);
|
|
35
|
-
strapi.log.info(`Descifrando campo ${key} en respuesta API`);
|
|
36
|
-
data[key] = decrypted;
|
|
37
|
-
} catch (error) {
|
|
38
|
-
strapi.log.error(`Error descifrando campo ${key}: ${error.message}`);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
if (Array.isArray(ctx.body.data)) {
|
|
45
|
-
ctx.body.data.forEach(decryptData);
|
|
46
|
-
} else {
|
|
47
|
-
decryptData(ctx.body.data);
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
strapi.db.lifecycles.subscribe({
|
|
52
|
-
async beforeCreate(event) {
|
|
16
|
+
// Registrar lifecycle específico para este content type
|
|
17
|
+
strapi.db.lifecycles.subscribe({
|
|
18
|
+
models: [uid],
|
|
19
|
+
|
|
20
|
+
async beforeCreate(event) {
|
|
53
21
|
if (!event.model?.uid) return;
|
|
54
22
|
|
|
55
23
|
const { data } = event.params;
|
|
@@ -152,6 +120,7 @@ module.exports = ({ strapi }) => {
|
|
|
152
120
|
}
|
|
153
121
|
}
|
|
154
122
|
}
|
|
155
|
-
|
|
123
|
+
},
|
|
124
|
+
});
|
|
156
125
|
});
|
|
157
126
|
};
|