Roberto Sánchez
2014-01-18 602c4c4501dcd89cbce1d6ba61ba6bc75761d643
#396 feature - Getting all orgs from user including the children ones
3 files modified
changed files
securis/src/main/java/net/curisit/securis/db/User.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/SecurityInterceptor.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/UserResource.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/db/User.java
....@@ -3,7 +3,9 @@
33 import java.io.Serializable;
44 import java.util.ArrayList;
55 import java.util.Date;
6
+import java.util.HashSet;
67 import java.util.List;
8
+import java.util.Set;
79
810 import javax.persistence.Column;
911 import javax.persistence.Entity;
....@@ -70,7 +72,7 @@
7072 inverseJoinColumns =
7173 { @JoinColumn(name = "organization_id", referencedColumnName = "id") } //
7274 )
73
- private List<Organization> organizations;
75
+ private Set<Organization> organizations;
7476
7577 public String getUsername() {
7678 return username;
....@@ -160,17 +162,17 @@
160162 this.lang = lang;
161163 }
162164
163
- public List<Organization> getOrganizations() {
165
+ public Set<Organization> getOrganizations() {
164166 return organizations;
165167 }
166168
167
- public void setOrganizations(List<Organization> organizations) {
169
+ public void setOrganizations(Set<Organization> organizations) {
168170 this.organizations = organizations;
169171 }
170172
171173 @JsonProperty("organizations_ids")
172174 public void setOrgsIds(List<Integer> orgsIds) {
173
- organizations = new ArrayList<>();
175
+ organizations = new HashSet<>();
174176 for (Integer orgid : orgsIds) {
175177 Organization o = new Organization();
176178 o.setId(orgid);
....@@ -189,6 +191,22 @@
189191 return ids;
190192 }
191193
194
+ @JsonIgnore
195
+ public Set<Integer> getAllOrgsIds() {
196
+ if (organizations == null)
197
+ return null;
198
+ Set<Integer> ids = new HashSet<>();
199
+ includeAllOrgs(this.organizations, ids);
200
+ return ids;
201
+ }
202
+
203
+ private void includeAllOrgs(Set<Organization> list, Set<Integer> orgIds) {
204
+ for (Organization org : list) {
205
+ orgIds.add(org.getId());
206
+ includeAllOrgs(org.getChildOrganizations(), orgIds);
207
+ }
208
+ }
209
+
192210 static public class Rol {
193211 static public final int ADVANCE = 0x01;
194212 static public final int ADMIN = 0x02;
securis/src/main/java/net/curisit/securis/services/SecurityInterceptor.java
....@@ -3,6 +3,7 @@
33 import java.io.IOException;
44 import java.lang.reflect.Method;
55 import java.util.List;
6
+import java.util.Set;
67
78 import javax.inject.Inject;
89 import javax.persistence.EntityManager;
....@@ -60,8 +61,27 @@
6061 log.info("User {} has no necessary role to access url: {}", username, servletRequest.getPathInfo());
6162 containerRequestContext.abortWith(Response.status(Status.UNAUTHORIZED).build());
6263 }
64
+ Set<Integer> orgs = getUserOrganizations(username);
65
+ servletRequest.setAttribute("user_orgs", orgs);
6366 }
6467 }
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;
6585 }
6686
6787 private int getUserRoles(String username) {
....@@ -79,6 +99,7 @@
7999 }
80100 // We store user roles in cache only for one hour
81101 cache.set("roles_" + username, userRoles, 3600);
102
+ cache.set("orgs_" + username, user.getOrgsIds(), 3600);
82103 }
83104 }
84105 return userRoles == null ? 0 : userRoles.intValue();
securis/src/main/java/net/curisit/securis/services/UserResource.java
....@@ -1,8 +1,9 @@
11 package net.curisit.securis.services;
22
3
-import java.util.ArrayList;
43 import java.util.Date;
4
+import java.util.HashSet;
55 import java.util.List;
6
+import java.util.Set;
67
78 import javax.inject.Inject;
89 import javax.inject.Provider;
....@@ -113,10 +114,10 @@
113114 return modify(user, user.getUsername(), token);
114115 }
115116
116
- List<Organization> orgs = null;
117
+ Set<Organization> orgs = null;
117118 List<Integer> orgsIds = user.getOrgsIds();
118119 if (orgsIds != null && orgsIds.size() > 0) {
119
- orgs = new ArrayList<>();
120
+ orgs = new HashSet<>();
120121 for (Integer orgId : orgsIds) {
121122 Organization o = em.find(Organization.class, orgId);
122123 if (o == null) {
....@@ -152,10 +153,10 @@
152153 return create(user, token);
153154 }
154155
155
- List<Organization> orgs = null;
156
+ Set<Organization> orgs = null;
156157 List<Integer> orgsIds = user.getOrgsIds();
157158 if (orgsIds != null && orgsIds.size() > 0) {
158
- orgs = new ArrayList<>();
159
+ orgs = new HashSet<>();
159160 for (Integer orgId : orgsIds) {
160161 Organization o = em.find(Organization.class, orgId);
161162 if (o == null) {