A profile attribute is a name-value pair associated with a user account. It is ordinarily used to extend the object's standard access grants, but it has other uses as well. To define or modify the profile attributes for a user account, see the section on editing user attributes in the JasperReports Server Administrator Guide.
For example, this Groovy expression tests users for the Cities access grant in the store_city field in the store table:
<filterExpression>store.store_city in (groovy('authentication.getPrincipal().getAttributes().find{ it.attrName == "Cities" }.attrValue.split(",").collect {"''" + it + "''" }.join(",").replaceFirst("^''","").replaceFirst("''\$","")'))</filterExpression>
Using profile attributes enables you to obtain similar results with simpler expressions. The example below uses a principal expression to find all users with the Cities profile attribute, then it uses a filter expression to grant access only to the users among them whose Cities profile attribute is San Francisco:
<resourceAccessGrant id="Jointree_1_row_access_grant_2"> <principalExpression><![CDATA[authentication.getPrincipal().getAttributes().any{ it.getAttrName() in ['Cities'] && it.getAttrValue() in ['San Francisco'] }]]></principalExpression> <filterExpression>store.store_city in ('San Francisco')</filterExpression> </resourceAccessGrant> |
The next example tests the same profile attribute, Cities. However, instead of granting access only to users with a specific profile attribute, you use variable substitution in the filter expression to grant access to all users according to their Cities attribute. A user with the San Francisco profile attribute gets access to that city, a user with the Denver profile attribute gets access to that city, and so on:
<resourceAccessGrant id="Jointree_1_row_access_grant_4"> <principalExpression><![CDATA[authentication.getPrincipal().getAttributes().any{ it.getAttrName() in ['Cities'] }]]></principalExpression> <filterExpression>store.store_city in (groovy('authentication.getPrincipal().getAttributes().find{ it.attrName == "Cities" }.attrValue'))</filterExpression> </resourceAccessGrant> |
Finally, testProfileAttribute is a function for taking advantage of the profile attributes feature. An extension of the DomEL language, testProfileAttribute examines an object for a given profile attribute and permits access to the related data when the attribute is present.
Using the testProfileAttribute function, you can write simpler expressions than you can using Groovy. For instance, this filter expression tests users for the Cities profile attribute. Compare it to the expression in the previous example:
<filterExpression>testProfileAttribute(store.store_city, 'Cities')</filterExpression>
For more information about profile attributes, variable substitution, and testProfileAttribute, see the JasperReports Server Ultimate Guide and Jaspersoft OLAP Ultimate Guide.