KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opencms > workplace > CmsLoginNew


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/workplace/CmsLoginNew.java,v $
3  * Date : $Date: 2005/06/27 23:22:07 $
4  * Version: $Revision: 1.7 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2001 The OpenCms Group
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about OpenCms, please see the
22  * OpenCms Website: http://www.opencms.org
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27  */

28
29 package com.opencms.workplace;
30
31 import org.opencms.db.CmsDbEntryNotFoundException;
32 import org.opencms.db.CmsUserSettings;
33 import org.opencms.file.CmsObject;
34 import org.opencms.file.CmsProject;
35 import org.opencms.file.CmsUser;
36 import org.opencms.i18n.CmsMessages;
37 import org.opencms.main.CmsException;
38 import org.opencms.main.CmsLog;
39 import org.opencms.main.OpenCms;
40 import org.opencms.security.CmsSecurityException;
41 import org.opencms.workplace.CmsFrameset;
42 import org.opencms.workplace.CmsWorkplaceView;
43
44 import com.opencms.core.I_CmsSession;
45 import com.opencms.legacy.CmsLegacyException;
46 import com.opencms.legacy.CmsXmlTemplateLoader;
47 import com.opencms.template.A_CmsXmlContent;
48 import com.opencms.template.CmsCacheDirectives;
49 import com.opencms.template.CmsXmlTemplate;
50 import com.opencms.template.CmsXmlTemplateFile;
51
52 import java.util.Hashtable JavaDoc;
53 import java.util.Iterator JavaDoc;
54
55 /**
56  * Template class for displaying the login screen of the OpenCms workplace.<P>
57  * Reads template files of the content type <code>CmsXmlWpTemplateFile</code>.
58  *
59  * @author Alexander Kandzior
60  * @version $Revision: 1.7 $
61  *
62  * @deprecated Will not be supported past the OpenCms 6 release.
63  */

