001: package ru.emdev.EmForge.security;
002:
003: import java.util.Date;
004:
005: import org.acegisecurity.GrantedAuthority;
006: import org.acegisecurity.userdetails.UserDetailsService;
007: import org.apache.commons.lang.StringUtils;
008:
009: import ru.emdev.EmForge.security.web.SiteRole;
010:
011: /**
012: * Simple POJO-Based implementation of EmForge User Details
013: */
014: public class EmForgeUserImpl implements EmForgeUserDetails {
015:
016: private static final long serialVersionUID = 1451145719985826674L;
017:
018: private String m_displayName;
019: private String m_userName;
020: private String m_password;
021: private String m_email;
022:
023: private String m_managerName;
024: private EmForgeUserDetails m_manager;
025:
026: private GrantedAuthority[] m_roles;
027: private boolean m_isEnabled;
028:
029: private UserDetailsService m_userDetailsService;
030:
031: private boolean m_isAnonymous;
032:
033: private String m_vcUserName;
034: private String m_vcPassword;
035:
036: private Date m_registeredAt;
037:
038: /**
039: * Initializes default values
040: */
041: public EmForgeUserImpl() {
042:
043: m_isAnonymous = false;
044: m_registeredAt = new Date();
045: }
046:
047: /**
048: * @param i_userName
049: * @param i_password
050: * @param i_displayName
051: * @param i_email
052: * @param i_managerName
053: * @param i_isEnabled
054: * @param i_roles
055: */
056: public EmForgeUserImpl(String i_userName, String i_password,
057: String i_displayName, String i_email, String i_managerName,
058: boolean i_isEnabled, GrantedAuthority[] i_roles) {
059:
060: this ();
061: m_userName = i_userName;
062: m_password = i_password;
063: m_displayName = i_displayName;
064: m_email = i_email;
065: m_managerName = i_managerName;
066: m_isEnabled = i_isEnabled;
067: m_roles = i_roles;
068: }
069:
070: /**
071: * @param i_displayName
072: */
073: public void setDisplayName(String i_displayName) {
074:
075: m_displayName = i_displayName;
076: }
077:
078: /**
079: * @see ru.emdev.EmForge.security.EmForgeUserDetails#getDisplayName()
080: */
081: public String getDisplayName() {
082:
083: return m_displayName;
084: }
085:
086: /**
087: * @param i_email
088: */
089: public void setEmail(String i_email) {
090:
091: m_email = i_email;
092: }
093:
094: /**
095: * @see ru.emdev.EmForge.security.EmForgeUserDetails#getEmail()
096: */
097: public String getEmail() {
098:
099: return m_email;
100: }
101:
102: /**
103: * @param i_manager
104: */
105: public void setManager(EmForgeUserDetails i_manager) {
106:
107: m_manager = i_manager;
108: }
109:
110: /**
111: * @see ru.emdev.EmForge.security.EmForgeUserDetails#getManager()
112: */
113: public EmForgeUserDetails getManager() {
114:
115: if (m_manager == null && m_managerName != null
116: && m_userDetailsService != null) {
117: m_manager = (EmForgeUserDetails) m_userDetailsService
118: .loadUserByUsername(m_managerName);
119: }
120: return m_manager;
121: }
122:
123: /**
124: * @see ru.emdev.EmForge.security.EmForgeUserDetails#isAnonymous()
125: */
126: public boolean isAnonymous() {
127:
128: return m_isAnonymous;
129: }
130:
131: /**
132: * @param anonymousFlag
133: */
134: public void setAnonymous(boolean anonymousFlag) {
135:
136: m_isAnonymous = anonymousFlag;
137: }
138:
139: /**
140: * @see org.acegisecurity.userdetails.UserDetails#getAuthorities()
141: */
142: public GrantedAuthority[] getAuthorities() {
143:
144: return m_roles;
145: }
146:
147: /**
148: * @param i_password
149: */
150: public void setPassword(String i_password) {
151:
152: m_password = i_password;
153: }
154:
155: /**
156: * @see org.acegisecurity.userdetails.UserDetails#getPassword()
157: */
158: public String getPassword() {
159:
160: return m_password;
161: }
162:
163: /**
164: * @param i_userName
165: */
166: public void setUsername(String i_userName) {
167:
168: m_userName = i_userName;
169: }
170:
171: /**
172: * @see org.acegisecurity.userdetails.UserDetails#getUsername()
173: */
174: public String getUsername() {
175:
176: return m_userName;
177: }
178:
179: /**
180: * @see org.acegisecurity.userdetails.UserDetails#isAccountNonExpired()
181: */
182: public boolean isAccountNonExpired() {
183:
184: return true;
185: }
186:
187: /**
188: * @see org.acegisecurity.userdetails.UserDetails#isAccountNonLocked()
189: */
190: public boolean isAccountNonLocked() {
191:
192: return true;
193: }
194:
195: /**
196: * @see org.acegisecurity.userdetails.UserDetails#isCredentialsNonExpired()
197: */
198: public boolean isCredentialsNonExpired() {
199:
200: return true;
201: }
202:
203: /**
204: * @see org.acegisecurity.userdetails.UserDetails#isEnabled()
205: */
206: public boolean isEnabled() {
207:
208: return m_isEnabled;
209: }
210:
211: /**
212: * @param userDetailsService
213: */
214: public void setUserDetailsService(
215: UserDetailsService userDetailsService) {
216:
217: m_userDetailsService = userDetailsService;
218: }
219:
220: /**
221: * @see ru.emdev.EmForge.security.EmForgeUserDetails#getVcUserName()
222: */
223: public String getVcUserName() {
224:
225: if (m_vcUserName != null) {
226: return m_vcUserName;
227: }
228:
229: return m_userName;
230: }
231:
232: /**
233: * @param i_vcUserName
234: */
235: public void setVcUserName(String i_vcUserName) {
236:
237: m_vcUserName = i_vcUserName;
238: }
239:
240: /**
241: * @see ru.emdev.EmForge.security.EmForgeUserDetails#getVcPassword()
242: */
243: public String getVcPassword() {
244:
245: if (m_vcPassword != null) {
246: return m_vcPassword;
247: }
248:
249: return m_password;
250: }
251:
252: /**
253: * @param i_vcPassword
254: */
255: public void setVcPassword(String i_vcPassword) {
256:
257: m_vcPassword = i_vcPassword;
258: }
259:
260: /**
261: * @see ru.emdev.EmForge.security.EmForgeUserDetails#hasRole(java.lang.String)
262: */
263: public boolean hasRole(String i_role) {
264:
265: for (GrantedAuthority authority : m_roles) {
266: if (authority.getAuthority().equals(i_role)) {
267: return true;
268: }
269: }
270:
271: return false;
272: }
273:
274: /**
275: * @return <code>True</code> if the user is an admin/writer, otherwise <code>False</code>
276: */
277: public boolean isWriter() {
278:
279: return hasRole(SiteRole.ADMIN.getId())
280: || hasRole(SiteRole.WRITER.getId());
281: }
282:
283: /**
284: * @return User registration date
285: */
286: public Date getRegisteredAt() {
287:
288: return m_registeredAt;
289: }
290:
291: /**
292: * @see java.lang.Object#equals(java.lang.Object)
293: */
294: @Override
295: public boolean equals(Object i_obj) {
296:
297: boolean result = false;
298:
299: if (i_obj != null) {
300: if (i_obj == this ) {
301: result = true;
302:
303: } else if (i_obj instanceof EmForgeUserDetails) {
304: result = StringUtils.equals(
305: ((EmForgeUserDetails) i_obj).getUsername(),
306: getUsername());
307: }
308: }
309:
310: return result;
311: }
312: }
|