Restricting data by role XavaPro 7.6 includes a new filter called RolesFilter that allows restricting data access based on the current user's roles. This filter works by comparing the user's roles with a property of the entity that contains a role name. To implement this restriction, follow these steps: 1. Define a property for the role in your entity @Column(length=30, columnDefinition = "VARCHAR(30) DEFAULT 'user'")
private String role;
This property will store the name of the role that has permission to access the record. You can set a default value if you wish, as in the previous example where the default value is 'user'. 2. Configure the filter in the @Tab annotation @Tab(baseCondition = "${role} IN (?)",
filter=com.openxava.naviox.filters.RolesFilter.class,
properties="year, number, date, customer.number, customer.name, ...")
The base condition ${role} IN (?) indicates that records will be filtered where the value of the role property is included in the list of roles of the current user. The RolesFilter automatically provides this list of roles. 3. How it works When a user accesses the list, the RolesFilter gets all the roles assigned to the current user and uses them to filter the data. For example:
- If a user has only the 'user' role assigned, they will only see records where the role property has the value 'user'.
- If a user has the 'admin' and 'user' roles assigned, they will see the records where the role property has the value 'admin' or 'user'.
This allows implementing a role-based data access system, where different types of users can see different sets of data according to their assigned roles. Remember to import the necessary classes: import com.openxava.naviox.filters.RolesFilter;
Notice that the package is com.openxava.naviox, not org.openxava. |