64
65 public class CmsLoginNew extends CmsXmlTemplate {
66
67     /** Debug flag, set to 9 for maximum verbosity */
68     private static final int DEBUG = 0;
69
70     /**
71      * Gets the content of the login template and processes the data input.<p>
72      *
73      * If the user has authenticated himself to the system,
74      * the login window is closed and the workplace is opened.
75      * If the login was incorrect, an error message is displayed and the login
76      * dialog is displayed again.
77      *
78      * @param cms request initialized CmsObject
79      * @param templateFile the login template file
80      * @param elementName not used
81      * @param parameters parameters of the request and the template
82      * @param templateSelector selector of the template tag to be displayed
83      * @return the processed data of the template
84      * @throws CmsException if something goes wrong
85      */

86     public byte[] getContent(CmsObject cms, String JavaDoc templateFile,
87         String JavaDoc elementName, Hashtable JavaDoc parameters, String JavaDoc templateSelector)
88     throws CmsException {
89                                               
90         if (DEBUG > 1) System.err.println("\nCmsLoginNew: Login process started");
91         
92         // Initialize language and encoding
93
CmsXmlLanguageFile langFile = new CmsXmlLanguageFile(cms);
94         m_messages = langFile.getMessages();
95         // Ensure encoding for the login page is set correctly accoring to selected language
96
cms.getRequestContext().setEncoding(langFile.getEncoding());
97         
98         // Check if a "logout=true" parameter is present, if so trash the session
99
boolean logout = (null != (String JavaDoc)parameters.get("logout"));
100         
101         I_CmsSession session = CmsXmlTemplateLoader.getSession(cms.getRequestContext(), false);
102         // Check if there already is a session
103
if (session != null) {
104             // Old session found, must be invalidated
105
if (logout) {
106                 session.invalidate();
107                 if (DEBUG > 2) System.err.println("CmsLoginNew: logout, trashed old session");
108             } else {
109                 if (DEBUG > 2) System.err.println("CmsLoginNew: kept old session, no logout parameter found");
110             }
111         } else {
112             if (DEBUG > 2) System.err.println("CmsLoginNew: no current active session");
113         }
114
115         // the template to be displayed
116
CmsXmlTemplateFile xmlTemplateDocument = new CmsXmlTemplateFile(cms, templateFile);
117                 
118         String JavaDoc username = null;
119         CmsUser user;
120
121         // get user name and password
122
String JavaDoc name = (String JavaDoc)parameters.get("OPENCMSUSERNAME");
123         String JavaDoc password = (String JavaDoc)parameters.get("OPENCMSPASSWORD");
124         // get further startup parameters
125
String JavaDoc startTaskId = (String JavaDoc)parameters.get(CmsWorkplaceDefault.C_PARA_STARTTASKID);
126         String JavaDoc startProjectId = (String JavaDoc)parameters.get(CmsWorkplaceDefault.C_PARA_STARTPROJECTID);
127
128         if (DEBUG > 1) System.err.println("CmsLoginNew: name=" + name + " password=" + password + " task=" + startTaskId + " project=" + startProjectId);
129
130         if ((name != null) && (password != null)) {
131             if (DEBUG > 1) System.err.println("CmsLoginNew: trying to log in");
132             // user and password have been submitted, try to log in the user
133
boolean validLogin;
134             try {
135                 username = cms.loginUser(name, password);
136                 validLogin = true;
137                 if (DEBUG > 1) System.err.println("CmsLoginNew: cms.loginUser() successfull");
138             } catch (CmsException e) {
139                 // invalid login
140
validLogin = false;
141                 if (DEBUG > 1) System.err.println("CmsLoginNew: cms.loginUser() failed");
142             }
143
144             if ((username != null) && (username.equals(OpenCms.getDefaultUsers().getUserGuest()))) {
145                 // please no Guest user in the workplace
146
// use the same behaviour as if the access was unauthorized
147
validLogin = false;
148                 if (DEBUG > 1) System.err.println("CmsLoginNew: user was guest user");
149             } else if ((username != null)
150                 && (! cms.userInGroup(username, OpenCms.getDefaultUsers().getGroupUsers()))
151                 && (! cms.userInGroup(username, OpenCms.getDefaultUsers().getGroupProjectmanagers()))
152                 && (! cms.userInGroup(username, OpenCms.getDefaultUsers().getGroupAdministrators()))) {
153                 // user MUST be in at last one of the default groups for administrators, users or project managers
154
// use the same behaviour as if the access was unauthorized
155
validLogin = false;
156                 if (DEBUG > 1) System.err.println("CmsLoginNew: user was not in default groups");
157             }
158             
159             if (! validLogin) {
160                 if (CmsLog.getLog(this).isInfoEnabled()) {
161                     CmsLog.getLog(this).info("Failed login attempt for user '" + name + "'");
162                 }
163                 throw new CmsLegacyException("[OpenCms login failed]", CmsLegacyException.C_NO_USER);
164             }
165             if (DEBUG > 0) System.err.println("CmsLoginNew: user " + username + " logged in");
166
167             // get a session for this user so that he is authentificated at the
168
// end of this request
169
session = CmsXmlTemplateLoader.getSession(cms.getRequestContext(), true);
170             if (CmsLog.getLog(this).isInfoEnabled()) {
171                 CmsLog.getLog(this).info("Login of user '" + username + "'");
172             }
173
174             // read the user data from the databsse
175
user = cms.readUser(username);
176             
177             // set the startup project id
178
setStartProjectId(cms, session, startProjectId);
179
180             // set startup task view
181
setStartTaskId(session, startTaskId);
182             
183             // set the additional user preferences
184
Hashtable JavaDoc preferences = (Hashtable JavaDoc)user.getAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_PREFERENCES);
185             // check if preferences are existing, otherwise use defaults
186
if (preferences == null) {
187                 preferences = getDefaultPreferences();
188             }
189             // check of the users language setting (if he has one)
190
session.removeValue(com.opencms.core.I_CmsConstants.C_START_LOCALE);
191             
192             preferences.put(com.opencms.core.I_CmsConstants.C_START_LOCALE, cms.getRequestContext().getLocale().toString());
193             session.putValue(CmsUserSettings.ADDITIONAL_INFO_PREFERENCES, preferences);
194
195             langFile = new CmsXmlLanguageFile(cms, cms.getRequestContext().getLocale().getLanguage());
196             if (DEBUG > 1) System.err.println("CmsLoginNew: encoding: " + langFile.getEncoding());
197             cms.getRequestContext().setEncoding(langFile.getEncoding());
198             
199             // trigger call of "login()" JavaScript in Template on page load
200
xmlTemplateDocument.setData("onload", "onload='login();'");
201         } else if ((! logout) && ((cms.getRequestContext().currentUser()) != null)
202             && (! OpenCms.getDefaultUsers().getUserGuest().equals(cms.getRequestContext().currentUser().getName()))
203             && ((cms.userInGroup(cms.getRequestContext().currentUser().getName(), OpenCms.getDefaultUsers().getGroupUsers()))
204                 || (cms.userInGroup(cms.getRequestContext().currentUser().getName(), OpenCms.getDefaultUsers().getGroupProjectmanagers()))
205                 || (cms.userInGroup(cms.getRequestContext().currentUser().getName(), OpenCms.getDefaultUsers().getGroupAdministrators())))) {
206             // the user is already logged in and no logout parameter is present, open a new window
207
if (DEBUG > 1) System.err.println("CmsLoginNew: re-using old login");
208             xmlTemplateDocument.setData("onload", "onload='login();'");
209         } else {
210             // no user logged in, no call to "login()" JavaScript
211
if (DEBUG > 1) System.err.println("CmsLoginNew: no login or logout, displaying template");
212             xmlTemplateDocument.setData("onload", "onload='init();'");
213         }
214
215         long id = System.currentTimeMillis();
216         xmlTemplateDocument.setData("windowId", new Long JavaDoc(id).toString());
217         xmlTemplateDocument.setData("startTaskId", startTaskId);
218         xmlTemplateDocument.setData("startProjectId", startProjectId);
219
220         if (DEBUG > 1) System.err.println("CmsLoginNew: Login process finished");
221
222         // process the selected template
223
return startProcessing(cms, xmlTemplateDocument, "", parameters, templateSelector);
224     }
225
226     /**
227      * Sets the startup project to the one read from the user
228      * preferences or the one from the request parameters.
229      *
230      * @param cms the initialized CmsObject
231      * @param session the initialized user session
232      * @param startProjectId the id value of the request parameter (might be null)
233      * @throws CmsException in case of issues reading the registry
234      */

