How to retrieve tenant_id of current user for reporting

Hi All,

I’m using the reporting and multitenancy add-on. For some reports I need to use complex native SQL statements in which I need to put in the WHERE clause a filter for the respective tenant_id of the user logged in.

I had the idea to create a header band to retrieve the tenant_id like below:

import com.haulmont.cuba.core.global.AppBeans;
import com.haulmont.cuba.core.global.UserSessionSource;

String tenant = AppBeans.get(UserSessionSource.class)
.getUserSession().getUser().tenantId;
return [[ ‘tenant_id’ : tenant]]

Then I try to use the tenant_id in the SQL statement:


where tenant_id = ${header.tenant_id}

I get the following error message:

image

Any idea what I did wrong?

Hello @udegel

You can call getCurrentUserTenantId() from the TenantProvider bean to get tenantId.

Example:

import com.haulmont.cuba.core.global.AppBeans
import com.haulmont.addon.sdbmt.core.app.multitenancy.TenantProvider

def tenantId = AppBeans.get(TenantProvider.class).getCurrentUserTenantId()

return [['tenantId' : tenantId]]

Regards,
Nikita

Hi Nikita,
works perfectly! Thank you so much!
BR, Uwe