@growy/strapi-plugin-encrypted-field 1.8.0 → 1.9.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@growy/strapi-plugin-encrypted-field",
3
- "version": "1.8.0",
3
+ "version": "1.9.0",
4
4
  "description": "Campo personalizado de texto cifrado para Strapi",
5
5
  "strapi": {
6
6
  "name": "encrypted-field",
@@ -113,8 +113,9 @@ module.exports = ({ strapi }) => {
113
113
  if (Object.prototype.hasOwnProperty.call(result, key)) {
114
114
  const value = result[key];
115
115
  if (typeof value === 'string' && value) {
116
- strapi.log.info(`Descifrando campo ${key} en ${event.model.uid}`);
117
- result[key] = decrypt(value, strapi);
116
+ const decrypted = decrypt(value, strapi);
117
+ strapi.log.info(`Descifrando campo ${key} en ${event.model.uid}: ${value.substring(0, 20)}... -> ${decrypted}`);
118
+ result[key] = decrypted;
118
119
  }
119
120
  }
120
121
  }
@@ -135,7 +136,9 @@ module.exports = ({ strapi }) => {
135
136
  if (Object.prototype.hasOwnProperty.call(item, key)) {
136
137
  const value = item[key];
137
138
  if (typeof value === 'string' && value) {
138
- item[key] = decrypt(value, strapi);
139
+ const decrypted = decrypt(value, strapi);
140
+ strapi.log.info(`Descifrando campo ${key}: ${value.substring(0, 20)}... -> ${decrypted}`);
141
+ item[key] = decrypted;
139
142
  }
140
143
  }
141
144
  }
@@ -4,40 +4,63 @@ module.exports = (config, { strapi }) => {
4
4
  return async (ctx, next) => {
5
5
  await next();
6
6
 
7
- if (!ctx.body || !ctx.body.data) return;
7
+ if (!ctx.body) return;
8
8
 
9
- const decryptData = (data) => {
10
- if (!data || typeof data !== 'object') return;
9
+ // Función recursiva para descifrar campos en cualquier nivel de anidación
10
+ const decryptRecursive = (obj, modelUid = null) => {
11
+ if (!obj || typeof obj !== 'object') return;
11
12
 
12
- const contentType = ctx.params?.model || ctx.state?.route?.info?.type;
13
- if (!contentType) return;
14
-
15
- let model;
16
- try {
17
- model = strapi.getModel(contentType);
18
- } catch (e) {
13
+ // Si es un array, procesar cada elemento
14
+ if (Array.isArray(obj)) {
15
+ obj.forEach(item => decryptRecursive(item, modelUid));
19
16
  return;
20
17
  }
21
18
 
22
- if (!model?.attributes) return;
19
+ // Detectar si es un componente por el campo __component
20
+ let currentModelUid = modelUid;
21
+ if (obj.__component) {
22
+ currentModelUid = obj.__component;
23
+ }
24
+
25
+ // Obtener el modelo si tenemos un UID
26
+ let model = null;
27
+ if (currentModelUid) {
28
+ try {
29
+ model = strapi.getModel(currentModelUid) || strapi.components[currentModelUid];
30
+ } catch (e) {
31
+ // Ignorar si no se encuentra el modelo
32
+ }
33
+ }
23
34
 
24
- for (const [key, attribute] of Object.entries(model.attributes)) {
25
- if (!isEncryptedField(attribute)) continue;
26
-
27
- if (data[key] && typeof data[key] === 'string') {
28
- try {
29
- data[key] = decrypt(data[key], strapi);
30
- } catch (error) {
31
- strapi.log.error(`Error descifrando campo ${key}: ${error.message}`);
35
+ // Descifrar campos del modelo actual
36
+ if (model?.attributes) {
37
+ for (const [key, attribute] of Object.entries(model.attributes)) {
38
+ if (isEncryptedField(attribute) && obj[key] && typeof obj[key] === 'string') {
39
+ try {
40
+ const decrypted = decrypt(obj[key], strapi);
41
+ strapi.log.info(`[Middleware] Descifrando ${key} en ${currentModelUid}: ${obj[key].substring(0, 20)}... -> ${decrypted}`);
42
+ obj[key] = decrypted;
43
+ } catch (error) {
44
+ strapi.log.error(`Error descifrando campo ${key}: ${error.message}`);
45
+ }
32
46
  }
33
47
  }
34
48
  }
49
+
50
+ // Procesar recursivamente todos los valores del objeto
51
+ for (const value of Object.values(obj)) {
52
+ if (value && typeof value === 'object') {
53
+ decryptRecursive(value, currentModelUid);
54
+ }
55
+ }
35
56
  };
36
57
 
37
- if (Array.isArray(ctx.body.data)) {
38
- ctx.body.data.forEach(decryptData);
58
+ // Procesar ctx.body.data si existe
59
+ if (ctx.body.data) {
60
+ decryptRecursive(ctx.body.data);
39
61
  } else {
40
- decryptData(ctx.body.data);
62
+ // Procesar ctx.body directamente
63
+ decryptRecursive(ctx.body);
41
64
  }
42
65
  };
43
66
  };
@@ -1,4 +1,10 @@
1
1
  module.exports = ({ strapi }) => {
2
+ const decryptMiddleware = require('./middlewares/decrypt');
3
+
4
+ strapi.server.use(decryptMiddleware({}, { strapi }));
5
+
6
+ strapi.log.info('✓ Middleware de descifrado registrado');
7
+
2
8
  strapi.customFields.register({
3
9
  name: 'encrypted-text',
4
10
  plugin: 'encrypted-field',