001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package info.jtrac.domain;
018:
019: import java.io.Serializable;
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.HashSet;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.Set;
026: import java.util.TreeMap;
027:
028: import org.acegisecurity.GrantedAuthority;
029: import org.acegisecurity.userdetails.UserDetails;
030:
031: /**
032: * Standard User entity with attributes such as name, password etc.
033: * The parent relationship is used for easy grouping of users and
034: * flexible inheritance of permission schemes TODO. The user type
035: * determines if this is a normal user or a user group. Only
036: * user groups can have child references.
037: *
038: * We also tie in to the Acegi security framework and implement
039: * the Acegi UserDetails interface so that Acegi can take care
040: * of Authentication and Authorization
041: */
042: public class User implements UserDetails, Serializable {
043:
044: public static final int SEARCH_NAME = 0;
045: public static final int SEARCH_LOGIN_NAME = 1;
046: public static final int SEARCH_EMAIL = 2;
047:
048: private long id;
049: private Integer type;
050: private User parent;
051: private String loginName;
052: private String name;
053: private String password;
054: private String email;
055: private Metadata metadata;
056: private String locale;
057: private boolean locked;
058: private Set<UserSpaceRole> userSpaceRoles = new HashSet<UserSpaceRole>();
059:
060: //=============================================================
061:
062: public void addSpaceWithRole(Space space, String roleKey) {
063: userSpaceRoles.add(new UserSpaceRole(this , space, roleKey));
064: }
065:
066: public void removeSpaceWithRole(Space space, String roleKey) {
067: userSpaceRoles.remove(new UserSpaceRole(this , space, roleKey));
068: }
069:
070: /**
071: * when the passed space is null this has a special significance
072: * it will return roles that are 'global'
073: */
074: public List<String> getRoleKeys(Space space) {
075: List<String> roleKeys = new ArrayList<String>();
076: for (UserSpaceRole usr : userSpaceRoles) {
077: Space s = usr.getSpace();
078: if (s == space || (s != null && s.equals(space))) {
079: roleKeys.add(usr.getRoleKey());
080: }
081: }
082: return roleKeys;
083: }
084:
085: public Map<Integer, String> getPermittedTransitions(Space space,
086: int status) {
087: return space.getMetadata().getPermittedTransitions(
088: getRoleKeys(space), status);
089: }
090:
091: public List<Field> getEditableFieldList(Space space, int status) {
092: return space.getMetadata().getEditableFields(
093: getRoleKeys(space), status);
094: }
095:
096: public Set<Space> getSpaces() {
097: Set<Space> spaces = new HashSet<Space>(userSpaceRoles.size());
098: for (UserSpaceRole usr : userSpaceRoles) {
099: if (usr.getSpace() != null) {
100: spaces.add(usr.getSpace());
101: }
102: }
103: return spaces;
104: }
105:
106: public int getSpaceCount() {
107: return getSpaces().size();
108: }
109:
110: public boolean isAdminForAllSpaces() {
111: return getRoleKeys(null).contains("ROLE_ADMIN");
112: }
113:
114: /**
115: * this returns 'valid' spaceRoles, where space not null and role not ROLE_ADMIN
116: * also sort by Space name for showing on the dashboard
117: * TODO multiple roles per space
118: */
119: public Collection<UserSpaceRole> getSpaceRoles() {
120: Map<String, UserSpaceRole> map = new TreeMap<String, UserSpaceRole>();
121: for (UserSpaceRole usr : userSpaceRoles) {
122: if (usr.getSpace() != null
123: && !usr.getRoleKey().equals("ROLE_ADMIN")) {
124: map.put(usr.getSpace().getName(), usr);
125: }
126: }
127: return map.values();
128: }
129:
130: public boolean isGuestForSpace(Space space) {
131: if (id == 0) {
132: return true;
133: }
134: for (UserSpaceRole usr : getUserSpaceRolesBySpaceId(space
135: .getId())) {
136: if (usr.getRoleKey().equals("ROLE_GUEST")) {
137: return true;
138: }
139: }
140: return false;
141: }
142:
143: private Collection<UserSpaceRole> getUserSpaceRolesBySpaceId(
144: long spaceId) {
145: List<UserSpaceRole> list = new ArrayList<UserSpaceRole>();
146: for (UserSpaceRole usr : userSpaceRoles) {
147: if (usr.getSpace() != null
148: && usr.getSpace().getId() == spaceId) {
149: list.add(usr);
150: }
151: }
152: return list;
153: }
154:
155: //============ ACEGI UserDetails implementation ===============
156:
157: public boolean isAccountNonExpired() {
158: return true;
159: }
160:
161: public boolean isAccountNonLocked() {
162: return !isLocked();
163: }
164:
165: public GrantedAuthority[] getAuthorities() {
166: Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
167: // grant full access only if not a Guest
168: if (id > 0) {
169: authorities.add(new UserSpaceRole(this , null, "ROLE_USER"));
170: }
171: for (UserSpaceRole usr : userSpaceRoles) {
172: authorities.add(usr);
173: }
174: return authorities.toArray(new GrantedAuthority[authorities
175: .size()]);
176: }
177:
178: public boolean isCredentialsNonExpired() {
179: return true;
180: }
181:
182: public boolean isEnabled() {
183: return true;
184: }
185:
186: public String getUsername() {
187: return getLoginName();
188: }
189:
190: public String getPassword() {
191: return password;
192: }
193:
194: //=============================================================
195:
196: public Set<UserSpaceRole> getUserSpaceRoles() {
197: return userSpaceRoles;
198: }
199:
200: public void setUserSpaceRoles(Set<UserSpaceRole> userSpaceRoles) {
201: this .userSpaceRoles = userSpaceRoles;
202: }
203:
204: public User getParent() {
205: return parent;
206: }
207:
208: public void setParent(User parent) {
209: this .parent = parent;
210: }
211:
212: public String getName() {
213: return name;
214: }
215:
216: public void setName(String name) {
217: this .name = name;
218: }
219:
220: public void setPassword(String password) {
221: this .password = password;
222: }
223:
224: public String getEmail() {
225: return email;
226: }
227:
228: public void setEmail(String email) {
229: this .email = email;
230: }
231:
232: public String getLocale() {
233: return locale;
234: }
235:
236: public void setLocale(String locale) {
237: this .locale = locale;
238: }
239:
240: public boolean isLocked() {
241: return locked;
242: }
243:
244: public void setLocked(boolean locked) {
245: this .locked = locked;
246: }
247:
248: public Metadata getMetadata() {
249: return metadata;
250: }
251:
252: public void setMetadata(Metadata metadata) {
253: this .metadata = metadata;
254: }
255:
256: public long getId() {
257: return id;
258: }
259:
260: public void setId(long id) {
261: this .id = id;
262: }
263:
264: public Integer getType() {
265: return type;
266: }
267:
268: public void setType(Integer type) {
269: this .type = type;
270: }
271:
272: public String getLoginName() {
273: return loginName;
274: }
275:
276: public void setLoginName(String loginName) {
277: this .loginName = loginName;
278: }
279:
280: @Override
281: public String toString() {
282: StringBuffer sb = new StringBuffer();
283: sb.append("id [").append(id);
284: sb.append("]; loginName [").append(loginName);
285: sb.append("]");
286: return sb.toString();
287: }
288:
289: @Override
290: public boolean equals(Object o) {
291: if (this == o) {
292: return true;
293: }
294: if (!(o instanceof User)) {
295: return false;
296: }
297: final User u = (User) o;
298: return u.getLoginName().equals(loginName);
299: }
300:
301: @Override
302: public int hashCode() {
303: if (loginName == null) {
304: return 0;
305: }
306: return loginName.hashCode();
307: }
308:
309: }
|