Get current user's first and last name

Hello,

there is a way to get the current user:

But now I want the first and last name of the user instead. I tried a several things and I think I need something like getFirstName() or something like that. (User (Camunda BPM Javadocs 7.8.14-ee))

Would anyone have an idea or the solution?^^

This function is not available out of the box like ${currentUser()}. Instead you need to make an api call to process engine services to retrieve the user object like below:

@Autowired
private IdentityService identityService;

@GetMapping(path = "/user-details" produces = MediaType.APPLICATION_JSON_VALUE)
public UserDto getUserDetails() {
	User user = identityService.createUserQuery().userId(getLoggedInUserId()).singleResult();
	return UserDto.fromUser(user, false);
}

private String getLoggedInUserId() {
	return identityService.getCurrentAuthentication().getUserId();
}

Once UserDto is retrieved you can access the object attributes.

1 Like

Thank you!
It took me too long to realize that all I have to do to get access is this :sweat_smile:

String firstName = getUserDetails().getProfile().getFirstName();
String lastName = getUserDetails().getProfile().getLastName();

But now I’m struggling to set the variables for using it. Is the best way to make it with @PostMapping or is there an easier way that I don’t see? ^^

Hi,

here is code snippet you could use.

import org.camunda.bpm.engine.IdentityService;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.ExecutionListener;
import org.camunda.bpm.engine.identity.User;


public class GetUserProfile implements ExecutionListener {

    @Override
    public void notify(DelegateExecution execution) throws Exception {
        IdentityService identityService = execution.getProcessEngine().getIdentityService();
// get current user
        String userId = execution.getProcessEngineServices()
                .getIdentityService()
                .getCurrentAuthentication()
                .getUserId();

        User user = identityService.createUserQuery().userId(userId).singleResult();
        execution.setVariable("vorname", user.getFirstName());
        execution.setVariable("nachname", user.getLastName());
        execution.setVariable("eMail", user.getEmail());
    }
}
1 Like

It runs, thanks! :slight_smile:

I also tried with a JavaDelegation with the code of @aravindhrs. The ExcecutionListener was finally the key!

1 Like