Role Based Rendering of UIComponents
This example will show you how to conditionally render a UIComponent based on the users role.
Last week during the JSF Book promo on JavaRanch someone asked about this. I promised to put together a quick demo and here it is...
Stuff about this example...
- The Page object creates the conditionally rendered component. This was the only way that I could figure out to make it work. If you can think of another please let me know. I spent some time trying to use an expression but there was no way to tell the value I was getting which component was asking if it should render.
- This simple example uses the Tomcat memory realm instead of a db. For real apps you would want to use a DB realm.
- The login and login-error pages are really simple.
- The test for the page uses mock objects of FacesContext and ExternalContext classes. Quick overview here by Vincent Massol. The latest version of the class mocker (instead of just interfaces) is now at 2.1 but the one included with this download is functional.
Here is the important code in case you don't want to download the example.
JSP Code
<h:inputText id="test" binding="#{page.inputField}"/>
The binding attribute binds the actual component to a UIComponent created by the object tied to 'page'. IOW when the view is being rendered it will get the component by calling 'getInputField()' on the page object.
Java Code
public UIInput getInputField() {
if (authorized()) {
inputField.setRendered(true);
} else {
inputField.setRendered(false);
}
return inputField;
}
private boolean authorized() {
boolean authorized = false;
// if its null try to find it
if(null == context) {
context = FacesContext.getCurrentInstance();
}
// should never happen...
if (null != context) {
// clearly 'foo' is a made up role your check should be
// more valid
authorized = context.getExternalContext().isUserInRole("foo");
}
return authorized;
}

