KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mvnforum > auth > OnlineUserFactoryImpl


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/auth/OnlineUserFactoryImpl.java,v 1.30 2006/04/14 17:05:26 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.30 $
5  * $Date: 2006/04/14 17:05:26 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding mvnForum MUST remain
12  * intact in the scripts and in the outputted HTML.
13  * The "powered by" text/logo with a link back to
14  * http://www.mvnForum.com and http://www.MyVietnam.net in
15  * the footer of the pages MUST remain visible when the pages
16  * are viewed on the internet or intranet.
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31  *
32  * Support can be obtained from support forums at:
33  * http://www.mvnForum.com/mvnforum/index
34  *
35  * Correspondence and Marketing Questions can be sent to:
36  * info at MyVietnam net
37  *
38  * @author: Minh Nguyen
39  * @author: Mai Nguyen
40  */

41 package com.mvnforum.auth;
42
43 import java.sql.Timestamp JavaDoc;
44
45 import javax.servlet.http.HttpServletRequest JavaDoc;
46 import javax.servlet.http.HttpServletResponse JavaDoc;
47
48 import com.mvnforum.*;
49 import com.mvnforum.db.*;
50 import net.myvietnam.mvncore.exception.*;
51 import net.myvietnam.mvncore.security.Encoder;
52 import net.myvietnam.mvncore.util.DateUtil;
53 import net.myvietnam.mvncore.web.GenericRequest;
54 import net.myvietnam.mvncore.web.GenericResponse;
55 import net.myvietnam.mvncore.web.impl.GenericRequestServletImpl;
56 import org.apache.commons.logging.Log;
57 import org.apache.commons.logging.LogFactory;
58
59 public class OnlineUserFactoryImpl implements OnlineUserFactory {
60
61     private static Log log = LogFactory.getLog(OnlineUserFactoryImpl.class);
62
63     public OnlineUserFactoryImpl() {
64     }
65
66     public OnlineUser getAuthenticatedUser(HttpServletRequest JavaDoc request,
67                                            HttpServletResponse JavaDoc response,
68                                            String JavaDoc loginName, String JavaDoc password,
69                                            boolean isEncodedPassword)
70         throws AuthenticationException, DatabaseException, AssertionException {
71
72         GenericRequest req = new GenericRequestServletImpl(request);
73
74         return getAuthenticatedUser(req, null, loginName, password, isEncodedPassword);
75     }
76
77     public OnlineUser getAuthenticatedUser(GenericRequest request,
78                                            GenericResponse response,
79                                            String JavaDoc loginName, String JavaDoc password,
80                                            boolean isEncodedPassword)
81         throws AuthenticationException, DatabaseException, AssertionException {
82
83         int memberID = 0;
84         double timeZone = 0;
85         boolean invisible = false;
86         String JavaDoc localeName = "";
87         Timestamp JavaDoc lastLogon = null;
88         String JavaDoc lastLogonIP = null;
89         int postsPerPage = 10;
90
91         try {
92             memberID = DAOFactory.getMemberDAO().getMemberIDFromMemberName(loginName);
93         } catch (ObjectNotFoundException e) {
94             throw new AuthenticationException(NotLoginException.WRONG_NAME);
95         } catch (Exception JavaDoc e) {
96             log.error("Unexpected error validating user", e);
97             /** @todo find a beter one than NotLoginException.NOT_LOGIN */
98             throw new AuthenticationException(NotLoginException.NOT_LOGIN);
99         }
100
101         try {
102             MemberBean memberBean = DAOFactory.getMemberDAO().getMember_forViewCurrentMember(memberID);
103
104             if (memberBean.getMemberStatus() != MemberBean.MEMBER_STATUS_ENABLE) {
105                 if (memberID != MVNForumConstant.MEMBER_ID_OF_ADMIN) {// Admin cannot be disabled
106
throw new AuthenticationException(NotLoginException.ACCOUNT_DISABLED);
107                 }
108             }
109             boolean disablePortlet = !MVNForumConfig.getEnablePortlet();
110             if (disablePortlet) {
111             
112                 if (DAOFactory.getMemberDAO().getActivateCode(memberID).equals(MemberBean.MEMBER_ACTIVATECODE_ACTIVATED) == false) {
113                     // not activated
114
if (MVNForumConfig.getRequireActivation()) {
115                         if (memberID != MVNForumConstant.MEMBER_ID_OF_ADMIN) {// Admin dont have to activate to login
116
throw new AuthenticationException(NotLoginException.NOT_ACTIVATED);
117                         }
118                     }
119                 }
120     
121                 if (validatePassword(loginName, password, isEncodedPassword) == false) {
122                     if ((MVNForumConfig.getEnablePasswordlessAuth() == false) || (password.length() > 0)) {
123                         throw new AuthenticationException(NotLoginException.WRONG_PASSWORD);
124                     }
125                 }
126     
127                 // now we have checked the authentication, then we update the lastlogon date
128
Timestamp JavaDoc now = DateUtil.getCurrentGMTTimestamp();
129     
130                 DAOFactory.getMemberDAO().updateLastLogon(memberID, now, request.getRemoteAddr());
131             }
132             timeZone = memberBean.getMemberTimeZone();
133             localeName = memberBean.getMemberLanguage();
134             lastLogon = memberBean.getMemberLastLogon();
135             postsPerPage = memberBean.getMemberPostsPerPage();
136             lastLogonIP = memberBean.getMemberLastIP();
137             invisible = memberBean.isInvisible();
138
139             // next, get the correct name from database
140
// Eg: if in database the MemberName is "Admin", and user enter "admin"
141
// We will convert "admin" to "Admin"
142
String JavaDoc memberName = memberBean.getMemberName();
143
144             OnlineUserImpl authenticatedUser = new OnlineUserImpl(request, false/*isGuest*/);
145             authenticatedUser.setMemberID(memberID);
146             authenticatedUser.setMemberName(memberName);
147             authenticatedUser.setInvisible(invisible);
148             authenticatedUser.setTimeZone(timeZone);
149             //NOTE: This MUST be the only way to get permission for a member,
150
// so we prevent getPermission for one user and set for other user
151
// Note: this method might throw AssertionException
152
MVNForumPermission permission = MVNForumPermissionFactory.getAuthenticatedPermission(memberID);
153             authenticatedUser.setPermission(permission);
154             authenticatedUser.setLocaleName(localeName);
155             authenticatedUser.setLastLogonTimestamp(lastLogon);
156             authenticatedUser.setLastLogonIP(lastLogonIP);
157             authenticatedUser.setGender(memberBean.getMemberGender() != 0);
158             authenticatedUser.setPostsPerPage(postsPerPage);
159
160             if (MVNForumConfig.getEnableCompany()) {
161                 try {
162                     int companyID = DAOFactory.getMemberCompanyDAO().getCompanyIDFromMemberID(memberID);
163                     CompanyBean companyBean = DAOFactory.getCompanyDAO().getCompany(companyID);
164
165                     // Load the css Path for this user
166
String JavaDoc cssPath = MyUtil.getCompanyCssPath(companyBean, request.getContextPath());
167                     authenticatedUser.setCssPath(cssPath);
168
169                     // Load the logo Path for this user
170
String JavaDoc logoPath = MyUtil.getCompanyLogoPath(companyBean, request.getContextPath());
171                     authenticatedUser.setLogoPath(logoPath);
172                 } catch (ObjectNotFoundException ex) {
173                     // not belong to a company, just ignore
174
}
175             }
176
177             return authenticatedUser;
178         } catch (ObjectNotFoundException e) {
179             throw new AuthenticationException(NotLoginException.WRONG_NAME);//we dont want this line to happen
180
} catch (DatabaseException e) {
181             log.error("Unexpected error validating user", e);
182             throw new AuthenticationException(NotLoginException.NOT_LOGIN);//we dont want this line to happen
183
}
184     }
185
186     public OnlineUser getAnonymousUser(HttpServletRequest JavaDoc req)
187         throws DatabaseException, AssertionException {
188
189         GenericRequest request = new GenericRequestServletImpl(req);
190         return this.getAnonymousUser(request);
191     }
192
193     public OnlineUser getAnonymousUser(GenericRequest request)
194         throws DatabaseException, AssertionException {
195
196         int memberID = MVNForumConstant.MEMBER_ID_OF_GUEST;
197         String JavaDoc memberName = MVNForumConfig.getDefaultGuestName();
198         double timeZone = MVNForumConfig.getDefaultGuestTimeZone();
199         String JavaDoc localeName = "";
200         Timestamp JavaDoc lastLogon = null;
201         String JavaDoc lastLogonIP = null;
202         int postsPerPage = MVNForumConfig.getRowsPerPage();
203
204         try {
205             MemberBean memberBean = DAOFactory.getMemberDAO().getMember_forViewCurrentMember(memberID);
206             if (memberBean.getMemberStatus() != MemberBean.MEMBER_STATUS_ENABLE) {
207                 //@todo: for now, Guest is always enabled
208
}
209             memberName = memberBean.getMemberName();
210             timeZone = memberBean.getMemberTimeZone();
211             localeName = memberBean.getMemberLanguage();
212             lastLogon = memberBean.getMemberLastLogon();
213             postsPerPage = memberBean.getMemberPostsPerPage();
214             lastLogonIP = memberBean.getMemberLastIP();
215
216             //@todo: Should we update LastLogon? I think we should, so we know when we had last guest visiting the site.
217
Timestamp JavaDoc now = DateUtil.getCurrentGMTTimestamp();
218             //@todo should we remember these information for the Guest
219
DAOFactory.getMemberDAO().updateLastLogon(memberID, now, request.getRemoteAddr());
220
221             OnlineUserImpl anonymousUser = new OnlineUserImpl(request, true/*isGuest*/);
222             anonymousUser.setMemberID(memberID);
223             anonymousUser.setMemberName(memberName);
224             anonymousUser.setTimeZone(timeZone);
225             MVNForumPermission permission = MVNForumPermissionFactory.getAnonymousPermission();
226             anonymousUser.setPermission(permission);
227             anonymousUser.setLocaleName(localeName);
228             anonymousUser.setLastLogonTimestamp(lastLogon);
229             anonymousUser.setLastLogonIP(lastLogonIP);
230             //no gender; anonymousUser.setGender(memberBean.getMemberGender() != 0);
231
anonymousUser.setPostsPerPage(postsPerPage);
232             return anonymousUser;
233         } catch (ObjectNotFoundException e) {
234             OnlineUserImpl anonymousUser = new OnlineUserImpl(request, true/*isGuest*/);
235             //anonymousUser.setMemberID(MVNForumConstant.MEMBER_ID_OF_GUEST);
236
//anonymousUser.setMemberName(MVNForumConfig.getDefaultGuestName());
237
MVNForumPermission permission = MVNForumPermissionFactory.getAnonymousPermission();
238             anonymousUser.setPermission(permission);
239             anonymousUser.setLocaleName("");
240             anonymousUser.setLastLogonTimestamp(new Timestamp JavaDoc(0));
241             anonymousUser.setPostsPerPage(postsPerPage);
242             anonymousUser.setTimeZone(timeZone);
243             return anonymousUser;
244         } catch (DatabaseException e) {
245             OnlineUserImpl anonymousUser = new OnlineUserImpl(request, true/*isGuest*/);
246             //anonymousUser.setMemberID(MVNForumConstant.MEMBER_ID_OF_GUEST);
247
//anonymousUser.setMemberName(MVNForumConfig.getDefaultGuestName());
248
MVNForumPermission permission = MVNForumPermissionFactory.getAnonymousPermission();
249             anonymousUser.setPermission(permission);
250             anonymousUser.setLocaleName("");
251             anonymousUser.setLastLogonTimestamp(new Timestamp JavaDoc(0));
252             anonymousUser.setPostsPerPage(postsPerPage);
253             anonymousUser.setTimeZone(timeZone);
254             return anonymousUser;
255         }
256     }
257
258     public void postLogin(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, OnlineUser onlineUser)
259         throws DatabaseException {
260
261         // We create default Message Folder for this user
262
int folderOption = 0;
263         int folderType = 0;
264         int folderStatus = 0;
265         Timestamp JavaDoc now = DateUtil.getCurrentGMTTimestamp();
266         try {
267             try {
268                 DAOFactory.getMessageFolderDAO().create(MVNForumConstant.MESSAGE_FOLDER_INBOX, onlineUser.getMemberID(),
269                                             0/*folderOrder*/, folderStatus, folderOption, folderType, now, now);
270             } catch (DuplicateKeyException ex) {
271                 // Already existed, just go ahead
272
}
273
274             try {
275                 DAOFactory.getMessageFolderDAO().create(MVNForumConstant.MESSAGE_FOLDER_DRAFT, onlineUser.getMemberID(),
276                                             1/*folderOrder*/, folderStatus, folderOption, folderType, now, now);
277             } catch (DuplicateKeyException ex) {
278                 // Already existed, just go ahead
279
}
280
281             try {
282                 DAOFactory.getMessageFolderDAO().create(MVNForumConstant.MESSAGE_FOLDER_SENT, onlineUser.getMemberID(),
283                                             2/*folderOrder*/, folderStatus, folderOption, folderType, now, now);
284             } catch (DuplicateKeyException ex) {
285                 // Already existed, just go ahead
286
}
287
288             try {
289                 DAOFactory.getMessageFolderDAO().create(MVNForumConstant.MESSAGE_FOLDER_TRASH, onlineUser.getMemberID(),
290                                             3/*folderOrder*/, folderStatus, folderOption, folderType, now, now);
291             } catch (DuplicateKeyException ex) {
292                 // Already existed, just go ahead
293
}
294         } catch (CreateException ce) {
295             throw new DatabaseException("Cannot created Message Folder.");
296         } catch (ForeignKeyNotFoundException fe) {
297             throw new DatabaseException("Cannot created Message Folder because the foreign key is not existed.");
298         }
299     }
300
301     public void logout(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
302         //do nothing
303
}
304
305     public void logout(GenericRequest request, GenericResponse response) {
306         //do nothing
307
}
308
309     public String JavaDoc getEncodedPassword(String JavaDoc loginName, String JavaDoc password) {
310         return Encoder.getMD5_Base64(password);
311     }
312
313     public boolean validatePassword(String JavaDoc loginName, String JavaDoc password, boolean isEncodedPassword)
314         throws AuthenticationException {
315
316         try {
317             int memberId = DAOFactory.getMemberDAO().getMemberIDFromMemberName(loginName);
318             if ((memberId == 0) || (memberId == MVNForumConstant.MEMBER_ID_OF_GUEST)) {
319                 return true;
320             }
321
322             String JavaDoc encodedPassword;
323             if (isEncodedPassword) {
324                 encodedPassword = password;
325             } else {
326                 encodedPassword = getEncodedPassword(loginName, password);
327             }
328
329             if (isEncodedPassword && password.equals(OnlineUserManager.PASSWORD_OF_METHOD_REALM)) {
330                 if (MVNForumConfig.getEnableLoginInfoInRealm()) {
331                     return true;
332                 }
333             }
334             if (isEncodedPassword && password.equals(OnlineUserManager.PASSWORD_OF_METHOD_CUSTOMIZATION)) {
335                 if (MVNForumConfig.getEnableLoginInfoInCustomization()) {
336                     return true;
337                 }
338             }
339             return encodedPassword.equals(DAOFactory.getMemberDAO().getPassword(memberId));
340         } catch (ObjectNotFoundException e) {
341             throw new AuthenticationException(NotLoginException.WRONG_NAME);
342         } catch (Exception JavaDoc e) {
343             /** @todo find a beter one than NotLoginException.NOT_LOGIN */
344             throw new AuthenticationException(NotLoginException.NOT_LOGIN);
345         }
346     }
347
348     public void ensureCorrectPassword(String JavaDoc loginName, String JavaDoc password, boolean isEncodedPassword)
349         throws AuthenticationException {
350
351         boolean isCorrectPassword = validatePassword(loginName, password, isEncodedPassword);
352         if (isCorrectPassword == false) {
353             throw new AuthenticationException(NotLoginException.WRONG_PASSWORD);
354         }
355     }
356 }
357
Popular Tags