@growy/strapi-plugin-encrypted-field 1.7.0 → 1.7.2

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/server/bootstrap.js +99 -93
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@growy/strapi-plugin-encrypted-field",
3
- "version": "1.7.0",
3
+ "version": "1.7.2",
4
4
  "description": "Campo personalizado de texto cifrado para Strapi",
5
5
  "strapi": {
6
6
  "name": "encrypted-field",
@@ -1,125 +1,131 @@
1
1
  const { encrypt, decrypt, validateValue, isEncryptedField } = require('./utils/crypto');
2
2
 
3
3
  module.exports = ({ strapi }) => {
4
- // Registrar lifecycles para cada content type que tenga campos cifrados
4
+
5
5
  const models = Object.values(strapi.contentTypes);
6
6
 
7
+ strapi.log.info(`Total de modelos encontrados: ${models.length}`);
8
+
7
9
  models.forEach((model) => {
8
- const hasEncryptedFields = Object.values(model.attributes || {}).some(isEncryptedField);
10
+ const encryptedFields = Object.entries(model.attributes || {})
11
+ .filter(([key, attr]) => isEncryptedField(attr))
12
+ .map(([key]) => key);
9
13
 
10
- if (!hasEncryptedFields) return;
14
+ if (encryptedFields.length === 0) return;
11
15
 
12
16
  const uid = model.uid;
13
17
 
14
- strapi.log.info(`Registrando lifecycles de cifrado para ${uid}`);
18
+ strapi.log.info(`✓ Registrando lifecycles de cifrado para ${uid} - Campos: ${encryptedFields.join(', ')}`);
15
19
 
16
- // Registrar lifecycle específico para este content type
20
+
17
21
  strapi.db.lifecycles.subscribe({
18
22
  models: [uid],
19
23
 
20
24
  async beforeCreate(event) {
21
- if (!event.model?.uid) return;
22
-
23
- const { data } = event.params;
24
- const model = strapi.getModel(event.model.uid);
25
-
26
- if (!model?.attributes) return;
27
-
28
- for (const [key, attribute] of Object.entries(model.attributes)) {
29
- if (!isEncryptedField(attribute)) continue;
30
- if (Object.prototype.hasOwnProperty.call(data, key)) {
31
- const value = data[key];
32
- if (value === null || value === undefined || value === '') continue;
25
+ const { data } = event.params;
26
+
27
+ strapi.log.info(`[beforeCreate] Evento recibido para ${event.model?.uid}`);
28
+ strapi.log.info(`[beforeCreate] Data recibida: ${JSON.stringify(data)}`);
29
+
30
+ if (!event.model?.uid) return;
31
+
32
+ const currentModel = strapi.getModel(event.model.uid);
33
+
34
+ if (!currentModel?.attributes) return;
35
+
36
+ for (const [key, attribute] of Object.entries(currentModel.attributes)) {
37
+ if (!isEncryptedField(attribute)) continue;
33
38
 
34
- // No cifrar si ya está cifrado (formato: iv:authTag:encrypted)
35
- if (typeof value === 'string' && value.split(':').length === 3) {
36
- strapi.log.info(`Campo ${key} ya está cifrado, saltando cifrado`);
37
- continue;
38
- }
39
+ strapi.log.info(`[beforeCreate] Campo cifrado detectado: ${key}`);
39
40
 
40
- const validation = validateValue(value, attribute);
41
- if (!validation.valid) {
42
- throw new Error(`Validación fallida para el campo "${key}": ${validation.error}`);
41
+ if (Object.prototype.hasOwnProperty.call(data, key)) {
42
+ const value = data[key];
43
+
44
+ strapi.log.info(`[beforeCreate] Valor del campo ${key}: ${value}`);
45
+
46
+ if (value === null || value === undefined || value === '') {
47
+ strapi.log.info(`[beforeCreate] Valor vacío, saltando cifrado`);
48
+ continue;
49
+ }
50
+
51
+ const validation = validateValue(value, attribute);
52
+ if (!validation.valid) {
53
+ throw new Error(`Validación fallida para el campo "${key}": ${validation.error}`);
54
+ }
55
+
56
+ strapi.log.info(`✓ Cifrando campo ${key} con valor: ${value}`);
57
+ data[key] = encrypt(value, strapi);
58
+ strapi.log.info(`✓ Campo ${key} cifrado exitosamente`);
43
59
  }
44
-
45
- strapi.log.info(`Cifrando campo ${key} en ${event.model.uid}`);
46
- data[key] = encrypt(value, strapi);
47
60
  }
48
- }
49
- },
61
+ },
50
62
 
51
- async beforeUpdate(event) {
52
- if (!event.model?.uid) return;
53
-
54
- const { data } = event.params;
55
- const model = strapi.getModel(event.model.uid);
56
-
57
- if (!model?.attributes) return;
58
-
59
- for (const [key, attribute] of Object.entries(model.attributes)) {
60
- if (!isEncryptedField(attribute)) continue;
61
- if (Object.prototype.hasOwnProperty.call(data, key)) {
62
- const value = data[key];
63
- if (value === null || value === undefined || value === '') continue;
64
-
65
- // No cifrar si ya está cifrado (formato: iv:authTag:encrypted)
66
- if (typeof value === 'string' && value.split(':').length === 3) {
67
- strapi.log.info(`Campo ${key} ya está cifrado, saltando cifrado`);
68
- continue;
69
- }
70
-
71
- const validation = validateValue(value, attribute);
72
- if (!validation.valid) {
73
- throw new Error(`Validación fallida para el campo "${key}": ${validation.error}`);
63
+ async beforeUpdate(event) {
64
+ if (!event.model?.uid) return;
65
+
66
+ const { data } = event.params;
67
+ const currentModel = strapi.getModel(event.model.uid);
68
+
69
+ if (!currentModel?.attributes) return;
70
+
71
+ for (const [key, attribute] of Object.entries(currentModel.attributes)) {
72
+ if (!isEncryptedField(attribute)) continue;
73
+ if (Object.prototype.hasOwnProperty.call(data, key)) {
74
+ const value = data[key];
75
+ if (value === null || value === undefined || value === '') continue;
76
+
77
+ const validation = validateValue(value, attribute);
78
+ if (!validation.valid) {
79
+ throw new Error(`Validación fallida para el campo "${key}": ${validation.error}`);
80
+ }
81
+
82
+ strapi.log.info(`Cifrando campo ${key} en ${event.model.uid}`);
83
+ data[key] = encrypt(value, strapi);
74
84
  }
75
-
76
- strapi.log.info(`Cifrando campo ${key} en ${event.model.uid}`);
77
- data[key] = encrypt(value, strapi);
78
85
  }
79
- }
80
- },
86
+ },
81
87
 
82
- async afterFindOne(event) {
83
- const { result } = event;
84
- if (!result) return;
85
- if (!event.model?.uid) return;
86
-
87
- const model = strapi.getModel(event.model.uid);
88
-
89
- if (!model?.attributes) return;
90
-
91
- for (const [key, attribute] of Object.entries(model.attributes)) {
92
- if (!isEncryptedField(attribute)) continue;
93
- if (Object.prototype.hasOwnProperty.call(result, key)) {
94
- const value = result[key];
95
- if (typeof value === 'string' && value) {
96
- strapi.log.info(`Descifrando campo ${key} en ${event.model.uid}`);
97
- result[key] = decrypt(value, strapi);
88
+ async afterFindOne(event) {
89
+ const { result } = event;
90
+ if (!result) return;
91
+ if (!event.model?.uid) return;
92
+
93
+ const currentModel = strapi.getModel(event.model.uid);
94
+
95
+ if (!currentModel?.attributes) return;
96
+
97
+ for (const [key, attribute] of Object.entries(currentModel.attributes)) {
98
+ if (!isEncryptedField(attribute)) continue;
99
+ if (Object.prototype.hasOwnProperty.call(result, key)) {
100
+ const value = result[key];
101
+ if (typeof value === 'string' && value) {
102
+ strapi.log.info(`Descifrando campo ${key} en ${event.model.uid}`);
103
+ result[key] = decrypt(value, strapi);
104
+ }
98
105
  }
99
106
  }
100
- }
101
- },
107
+ },
102
108
 
103
- async afterFindMany(event) {
104
- const { result } = event;
105
- if (!result || !Array.isArray(result)) return;
106
- if (!event.model?.uid) return;
107
-
108
- const model = strapi.getModel(event.model.uid);
109
-
110
- if (!model?.attributes) return;
111
-
112
- for (const item of result) {
113
- for (const [key, attribute] of Object.entries(model.attributes)) {
114
- if (!isEncryptedField(attribute)) continue;
115
- if (Object.prototype.hasOwnProperty.call(item, key)) {
116
- const value = item[key];
117
- if (typeof value === 'string' && value) {
118
- item[key] = decrypt(value, strapi);
109
+ async afterFindMany(event) {
110
+ const { result } = event;
111
+ if (!result || !Array.isArray(result)) return;
112
+ if (!event.model?.uid) return;
113
+
114
+ const currentModel = strapi.getModel(event.model.uid);
115
+
116
+ if (!currentModel?.attributes) return;
117
+
118
+ for (const item of result) {
119
+ for (const [key, attribute] of Object.entries(currentModel.attributes)) {
120
+ if (!isEncryptedField(attribute)) continue;
121
+ if (Object.prototype.hasOwnProperty.call(item, key)) {
122
+ const value = item[key];
123
+ if (typeof value === 'string' && value) {
124
+ item[key] = decrypt(value, strapi);
125
+ }
119
126
  }
120
127
  }
121
128
  }
122
- }
123
129
  },
124
130
  });
125
131
  });