securis/src/main/java/net/curisit/securis/db/User.java
.. .. @@ -3,7 +3,9 @@ 3 3 import java.io.Serializable; 4 4 import java.util.ArrayList; 5 5 import java.util.Date; 6 +import java.util.HashSet;6 7 import java.util.List; 8 +import java.util.Set;7 9 8 10 import javax.persistence.Column; 9 11 import javax.persistence.Entity; .. .. @@ -70,7 +72,7 @@ 70 72 inverseJoinColumns = 71 73 { @JoinColumn(name = "organization_id", referencedColumnName = "id") } // 72 74 ) 73 - private List<Organization> organizations;75 + private Set<Organization> organizations;74 76 75 77 public String getUsername() { 76 78 return username; .. .. @@ -160,17 +162,17 @@ 160 162 this.lang = lang; 161 163 } 162 164 163 - public List<Organization> getOrganizations() {165 + public Set<Organization> getOrganizations() {164 166 return organizations; 165 167 } 166 168 167 - public void setOrganizations(List<Organization> organizations) {169 + public void setOrganizations(Set<Organization> organizations) {168 170 this.organizations = organizations; 169 171 } 170 172 171 173 @JsonProperty("organizations_ids") 172 174 public void setOrgsIds(List<Integer> orgsIds) { 173 - organizations = new ArrayList<>();175 + organizations = new HashSet<>();174 176 for (Integer orgid : orgsIds) { 175 177 Organization o = new Organization(); 176 178 o.setId(orgid); .. .. @@ -189,6 +191,22 @@ 189 191 return ids; 190 192 } 191 193 194 + @JsonIgnore195 + 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 +192 210 static public class Rol { 193 211 static public final int ADVANCE = 0x01; 194 212 static public final int ADMIN = 0x02; securis/src/main/java/net/curisit/securis/services/SecurityInterceptor.java
.. .. @@ -3,6 +3,7 @@ 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,8 +61,27 @@ 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 hour80 + cache.set("orgs_" + username, userOrgs, 3600);81 + }82 + }83 +84 + return userOrgs;65 85 } 66 86 67 87 private int getUserRoles(String username) { .. .. @@ -79,6 +99,7 @@ 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(); securis/src/main/java/net/curisit/securis/services/UserResource.java
.. .. @@ -1,8 +1,9 @@ 1 1 package net.curisit.securis.services; 2 2 3 -import java.util.ArrayList;4 3 import java.util.Date; 4 +import java.util.HashSet;5 5 import java.util.List; 6 +import java.util.Set;6 7 7 8 import javax.inject.Inject; 8 9 import javax.inject.Provider; .. .. @@ -113,10 +114,10 @@ 113 114 return modify(user, user.getUsername(), token); 114 115 } 115 116 116 - List<Organization> orgs = null;117 + Set<Organization> orgs = null;117 118 List<Integer> orgsIds = user.getOrgsIds(); 118 119 if (orgsIds != null && orgsIds.size() > 0) { 119 - orgs = new ArrayList<>();120 + orgs = new HashSet<>();120 121 for (Integer orgId : orgsIds) { 121 122 Organization o = em.find(Organization.class, orgId); 122 123 if (o == null) { .. .. @@ -152,10 +153,10 @@ 152 153 return create(user, token); 153 154 } 154 155 155 - List<Organization> orgs = null;156 + Set<Organization> orgs = null;156 157 List<Integer> orgsIds = user.getOrgsIds(); 157 158 if (orgsIds != null && orgsIds.size() > 0) { 158 - orgs = new ArrayList<>();159 + orgs = new HashSet<>();159 160 for (Integer orgId : orgsIds) { 160 161 Organization o = em.find(Organization.class, orgId); 161 162 if (o == null) {