235     private void setStartProjectId(CmsObject cms, I_CmsSession session, String JavaDoc startProjectId)
236     throws CmsException {
237         // set current project to the default online project or to
238
// project specified in the users preferences
239
int currentProject = CmsProject.ONLINE_PROJECT_ID;
240         
241         if ((startProjectId != null) && (! "".equals(startProjectId))) {
242             // try to set project to id from parameters
243
try {
244                 currentProject = (new Integer JavaDoc(startProjectId)).intValue();
245             } catch (NumberFormatException JavaDoc e) {
246                 // currentProject will still have online project value
247
}
248         } else {
249             // check out the user information if a default project is stored there.
250
CmsUserSettings settings = new CmsUserSettings(cms);
251             try {
252                 CmsProject project = cms.readProject(settings.getStartProject());
253                 currentProject = project.getId();
254             } catch (CmsDbEntryNotFoundException e) {
255                 // the project does not exist, maybe it was deleted
256
// set ID to online project
257
currentProject = CmsProject.ONLINE_PROJECT_ID;
258             }
259         }
260
261         // try to set the current project
262
try {
263             CmsProject project = cms.readProject(currentProject);
264             if (! cms.getAllAccessibleProjects().contains(project)) {
265                 // user has no (more) access to the project
266
currentProject = CmsProject.ONLINE_PROJECT_ID;
267             }
268         } catch (Exception JavaDoc e) {
269             // project will default to online project
270
currentProject = CmsProject.ONLINE_PROJECT_ID;
271         }
272         
273         // set the current project id
274
cms.getRequestContext().setCurrentProject(cms.readProject(currentProject));
275     }
276
277     /**
278      * Sets the startup view to display the selected start task.
279      *
280      * @param session the initialized user session
281      * @param startTaskId the id of the task to display
282      */

283     private void setStartTaskId(I_CmsSession session, String JavaDoc startTaskId) {
284         if ((startTaskId == null) || ("".equals(startTaskId))) {
285             return;
286         }
287         String JavaDoc link = "";
288         Iterator JavaDoc i = OpenCms.getWorkplaceManager().getViews().iterator();
289         while (i.hasNext()) {
290             CmsWorkplaceView view = (CmsWorkplaceView)i.next();
291             if (view.getKey().equals("${key.select.tasks}")) {
292                 link = view.getUri();
293                 break;
294             }
295         }
296         session.putValue(CmsWorkplaceDefault.C_PARA_STARTTASKID, startTaskId);
297         session.putValue(CmsWorkplaceDefault.C_PARA_VIEW, link);
298     }
299
300     private CmsMessages m_messages;
301
302     /**
303      * Sets the default preferences for the current user if those values are not available.
304      * @return Hashtable with default preferences.
305      */

