| | |
| | | import org.springframework.security.core.authority.SimpleGrantedAuthority; |
| | | |
| | | import java.util.Collections; |
| | | import java.util.Arrays; |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | |
| | | public class AppUserPrincipal { |
| | |
| | | private final String username; |
| | | private final String role; |
| | | private final ClientType clientType; |
| | | private final List<String> menuCodes; |
| | | |
| | | public AppUserPrincipal(Long userId, String username, String role, ClientType clientType) { |
| | | this(userId, username, role, clientType, Collections.<String>emptyList()); |
| | | } |
| | | |
| | | public AppUserPrincipal(Long userId, String username, String role, ClientType clientType, |
| | | Collection<String> menuCodes) { |
| | | this.userId = userId; |
| | | this.username = username; |
| | | this.role = role; |
| | | this.clientType = clientType; |
| | | this.menuCodes = new ArrayList<String>(menuCodes); |
| | | } |
| | | public Long getUserId() { return userId; } |
| | | public String getUsername() { return username; } |
| | | public String getRole() { return role; } |
| | | public ClientType getClientType() { return clientType; } |
| | | public List<String> getMenuCodes() { return Collections.unmodifiableList(menuCodes); } |
| | | public List<GrantedAuthority> getAuthorities() { |
| | | if (clientType == ClientType.ADMIN && !"ADMIN".equals(role)) { |
| | | return Arrays.<GrantedAuthority>asList( |
| | | new SimpleGrantedAuthority("ROLE_ADMIN"), |
| | | new SimpleGrantedAuthority("ROLE_" + role)); |
| | | if (clientType == ClientType.ADMIN) { |
| | | List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); |
| | | authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN")); |
| | | if (!"ADMIN".equals(role)) { |
| | | authorities.add(new SimpleGrantedAuthority("ROLE_" + role)); |
| | | } |
| | | for (String menuCode : menuCodes) { |
| | | authorities.add(new SimpleGrantedAuthority("PERM_" + menuCode)); |
| | | } |
| | | return authorities; |
| | | } |
| | | return Collections.<GrantedAuthority>singletonList(new SimpleGrantedAuthority("ROLE_" + role)); |
| | | } |