Hello!
If both application and Superset use the same database, you can set active tenant using DatasetConstraintsProvider.
As Superset uses datasets for filling dashboards, we can constrain loading data. The DatasetConstraint
should contain information about native SQL “WHERE” clause, so there we should provide condition with current tenant. For instance:
@Component
public class TenantDatasetConstraintProvider implements DatasetConstraintsProvider {
private final TenantProvider tenantProvider;
public TenantDatasetConstraintProvider(TenantProvider tenantProvider) {
this.tenantProvider = tenantProvider;
}
@Override
public List<DatasetConstraint> getConstraints() {
return List.of(new DatasetConstraint(24,
"MY_TENANT_ID = '" + tenantProvider.getCurrentUserTenantId() + "'"));
}
}
And then use it as “Install” handler for dashboard component:
@Autowired
private TenantDatasetConstraintProvider tenantDatasetConstraintProvider;
@Install(to = "dashboard", subject = "datasetConstraintsProvider")
private List<DatasetConstraint> dashboardDatasetConstraintsProvider() {
return tenantDatasetConstraintProvider.getConstraints();
}
Note that this condition is applied to result of a dataset. If your dataset does not contain such column it won’t load the data.