3 Replies Latest reply on Oct 3, 2012 11:08 AM by mbarquet

    Validate user's role to show/hide a button or a panel

    mbarquet

      My xhtml page has this panel Group with buttons so if I am an admin user I should have every button of the panel but if I am just a user then i should have only mostrar button how can I do that  for the roles cg_admin and cg_users

       

      <h:panelGroup id="idPanelToolBar">
           <rich:toolBar id="idToolBar" itemSeparator="none">
          
            <rich:toolBarGroup location="left" styleClass="fondoToolBarGroup">
               <a4j:commandButton styleClass="boton botonNuevo"
                value="#{text['commandButtom.nuevo']}"
                action="#{divisionPuestoBean.crear}" limitToList="true"
                reRender="divCRUD,idPanelBarraBotoneraSuperior" ajaxSingle="true">
               </a4j:commandButton>
            </rich:toolBarGroup>    

            <rich:toolBarGroup location="left" styleClass="fondoToolBarGroup"
             rendered="#{divisionPuestoBean.modoVista == 'editar'}">
             <a4j:commandButton styleClass="boton botonGuardar"
              value="#{text['commandButtom.guardar']}"
              action="#{divisionPuestoBean.actualizar}" limitToList="true"
              reRender="divCRUD,idPanelBarraBotoneraSuperior"
              rendered="#{divisionPuestoBean.modoVista == 'editar'}">
              <f:param name="id" value="#{divisionPuestoBean.divisionPuesto.id}" />
             </a4j:commandButton>
            </rich:toolBarGroup>

            <rich:toolBarGroup location="left" styleClass="fondoToolBarGroup"
             rendered="#{divisionPuestoBean.modoVista == 'mostrar'}">
             <a4j:commandButton styleClass="boton botonEliminar"
              value="#{text['commandButtom.eliminar']}"
              action="#{divisionPuestoBean.eliminar}" limitToList="true"
              reRender="divCRUD,idPanelBarraBotoneraSuperior"
              rendered="#{divisionPuestoBean.modoVista == 'mostrar'}"
              onclick="if(confirm('Esta seguro de eliminar esta división o puesto?') == false )  return false;">
              <f:param name="id" value="#{divisionPuestoBean.divisionPuesto.id}" />
             </a4j:commandButton>
            </rich:toolBarGroup>

            <rich:toolBarGroup location="left" styleClass="fondoToolBarGroup"
             rendered="#{divisionPuestoBean.modoVista == 'mostrar'}">
             <a4j:commandButton styleClass="boton botonEditar"
              value="#{text['commandButtom.editar']}"
              action="#{divisionPuestoBean.editar}" limitToList="true"
              reRender="divCRUD,idPanelBarraBotoneraSuperior" 
              rendered="#{divisionPuestoBean.modoVista == 'mostrar'}">
              <f:param name="id" value="#{divisionPuestoBean.divisionPuesto.id}" />
             </a4j:commandButton>
            </rich:toolBarGroup>
           
            <rich:toolBarGroup location="left" styleClass="fondoToolBarGroup"
             rendered="#{divisionPuestoBean.modoVista == 'crear'}">
             <a4j:commandButton styleClass="boton botonGuardar"
              value="#{text['commandButtom.guardar']}"
              action="#{divisionPuestoBean.guardar}" limitToList="true"
              reRender="divCRUD,idPanelBarraBotoneraSuperior"  process="idCriterioDivision">
             </a4j:commandButton>
            </rich:toolBarGroup>
           

           </rich:toolBar>
          </h:panelGroup>

        • 1. Re: Validate user's role to show/hide a button or a panel
          iabughosh

          Hello Barquet,

          you can use rich:isUserInRole, ex:

           

          <a4j:commandButton id="myBtn" rendered="#{rich:isUserInRole('cg_admin')}"/>

          or

          <a4j:commandButton id="myBtn" rendered="#{rich:isUserInRole('cg_users')}"/>

           

          regards.

          • 2. Re: Validate user's role to show/hide a button or a panel
            vi_mahe_ka1

            above solution works fine. u need to add the roles in web.xml

             

            however you can do it in alternate way also using rendered attribute of button.

            Add a boolean property in ManagedBean like displayButtonFlag;

            check the role in ManagedBean (divisionPuestoBean)

            set the flag true or false based on the condition

            use rendered="#{divisionPuestoBean.displayButtonFlag}"

             

            another option without using the flag

            rendered="#{divisionPuestoBean.role eq'cg_admin'}"

            rendered="#{divisionPuestoBean.role eq'cg_users'}"

             

            1 of 1 people found this helpful
            • 3. Re: Validate user's role to show/hide a button or a panel
              mbarquet

              Hi Mahesh and Ibrahim, tanKS for your quick answers sorry I didnt add my web .xml but the roles are already there, the lines you provided were really useful that worked great thanks again