Jump to content
We've recently updated our Privacy Statement, available here ×

Restrict SQL Query Input Control with Tenant


chiphi13

Recommended Posts

I am currently using the latest version of JasperServer V3.5 and need to be able to pass the tenant ID into the sql query of the input controls in order to restrict the data that gets returned.  Is there a way to pass inject into the sql query dynamic parameters? 

Link to comment
Share on other sites

  • 3 weeks later...
  • Replies 6
  • Created
  • Last Reply

Top Posters In This Topic

Im also looking for this solution.

Im trying to pass the currently logged in user to the underlying sql query for the input control.

I have a report but would like that needs a branch code as a parameter(that i can do) , but this branch code is selected form a drop down list which has underlying sql query which needs a username to determine which Branches are linked to the logged in user.

 

eg the underlying query for the input control looks like this

 

select *

from Branches

where username = $P!{currently loggedInUserName}

 

A selection should be made from the available list of branches based on the current user that is logged in

is there anyway i can pass the currently logged in user to the underlying query for the input control?

It will save me a lot of time (and headaches   :-)  )if this is possible.

Link to comment
Share on other sites

There is always a LoggedInUser parameter available to reports or input controls run in JasperServer. It is of type com.jaspersoft.jasperserver.api.metadata.user.domain.User. You can get the user name from that.

 

For JasperServer v3.1 and later, there has been a hook added to the EngineServiceImpl so you can inject values into the input control query.

 

Create a class that implements com.jaspersoft.jasperserver.api.engine.common.service.IQueryManipulator, which has a single method,

updateQuery(String queryString, Map parameters)

 

In the logic of your method, you can replace what you want in the query string before it is actually used in a query. In your case, you can get the LoggedInUser parameter out of the parameters Map.

 

In WEB-INF/applicationContext.xml, you will need to change the engineService bean as follows:

 

    <bean id="engineService" class="com.jaspersoft.jasperserver.api.engine.jasperreports.service.impl.EngineServiceImpl" destroy-method="release">

    ....

            <property name="queryManipulator">

               <bean class="your.IQueryManipulator.Implementation.Class"/>

            </property>

    ....

   </bean>

 

Sherman

Jaspersoft

Link to comment
Share on other sites

The LoggedInUser parameter is working fine in my reports.

But my problem is that i should limit a dropdown list(based on the current logged in user) where a branch is selected from and then passed to the report. So this cancels out the LoggedInUser parameter.

 

The second solution u suggested sounds more like what i need.

The problem is just that i dont know exactly what to do.

The way i understand it is that jasper will call this class (i have to define myself) everytime before it will execute a query and replace a specific string in the query(as i defined it in the class) with one of the parameters in the Map object the method in the class receives. This method then returns the altered query string to jasperserver which in turn then executes it.

 

Is there documentation on this  showing me examples?

it would be really appreciated if u can point me in the right direction for examples.

 

Many Regrads

Link to comment
Share on other sites

Your understanding is correct.

 

I can't give you specific examples, but the style would be to put ${LoggedInUserName} in your query for the input control, like:

 

select department from person where username = '${LoggedInUserName}'

 

In your updateQuery(String queryString, Map parameters) method, you would replace ${LoggedInUserName} with the currently logged in user name, which you can get like:

 

        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
 

        UserDetails details = auth.getPrincipal();

 

        String userName = details.getUsername();

 

 

Sherman

Jaspersoft

Link to comment
Share on other sites

  • 2 weeks later...
  • 4 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...