User sessiondetails in controller & screenxml

Hi,

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.

Hi,
To obtain current user, you need to call:

@Autowired
private CurrentAuthentication currentAuthentication;

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

see doc page:
https://docs.jmix.io/jmix/0.x/security/authentication.html#current

1 Like
UserDetails userDetails = currentAuthentication.getUser();

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

        batchesDl.setParameter("currentUserTenant", currentUser.getTenantfk());
    }*/

    batchesDl.load();

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:
User.java:

@ManyToOne
@JoinColumn("TENANT_ID")
private Tenant tenant;
  1. When you need to get the current Tenant, just invoke a JPQL query with DataManager:
@Autowired
private DataManager dataManager;

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

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

https://docs.jmix.io/jmix/0.x/data-access/data-manager.html

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