JHipster Role based masking of certain columns

436 views Asked by At

In a JHipster based project, we need to selectively filter out certain columns based on role/user logged in. All users will be able to view/modify most of the columns, but only some privileged users will be able to view/modify certain secure fields/columns.

It looks like the only option to get this done is using EntityListeners. I can use an EntityListener and mask a certain column during PostLoad event. Say for example, I mask the column my_secure_column with XXX and display to the user.

User then changes some other fields/columns (that he has access to) and submits the form. Do I have to again trap the partially filled in entity in PreUpdate event, get the original value for my_secure_column from database and set it before persisting?

All this seems inefficient. Scoured several hours but couldn't find a specific implementation that best suits this use case.

Edit 1: This looks like a first step to achieving this in a slightly better way. Updating Entities with Update Query in Spring Data JPA

I could use specific partial updates like updateAsUserRole, updateAsManagerRole, etc., instead of persisting the whole entity all the time.

@Repository
public interface CompanyRepository extends JpaRepository<Company, Integer> {
   @Modifying(clearAutomatically = true)
   @Query("UPDATE Company c SET c.address = :address WHERE c.id = :companyId")
   int updateAddress(@Param("companyId") int companyId, @Param("address") String address);
}
1

There are 1 answers

3
Klaus Groenbaek On

Column based security is not an easy problem to solve, and especially in combination with JPA.

Ideally you like to avoid even loading the columns, but since you are selecting entities this is not possible by default, so you have to remove the restricted content by overriding the value after load.

As an alternative you can create a view bean (POJO) and then use JPQL Constructor Expression. Personally I would use CriteriaBuilder. construct() instead of concatenating a JPQL query, but same principle.

With regards to updating the data, the UI should of cause not allow the editing of restricted fields. However you still have to validate on the backend, and I would recommend that you check if the column was modify before calling JPA. Typically you have the modifications in a DTO and would need to load the Entity anyway, if a restricted column was modified, you would send an error back. This way you only call JPA after the security has been checked.