Skip to content

Commit 87f42d4

Browse files
committed
RoleHierarchyImpl builder supports authority to get the roles for.
Closes gh-15264 Signed-off-by: Niels Basjes <[email protected]>
1 parent 363159e commit 87f42d4

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

core/src/main/java/org/springframework/security/access/hierarchicalroles/RoleHierarchyImpl.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -282,7 +282,18 @@ private Builder(String rolePrefix) {
282282
*/
283283
public ImpliedRoles role(String role) {
284284
Assert.hasText(role, "role must not be empty");
285-
return new ImpliedRoles(role);
285+
return new ImpliedRoles(this.rolePrefix.concat(role));
286+
}
287+
288+
/**
289+
* Creates a new hierarchy branch to define an authority and its child roles.
290+
* @param authority the highest authority in this branch
291+
* @return a {@link ImpliedRoles} to define the child roles for the
292+
* <code>authority</code>
293+
*/
294+
public ImpliedRoles authority(String authority) {
295+
Assert.hasText(authority, "authority must not be empty");
296+
return new ImpliedRoles(authority);
286297
}
287298

288299
/**
@@ -299,7 +310,7 @@ private Builder addHierarchy(String role, String... impliedRoles) {
299310
for (String impliedRole : impliedRoles) {
300311
withPrefix.add(new SimpleGrantedAuthority(this.rolePrefix.concat(impliedRole)));
301312
}
302-
this.hierarchy.put(this.rolePrefix.concat(role), withPrefix);
313+
this.hierarchy.put(role, withPrefix);
303314
return this;
304315
}
305316

core/src/test/java/org/springframework/security/access/hierarchicalroles/RoleHierarchyImplTests.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -249,29 +249,48 @@ public void testBuilderWithDefaultRolePrefix() {
249249
.implies("B")
250250
.role("B")
251251
.implies("C", "D")
252+
.authority("C")
253+
.implies("E", "F", "B")
252254
.build();
253-
List<GrantedAuthority> flatAuthorities = AuthorityUtils.createAuthorityList("ROLE_A");
254-
List<GrantedAuthority> allAuthorities = AuthorityUtils.createAuthorityList("ROLE_A", "ROLE_B", "ROLE_C",
255+
List<GrantedAuthority> flatAuthorities1 = AuthorityUtils.createAuthorityList("ROLE_A");
256+
List<GrantedAuthority> allAuthorities1 = AuthorityUtils.createAuthorityList("ROLE_A", "ROLE_B", "ROLE_C",
255257
"ROLE_D");
256258

257259
assertThat(roleHierarchyImpl).isNotNull();
258-
assertThat(roleHierarchyImpl.getReachableGrantedAuthorities(flatAuthorities))
259-
.containsExactlyInAnyOrderElementsOf(allAuthorities);
260+
assertThat(roleHierarchyImpl.getReachableGrantedAuthorities(flatAuthorities1))
261+
.containsExactlyInAnyOrderElementsOf(allAuthorities1);
262+
263+
List<GrantedAuthority> flatAuthorities2 = AuthorityUtils.createAuthorityList("C");
264+
List<GrantedAuthority> allAuthorities2 = AuthorityUtils.createAuthorityList("C", "ROLE_B", "ROLE_C", "ROLE_D",
265+
"ROLE_E", "ROLE_F");
266+
assertThat(roleHierarchyImpl.getReachableGrantedAuthorities(flatAuthorities2))
267+
.containsExactlyInAnyOrderElementsOf(allAuthorities2);
268+
260269
}
261270

262271
@Test
263272
public void testBuilderWithRolePrefix() {
264273
RoleHierarchyImpl roleHierarchyImpl = RoleHierarchyImpl.withRolePrefix("CUSTOM_PREFIX_")
265274
.role("A")
266275
.implies("B")
276+
.role("B")
277+
.implies("C", "D")
278+
.authority("C")
279+
.implies("E", "F", "B")
267280
.build();
268-
List<GrantedAuthority> flatAuthorities = AuthorityUtils.createAuthorityList("CUSTOM_PREFIX_A");
269-
List<GrantedAuthority> allAuthorities = AuthorityUtils.createAuthorityList("CUSTOM_PREFIX_A",
270-
"CUSTOM_PREFIX_B");
281+
List<GrantedAuthority> flatAuthorities1 = AuthorityUtils.createAuthorityList("CUSTOM_PREFIX_A");
282+
List<GrantedAuthority> allAuthorities1 = AuthorityUtils.createAuthorityList("CUSTOM_PREFIX_A",
283+
"CUSTOM_PREFIX_B", "CUSTOM_PREFIX_C", "CUSTOM_PREFIX_D");
271284

272285
assertThat(roleHierarchyImpl).isNotNull();
273-
assertThat(roleHierarchyImpl.getReachableGrantedAuthorities(flatAuthorities))
274-
.containsExactlyInAnyOrderElementsOf(allAuthorities);
286+
assertThat(roleHierarchyImpl.getReachableGrantedAuthorities(flatAuthorities1))
287+
.containsExactlyInAnyOrderElementsOf(allAuthorities1);
288+
289+
List<GrantedAuthority> flatAuthorities2 = AuthorityUtils.createAuthorityList("C");
290+
List<GrantedAuthority> allAuthorities2 = AuthorityUtils.createAuthorityList("C", "CUSTOM_PREFIX_B",
291+
"CUSTOM_PREFIX_C", "CUSTOM_PREFIX_D", "CUSTOM_PREFIX_E", "CUSTOM_PREFIX_F");
292+
assertThat(roleHierarchyImpl.getReachableGrantedAuthorities(flatAuthorities2))
293+
.containsExactlyInAnyOrderElementsOf(allAuthorities2);
275294
}
276295

277296
@Test

docs/modules/ROOT/pages/servlet/authorization/architecture.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ static RoleHierarchy roleHierarchy() {
257257
.role("ADMIN").implies("STAFF")
258258
.role("STAFF").implies("USER")
259259
.role("USER").implies("GUEST")
260+
.authority("TEAM_ABC").implies("STAFF")
260261
.build();
261262
}
262263
@@ -280,6 +281,7 @@ Xml::
280281
ROLE_ADMIN > ROLE_STAFF
281282
ROLE_STAFF > ROLE_USER
282283
ROLE_USER > ROLE_GUEST
284+
TEAM_ABC > ROLE_STAFF
283285
</value>
284286
</constructor-arg>
285287
</bean>

0 commit comments

Comments
 (0)