use of io.gravitee.management.idp.api.authentication.UserDetailRole in project gravitee-management-rest-api by gravitee-io.
the class CurrentUserResource method getCurrentUser.
@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Get the authenticated user")
public Response getCurrentUser() {
if (isAuthenticated()) {
final UserDetails details = getAuthenticatedUserDetails();
final String userId = details.getUsername();
UserEntity userEntity;
try {
userEntity = userService.findByIdWithRoles(userId);
} catch (final UserNotFoundException unfe) {
final String unfeMessage = "User '{}' does not exist.";
if (LOG.isDebugEnabled()) {
LOG.info(unfeMessage, userId, unfe);
} else {
LOG.info(unfeMessage, userId);
}
return logout();
}
List<GrantedAuthority> authorities = new ArrayList<>(details.getAuthorities());
UserDetails userDetails = new UserDetails(userEntity.getId(), details.getPassword(), authorities);
userDetails.setId(userEntity.getId());
userDetails.setFirstname(details.getFirstname());
userDetails.setLastname(details.getLastname());
userDetails.setUsername(userEntity.getUsername());
userDetails.setEmail(details.getEmail());
// convert UserEntityRoles to UserDetailsRoles
userDetails.setRoles(userEntity.getRoles().stream().map(userEntityRole -> {
UserDetailRole userDetailRole = new UserDetailRole();
userDetailRole.setScope(userEntityRole.getScope().name());
userDetailRole.setName(userEntityRole.getName());
userDetailRole.setPermissions(userEntityRole.getPermissions());
return userDetailRole;
}).collect(Collectors.toList()));
return Response.ok(userDetails, MediaType.APPLICATION_JSON).build();
} else {
return Response.ok().build();
}
}
Aggregations