Search in sources :

Example 1 with UserDetailRole

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();
    }
}
Also used : UserNotFoundException(io.gravitee.management.service.exceptions.UserNotFoundException) UserDetails(io.gravitee.management.idp.api.authentication.UserDetails) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) UserDetailRole(io.gravitee.management.idp.api.authentication.UserDetailRole) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

UserDetailRole (io.gravitee.management.idp.api.authentication.UserDetailRole)1 UserDetails (io.gravitee.management.idp.api.authentication.UserDetails)1 UserNotFoundException (io.gravitee.management.service.exceptions.UserNotFoundException)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ArrayList (java.util.ArrayList)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1