To acess the user's authentication information from within a JSF page, use the securityContext bean and any of its available methods. For instance, using Expression Language (EL), the following will return true/false indicating whether the user is authenticated or not: #{securityContext.authenticated}. Similarly, to determine whether the user has been assigned a specific role, use the following EL snippet #{securityContext.userInRole['SomeRole']}. It will return true if the user has been assigned the specific role.
Example:
// in the context of a JSF page
<af:commandLink id="login_logout" action = "#{securityContext.authenticated ? 'logout' : 'login'}" text="#{securityContext.authenticated ? 'Logout' : 'Login'}/>
<af:commandToolbarButton id="delete" actionListener="#{backingBean.delete}" disabled="#{securityContext.userInRole['CanDelete']==false}" text="Delete"/>
Context:
JSF Page
Bits of code related to Oracle's Application Development Framework (ADF). Some of the posts in this blog may seem elementary. They are. Nevertheless, you will be amazed to find out how many beginner ADF practitioners are struggling with basic concepts and sample code. Hopefully they will find some here.
Showing posts with label Security. Show all posts
Showing posts with label Security. Show all posts
Monday, May 10, 2010
Sunday, April 18, 2010
Bit #12 - Accessing the authenticated user's security roles from a backing bean
To access the authenticated user's security roles from a backing bean, first retrieve the SecurityContext from the current ADFContext instance and then call its getUserRoles() method. getUserRoles() returns a String array of all the roles defined for the user. To determine whether a specific role is assigned to the user, call the SecurityContext isUserInRole() method specifying the role as an argument. This method will return a boolean indicator of whether the role is assigned to the user or not.
Example:
// in the context of a backing bean
public String[] getUserRoles(){
return ADFContext.getCurrent().getSecurityContext().getUserRoles();
}
public boolean isUserInRole(){
return ADFContext.getCurrent().getSecurityContext().isUserInRole("RoleName");
}
Context:
Backing Bean
Reference:
Oracle Fusion Middleware Java API Reference for Oracle ADF Share, getUserRoles
Example:
// in the context of a backing bean
public String[] getUserRoles(){
return ADFContext.getCurrent().getSecurityContext().getUserRoles();
}
public boolean isUserInRole(){
return ADFContext.getCurrent().getSecurityContext().isUserInRole("RoleName");
}
Context:
Backing Bean
Reference:
Oracle Fusion Middleware Java API Reference for Oracle ADF Share, getUserRoles
Subscribe to:
Posts (Atom)