36
36
import org .fao .geonet .ApplicationContextHolder ;
37
37
import org .fao .geonet .api .ApiParams ;
38
38
import org .fao .geonet .api .ApiUtils ;
39
+ import org .fao .geonet .api .exception .NotAllowedException ;
39
40
import org .fao .geonet .api .processing .report .MetadataReplacementProcessingReport ;
40
41
import org .fao .geonet .api .processing .report .XsltMetadataProcessingReport ;
41
42
import org .fao .geonet .domain .AbstractMetadata ;
43
+ import org .fao .geonet .domain .Profile ;
42
44
import org .fao .geonet .events .history .RecordProcessingChangeEvent ;
43
45
import org .fao .geonet .kernel .DataManager ;
44
46
import org .fao .geonet .kernel .MetadataIndexerProcessor ;
45
47
import org .fao .geonet .kernel .SchemaManager ;
46
48
import org .fao .geonet .kernel .datamanager .IMetadataUtils ;
47
49
import org .fao .geonet .kernel .setting .SettingManager ;
50
+ import org .fao .geonet .kernel .setting .Settings ;
51
+ import org .fao .geonet .util .UserUtil ;
48
52
import org .fao .geonet .utils .Diff ;
49
53
import org .fao .geonet .utils .DiffType ;
50
54
import org .fao .geonet .utils .Log ;
56
60
import org .springframework .http .HttpStatus ;
57
61
import org .springframework .http .MediaType ;
58
62
import org .springframework .http .ResponseEntity ;
63
+ import org .springframework .security .access .hierarchicalroles .RoleHierarchy ;
59
64
import org .springframework .security .access .prepost .PreAuthorize ;
60
65
import org .springframework .stereotype .Controller ;
61
66
import org .springframework .web .bind .annotation .*;
@@ -91,6 +96,9 @@ public class DatabaseProcessApi {
91
96
@ Autowired
92
97
SettingManager settingManager ;
93
98
99
+ @ Autowired
100
+ RoleHierarchy roleHierarchy ;
101
+
94
102
@ io .swagger .v3 .oas .annotations .Operation (
95
103
summary = "Preview of search and replace text." ,
96
104
description =" When errors occur during processing, the processing report is returned in JSON format." )
@@ -175,14 +183,17 @@ public ResponseEntity<Object> previewProcessSearchAndReplace(
175
183
Element preview = new Element ("preview" );
176
184
177
185
try {
186
+ ServiceContext serviceContext = ApiUtils .createServiceContext (request );
187
+ checkUserProfileToBatchEditMetadata (serviceContext .getUserSession ());
188
+
178
189
Set <String > records = ApiUtils .getUuidsParameterOrSelection (uuids , bucket , session );
179
190
180
191
final String siteURL = request .getRequestURL ().toString () + "?" + request .getQueryString ();
181
192
for (String uuid : records ) {
182
193
String id = dataMan .getMetadataId (uuid );
183
194
Log .info ("org.fao.geonet.services.metadata" ,
184
195
"Processing metadata for preview with id:" + id );
185
- ServiceContext serviceContext = ApiUtils . createServiceContext ( request );
196
+
186
197
Element record = DatabaseProcessUtils .process (
187
198
serviceContext ,
188
199
id , useRegexp , search , replace , regexpFlags ,
@@ -307,6 +318,8 @@ public XsltMetadataProcessingReport processSearchAndReplace(
307
318
new MetadataReplacementProcessingReport (search + "-" + replace );
308
319
309
320
try {
321
+ ServiceContext serviceContext = ApiUtils .createServiceContext (request );
322
+ checkUserProfileToBatchEditMetadata (serviceContext .getUserSession ());
310
323
Set <String > records = ApiUtils .getUuidsParameterOrSelection (uuids , bucket , session );
311
324
UserSession userSession = ApiUtils .getUserSession (httpSession );
312
325
@@ -315,7 +328,7 @@ public XsltMetadataProcessingReport processSearchAndReplace(
315
328
processingReport .setTotalRecords (records .size ());
316
329
317
330
BatchDatabaseUpdateMetadataReindexer m = new BatchDatabaseUpdateMetadataReindexer (
318
- ApiUtils . createServiceContext ( request ) ,
331
+ serviceContext ,
319
332
dataMan , records , useRegexp , search , replace , regexpFlags , httpSession , siteURL ,
320
333
processingReport , request , index , updateDateStamp , userSession .getUserIdAsInt ());
321
334
m .process (settingManager .getSiteId ());
@@ -402,4 +415,22 @@ public void process(String catalogueId) throws Exception {
402
415
}
403
416
}
404
417
}
418
+
419
+ /**
420
+ * Checks if the user profile is allowed to batch edit metadata.
421
+ *
422
+ * @param userSession
423
+ */
424
+ private void checkUserProfileToBatchEditMetadata (UserSession userSession ) {
425
+ if (userSession .getProfile () != Profile .Administrator ) {
426
+ String allowedUserProfileToImportMetadata =
427
+ StringUtils .defaultIfBlank (settingManager .getValue (Settings .METADATA_BATCH_EDITING_ACCESS_LEVEL ), Profile .Editor .toString ());
428
+
429
+ // Is the user profile is higher than the profile allowed to import metadata?
430
+ if (!UserUtil .hasHierarchyRole (allowedUserProfileToImportMetadata , this .roleHierarchy )) {
431
+ throw new NotAllowedException ("The user has no permissions to batch edit metadata." );
432
+ }
433
+ }
434
+
435
+ }
405
436
}
0 commit comments