use of org.eclipse.vorto.repository.web.security.VortoUser in project vorto by eclipse.
the class HomeController method getUser.
@SuppressWarnings("unchecked")
@ApiOperation(value = "Returns the currently logged in User")
@ApiResponses(value = { @ApiResponse(code = 401, message = "Unauthorized"), @ApiResponse(code = 200, message = "OK") })
@RequestMapping(value = { "/user", "/me" }, method = RequestMethod.GET)
public ResponseEntity<Map<String, String>> getUser(Principal user, final HttpServletRequest request) {
Map<String, String> map = new LinkedHashMap<>();
if (user == null)
return new ResponseEntity<Map<String, String>>(map, HttpStatus.UNAUTHORIZED);
for (GrantedAuthority authority : SecurityContextHolder.getContext().getAuthentication().getAuthorities()) {
map.put("role", authority.getAuthority());
}
if (user instanceof OAuth2Authentication) {
OAuth2Authentication oauth2User = (OAuth2Authentication) user;
map.put("name", oauth2User.getName());
User registeredUser = userRepository.findByUsername(oauth2User.getName());
if (registeredUser != null) {
map.put("email", registeredUser.getEmail());
map.put("isRegistered", Boolean.toString(true));
} else {
map.put("email", ((Map<String, String>) oauth2User.getUserAuthentication().getDetails()).get("email"));
map.put("isRegistered", Boolean.toString(false));
}
Map<String, String> userDetails = ((Map<String, String>) oauth2User.getUserAuthentication().getDetails());
map.put("loginType", userDetails.get(LOGIN_TYPE));
} else {
VortoUser vortoUser = (VortoUser) ((UsernamePasswordAuthenticationToken) user).getPrincipal();
map.put("name", vortoUser.getUsername());
map.put("email", vortoUser.getEmail());
map.put("isRegistered", "true");
map.put("loginType", "default");
}
return new ResponseEntity<Map<String, String>>(map, HttpStatus.OK);
}
Aggregations