Upgrade load user json dto if it is OAuth2Authentication
parent
1a4c5c6bd2
commit
b637fb08ec
|
@ -20,7 +20,7 @@ public class UserJsonDto implements Serializable {
|
|||
private String phone;
|
||||
private String email;
|
||||
|
||||
private List<Privilege> privileges = new ArrayList<>();
|
||||
private List<String> privileges = new ArrayList<>();
|
||||
|
||||
public UserJsonDto() {
|
||||
}
|
||||
|
@ -32,7 +32,11 @@ public class UserJsonDto implements Serializable {
|
|||
|
||||
this.phone = user.phone();
|
||||
this.email = user.email();
|
||||
this.privileges = user.privileges();
|
||||
|
||||
final List<Privilege> privilegeList = user.privileges();
|
||||
for (Privilege privilege : privilegeList) {
|
||||
this.privileges.add(privilege.name());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isArchived() {
|
||||
|
@ -75,11 +79,11 @@ public class UserJsonDto implements Serializable {
|
|||
this.email = email;
|
||||
}
|
||||
|
||||
public List<Privilege> getPrivileges() {
|
||||
public List<String> getPrivileges() {
|
||||
return privileges;
|
||||
}
|
||||
|
||||
public void setPrivileges(List<Privilege> privileges) {
|
||||
public void setPrivileges(List<String> privileges) {
|
||||
this.privileges = privileges;
|
||||
}
|
||||
}
|
|
@ -6,11 +6,16 @@ import cc.wdcy.domain.user.User;
|
|||
import cc.wdcy.domain.user.UserRepository;
|
||||
import cc.wdcy.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* @author Shengzhao Li
|
||||
*/
|
||||
|
@ -32,7 +37,26 @@ public class UserServiceImpl implements UserService {
|
|||
|
||||
@Override
|
||||
public UserJsonDto loadCurrentUserJsonDto() {
|
||||
final WdcyUserDetails userDetails = (WdcyUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
||||
return new UserJsonDto(userRepository.findByGuid(userDetails.user().guid()));
|
||||
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
|
||||
if (authentication instanceof OAuth2Authentication && authentication.getPrincipal() instanceof String) {
|
||||
return loadOauthUserJsonDto((OAuth2Authentication) authentication);
|
||||
} else {
|
||||
final WdcyUserDetails userDetails = (WdcyUserDetails) authentication.getPrincipal();
|
||||
return new UserJsonDto(userRepository.findByGuid(userDetails.user().guid()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private UserJsonDto loadOauthUserJsonDto(OAuth2Authentication oAuth2Authentication) {
|
||||
UserJsonDto userJsonDto = new UserJsonDto();
|
||||
userJsonDto.setUsername(oAuth2Authentication.getName());
|
||||
|
||||
final Collection<GrantedAuthority> authorities = oAuth2Authentication.getAuthorities();
|
||||
for (GrantedAuthority authority : authorities) {
|
||||
userJsonDto.getPrivileges().add(authority.getAuthority());
|
||||
}
|
||||
|
||||
return userJsonDto;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue