User Permissions - Jsf - PrimeFaces

0

Hi. I have the following case, I need to enable or disable components of PrimeFaces taking into account the permissions assigned in the Database, most examples are by pages in this case I need to control the components for example:

I have a table permissions in the database associated with a user:

User: Pedro
Display: Users
Component: ButtonSave
Permission: Yes / No

If the user has permission = Yes, disabled="false"

<p:commandButton value="Disabled" id="BotonGuardar" disabled="false" />

If permission = No, disabled="true"

<p:commandButton value="Disabled" id="BotonGuardar" disabled="true" />

The idea is to control this behavior from the Bean for each user. Thanks

    
asked by Jhon Alexis Ramirez 08.08.2017 в 17:23
source

3 answers

1

You can use Expression Language in the following way, assuming that the permission is stored in a boolean variable in the managed bean of the permission name with their respective getter and setter and that the facelet points to a managed bean with the name bean:

View

<p:commandButton value="Disabled" id="BotonGuardar" disabled="#{bean.permiso}" />

ManagedBean

 @ManagedBean
 public class Bean{
      private boolean permiso;

       public boolean getPermiso(){
            return permiso;
       }
       public void setPermiso(boolean permiso){
           this.permiso = permiso;
       } 
 }
    
answered by 08.08.2017 в 17:35
1

An elegant way to do this is to define a generic method that deals with accessing BD based on the variable component and the user:

<p:commandButton value="Disabled" id="BotonGuardar" disabled="#{seguridad.debeDeshabilitarse(component.id)}" />

component is a variable that is loaded automatically with the information of the component (I assume that you use JSF 2).

Thus, you could create a Security class that you retrieve from session or where you have stored the user and connect to BD to check if the component should be shown or not.

@ManagedBean
public class Seguridad implements Serializable{
    public Boolean debeDeshabilitarse(String componentId){
        MiDAO dao = getDao();
        Usuario usuario = getUsuario();
        return dao.debeDeshabilitarse(componentId, usuario);
    }
}

And in BD you would have a table with a primary key (Component, User) that will provide you with the information.

This way you could reuse the function throughout the application in a comfortable and equal way in all cases.

    
answered by 10.08.2017 в 12:44
0

If what you need is to validate whether a button must be enabled or not, what you should do is create a method in the bean that returns true or false and then assign it to the label, eg:

//  XHTML
<p:commandButton value="Disabled" id="BotonGuardar" 
    disabled="#{miBean.validacion()}" // Debe devolver true o false

//  Bean
...
    public Boolean validacion(){
      return true;
    }
...

I hope my answer has helped you, regards.

    
answered by 08.08.2017 в 17:29