306
307     private Hashtable JavaDoc getDefaultPreferences() {
308         Hashtable JavaDoc pref = new Hashtable JavaDoc();
309         // set the default columns in the filelist
310
int filelist = 4095 + 512;
311         pref.put(CmsWorkplaceDefault.C_USERPREF_FILELIST, new Integer JavaDoc(filelist));
312         return pref;
313     }
314
315     /**
316      * Customized <code>getTitle()</code> method for adding the current
317      * version information to the title of the login screen.
318      *
319      * @param cms for accessing system resources
320      * @param tagcontent (unused)
321      * @param doc reference to the A_CmsXmlContent object of the initiating XML document.
322      * @param userObject must ba a <code>java.util.Hashtable</code> with request parameters
323      * @return String with customized title information
324      * @throws CmsException in case of errors processing the template
325      */

326     public Object JavaDoc getTitle(CmsObject cms, String JavaDoc tagcontent, A_CmsXmlContent doc, Object JavaDoc userObject)
327     throws CmsException {
328         String JavaDoc title = (String JavaDoc)super.getTitle(cms, tagcontent, doc, userObject);
329         if (title == null) title = "";
330         title += " - " + OpenCms.getSystemInfo().getVersionName();
331         return title;
332     }
333
334     /**
335      * Returns a String with the version information of this OpenCms instance
336      *
337      * @param cms for accessing system resources
338      * @param tagcontent (unused)
339      * @param doc reference to the A_CmsXmlContent object of the initiating XML document.
340      * @param userObject must ba a <code>java.util.Hashtable</code> with request parameters
341      * @return String with the version information of this OpenCms instance
342      * @throws CmsException in case of errors processing the template
343      */

344     public Object JavaDoc version(CmsObject cms, String JavaDoc tagcontent, A_CmsXmlContent doc, Object JavaDoc userObject)
345     throws CmsException {
346         return OpenCms.getSystemInfo().getVersionName();
347     }
348
349     /**
350      * Returns a localized String for the key value given as <code>tagcontent</code>
351      * parameter.
352      *
353      * @param cms for accessing system resources
354      * @param tagcontent key value for the resource bundle
355      * @param doc reference to the A_CmsXmlContent object of the initiating XML document.
356      * @param userObject must ba a <code>java.util.Hashtable</code> with request parameters
357      * @return String with the version information of this OpenCms instance
358      * @throws CmsException in case of errors processing the template
359      */

360     public Object JavaDoc message(CmsObject cms, String JavaDoc tagcontent, A_CmsXmlContent doc, Object JavaDoc userObject)
361     throws CmsException {
362         return m_messages.key(tagcontent);
363     }
364     
365     /**
366      * Returns the path to the workplace top level uri.<p>
367      *
368      * @param cms for accessing system resources
369      * @param tagcontent key value for the resource bundle
370      * @param doc reference to the A_CmsXmlContent object of the initiating XML document.
371      * @param userObject must ba a <code>java.util.Hashtable</code> with request parameters
372      * @return String with the version information of this OpenCms instance
373      * @throws CmsException in case of errors processing the template
374      */

375     public Object JavaDoc workplaceUri(CmsObject cms, String JavaDoc tagcontent, A_CmsXmlContent doc, Object JavaDoc userObject)
376     throws CmsException {
377         try {
378             cms.readResource(CmsFrameset.JSP_WORKPLACE_URI);
379             return OpenCms.getLinkManager().substituteLink(cms, CmsFrameset.JSP_WORKPLACE_URI);
380         } catch (CmsSecurityException se) {
381             return OpenCms.getLinkManager().substituteLink(cms, CmsFrameset.JSP_WORKPLACE_URI);
382         } catch (CmsException ce) {
383             // return default xml uri
384
}
385         return OpenCms.getLinkManager().substituteLink(cms, CmsWorkplaceAction.XML_WORKPLACE_URI);
386     }
387
388     /**
389      * Prevent caching of this template
390      *
391      * @param cms for accessing system resources
392      * @param templateFile filename of the template file
393      * @param elementName element name of this template in our parent template.
394      * @param parameters hash with all template class parameters
395      * @param templateSelector template section that should be processed
396      * @return false
397      */

398     public boolean isCacheable(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName,
399             Hashtable JavaDoc parameters, String JavaDoc templateSelector) {
400         return false;
401     }
402     
403     /**
404      * Prevent caching of this template in the element cache
405      *
406      * @param cms for accessing system resources
407      * @param templateFile filename of the template file
408      * @param elementName element name of this template in our parent template.
409      * @param parameters hash with all template class parameters
410      * @param templateSelector template section that should be processed
411      * @return <code>new CmsCacheDirectives(false)</code>
412      */

413     public CmsCacheDirectives getCacheDirectives(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName, Hashtable JavaDoc parameters, String JavaDoc templateSelector) {
414         return new CmsCacheDirectives(false);
415     }
416 }
417
Popular Tags