| .. | .. |
|---|
| 31 | 31 | import org.jboss.resteasy.spi.ResteasyProviderFactory; |
|---|
| 32 | 32 | |
|---|
| 33 | 33 | @Provider |
|---|
| 34 | | -// @PreMatching |
|---|
| 35 | 34 | @Priority(Priorities.AUTHENTICATION) |
|---|
| 36 | 35 | public class SecurityInterceptor implements javax.ws.rs.container.ContainerRequestFilter { |
|---|
| 37 | 36 | private static final Logger LOG = LogManager.getLogger(SecurityInterceptor.class); |
|---|
| 38 | | - |
|---|
| 39 | | - @Inject |
|---|
| 40 | | - private TokenHelper tokenHelper; |
|---|
| 41 | 37 | |
|---|
| 42 | 38 | @Context |
|---|
| 43 | 39 | private HttpServletRequest servletRequest; |
|---|
| 44 | 40 | |
|---|
| 45 | 41 | @Inject |
|---|
| 46 | | - CacheTTL cache; |
|---|
| 47 | | - |
|---|
| 48 | | - @Context |
|---|
| 49 | | - Dispatcher dispatcher; |
|---|
| 42 | + private CacheTTL cache; |
|---|
| 50 | 43 | |
|---|
| 51 | 44 | @Inject |
|---|
| 52 | | - com.google.inject.Provider<EntityManager> emProvider; |
|---|
| 45 | + private TokenHelper tokenHelper; |
|---|
| 46 | + |
|---|
| 47 | + @Context |
|---|
| 48 | + private Dispatcher dispatcher; |
|---|
| 49 | + |
|---|
| 50 | + @Inject |
|---|
| 51 | + private EntityManager em; |
|---|
| 53 | 52 | |
|---|
| 54 | 53 | public void filter(ContainerRequestContext containerRequestContext) throws IOException { |
|---|
| 54 | + |
|---|
| 55 | 55 | ResourceMethodInvoker methodInvoker = (ResourceMethodInvoker) containerRequestContext |
|---|
| 56 | 56 | .getProperty("org.jboss.resteasy.core.ResourceMethodInvoker"); |
|---|
| 57 | 57 | Method method = methodInvoker.getMethod(); |
|---|
| .. | .. |
|---|
| 76 | 76 | containerRequestContext.setSecurityContext(scw); |
|---|
| 77 | 77 | // Next line provide injection in resource methods |
|---|
| 78 | 78 | ResteasyProviderFactory.pushContext(BasicSecurityContext.class, scw); |
|---|
| 79 | + ResteasyProviderFactory.pushContext(EntityManager.class, em); |
|---|
| 79 | 80 | LOG.debug("Added custom SecurityContext for user {}, orgs: {}", username, orgs); |
|---|
| 80 | 81 | } |
|---|
| 81 | 82 | } |
|---|
| .. | .. |
|---|
| 85 | 86 | Set<Integer> userOrgs = cache.get("orgs_" + username, Set.class); |
|---|
| 86 | 87 | if (userOrgs == null) { |
|---|
| 87 | 88 | // Theorically this shouldn't be never null, but just in case... |
|---|
| 88 | | - EntityManager em = emProvider.get(); |
|---|
| 89 | 89 | User user = em.find(User.class, username); |
|---|
| 90 | 90 | if (user != null) { |
|---|
| 91 | 91 | userOrgs = user.getAllOrgsIds(); |
|---|
| .. | .. |
|---|
| 103 | 103 | } |
|---|
| 104 | 104 | Integer userRoles = cache.get("roles_" + username, Integer.class); |
|---|
| 105 | 105 | if (userRoles == null) { |
|---|
| 106 | | - EntityManager em = emProvider.get(); |
|---|
| 107 | 106 | User user = em.find(User.class, username); |
|---|
| 108 | 107 | if (user != null) { |
|---|
| 109 | 108 | userRoles = 0; |
|---|