001: package ru.emdev.EmForge.security.dao;
002:
003: import java.util.Date;
004: import java.util.HashSet;
005: import java.util.Set;
006:
007: import org.acegisecurity.GrantedAuthority;
008: import org.acegisecurity.GrantedAuthorityImpl;
009: import org.apache.commons.lang.StringUtils;
010:
011: import ru.emdev.EmForge.security.EmForgeUserDetails;
012:
013: /**
014: * Data-Object for storing user information in database
015: *
016: * @author akakunin
017: */
018: public class User implements EmForgeUserDetails {
019:
020: private static final long serialVersionUID = 6400004223098708678L;
021:
022: /** Database Part */
023: private Long userId;
024:
025: private String username;
026: private String password;
027: private String firstName;
028: private String lastName;
029:
030: private User manager;
031: private Set<Role> roles;
032:
033: /** UserDetails Part */
034: private String email;
035: private String vcUserName;
036: private String vcPassword;
037:
038: private Boolean isActive;
039: private String activationCode;
040: private Date registeredAt;
041:
042: /**
043: * @see ru.emdev.EmForge.security.EmForgeUserDetails#getEmail()
044: */
045: public String getEmail() {
046:
047: return email;
048: }
049:
050: /**
051: * @param email
052: */
053: public void setEmail(String email) {
054:
055: this .email = email;
056: }
057:
058: /**
059: * @return
060: */
061: public String getFirstName() {
062:
063: return firstName;
064: }
065:
066: /**
067: * @param firstName
068: */
069: public void setFirstName(String firstName) {
070:
071: this .firstName = firstName;
072: }
073:
074: /**
075: * @return
076: */
077: public String getLastName() {
078:
079: return lastName;
080: }
081:
082: /**
083: * @param lastName
084: */
085: public void setLastName(String lastName) {
086:
087: this .lastName = lastName;
088: }
089:
090: /**
091: * @return
092: */
093: public Long getId() {
094:
095: return userId;
096: }
097:
098: /**
099: * @param userId
100: */
101: public void setId(Long userId) {
102:
103: this .userId = userId;
104: }
105:
106: /**
107: * @see org.acegisecurity.userdetails.UserDetails#getPassword()
108: */
109: public String getPassword() {
110:
111: return password;
112: }
113:
114: /**
115: * @param password
116: */
117: public void setPassword(String password) {
118:
119: this .password = password;
120: }
121:
122: /**
123: * @return
124: */
125: public Set<Role> getRoles() {
126:
127: return roles;
128: }
129:
130: /**
131: * @param roles
132: */
133: public void setRoles(Set<Role> roles) {
134:
135: this .roles = roles;
136: }
137:
138: /**
139: * @param i_role
140: */
141: public void addRole(Role i_role) {
142:
143: if (roles == null) {
144: roles = new HashSet<Role>();
145: }
146:
147: roles.add(i_role);
148: }
149:
150: /**
151: * @see ru.emdev.EmForge.security.EmForgeUserDetails#getDisplayName()
152: */
153: public String getDisplayName() {
154:
155: if (StringUtils.isEmpty(firstName)
156: && StringUtils.isEmpty(lastName)) {
157: return getUsername();
158: }
159:
160: String displayName = firstName;
161:
162: if (!StringUtils.isEmpty(firstName)
163: && !StringUtils.isEmpty(lastName)) {
164: displayName += " ";
165: }
166: displayName += lastName;
167:
168: return displayName;
169: }
170:
171: /**
172: * @see ru.emdev.EmForge.security.EmForgeUserDetails#getVcPassword()
173: */
174: public String getVcPassword() {
175:
176: return vcPassword;
177: }
178:
179: /**
180: * @param i_vcPassword
181: */
182: public void setVcPassword(String i_vcPassword) {
183:
184: vcPassword = i_vcPassword;
185: }
186:
187: /**
188: * @see ru.emdev.EmForge.security.EmForgeUserDetails#getVcUserName()
189: */
190: public String getVcUserName() {
191:
192: if (vcUserName != null) {
193: return vcUserName;
194: } else {
195: return username;
196: }
197: }
198:
199: /**
200: * @param i_vcUser
201: */
202: public void setVcUserName(String i_vcUser) {
203:
204: vcUserName = i_vcUser;
205: }
206:
207: /**
208: * @return
209: */
210: public Boolean isActive() {
211:
212: return isActive;
213: }
214:
215: /**
216: * @param active
217: */
218: public void setActive(Boolean active) {
219:
220: isActive = active;
221: }
222:
223: /**
224: * @return
225: */
226: public String getActivationCode() {
227:
228: return activationCode;
229: }
230:
231: /**
232: * @param activationCode
233: */
234: public void setActivationCode(String activationCode) {
235:
236: this .activationCode = activationCode;
237: }
238:
239: /**
240: * @return User registration date
241: */
242: public Date getRegisteredAt() {
243:
244: return registeredAt;
245: }
246:
247: /**
248: * @param i_registeredAt User registration date
249: */
250: public void setRegisteredAt(Date i_registeredAt) {
251:
252: registeredAt = i_registeredAt;
253: }
254:
255: /**
256: * @see ru.emdev.EmForge.security.EmForgeUserDetails#isAnonymous()
257: */
258: public boolean isAnonymous() {
259:
260: // TODO Auto-generated method stub
261: return false;
262: }
263:
264: /**
265: * @see org.acegisecurity.userdetails.UserDetails#getAuthorities()
266: */
267: public GrantedAuthority[] getAuthorities() {
268:
269: Set<GrantedAuthority> result = new HashSet<GrantedAuthority>();
270:
271: if (roles != null) {
272: for (Role role : roles) {
273: // we return here only site-wide roles
274: if (role.getRoleType().equals(Role.ROLE_TYPE_SITE)) {
275: result
276: .add(new GrantedAuthorityImpl(role
277: .getName()));
278: }
279: }
280: }
281:
282: return (GrantedAuthority[]) result
283: .toArray(new GrantedAuthority[result.size()]);
284: }
285:
286: /**
287: * @see org.acegisecurity.userdetails.UserDetails#getUsername()
288: */
289: public String getUsername() {
290:
291: return username;
292: }
293:
294: /**
295: * @param i_userName
296: */
297: public void setUsername(String i_userName) {
298:
299: username = i_userName;
300: }
301:
302: /**
303: * @see org.acegisecurity.userdetails.UserDetails#isAccountNonExpired()
304: */
305: public boolean isAccountNonExpired() {
306:
307: return true;
308: }
309:
310: /**
311: * @see org.acegisecurity.userdetails.UserDetails#isAccountNonLocked()
312: */
313: public boolean isAccountNonLocked() {
314:
315: return true;
316: }
317:
318: /**
319: * @see org.acegisecurity.userdetails.UserDetails#isCredentialsNonExpired()
320: */
321: public boolean isCredentialsNonExpired() {
322:
323: return true;
324: }
325:
326: /**
327: * @see org.acegisecurity.userdetails.UserDetails#isEnabled()
328: */
329: public boolean isEnabled() {
330:
331: return isActive != null && isActive;
332: }
333:
334: /**
335: * @see ru.emdev.EmForge.security.EmForgeUserDetails#getManager()
336: */
337: public User getManager() {
338:
339: return manager;
340: }
341:
342: /**
343: * @param manager
344: */
345: public void setManager(User manager) {
346:
347: this .manager = manager;
348: }
349:
350: /**
351: * @see ru.emdev.EmForge.security.EmForgeUserDetails#hasRole(java.lang.String)
352: */
353: public boolean hasRole(String i_role) {
354:
355: if (roles != null) {
356: for (Role role : roles) {
357: // we return here only site-wide roles
358: if (role.getName().equals(i_role)) {
359: return true;
360: }
361: }
362: }
363:
364: return false;
365: }
366:
367: /**
368: * @see java.lang.Object#equals(java.lang.Object)
369: */
370: @Override
371: public boolean equals(Object i_obj) {
372:
373: boolean result = false;
374:
375: if (i_obj != null) {
376: if (i_obj == this ) {
377: result = true;
378:
379: } else if (i_obj instanceof EmForgeUserDetails) {
380: result = StringUtils.equals(
381: ((EmForgeUserDetails) i_obj).getUsername(),
382: getUsername());
383: }
384: }
385:
386: return result;
387: }
388:
389: }
|