@@ -51,8 +51,9 @@ public class DynamicRoleBasedAPIAccessChecker extends AdapterBase implements API
5151 private List <PluggableService > services ;
5252 private Map <RoleType , Set <String >> annotationRoleBasedApisMap = new HashMap <RoleType , Set <String >>();
5353
54- final private LazyCache <Long , Account > accountCache ;
55- final private LazyCache <Long , Pair <Role , List <RolePermission >>> rolePermissionsCache ;
54+ private LazyCache <Long , Account > accountCache ;
55+ private LazyCache <Long , Pair <Role , List <RolePermission >>> rolePermissionsCache ;
56+ private int cachePeriod ;
5657
5758 private static final Logger LOGGER = Logger .getLogger (DynamicRoleBasedAPIAccessChecker .class .getName ());
5859
@@ -61,10 +62,6 @@ protected DynamicRoleBasedAPIAccessChecker() {
6162 for (RoleType roleType : RoleType .values ()) {
6263 annotationRoleBasedApisMap .put (roleType , new HashSet <String >());
6364 }
64- accountCache = new LazyCache <>(32 , 60 ,
65- this ::getAccountFromId );
66- rolePermissionsCache = new LazyCache <>(32 , 60 ,
67- this ::getRolePermissions );
6865 }
6966
7067 @ Override
@@ -127,16 +124,30 @@ protected Pair<Role, List<RolePermission>> getRolePermissions(long roleId) {
127124 return new Pair <>(accountRole , roleService .findAllPermissionsBy (accountRole .getId ()));
128125 }
129126
127+ protected Pair <Role , List <RolePermission >> getRolePermissionsUsingCache (long roleId ) {
128+ if (cachePeriod > 0 ) {
129+ return rolePermissionsCache .get (roleId );
130+ }
131+ return getRolePermissions (roleId );
132+ }
133+
134+ protected Account getAccountFromIdUsingCache (long accountId ) {
135+ if (cachePeriod > 0 ) {
136+ return accountCache .get (accountId );
137+ }
138+ return getAccountFromId (accountId );
139+ }
140+
130141 @ Override
131142 public boolean checkAccess (User user , String commandName ) throws PermissionDeniedException {
132143 if (!isEnabled ()) {
133144 return true ;
134145 }
135- Account account = accountCache . get (user .getAccountId ());
146+ Account account = getAccountFromIdUsingCache (user .getAccountId ());
136147 if (account == null ) {
137148 throw new PermissionDeniedException (String .format ("Account for user id [%s] cannot be found" , user .getUuid ()));
138149 }
139- Pair <Role , List <RolePermission >> roleAndPermissions = rolePermissionsCache . get (account .getRoleId ());
150+ Pair <Role , List <RolePermission >> roleAndPermissions = getRolePermissionsUsingCache (account .getRoleId ());
140151 final Role accountRole = roleAndPermissions .first ();
141152 if (accountRole == null ) {
142153 throw new PermissionDeniedException (String .format ("Account role for user id [%s] cannot be found." , user .getUuid ()));
@@ -153,7 +164,7 @@ public boolean checkAccess(User user, String commandName) throws PermissionDenie
153164 }
154165
155166 public boolean checkAccess (Account account , String commandName ) {
156- Pair <Role , List <RolePermission >> roleAndPermissions = rolePermissionsCache . get (account .getRoleId ());
167+ Pair <Role , List <RolePermission >> roleAndPermissions = getRolePermissionsUsingCache (account .getRoleId ());
157168 final Role accountRole = roleAndPermissions .first ();
158169 if (accountRole == null ) {
159170 throw new PermissionDeniedException (String .format ("The account [%s] has role null or unknown." , account ));
@@ -198,6 +209,9 @@ public void addApiToRoleBasedAnnotationsMap(final RoleType roleType, final Strin
198209 @ Override
199210 public boolean configure (String name , Map <String , Object > params ) throws ConfigurationException {
200211 super .configure (name , params );
212+ cachePeriod = Math .max (0 , RoleService .DynamicApiCheckerCachePeriod .value ());
213+ accountCache = new LazyCache <>(32 , cachePeriod , this ::getAccountFromId );
214+ rolePermissionsCache = new LazyCache <>(32 , cachePeriod , this ::getRolePermissions );
201215 return true ;
202216 }
203217
0 commit comments