@@ -56,15 +56,14 @@ public class UserService {
56
56
private final long tokenExpirationTime ;
57
57
private static final long defaultTokenExpirationTime = 1000L * 60 * 60 ; // 1 hour
58
58
private final SessionService sessionService ;
59
+ private final boolean openAccessIsEnabled ;
59
60
60
61
public long longTermTokenExpirationTime ;
61
62
62
63
private final String applicationUUID ;
63
64
private final ObjectMapper objectMapper = new ObjectMapper ();
64
65
private final JWTUtil jwtUtil ;
65
66
66
- private final Set <String > openAccessIdpValues = Set .of ("fence" , "ras" );
67
-
68
67
@ Autowired
69
68
public UserService (BasicMailService basicMailService , TOSService tosService ,
70
69
UserRepository userRepository ,
@@ -74,7 +73,8 @@ public UserService(BasicMailService basicMailService, TOSService tosService,
74
73
@ Value ("${application.token.expiration.time}" ) long tokenExpirationTime ,
75
74
@ Value ("${application.default.uuid}" ) String applicationUUID ,
76
75
@ Value ("${application.long.term.token.expiration.time}" ) long longTermTokenExpirationTime ,
77
- JWTUtil jwtUtil , SessionService sessionService ) {
76
+ JWTUtil jwtUtil , SessionService sessionService ,
77
+ @ Value ("${open.idp.provider.is.enabled}" ) boolean openIdpProviderIsEnabled ) {
78
78
this .basicMailService = basicMailService ;
79
79
this .tosService = tosService ;
80
80
this .userRepository = userRepository ;
@@ -89,6 +89,7 @@ public UserService(BasicMailService basicMailService, TOSService tosService,
89
89
long defaultLongTermTokenExpirationTime = 1000L * 60 * 60 * 24 * 30 ;
90
90
this .longTermTokenExpirationTime = longTermTokenExpirationTime > 0 ? longTermTokenExpirationTime : defaultLongTermTokenExpirationTime ;
91
91
this .sessionService = sessionService ;
92
+ this .openAccessIsEnabled = openIdpProviderIsEnabled ;
92
93
}
93
94
94
95
public HashMap <String , String > getUserProfileResponse (Map <String , Object > claims ) {
@@ -387,19 +388,48 @@ public Optional<String> getQueryTemplate(String applicationId) {
387
388
388
389
SecurityContext securityContext = SecurityContextHolder .getContext ();
389
390
Optional <CustomUserDetails > customUserDetails = Optional .ofNullable ((CustomUserDetails ) securityContext .getAuthentication ().getPrincipal ());
390
- if (customUserDetails .isEmpty () || customUserDetails .get ().getUser () == null ) {
391
- logger .error ("Security context didn't have a user stored." );
392
- return Optional .empty ();
391
+ if ((customUserDetails .isEmpty () || customUserDetails .get ().getUser () == null ) && openAccessIsEnabled ) {
392
+ Optional <Application > application = this .applicationRepository .findById (UUID .fromString (applicationId ));
393
+ if (application .isEmpty ()) {
394
+ logger .error ("getQueryTemplate() cannot find corresponding application by UUID: {}" , UUID .fromString (applicationId ));
395
+ throw new IllegalArgumentException ("Cannot find application by input UUID: " + UUID .fromString (applicationId ));
396
+ }
397
+
398
+ return Optional .ofNullable (openMergeTemplate (application .orElse (null )));
399
+ } else {
400
+ if (customUserDetails .isEmpty () || customUserDetails .get ().getUser () == null ) {
401
+ logger .error ("Security context didn't have a user stored." );
402
+ return Optional .empty ();
403
+ }
404
+
405
+ User user = customUserDetails .get ().getUser ();
406
+ Optional <Application > application = this .applicationRepository .findById (UUID .fromString (applicationId ));
407
+ if (application .isEmpty ()) {
408
+ logger .error ("getQueryTemplate() cannot find corresponding application by UUID: {}" , UUID .fromString (applicationId ));
409
+ throw new IllegalArgumentException ("Cannot find application by input UUID: " + UUID .fromString (applicationId ));
410
+ }
411
+
412
+ return Optional .ofNullable (mergeTemplate (user , application .orElse (null )));
393
413
}
414
+ }
394
415
395
- User user = customUserDetails .get ().getUser ();
396
- Optional <Application > application = this .applicationRepository .findById (UUID .fromString (applicationId ));
397
- if (application .isEmpty ()) {
398
- logger .error ("getQueryTemplate() cannot find corresponding application by UUID: {}" , UUID .fromString (applicationId ));
399
- throw new IllegalArgumentException ("Cannot find application by input UUID: " + UUID .fromString (applicationId ));
416
+ private String openMergeTemplate (Application application ) {
417
+ Set <Privilege > applicationPrivileges = application .getPrivileges ();
418
+ Role openAccessRole = roleService .findByName (MANAGED_OPEN_ACCESS_ROLE_NAME );
419
+ // get all of the privileges for the open access role
420
+ Set <Privilege > privileges = openAccessRole .getPrivileges ();
421
+ privileges .addAll (applicationPrivileges );
422
+ // get the query template for each privilege
423
+ Map mergedTemplateMap = getMergedQueryTemplateMap (privileges );
424
+ String resultJSON ;
425
+ try {
426
+ resultJSON = objectMapper .writeValueAsString (mergedTemplateMap );
427
+ } catch (JsonProcessingException ex ) {
428
+ logger .error ("mergeTemplate() cannot convert map to json string. The map mergedTemplate is: {}" , mergedTemplateMap );
429
+ throw new IllegalArgumentException ("Inner application error, please contact admin." );
400
430
}
401
431
402
- return Optional . ofNullable ( mergeTemplate ( user , application . orElse ( null ))) ;
432
+ return resultJSON ;
403
433
}
404
434
405
435
public Map <String , String > getDefaultQueryTemplate () {
@@ -416,8 +446,22 @@ public Map<String, String> getDefaultQueryTemplate() {
416
446
@ Cacheable (value = "mergedTemplateCache" , keyGenerator = "customKeyGenerator" )
417
447
public String mergeTemplate (User user , Application application ) {
418
448
String resultJSON ;
449
+ Set <Privilege > privilegesByApplication = user .getPrivilegesByApplication (application );
450
+ Map mergedTemplateMap = getMergedQueryTemplateMap (privilegesByApplication );
451
+
452
+ try {
453
+ resultJSON = objectMapper .writeValueAsString (mergedTemplateMap );
454
+ } catch (JsonProcessingException ex ) {
455
+ logger .error ("mergeTemplate() cannot convert map to json string. The map mergedTemplate is: {}" , mergedTemplateMap );
456
+ throw new IllegalArgumentException ("Inner application error, please contact admin." );
457
+ }
458
+
459
+ return resultJSON ;
460
+ }
461
+
462
+ private Map getMergedQueryTemplateMap (Set <Privilege > privilegesByApplication ) {
419
463
Map mergedTemplateMap = null ;
420
- for (Privilege privilege : user . getPrivilegesByApplication ( application ) ) {
464
+ for (Privilege privilege : privilegesByApplication ) {
421
465
String template = privilege .getQueryTemplate ();
422
466
logger .debug ("mergeTemplate() processing template:{}" , template );
423
467
if (template == null || template .trim ().isEmpty ()) {
@@ -442,15 +486,7 @@ public String mergeTemplate(User user, Application application) {
442
486
443
487
mergedTemplateMap = JsonUtils .mergeTemplateMap (mergedTemplateMap , templateMap );
444
488
}
445
-
446
- try {
447
- resultJSON = objectMapper .writeValueAsString (mergedTemplateMap );
448
- } catch (JsonProcessingException ex ) {
449
- logger .error ("mergeTemplate() cannot convert map to json string. The map mergedTemplate is: {}" , mergedTemplateMap );
450
- throw new IllegalArgumentException ("Inner application error, please contact admin." );
451
- }
452
-
453
- return resultJSON ;
489
+ return mergedTemplateMap ;
454
490
}
455
491
456
492
@ CacheEvict (value = "mergedTemplateCache" )
0 commit comments