How to create conditions based on a user role using JSF / MyFaces?

What parameters do I need to read the current user roles from my JSP pages? I know the attribute visibleOnUserRole="myRole"for Tomahawk components, but I need roles for more complex things than ease of visibility.

+3
source share
2 answers

ExternalContext provides user and role information.

public class RolesAccess implements Serializable {

    public String getUserPrincipalName() {
        FacesContext context = FacesContext.getCurrentInstance();
        Principal principal = context.getExternalContext().getUserPrincipal();
        if(principal == null) {
            return null;
        }
        return principal.getName();
    }

    public String getUser() {
        FacesContext context = FacesContext.getCurrentInstance();
        return context.getExternalContext().getRemoteUser();
    }

    public boolean isManager() {
        FacesContext context = FacesContext.getCurrentInstance();
        return context.getExternalContext().isUserInRole("manager");
    }

}

If you use JSF in servlets, this information matches the values ​​displayed by the HttpServletRequest .

You can use managed beans to display values ​​in an expression language .

<f:view>
    <h:outputLabel value="#{rolesBean.userPrincipalName}" />
    <h:outputLabel value="#{rolesBean.user}" />
    <h:outputLabel value="#{rolesBean.manager}" />
</f:view>
+7
source

Java EE 6 ( , /), request Facelets. .

.

<h:outputText value="hi, admin!" rendered="#{request.isUserInRole('Admin')}" />
+2

Source: https://habr.com/ru/post/1699504/


All Articles