41
41
import org .apache .shiro .authz .Permission ;
42
42
import org .apache .shiro .authz .permission .WildcardPermission ;
43
43
import org .ohdsi .circe .helper .ResourceHelper ;
44
+ import org .ohdsi .webapi .util .CacheHelper ;
44
45
import org .springframework .beans .factory .annotation .Value ;
45
46
import org .springframework .boot .autoconfigure .cache .JCacheManagerCustomizer ;
46
47
import org .springframework .cache .annotation .CacheEvict ;
@@ -62,14 +63,18 @@ public static class CachingSetup implements JCacheManagerCustomizer {
62
63
63
64
@ Override
64
65
public void customize (CacheManager cacheManager ) {
66
+ // due to unit tests causing application contexts to reload cache manager caches, we
67
+ // have to check for the existance of a cache before creating it
68
+ Set <String > cacheNames = CacheHelper .getCacheNames (cacheManager );
65
69
// Evict when a user, role or permission is modified/deleted.
66
- cacheManager .createCache (AUTH_INFO_CACHE , new MutableConfiguration <String , UserSimpleAuthorizationInfo >()
67
- .setTypes (String .class , UserSimpleAuthorizationInfo .class )
68
- .setStoreByValue (false )
69
- .setStatisticsEnabled (true ));
70
+ if (!cacheNames .contains (AUTH_INFO_CACHE )) {
71
+ cacheManager .createCache (AUTH_INFO_CACHE , new MutableConfiguration <String , UserSimpleAuthorizationInfo >()
72
+ .setTypes (String .class , UserSimpleAuthorizationInfo .class )
73
+ .setStoreByValue (false )
74
+ .setStatisticsEnabled (true ));
75
+ }
70
76
}
71
77
}
72
-
73
78
74
79
@ Value ("${datasource.ohdsi.schema}" )
75
80
private String ohdsiSchema ;
@@ -95,6 +100,8 @@ public void customize(CacheManager cacheManager) {
95
100
@ Autowired
96
101
private JdbcTemplate jdbcTemplate ;
97
102
103
+ private ThreadLocal <ConcurrentHashMap <String , UserSimpleAuthorizationInfo >> authorizationInfoCache = ThreadLocal .withInitial (ConcurrentHashMap ::new );
104
+
98
105
public static class PermissionsDTO {
99
106
100
107
public Map <String , List <String >> permissions = null ;
@@ -163,39 +170,42 @@ public Iterable<RoleEntity> getRoles(boolean includePersonalRoles) {
163
170
@ Cacheable (cacheNames = CachingSetup .AUTH_INFO_CACHE )
164
171
public UserSimpleAuthorizationInfo getAuthorizationInfo (final String login ) {
165
172
166
- final UserSimpleAuthorizationInfo info = new UserSimpleAuthorizationInfo ();
167
-
168
- final UserEntity userEntity = userRepository .findByLogin (login );
169
- if (userEntity == null ) {
170
- throw new UnknownAccountException ("Account does not exist" );
171
- }
172
-
173
- info .setUserId (userEntity .getId ());
174
- info .setLogin (userEntity .getLogin ());
175
-
176
- for (UserRoleEntity userRole : userEntity .getUserRoles ()) {
177
- info .addRole (userRole .getRole ().getName ());
178
- }
179
-
180
- // convert permission index from queryUserPermissions() into a map of WildcardPermissions
181
- Map <String , List <String >> permsIdx = this .queryUserPermissions (login ).permissions ;
182
- Map permissionMap = new HashMap <String , List <Permission >>();
183
- Set <String > permissionNames = new HashSet <>();
184
-
185
- for (String permIdxKey : permsIdx .keySet ()) {
186
- List <String > perms = permsIdx .get (permIdxKey );
187
- permissionNames .addAll (perms );
188
- // convert raw string permission into Wildcard perm for each element in this key's array.
189
- permissionMap .put (permIdxKey , perms .stream ().map (perm -> new WildcardPermission (perm )).collect (Collectors .toList ()));
190
- }
191
-
192
- info .setStringPermissions (permissionNames );
193
- info .setPermissionIdx (permissionMap );
194
- return info ;
195
- }
173
+ return authorizationInfoCache .get ().computeIfAbsent (login , newLogin -> {
174
+ final UserSimpleAuthorizationInfo info = new UserSimpleAuthorizationInfo ();
175
+
176
+ final UserEntity userEntity = userRepository .findByLogin (login );
177
+ if (userEntity == null ) {
178
+ throw new UnknownAccountException ("Account does not exist" );
179
+ }
180
+
181
+ info .setUserId (userEntity .getId ());
182
+ info .setLogin (userEntity .getLogin ());
183
+
184
+ for (UserRoleEntity userRole : userEntity .getUserRoles ()) {
185
+ info .addRole (userRole .getRole ().getName ());
186
+ }
187
+
188
+ // convert permission index from queryUserPermissions() into a map of WildcardPermissions
189
+ Map <String , List <String >> permsIdx = this .queryUserPermissions (login ).permissions ;
190
+ Map permissionMap = new HashMap <String , List <Permission >>();
191
+ Set <String > permissionNames = new HashSet <>();
192
+
193
+ for (String permIdxKey : permsIdx .keySet ()) {
194
+ List <String > perms = permsIdx .get (permIdxKey );
195
+ permissionNames .addAll (perms );
196
+ // convert raw string permission into Wildcard perm for each element in this key's array.
197
+ permissionMap .put (permIdxKey , perms .stream ().map (perm -> new WildcardPermission (perm )).collect (Collectors .toList ()));
198
+ }
199
+
200
+ info .setStringPermissions (permissionNames );
201
+ info .setPermissionIdx (permissionMap );
202
+ return info ;
203
+ });
204
+ }
196
205
197
206
@ CacheEvict (cacheNames = CachingSetup .AUTH_INFO_CACHE , allEntries = true )
198
207
public void clearAuthorizationInfoCache () {
208
+ authorizationInfoCache .remove ();
199
209
}
200
210
201
211
@ Transactional
0 commit comments