1

In a jsf application data is managed injecting a PersistenceContext.

 @PersistenceContext(unitName = "MyPU")
 private EntityManager em;

PersistenceContext is static and choosen at compile time. Is there a way to inject a different PersistenceContext based on the user ? My idea is to enforce authorization checks on database side too, so if there is a hole in application security the user cannot access or modify restricted data.

MWiesner
  • 8,868
  • 11
  • 36
  • 70
Filippo
  • 1,123
  • 1
  • 11
  • 28

1 Answers1

0

Create some factory :

@Stateless
public class PersistenceContextFactory {
  @PersistenceContext(unitName="MyPU")
  private EntityManager emPU;

  @PersistenceContext(unitName="MyOtherPU")
  private EntityManager emOtherPU;

  public EntityManager getEntityManager(User user) {
    if(user.hasSomeRight()) {
      return emPU;
    } else {
      return emOtherPU;
    }
  }
}
grigouille
  • 511
  • 3
  • 14