-
Notifications
You must be signed in to change notification settings - Fork 37
선택 - RoleHierarchy 리뷰 부탁드립니다. #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jukekxm
wants to merge
2
commits into
next-step:jukekxm
Choose a base branch
from
jukekxm:jukekxm-option
base: jukekxm
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/nextstep/security/authorization/NullRoleHierarchy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package nextstep.security.authorization; | ||
|
||
import java.util.Collection; | ||
|
||
public class NullRoleHierarchy implements RoleHierarchy { | ||
@Override | ||
public Collection<String> getReachableGrantedAuthorities(Collection<String> authorities) { | ||
return authorities; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/nextstep/security/authorization/RoleHierarchy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package nextstep.security.authorization; | ||
|
||
import java.util.Collection; | ||
|
||
public interface RoleHierarchy { | ||
|
||
Collection<String> getReachableGrantedAuthorities(Collection<String> authorities); | ||
} |
75 changes: 75 additions & 0 deletions
75
src/main/java/nextstep/security/authorization/RoleHierarchyImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package nextstep.security.authorization; | ||
|
||
import org.springframework.util.CollectionUtils; | ||
|
||
import java.util.*; | ||
|
||
public class RoleHierarchyImpl implements RoleHierarchy { | ||
|
||
private final Map<String, Set<String>> rolesReachableInOneOrMoreStepsMap; | ||
|
||
public RoleHierarchyImpl(Map<String, Set<String>> rolesReachableInOneOrMoreStepsMap) { | ||
this.rolesReachableInOneOrMoreStepsMap = rolesReachableInOneOrMoreStepsMap; | ||
} | ||
|
||
public static Builder with() { | ||
return new Builder(); | ||
} | ||
|
||
@Override | ||
public Collection<String> getReachableGrantedAuthorities(Collection<String> authorities) { | ||
|
||
Set<String> reachableRoles = new HashSet<>(); | ||
|
||
if (CollectionUtils.isEmpty(authorities)) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
for (String authority : authorities) { | ||
addReachableRoles(authority, reachableRoles); | ||
} | ||
|
||
return reachableRoles; | ||
} | ||
|
||
private void addReachableRoles(String authority, Set<String> reachableRoles) { | ||
reachableRoles.add(authority); | ||
Set<String> lowerRoles = rolesReachableInOneOrMoreStepsMap.computeIfAbsent(authority, k -> new HashSet<>()); | ||
reachableRoles.addAll(lowerRoles); | ||
} | ||
|
||
public static class Builder { | ||
private final Map<String, Set<String>> hierarchy; | ||
|
||
private Builder() { | ||
this.hierarchy = new HashMap<>(); | ||
} | ||
|
||
public RoleHierarchyImpl build() { | ||
return new RoleHierarchyImpl(hierarchy); | ||
} | ||
|
||
public ImpliedRoles role(String role) { | ||
return new ImpliedRoles(role); | ||
} | ||
|
||
private Builder addHierarchy(String role, String... impliedRoles) { | ||
Set<String> impliedRoleSet = hierarchy.computeIfAbsent(role, k -> new HashSet<>()); | ||
impliedRoleSet.addAll(Arrays.asList(impliedRoles)); | ||
return this; | ||
} | ||
|
||
public final class ImpliedRoles { | ||
private final String role; | ||
|
||
public ImpliedRoles(String role) { | ||
this.role = role; | ||
} | ||
|
||
public Builder implies(String... impliedRoles) { | ||
return Builder.this.addHierarchy(this.role, impliedRoles); | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package nextstep.app; | ||
|
||
import nextstep.security.authorization.NullRoleHierarchy; | ||
import nextstep.security.authorization.RoleHierarchyImpl; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class RoleHierachyTest { | ||
|
||
@DisplayName("상위 권한의 reachable authorities는 사용자의 권한과 하위 권한을 같이 반환한다.") | ||
@Test | ||
public void roleHierarchyTest() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 테스트 코드 👍 코드 개선해 보시면서 아래 케이스에 대한 테스트 코드도 작성해 보면 좋을 것 같네요 😄 RoleHierarchyImpl.with()
.role("ADMIN").implies("USER")
.role("USER").implies("GUEST")
.build(); |
||
|
||
// given | ||
RoleHierarchyImpl roleHierarchy = RoleHierarchyImpl.with() | ||
.role("ADMIN").implies("USER") | ||
.build(); | ||
|
||
Set<String> authorites = new HashSet<>(); | ||
authorites.add("ADMIN"); | ||
|
||
// when | ||
Collection<String> reachableGrantedAuthorities = roleHierarchy.getReachableGrantedAuthorities(authorites); | ||
|
||
// then | ||
Assertions.assertThat(reachableGrantedAuthorities).containsOnly("ADMIN", "USER"); | ||
} | ||
|
||
@DisplayName("NullRoleHierarchy의 reachable authorities는 사용자의 권한 리스트를 그대로 반환한다.") | ||
@Test | ||
public void nullRoleHierarchyTest() { | ||
|
||
// given | ||
NullRoleHierarchy nullRoleHierarchy = new NullRoleHierarchy(); | ||
|
||
Set<String> authorites = new HashSet<>(); | ||
authorites.add("ADMIN"); | ||
authorites.add("USER"); | ||
|
||
// when | ||
Collection<String> result = nullRoleHierarchy.getReachableGrantedAuthorities(authorites); | ||
|
||
// then | ||
Assertions.assertThat(result).isSameAs(authorites); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
만약 다음과 같이 권한의 계층 구조를 설정할 경우에는 어떻게 처리되어야 할까요?
현재 구조에서는 직접적인 하위 역할만 추가하고 있어서, ADMIN이 USER만 포함하고 GUEST까지 확장되지 않는 문제가 발생할 수 있습니다.
Spring Security의 RoleHierarchyImpl은 buildRolesReachableInOneOrMoreStepsMap()을 활용하여 모든 계층 관계를 반영하는데, 현재 코드에서는 해당 로직이 없어 다단계 계층 탐색이 누락될 가능성이 있습니다 😢
개선해보면 어떨까요? 😄