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

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 +80 -92
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.1",
4
4
  "description": "Campo personalizado de texto cifrado para Strapi",
5
5
  "strapi": {
6
6
  "name": "encrypted-field",
@@ -1,7 +1,7 @@
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
7
  models.forEach((model) => {
@@ -13,113 +13,101 @@ module.exports = ({ strapi }) => {
13
13
 
14
14
  strapi.log.info(`Registrando lifecycles de cifrado para ${uid}`);
15
15
 
16
- // Registrar lifecycle específico para este content type
16
+
17
17
  strapi.db.lifecycles.subscribe({
18
18
  models: [uid],
19
19
 
20
20
  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;
33
-
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
-
40
- const validation = validateValue(value, attribute);
41
- if (!validation.valid) {
42
- throw new Error(`Validación fallida para el campo "${key}": ${validation.error}`);
21
+ if (!event.model?.uid) return;
22
+
23
+ const { data } = event.params;
24
+ const currentModel = strapi.getModel(event.model.uid);
25
+
26
+ if (!currentModel?.attributes) return;
27
+
28
+ for (const [key, attribute] of Object.entries(currentModel.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;
33
+
34
+ const validation = validateValue(value, attribute);
35
+ if (!validation.valid) {
36
+ throw new Error(`Validación fallida para el campo "${key}": ${validation.error}`);
37
+ }
38
+
39
+ strapi.log.info(`Cifrando campo ${key} en ${event.model.uid}`);
40
+ data[key] = encrypt(value, strapi);
43
41
  }
44
-
45
- strapi.log.info(`Cifrando campo ${key} en ${event.model.uid}`);
46
- data[key] = encrypt(value, strapi);
47
42
  }
48
- }
49
- },
43
+ },
50
44
 
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}`);
45
+ async beforeUpdate(event) {
46
+ if (!event.model?.uid) return;
47
+
48
+ const { data } = event.params;
49
+ const currentModel = strapi.getModel(event.model.uid);
50
+
51
+ if (!currentModel?.attributes) return;
52
+
53
+ for (const [key, attribute] of Object.entries(currentModel.attributes)) {
54
+ if (!isEncryptedField(attribute)) continue;
55
+ if (Object.prototype.hasOwnProperty.call(data, key)) {
56
+ const value = data[key];
57
+ if (value === null || value === undefined || value === '') continue;
58
+
59
+ const validation = validateValue(value, attribute);
60
+ if (!validation.valid) {
61
+ throw new Error(`Validación fallida para el campo "${key}": ${validation.error}`);
62
+ }
63
+
64
+ strapi.log.info(`Cifrando campo ${key} en ${event.model.uid}`);
65
+ data[key] = encrypt(value, strapi);
74
66
  }
75
-
76
- strapi.log.info(`Cifrando campo ${key} en ${event.model.uid}`);
77
- data[key] = encrypt(value, strapi);
78
67
  }
79
- }
80
- },
68
+ },
81
69
 
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);
70
+ async afterFindOne(event) {
71
+ const { result } = event;
72
+ if (!result) return;
73
+ if (!event.model?.uid) return;
74
+
75
+ const currentModel = strapi.getModel(event.model.uid);
76
+
77
+ if (!currentModel?.attributes) return;
78
+
79
+ for (const [key, attribute] of Object.entries(currentModel.attributes)) {
80
+ if (!isEncryptedField(attribute)) continue;
81
+ if (Object.prototype.hasOwnProperty.call(result, key)) {
82
+ const value = result[key];
83
+ if (typeof value === 'string' && value) {
84
+ strapi.log.info(`Descifrando campo ${key} en ${event.model.uid}`);
85
+ result[key] = decrypt(value, strapi);
86
+ }
98
87
  }
99
88
  }
100
- }
101
- },
89
+ },
102
90
 
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);
91
+ async afterFindMany(event) {
92
+ const { result } = event;
93
+ if (!result || !Array.isArray(result)) return;
94
+ if (!event.model?.uid) return;
95
+
96
+ const currentModel = strapi.getModel(event.model.uid);
97
+
98
+ if (!currentModel?.attributes) return;
99
+
100
+ for (const item of result) {
101
+ for (const [key, attribute] of Object.entries(currentModel.attributes)) {
102
+ if (!isEncryptedField(attribute)) continue;
103
+ if (Object.prototype.hasOwnProperty.call(item, key)) {
104
+ const value = item[key];
105
+ if (typeof value === 'string' && value) {
106
+ item[key] = decrypt(value, strapi);
107
+ }
119
108
  }
120
109
  }
121
110
  }
122
- }
123
111
  },
124
112
  });
125
113
  });