User sessiondetails in controller & screenxml


I read a good portion of the documentation and forums topics however I’m stuck with my ‘issue’.

I would like to recover the user details (entity object data) that you can find in the session so that I can use it for business logic & querying related data based on some additional metadata fields.

I’m currently not finding a good or best practice approach

Please help.

To obtain current user, you need to call:

private CurrentAuthentication currentAuthentication;

        UserDetails userDetails = currentAuthentication.getUser();
        if (userDetails instanceof User) {
            User currentUser = (User) userDetails;

see doc page:

1 Like
UserDetails userDetails = currentAuthentication.getUser();

    if (userDetails instanceof Account) {
        Account currentUser = (Account) userDetails;

        batchesDl.setParameter("currentUserTenant", currentUser.getTenantfk());


I got this code now, which works however as is normal with spring boot security, I only have the fields available that are part of userDetails.

I have something in the documentation about querying custom data but I’m missing a small link the the jpa repository of userservice that i can use to do a findbyId()

Which would be the recommended way in jmix to do this?

It’s not clear what you need, sorry. Can you show with a bit of code, what data do you need?

What i want to do is create a tenant layer in the application and use the tenant object from the user to query other data

Note that there is going to be a “single database multitenancy” addon in the Jmix:

If you want to create custom functionality, then:

  1. Create entity Tenant in your project
  2. Add association between User and Tenant, e.g. many-to-one:

private Tenant tenant;
  1. When you need to get the current Tenant, just invoke a JPQL query with DataManager:
private DataManager dataManager;

User currentUser = ...;
Tenant tenant = dataManager.load(Tenant.class)
        .query("select t from User u, Tenant t where u.tenant = t and = :userId")
        .parameter("userId", currentUser.getId())

This operation can be extracted into Spring bean for convenient use.

1 Like

Can we already use the addon or has it yet to be released as stable?

I would advise to wait for the release 1.0, it will be soon.

1 Like