1 16 17 package info.jtrac.domain; 18 19 import java.io.Serializable ; 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Collections ; 23 import java.util.HashSet ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.Set ; 27 28 import org.acegisecurity.GrantedAuthority; 29 import org.acegisecurity.userdetails.UserDetails; 30 31 42 public class User implements UserDetails, Serializable { 43 44 private long id; 45 private Integer type; 46 private User parent; 47 private String loginName; 48 private String name; 49 private String password; 50 private String email; 51 private Metadata metadata; 52 private String locale; 53 private boolean locked; 54 private Set <UserSpaceRole> userSpaceRoles = new HashSet <UserSpaceRole>(); 55 private Map <Long , Boolean > spacesWhereAbleToCreateNewItem; 56 57 59 public void addSpaceWithRole(Space space, String roleKey) { 60 userSpaceRoles.add(new UserSpaceRole(this, space, roleKey)); 61 } 62 63 public void removeSpaceWithRole(Space space, String roleKey) { 64 userSpaceRoles.remove(new UserSpaceRole(this, space, roleKey)); 65 } 66 67 private List <String > getRoleKeys(Space space) { 68 List <String > roleKeys = new ArrayList <String >(); 69 for(UserSpaceRole usr : userSpaceRoles) { 70 Space s = usr.getSpace(); 71 if (s == space || ( s != null && s.equals(space))) { 72 roleKeys.add(usr.getRoleKey()); 73 } 74 } 75 return roleKeys; 76 } 77 78 public Map <Integer , String > getPermittedTransitions(Space space, int status) { 79 return space.getMetadata().getPermittedTransitions(getRoleKeys(space), status); 80 } 81 82 public List <Field> getEditableFieldList(Space space, int status) { 83 return space.getMetadata().getEditableFields(getRoleKeys(space), Collections.singletonList(status)); 84 } 85 86 public Set <Space> getSpaces() { 87 Set <Space> spaces = new HashSet <Space>(userSpaceRoles.size()); 88 for (UserSpaceRole usr : userSpaceRoles) { 89 if (usr.getSpace() != null) { 90 spaces.add(usr.getSpace()); 91 } 92 } 93 return spaces; 94 } 95 96 public int getSpaceCount() { 97 return getSpaces().size(); 98 } 99 100 public boolean isAdminForAllSpaces() { 101 return getRoleKeys(null).contains("ROLE_ADMIN"); 102 } 103 104 108 public List <UserSpaceRole> getSpaceRoles() { 109 List <UserSpaceRole> list = new ArrayList <UserSpaceRole>(userSpaceRoles.size()); 110 for(UserSpaceRole usr : userSpaceRoles) { 111 if(usr.getSpace() != null && usr.getRoleKey() != "ROLE_ADMIN") { 112 list.add(usr); 113 } 114 } 115 return list; 116 } 117 118 120 public boolean isAccountNonExpired() { 121 return true; 122 } 123 124 public boolean isAccountNonLocked() { 125 return !isLocked(); 126 } 127 128 public GrantedAuthority[] getAuthorities() { 129 Collection <GrantedAuthority> authorities = new ArrayList <GrantedAuthority>(); 130 if (id > 0) { 132 authorities.add(new UserSpaceRole(this, null, "ROLE_USER")); 133 } 134 for (UserSpaceRole usr : userSpaceRoles) { 135 authorities.add(usr); 136 } 137 return authorities.toArray(new GrantedAuthority[authorities.size()]); 138 } 139 140 public boolean isCredentialsNonExpired() { 141 return true; 142 } 143 144 public boolean isEnabled() { 145 return true; 146 } 147 148 public String getUsername() { 149 return getLoginName(); 150 } 151 152 public String getPassword() { 153 return password; 154 } 155 156 158 public Map <Long , Boolean > getSpacesWhereAbleToCreateNewItem() { 159 return spacesWhereAbleToCreateNewItem; 160 } 161 162 public void setSpacesWhereAbleToCreateNewItem(Map <Long , Boolean > spacesWhereAbleToCreateNewItem) { 163 this.spacesWhereAbleToCreateNewItem = spacesWhereAbleToCreateNewItem; 164 } 165 166 public Set <UserSpaceRole> getUserSpaceRoles() { 167 return userSpaceRoles; 168 } 169 170 public void setUserSpaceRoles(Set <UserSpaceRole> userSpaceRoles) { 171 this.userSpaceRoles = userSpaceRoles; 172 } 173 174 public User getParent() { 175 return parent; 176 } 177 178 public void setParent(User parent) { 179 this.parent = parent; 180 } 181 182 public String getName() { 183 return name; 184 } 185 186 public void setName(String name) { 187 this.name = name; 188 } 189 190 public void setPassword(String password) { 191 this.password = password; 192 } 193 194 public String getEmail() { 195 return email; 196 } 197 198 public void setEmail(String email) { 199 this.email = email; 200 } 201 202 public String getLocale() { 203 return locale; 204 } 205 206 public void setLocale(String locale) { 207 this.locale = locale; 208 } 209 210 public boolean isLocked() { 211 return locked; 212 } 213 214 public void setLocked(boolean locked) { 215 this.locked = locked; 216 } 217 218 public Metadata getMetadata() { 219 return metadata; 220 } 221 222 public void setMetadata(Metadata metadata) { 223 this.metadata = metadata; 224 } 225 226 public long getId() { 227 return id; 228 } 229 230 public void setId(long id) { 231 this.id = id; 232 } 233 234 public Integer getType() { 235 return type; 236 } 237 238 public void setType(Integer type) { 239 this.type = type; 240 } 241 242 public String getLoginName() { 243 return loginName; 244 } 245 246 public void setLoginName(String loginName) { 247 this.loginName = loginName; 248 } 249 250 @Override 251 public String toString() { 252 StringBuffer sb = new StringBuffer (); 253 sb.append("id [").append(id); 254 sb.append("]; loginName [").append(loginName); 255 sb.append("]"); 256 return sb.toString(); 257 } 258 259 @Override 260 public boolean equals(Object o) { 261 if (this == o) { 262 return true; 263 } 264 if (!(o instanceof User)) { 265 return false; 266 } 267 final User u = (User) o; 268 return u.getLoginName().equals(loginName); 269 } 270 271 @Override 272 public int hashCode() { 273 return loginName.hashCode(); 274 } 275 276 } 277 | Popular Tags |