@@ -215,33 +215,19 @@ private void buildRolesReachableInOneOrMoreStepsMap() {
215
215
// iterate over all higher roles from rolesReachableInOneStepMap
216
216
217
217
for (GrantedAuthority role : this .rolesReachableInOneStepMap .keySet ()) {
218
- Set <GrantedAuthority > rolesToVisitSet = new HashSet <>();
219
-
220
- if (this .rolesReachableInOneStepMap .containsKey (role )) {
221
- rolesToVisitSet .addAll (this .rolesReachableInOneStepMap .get (role ));
222
- }
223
-
218
+ Set <GrantedAuthority > rolesToVisitSet = new HashSet <>(this .rolesReachableInOneStepMap .get (role ));
224
219
Set <GrantedAuthority > visitedRolesSet = new HashSet <>();
225
220
226
221
while (!rolesToVisitSet .isEmpty ()) {
227
222
// take a role from the rolesToVisit set
228
223
GrantedAuthority aRole = rolesToVisitSet .iterator ().next ();
229
224
rolesToVisitSet .remove (aRole );
230
- visitedRolesSet .add (aRole );
231
- if (this .rolesReachableInOneStepMap .containsKey (aRole )) {
232
- Set <GrantedAuthority > newReachableRoles = this .rolesReachableInOneStepMap
233
- .get (aRole );
234
-
235
- // definition of a cycle: you can reach the role you are starting from
236
- if (rolesToVisitSet .contains (role )
237
- || visitedRolesSet .contains (role )) {
238
- throw new CycleInRoleHierarchyException ();
239
- }
240
- else {
241
- // no cycle
242
- rolesToVisitSet .addAll (newReachableRoles );
243
- }
225
+ if (!visitedRolesSet .add (aRole ) || !this .rolesReachableInOneStepMap .containsKey (aRole )) {
226
+ continue ; // Already visited role or role with missing hierarchy
227
+ } else if (role .equals (aRole )) {
228
+ throw new CycleInRoleHierarchyException ();
244
229
}
230
+ rolesToVisitSet .addAll (this .rolesReachableInOneStepMap .get (aRole ));
245
231
}
246
232
this .rolesReachableInOneOrMoreStepsMap .put (role , visitedRolesSet );
247
233
0 commit comments