| .. | .. |
|---|
| 3 | 3 | import java.io.IOException; |
|---|
| 4 | 4 | import java.lang.reflect.Method; |
|---|
| 5 | 5 | import java.util.List; |
|---|
| 6 | +import java.util.Set; |
|---|
| 6 | 7 | |
|---|
| 7 | 8 | import javax.inject.Inject; |
|---|
| 8 | 9 | import javax.persistence.EntityManager; |
|---|
| .. | .. |
|---|
| 60 | 61 | log.info("User {} has no necessary role to access url: {}", username, servletRequest.getPathInfo()); |
|---|
| 61 | 62 | containerRequestContext.abortWith(Response.status(Status.UNAUTHORIZED).build()); |
|---|
| 62 | 63 | } |
|---|
| 64 | + Set<Integer> orgs = getUserOrganizations(username); |
|---|
| 65 | + servletRequest.setAttribute("user_orgs", orgs); |
|---|
| 63 | 66 | } |
|---|
| 64 | 67 | } |
|---|
| 68 | + } |
|---|
| 69 | + |
|---|
| 70 | + private Set<Integer> getUserOrganizations(String username) { |
|---|
| 71 | + @SuppressWarnings("unchecked") |
|---|
| 72 | + Set<Integer> userOrgs = cache.get("orgs_" + username, Set.class); |
|---|
| 73 | + if (userOrgs == null) { |
|---|
| 74 | + // Theorically this shouldn't be never null, but just in case... |
|---|
| 75 | + EntityManager em = emProvider.get(); |
|---|
| 76 | + User user = em.find(User.class, username); |
|---|
| 77 | + if (user != null) { |
|---|
| 78 | + userOrgs = user.getAllOrgsIds(); |
|---|
| 79 | + // We store user orgs in cache only for one hour |
|---|
| 80 | + cache.set("orgs_" + username, userOrgs, 3600); |
|---|
| 81 | + } |
|---|
| 82 | + } |
|---|
| 83 | + |
|---|
| 84 | + return userOrgs; |
|---|
| 65 | 85 | } |
|---|
| 66 | 86 | |
|---|
| 67 | 87 | private int getUserRoles(String username) { |
|---|
| .. | .. |
|---|
| 79 | 99 | } |
|---|
| 80 | 100 | // We store user roles in cache only for one hour |
|---|
| 81 | 101 | cache.set("roles_" + username, userRoles, 3600); |
|---|
| 102 | + cache.set("orgs_" + username, user.getOrgsIds(), 3600); |
|---|
| 82 | 103 | } |
|---|
| 83 | 104 | } |
|---|
| 84 | 105 | return userRoles == null ? 0 : userRoles.intValue(); |
|---|