4
4
5
5
use Doctrine \ORM \EntityNotFoundException ;
6
6
use Monarc \Core \Exception \Exception ;
7
+ use Monarc \Core \Model \Entity \AnrSuperClass ;
8
+ use Monarc \Core \Model \Entity \UserSuperClass ;
9
+ use Monarc \Core \Service \ConnectedUserService ;
7
10
use Monarc \FrontOffice \Model \Entity \Amv ;
8
11
use Monarc \FrontOffice \Model \Entity \Anr ;
9
12
use Monarc \FrontOffice \Model \Entity \Asset ;
@@ -64,6 +67,8 @@ class AssetImportService
64
67
/** @var SoaCategoryTable */
65
68
private $ soaCategoryTable ;
66
69
70
+ private UserSuperClass $ connectedUser ;
71
+
67
72
public function __construct (
68
73
AssetTable $ assetTable ,
69
74
ThemeTable $ themeTable ,
@@ -75,7 +80,8 @@ public function __construct(
75
80
InstanceTable $ instanceTable ,
76
81
InstanceRiskTable $ instanceRiskTable ,
77
82
ReferentialTable $ referentialTable ,
78
- SoaCategoryTable $ soaCategoryTable
83
+ SoaCategoryTable $ soaCategoryTable ,
84
+ ConnectedUserService $ connectedUserService
79
85
) {
80
86
$ this ->assetTable = $ assetTable ;
81
87
$ this ->themeTable = $ themeTable ;
@@ -88,6 +94,7 @@ public function __construct(
88
94
$ this ->instanceRiskTable = $ instanceRiskTable ;
89
95
$ this ->referentialTable = $ referentialTable ;
90
96
$ this ->soaCategoryTable = $ soaCategoryTable ;
97
+ $ this ->connectedUser = $ connectedUserService ->getConnectedUser ();
91
98
}
92
99
93
100
public function importFromArray ($ monarcVersion , array $ data , Anr $ anr ): ?Asset
@@ -98,12 +105,16 @@ public function importFromArray($monarcVersion, array $data, Anr $anr): ?Asset
98
105
99
106
if (version_compare ($ monarcVersion , '2.8.2 ' ) < 0 ) {
100
107
throw new Exception ('Import of files exported from MONARC v2.8.1 or lower are not supported. '
101
- . ' Please contact us for more details. '
102
- );
108
+ . ' Please contact us for more details. ' );
103
109
}
104
110
105
111
$ asset = $ this ->assetTable ->findByAnrAndUuid ($ anr , $ data ['asset ' ]['uuid ' ]);
106
112
if ($ asset === null ) {
113
+ $ asset = $ this ->assetTable ->findByAnrAndCode ($ anr , $ data ['asset ' ]['code ' ]);
114
+
115
+ /* The code should be unique. */
116
+ $ assetCode = $ asset === null ? $ data ['asset ' ]['code ' ] : $ asset ->getCode () . '- ' . time ();
117
+
107
118
$ asset = (new Asset ())
108
119
->setUuid ($ data ['asset ' ]['uuid ' ])
109
120
->setAnr ($ anr )
@@ -112,7 +123,7 @@ public function importFromArray($monarcVersion, array $data, Anr $anr): ?Asset
112
123
->setStatus ($ data ['asset ' ]['status ' ] ?? 1 )
113
124
->setMode ($ data ['asset ' ]['mode ' ] ?? 0 )
114
125
->setType ($ data ['asset ' ]['type ' ])
115
- ->setCode ($ data [ ' asset ' ][ ' code ' ] );
126
+ ->setCode ($ assetCode );
116
127
117
128
$ this ->assetTable ->saveEntity ($ asset );
118
129
}
@@ -122,6 +133,8 @@ public function importFromArray($monarcVersion, array $data, Anr $anr): ?Asset
122
133
}
123
134
124
135
$ languageIndex = $ anr ->getLanguage ();
136
+ $ labelKey = 'label ' . $ languageIndex ;
137
+
125
138
$ localThemes = [];
126
139
$ themes = $ this ->themeTable ->findByAnr ($ anr );
127
140
foreach ($ themes as $ theme ) {
@@ -131,70 +144,102 @@ public function importFromArray($monarcVersion, array $data, Anr $anr): ?Asset
131
144
/*
132
145
* Threats
133
146
*/
134
- foreach ($ data ['threats ' ] as $ threatUuid => $ valueThreat ) {
135
- try {
147
+ $ threatsUuidsAndCodes = $ this ->threatTable ->findUuidsAndCodesByAnr ($ anr );
148
+ $ threatsUuids = array_column ($ threatsUuidsAndCodes , 'uuid ' );
149
+ $ threatsCodes = array_column ($ threatsUuidsAndCodes , 'code ' );
150
+ foreach ($ data ['threats ' ] as $ threatUuid => $ threatData ) {
151
+ if (isset ($ this ->cachedData ['threats ' ][$ threatData ['uuid ' ]])) {
152
+ continue ;
153
+ }
154
+
155
+ if (\in_array ((string )$ threatData ['uuid ' ], $ threatsUuids , true )) {
136
156
$ threat = $ this ->threatTable ->findByAnrAndUuid ($ anr , $ threatUuid );
137
157
$ this ->cachedData ['threats ' ][$ threat ->getUuid ()] = $ threat ;
138
- } catch (EntityNotFoundException $ exception ) {
158
+
159
+ /* Validate Theme. */
160
+ $ currentTheme = $ threat ->getTheme ();
161
+ if (!empty ($ threatData ['theme ' ])
162
+ && ($ currentTheme === null
163
+ || $ currentTheme ->getLabel ($ languageIndex ) !== $ data ['themes ' ][$ threatData ['theme ' ]][$ labelKey ]
164
+ )
165
+ ) {
166
+ if (isset ($ localThemes [$ data ['themes ' ][$ threatData ['theme ' ]][$ labelKey ]])) {
167
+ $ theme = $ localThemes [$ data ['themes ' ][$ threatData ['theme ' ]][$ labelKey ]];
168
+ } else {
169
+ $ theme = $ this ->getCreatedTheme ($ anr , $ data ['themes ' ][$ threatData ['theme ' ]]);
170
+ }
171
+
172
+ $ threat ->setTheme ($ theme );
173
+ $ this ->threatTable ->saveEntity ($ threat , false );
174
+ }
175
+ } else {
139
176
$ theme = null ;
140
- if (isset ($ localThemes [$ data ['themes ' ][$ valueThreat ['theme ' ]]['label ' . $ languageIndex ]])) { //theme exists
141
- $ theme = $ localThemes [$ data ['themes ' ][$ valueThreat ['theme ' ]]['label ' . $ languageIndex ]];
142
- } elseif (isset ($ data ['themes ' ][$ valueThreat ['theme ' ]])) {
143
- $ theme = new Theme ();
144
- $ theme ->setLanguage ($ languageIndex );
145
- $ theme ->exchangeArray ($ data ['themes ' ][$ valueThreat ['theme ' ]]);
146
- $ theme ->setAnr ($ anr );
147
- $ this ->themeTable ->saveEntity ($ theme , false );
177
+ if (isset ($ localThemes [$ data ['themes ' ][$ threatData ['theme ' ]][$ labelKey ]])) {
178
+ $ theme = $ localThemes [$ data ['themes ' ][$ threatData ['theme ' ]][$ labelKey ]];
179
+ } elseif (isset ($ data ['themes ' ][$ threatData ['theme ' ]])) {
180
+ $ theme = $ this ->getCreatedTheme ($ anr , $ data ['themes ' ][$ threatData ['theme ' ]]);
148
181
}
149
- $ threat = new Threat ();
150
- $ threat ->setLanguage ($ languageIndex );
151
- // TODO: Can be replaced with setters use, example in InstanceImportService.
152
- $ threat ->exchangeArray ($ valueThreat );
153
- $ threat ->setAnr ($ anr );
154
- $ threat ->setTheme ($ theme );
155
- $ this ->threatTable ->saveEntity ($ threat , false );
156
-
157
- $ this ->cachedData ['threats ' ][$ threat ->getUuid ()] = $ threat ;
158
182
159
- continue ;
160
- }
183
+ if (\in_array ($ threatData ['code ' ], $ threatsCodes , true )) {
184
+ $ threatData ['code ' ] .= '- ' . time ();
185
+ }
161
186
162
- if (!empty ($ valueThreat ['theme ' ])
163
- && (
164
- $ threat ->getTheme () === null
165
- || $ threat ->getTheme ()->getLabel ($ languageIndex ) !== $ data ['themes ' ][$ valueThreat ['theme ' ]]['label ' . $ languageIndex ]
166
- )
167
- ) {
168
- if (isset ($ localThemes [$ data ['themes ' ][$ valueThreat ['theme ' ]]['label ' . $ languageIndex ]])) {
169
- $ theme = $ localThemes [$ data ['themes ' ][$ valueThreat ['theme ' ]]['label ' . $ languageIndex ]];
170
- } else {
171
- $ theme = new Theme ();
172
- $ theme ->setLanguage ($ languageIndex );
173
- $ theme ->exchangeArray ($ data ['themes ' ][$ valueThreat ['theme ' ]]);
174
- $ theme ->setAnr ($ anr );
175
- $ this ->themeTable ->saveEntity ($ theme , false );
187
+ $ threat = (new Threat ())
188
+ ->setUuid ($ threatData ['uuid ' ])
189
+ ->setAnr ($ anr )
190
+ ->setCode ($ threatData ['code ' ])
191
+ ->setLabels ($ threatData )
192
+ ->setDescriptions ($ threatData )
193
+ ->setMode ((int )$ threatData ['mode ' ])
194
+ ->setStatus ((int )$ threatData ['status ' ])
195
+ ->setTrend ((int )$ threatData ['trend ' ])
196
+ ->setQualification ((int )$ threatData ['qualification ' ])
197
+ ->setComment ($ threatData ['comment ' ] ?? '' )
198
+ ->setCreator ($ this ->connectedUser ->getEmail ());
199
+ if (isset ($ threatData ['c ' ])) {
200
+ $ threat ->setConfidentiality ((int )$ threatData ['c ' ]);
201
+ }
202
+ if (isset ($ threatData ['i ' ])) {
203
+ $ threat ->setIntegrity ((int )$ threatData ['i ' ]);
204
+ }
205
+ if (isset ($ threatData ['a ' ])) {
206
+ $ threat ->setAvailability ((int )$ threatData ['a ' ]);
176
207
}
177
208
$ threat ->setTheme ($ theme );
209
+
178
210
$ this ->threatTable ->saveEntity ($ threat , false );
211
+
212
+ $ this ->cachedData ['threats ' ][$ threat ->getUuid ()] = $ threat ;
179
213
}
180
214
}
181
215
182
216
/*
183
217
* Vulnerabilities
184
218
*/
185
- $ vulnerabilitiesUuids = $ this ->vulnerabilityTable ->findUuidsByAnr ($ anr );
219
+ $ vulnerabilitiesUuidsAndCodes = $ this ->vulnerabilityTable ->findUuidsAndCodesByAnr ($ anr );
220
+ $ vulnerabilitiesUuids = array_column ($ vulnerabilitiesUuidsAndCodes , 'uuid ' );
221
+ $ vulnerabilitiesCodes = array_column ($ vulnerabilitiesUuidsAndCodes , 'code ' );
186
222
foreach ($ data ['vuls ' ] as $ valueVul ) {
187
223
if (!isset ($ this ->cachedData ['vulnerabilities ' ][(string )$ valueVul ['uuid ' ]])
188
224
&& !\in_array ((string )$ valueVul ['uuid ' ], $ vulnerabilitiesUuids , true )
189
225
) {
190
- $ vulnerability = new Vulnerability ();
191
- $ vulnerability ->setLanguage ($ languageIndex );
192
- $ vulnerability ->exchangeArray ($ valueVul );
193
- $ vulnerability ->setAnr ($ anr );
226
+ if (\in_array ($ valueVul ['code ' ], $ vulnerabilitiesCodes , true )) {
227
+ $ valueVul ['code ' ] .= '- ' . time ();
228
+ }
229
+
230
+ $ vulnerability = (new Vulnerability ())
231
+ ->setUuid ($ valueVul ['uuid ' ])
232
+ ->setAnr ($ anr )
233
+ ->setLabels ($ valueVul )
234
+ ->setDescriptions ($ valueVul )
235
+ ->setCode ($ valueVul ['code ' ])
236
+ ->setMode ($ valueVul ['mode ' ])
237
+ ->setStatus ($ valueVul ['status ' ])
238
+ ->setCreator ($ this ->connectedUser ->getEmail ());
239
+
194
240
$ this ->vulnerabilityTable ->saveEntity ($ vulnerability , false );
195
- $ vulnerabilitiesUuids [] = $ vulnerability ->getUuid ();
196
241
197
- $ this ->cachedData ['vulnerabilities ' ][$ vulnerability -> getUuid () ] = $ vulnerability ;
242
+ $ this ->cachedData ['vulnerabilities ' ][$ valueVul [ ' uuid ' ] ] = $ vulnerability ;
198
243
}
199
244
}
200
245
$ this ->vulnerabilityTable ->getDb ()->flush ();
@@ -258,7 +303,6 @@ public function importFromArray($monarcVersion, array $data, Anr $anr): ?Asset
258
303
}
259
304
260
305
if (!empty ($ valueAmv ['measures ' ])) {
261
- $ labelName = 'label ' . $ languageIndex ;
262
306
foreach ($ valueAmv ['measures ' ] as $ measureUuid ) {
263
307
$ measure = $ this ->cachedData ['measures ' ][$ measureUuid ]
264
308
?? $ this ->measureTable ->findByAnrAndUuid ($ anr , $ measureUuid );
@@ -274,12 +318,14 @@ public function importFromArray($monarcVersion, array $data, Anr $anr): ?Asset
274
318
275
319
// For backward compatibility issue.
276
320
if ($ referential === null
277
- && isset ($ data ['measures ' ][$ measureUuid ]['referential ' ][$ labelName ])
321
+ && isset ($ data ['measures ' ][$ measureUuid ]['referential ' ][$ labelKey ])
278
322
) {
279
323
$ referential = (new Referential ())
280
324
->setAnr ($ anr )
281
325
->setUuid ($ data ['measures ' ][$ measureUuid ]['referential ' ]['uuid ' ])
282
- ->{'setLabel ' . $ languageIndex }($ data ['measures ' ][$ measureUuid ]['referential ' ][$ labelName ]);
326
+ ->{'setLabel ' . $ languageIndex }(
327
+ $ data ['measures ' ][$ measureUuid ]['referential ' ][$ labelKey ]
328
+ );
283
329
$ this ->referentialTable ->saveEntity ($ referential );
284
330
}
285
331
@@ -290,7 +336,7 @@ public function importFromArray($monarcVersion, array $data, Anr $anr): ?Asset
290
336
291
337
$ category = $ this ->soaCategoryTable ->getEntityByFields ([
292
338
'anr ' => $ anr ->getId (),
293
- $ labelName => $ data ['measures ' ][$ measureUuid ]['category ' ][$ labelName ],
339
+ $ labelKey => $ data ['measures ' ][$ measureUuid ]['category ' ][$ labelKey ],
294
340
'referential ' => [
295
341
'anr ' => $ anr ->getId (),
296
342
'uuid ' => $ referential ->getUuid (),
@@ -300,7 +346,7 @@ public function importFromArray($monarcVersion, array $data, Anr $anr): ?Asset
300
346
$ category = (new SoaCategory ())
301
347
->setAnr ($ anr )
302
348
->setReferential ($ referential )
303
- ->{'setLabel ' . $ languageIndex }($ data ['measures ' ][$ measureUuid ]['category ' ][$ labelName ]);
349
+ ->{'setLabel ' . $ languageIndex }($ data ['measures ' ][$ measureUuid ]['category ' ][$ labelKey ]);
304
350
/** @var SoaCategoryTable $soaCategoryTable */
305
351
$ this ->soaCategoryTable ->saveEntity ($ category , false );
306
352
} else {
@@ -369,4 +415,16 @@ public function getCachedDataByKey(string $key): array
369
415
{
370
416
return $ this ->cachedData [$ key ] ?? [];
371
417
}
418
+
419
+ private function getCreatedTheme (Anr $ anr , array $ data ): Theme
420
+ {
421
+ $ theme = (new Theme ())
422
+ ->setAnr ($ anr )
423
+ ->setLabels ($ data )
424
+ ->setCreator ($ this ->connectedUser ->getEmail ());
425
+
426
+ $ this ->themeTable ->saveEntity ($ theme , false );
427
+
428
+ return $ theme ;
429
+ }
372
430
}
0 commit comments