-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
112 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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 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
7 changes: 3 additions & 4 deletions
7
aaa/src/main/java/com/github/nkonev/aaa/dto/UserAccountEventDTO.java
This file contains 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 |
---|---|---|
@@ -1,18 +1,17 @@ | ||
package com.github.nkonev.aaa.dto; | ||
|
||
import java.util.Set; | ||
|
||
public record UserAccountEventDTO( | ||
ForWho forWho, | ||
Set<UserRole> forWhoRoles, | ||
Long userId, // nullable | ||
String eventType, | ||
|
||
UserAccountDTO userAccount | ||
Object userAccount | ||
) { | ||
|
||
public enum ForWho { | ||
FOR_MYSELF, | ||
FOR_ROLE, | ||
FOR_ROLE_USER, | ||
FOR_ROLE_ADMIN, | ||
} | ||
} |
This file contains 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
57 changes: 57 additions & 0 deletions
57
aaa/src/main/java/com/github/nkonev/aaa/security/PrincipalToCheck.java
This file contains 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,57 @@ | ||
package com.github.nkonev.aaa.security; | ||
|
||
import com.github.nkonev.aaa.dto.UserAccountDetailsDTO; | ||
import com.github.nkonev.aaa.dto.UserRole; | ||
import org.springframework.security.access.hierarchicalroles.RoleHierarchy; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
|
||
import static com.github.nkonev.aaa.Constants.NonExistentUser; | ||
|
||
public sealed interface PrincipalToCheck permits KnownAdmin, UserToCheck { | ||
boolean isAdmin(); | ||
|
||
Long getId(); | ||
|
||
static PrincipalToCheck knownAdmin() { | ||
return new KnownAdmin(); | ||
} | ||
|
||
static PrincipalToCheck ofUserAccount(UserAccountDetailsDTO userAccount, RoleHierarchy roleHierarchy) { | ||
return new UserToCheck(userAccount, roleHierarchy); | ||
} | ||
} | ||
|
||
final class KnownAdmin implements PrincipalToCheck { | ||
|
||
@Override | ||
public boolean isAdmin() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Long getId() { | ||
return NonExistentUser; | ||
} | ||
} | ||
|
||
final class UserToCheck implements PrincipalToCheck { | ||
|
||
private final UserAccountDetailsDTO userAccount; | ||
|
||
private final RoleHierarchy roleHierarchy; | ||
|
||
UserToCheck(UserAccountDetailsDTO userAccount, RoleHierarchy roleHierarchy) { | ||
this.userAccount = userAccount; | ||
this.roleHierarchy = roleHierarchy; | ||
} | ||
|
||
@Override | ||
public boolean isAdmin() { | ||
return roleHierarchy.getReachableGrantedAuthorities(userAccount.getAuthorities()).contains(new SimpleGrantedAuthority(UserRole.ROLE_ADMIN.name())); | ||
} | ||
|
||
@Override | ||
public Long getId() { | ||
return userAccount.getId(); | ||
} | ||
} |
This file contains 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