KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cofax > cms > CofaxToolsLogin


1 /*
2  * CofaxToolsLogin is part of the Cofax content management system library.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Please see http://www.cofax.org for contact information and other related informaion.
19  *
20  * $Header: /cvsroot/cofax/cofax/src/org/cofax/cms/CofaxToolsLogin.java,v 1.19.2.1 2006/12/11 16:28:23 fxrobin Exp $
21  */

22 package org.cofax.cms;
23
24 import java.io.IOException JavaDoc;
25 import java.security.MessageDigest JavaDoc;
26 import java.security.NoSuchAlgorithmException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Vector JavaDoc;
31
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34 import javax.servlet.http.HttpSession JavaDoc;
35
36 import org.cofax.CofaxPage;
37 import org.cofax.DataStore;
38 import org.cofax.cms.login.ILoginHandler;
39
40 /**
41  * CofaxToolsLogin: Verifies user information and populates the user (session)
42  * values if verification is true. Login is called upon user login and will be
43  * fulfilled only once per session.
44  *
45  * @author Charles Harvey
46  * @author Lee Bolding
47  * @author Nicolas Richeton - Smile
48  */

49
50 public class CofaxToolsLogin {
51
52     private static ILoginHandler loginHandler = null;
53
54     /**
55      * Verify user information and populate the user (session) values if
56      * verification is true. Login is called upon user login and will be
57      * fulfilled only once per session.
58      */

59     public static String JavaDoc login(DataStore db, HttpServletRequest JavaDoc req, HttpSession JavaDoc session, HashMap JavaDoc userInfoHash, HttpServletResponse JavaDoc res, CofaxPage page) {
60         return login(db, req, session, userInfoHash, res, page, true);
61     }
62
63     /**
64      * Verify user information and populate the user (session) values if
65      * verification is true. Login is called upon user login and will be
66      * fulfilled only once per session.
67      */

68     public static String JavaDoc login(DataStore db, HttpServletRequest JavaDoc req, HttpSession JavaDoc session, HashMap JavaDoc userInfoHash, HttpServletResponse JavaDoc res, CofaxPage page,
69             boolean doRedirect) {
70
71         String JavaDoc tag;
72
73         // If user/ login is found, hash will be populated
74
if (userInfoHash.size() >= 1) {
75             // Instantiate a new user object
76
CofaxToolsUser user = new CofaxToolsUser();
77
78             // Put a pointer to the user object into the session
79
session.setAttribute("user", user);
80
81             // Put the user info hash returned from the database into the user
82
// object
83
user.userInfoHash = userInfoHash;
84
85             // Set the session timeout to 1 hour
86
session.setMaxInactiveInterval(3600);
87
88             // Create a hash for pubDesc/ pubID
89
HashMap JavaDoc userPubDescPubIDHash = new HashMap JavaDoc();
90
91             // Create a hash for pubName/ pubID
92
HashMap JavaDoc userPubNamePubIDHash = new HashMap JavaDoc();
93
94             // Create a hash for GroupID / Group Name
95
HashMap JavaDoc userGroupNameGroupIDHash = new HashMap JavaDoc();
96
97             // Create a hash for pubName/ pubDesc
98
HashMap JavaDoc userPubNamePubDescHash = new HashMap JavaDoc();
99
100             // Create a hash for reviously edited articles
101
HashMap JavaDoc userPreviousArticlesHash = new HashMap JavaDoc();
102             userPreviousArticlesHash.put("Previous Edits", "");
103
104             // Get the groups that the user belongs to
105
HashMap JavaDoc fillReqTwo = new HashMap JavaDoc();
106             fillReqTwo.put("USERID", (String JavaDoc) userInfoHash.get("USERID"));
107             tag = CofaxToolsDbUtils.fillTag(db, "getVectorOfGroupsByUserID");
108             Vector JavaDoc userGroupVector = CofaxToolsDbUtils.getPackageVector(db, fillReqTwo, tag);
109
110             // Get the group types that the user belongs to
111
tag = CofaxToolsDbUtils.fillTag(db, "getVectorOfGroupTypesByUserID");
112             Vector JavaDoc userGroupTypeVector = CofaxToolsDbUtils.getPackageVector(db, fillReqTwo, tag);
113
114             // Set the userGroupHash in the user object to the hash returned
115
// from the database
116
user.userGroupVector = userGroupVector;
117             user.userGroupTypeVector = userGroupTypeVector;
118
119             // Get the publications from the groups that the user belongs to and
120
// insert them into a table for checking permissions later
121
Iterator JavaDoc keys = userGroupVector.iterator();
122             Vector JavaDoc userPubsVectorOHash = new Vector JavaDoc();
123             while (keys.hasNext()) {
124
125                 // Get the group id
126
String JavaDoc groupIDFromUserToGroup = (String JavaDoc) keys.next();
127                 if (groupIDFromUserToGroup != null && !groupIDFromUserToGroup.equals("")) {
128
129                     HashMap JavaDoc fillReqThree = new HashMap JavaDoc();
130                     fillReqThree.put("pubNameKey", groupIDFromUserToGroup);
131                     tag = CofaxToolsDbUtils.fillTag(db, "getAllFromPermGroupsByGroupID");
132
133                     CofaxToolsUtil.log("tag= " + tag);
134
135                     HashMap JavaDoc se = CofaxToolsDbUtils.getNameValuePackageHash(db, fillReqThree, tag);
136
137                     String JavaDoc groupType = (String JavaDoc) se.get("GROUPTYPE");
138                     String JavaDoc groupName = (String JavaDoc) se.get("GROUPNAME");
139                     String JavaDoc groupID = (String JavaDoc) se.get("GROUPID");
140                     userGroupNameGroupIDHash.put(groupID, groupName);
141
142                     HashMap JavaDoc fillReqFour = new HashMap JavaDoc();
143                     fillReqFour.put("pubNameKey", groupIDFromUserToGroup);
144                     tag = CofaxToolsDbUtils.fillTag(db, "getAllFromGroupToPubByGroupID");
145                     // HashMap s = CofaxToolsDbUtils.getNameValuePackageHash(db,
146
// fillReqFour, tag);
147
ArrayList JavaDoc s = (ArrayList JavaDoc) CofaxToolsDbUtils.getPackageData(db, fillReqFour, tag);
148
149                     // Iterator pubKeys = s.keySet().iterator();
150
Iterator JavaDoc pubKeys = s.iterator();
151                     while (pubKeys.hasNext()) {
152                         HashMap JavaDoc groupIDpubID = (HashMap JavaDoc) pubKeys.next();
153                         String JavaDoc pubID = (String JavaDoc) groupIDpubID.get("PUBID");
154
155                         String JavaDoc pubName = "";
156                         String JavaDoc pubDesc = "";
157                         // Populate the pubs table with pubID, groupType
158
HashMap JavaDoc ht = new HashMap JavaDoc();
159                         ht.put(pubID, groupType);
160                         userPubsVectorOHash.add(ht);
161                         // add pubDesc / pubID to the userPubDescPubIDHash
162

163                         if (!userPubDescPubIDHash.containsKey(pubID)) {
164                             HashMap JavaDoc htt = CofaxToolsUtil.getPubNamePubDescFromID(db, pubID);
165                             pubName = (String JavaDoc) htt.get("PUBNAME");
166                             pubDesc = (String JavaDoc) htt.get("PUBDESC");
167                             userPubDescPubIDHash.put(pubDesc, pubID);
168
169                             // add pubName, pubDesc to the
170
// userPubNamePubDescHash
171
if (!userPubNamePubDescHash.containsKey(pubDesc)) {
172                                 userPubNamePubDescHash.put(pubDesc, pubName);
173                             }
174                         }
175
176                         // add pubName / pubID to the userPubNamePubIDHash
177
if (!userPubNamePubIDHash.containsKey(pubID)) {
178                             userPubNamePubIDHash.put(pubName, pubID);
179                         }
180                     }
181                 }
182             }
183
184             // set all user values to the parameters gathered in the block
185
// above.
186
user.userPubsVectorOHash = userPubsVectorOHash;
187             user.userPubDescPubIDHash = userPubDescPubIDHash;
188             user.userPubNamePubIDHash = userPubNamePubIDHash;
189             user.userPubNamePubDescHash = userPubNamePubDescHash;
190             user.userPreviousArticlesHash = userPreviousArticlesHash;
191             user.userGroupNameGroupIDHash = userGroupNameGroupIDHash;
192
193             user.workingPub = (String JavaDoc) user.userInfoHash.get("HOMEPUB");
194             String JavaDoc role = (String JavaDoc) user.userInfoHash.get("ROLE");
195             String JavaDoc userFirstName = (String JavaDoc) user.userInfoHash.get("FIRSTNAME");
196             String JavaDoc userLastName = (String JavaDoc) user.userInfoHash.get("LASTNAME");
197             user.setUserWorkingPubConfigElements(db, session, user.workingPub);
198             user.workingPubDesc = CofaxToolsUtil.getPubDescFromID(db, user.workingPub);
199             user.workingPubName = CofaxToolsUtil.getPubNameFromID(db, user.workingPub);
200
201             String JavaDoc p = CofaxToolsUtil.getPublicationSelectID(db, session, (String JavaDoc) userInfoHash.get("HOMEPUB"), "PUBLICATION");
202             HashMap JavaDoc ht = new HashMap JavaDoc();
203             ht.put("userRole", role);
204             ht.put("userFirstName", userFirstName);
205             ht.put("userLastName", userLastName);
206             ht.put("selectPublication", p);
207
208             String JavaDoc querystring = req.getParameter("querystring");
209             boolean bOK = false;
210
211             if (doRedirect) {
212                 if (!("".equals(querystring)) && querystring != null) {
213                     try {
214                         String JavaDoc urlRedirectTo = "" + CofaxToolsServlet.aliasPath + "/tools/?" + querystring;
215                         if (!(urlRedirectTo.startsWith("/")))
216                             urlRedirectTo = "/" + urlRedirectTo;
217                         res.sendRedirect(urlRedirectTo);
218
219                         return "";
220                     } catch (IOException JavaDoc e) {
221                         System.out.println("CofaxToolsNavigation : error while redirect");
222                     }
223                 } else {
224                     page.putGlossaryValue("system:message", CofaxToolsUtil.getI18NMessage(req.getLocale(), "welcomeMessage") + " "
225                             + CofaxToolsUtil.getI18NMessage(req.getLocale(), "pleaseMakeSelection"));
226                     try {
227                         // FX : changing to default page.
228
String JavaDoc urlRedirectTo = "" + CofaxToolsServlet.aliasPath + "/tools/";
229                         if (!(urlRedirectTo.startsWith("/")))
230                             urlRedirectTo = "/" + urlRedirectTo;
231                         res.sendRedirect(urlRedirectTo);
232
233                         return "";
234                     } catch (IOException JavaDoc e) {
235                         System.out.println("CofaxToolsNavigation : error while redirect to cofax_navigation mode");
236                     }
237                     return ("");
238                 }
239             }
240             return ("");
241
242             // User login and password cannot be found in the database
243
} else {
244             page.putGlossaryValue("system:message", CofaxToolsUtil.getI18NMessage(req.getLocale(), "incorrectPassword"));
245             page.putGlossaryValue("system:highLightTab", "");
246             page.putGlossaryValue("querystring", (String JavaDoc) req.getParameter("querystring"));
247             try {
248                 CofaxToolsNavigation.includeResource(page, "" + CofaxToolsServlet.templatePath + "login.jsp", // FX :
249
// added
250
// templatePath
251
req, res, session);
252             } catch (Exception JavaDoc e) {
253                 System.err.println("THERE WAS AN ERROR: " + e);
254             }
255             return ("");
256         }
257     }
258
259     /**
260      * Verify user information and populate the user (session) values if
261      * verification is true. Login is called upon user login and will be
262      * fulfilled only once per session.
263      */

264     public static String JavaDoc login(DataStore db, HttpServletRequest JavaDoc req, HttpSession JavaDoc session, String JavaDoc login, String JavaDoc password, HttpServletResponse JavaDoc res, CofaxPage page) {
265
266         // Create a hash of all user info from tblPermUsers
267

268         HashMap JavaDoc userInfoHash = null;
269         if (loginHandler != null) {
270             userInfoHash = loginHandler.getUserHash(db, login, password);
271         } else {
272             userInfoHash = new HashMap JavaDoc();
273         }
274
275         return login(db, req, session, userInfoHash, res, page);
276     }
277
278     /***************************************************************************
279      * helper method for MD5 functions
280      **************************************************************************/

281     private static String JavaDoc toHex(byte[] digest) {
282         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
283         for (int i = 0; i < digest.length; i++) {
284             buf.append(Integer.toHexString(0x0100 + (digest[i] & 0x00ff)).substring(1));
285         }
286         return buf.toString();
287     }
288
289     private static String JavaDoc md5Password(String JavaDoc login, String JavaDoc password) {
290         String JavaDoc hash = "";
291         String JavaDoc md5Password = "";
292         try {
293             MessageDigest JavaDoc md = MessageDigest.getInstance("MD5");
294             hash = login + ":" + password;
295
296             byte[] rawPass = hash.getBytes();
297             try {
298                 md.update(rawPass);
299             } catch (Exception JavaDoc e) {
300                 CofaxToolsUtil.log("CofaxToolsLogin login : " + e);
301             }
302             md5Password = toHex(md.digest());
303         } catch (NoSuchAlgorithmException JavaDoc nsae) {
304             CofaxToolsUtil.log("CofaxToolsLogin login : " + nsae);
305         }
306
307         return md5Password;
308     }
309
310     /**
311      * @return
312      */

313     public static ILoginHandler getLoginHandler() {
314         return loginHandler;
315     }
316
317     /**
318      * @param login
319      */

320     public static void setLoginHandler(ILoginHandler login) {
321         loginHandler = login;
322     }
323
324 }
Popular Tags