View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.jetspeed.modules.actions;
18  
19  // Jetspeed
20  import org.apache.jetspeed.services.JetspeedSecurity;
21  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
22  import org.apache.jetspeed.services.logging.JetspeedLogger;
23  import org.apache.jetspeed.services.resources.JetspeedResources;
24  import org.apache.jetspeed.om.security.JetspeedUser;
25  import org.apache.jetspeed.services.rundata.JetspeedRunData;
26  import org.apache.jetspeed.services.Profiler;
27  
28  // Turbine
29  import org.apache.turbine.modules.Action;
30  import org.apache.turbine.modules.ActionLoader;
31  import org.apache.turbine.services.localization.Localization;
32  import org.apache.turbine.util.RunData;
33  import org.apache.turbine.TurbineConstants;
34  
35  /***
36   * Performs change password action
37   * 
38   * @author <a href="morciuch@apache.org">Mark Orciuch</a>
39   * @version $Id: ChangePassword.java,v 1.7 2004/02/23 02:59:06 jford Exp $
40   */
41  public class ChangePassword extends Action
42  {
43      
44      /***
45       * Static initialization of the logger for this class
46       */    
47      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(ChangePassword.class.getName());
48      
49      public void doPerform(RunData rundata) throws Exception
50      {
51  
52          String cancelBtn = rundata.getParameters().getString(Localization.getString(rundata, "PASSWORDFORM_CANCEL"));
53          String username  = rundata.getParameters().getString("username" , "");
54          String oldPassword  = JetspeedSecurity.convertPassword(rundata.getParameters().getString("old_password" , ""));
55          String password  = JetspeedSecurity.convertPassword(rundata.getParameters().getString("password", ""));
56          String password2 = JetspeedSecurity.convertPassword(rundata.getParameters().getString("password_confirm", ""));
57  
58          // CANCEL BUTTON
59          //
60          // check to see if the Cancel button was pressed.
61          // if so, return to default portal page
62          if (cancelBtn != null && cancelBtn.equalsIgnoreCase(Localization.getString(rundata, "PASSWORDFORM_CANCEL")))
63          {
64              return;
65          }
66  
67          String returnTemplate = JetspeedResources.getString(JetspeedResources.CHANGE_PASSWORD_TEMPLATE, "ChangePassword");
68  
69          try 
70          {
71  
72              JetspeedUser user = JetspeedSecurity.getUser(username);
73  
74              if (!password.equals(password2))
75              {
76                  rundata.setMessage(Localization.getString(rundata, "UPDATEACCOUNT_PWNOTMATCH"));
77                  rundata.setScreenTemplate(returnTemplate);
78                  return;
79              }
80  
81              if (password.equals(oldPassword))
82              {
83                  rundata.setMessage(Localization.getString(rundata, "PASSWORDFORM_THESAME_MSG"));
84                  rundata.setScreenTemplate(returnTemplate);
85                  return;
86              }
87  
88              // Change the password
89              JetspeedSecurity.changePassword(user, oldPassword, password);
90              rundata.setMessage(Localization.getString(rundata, "PASSWORDFORM_DONE"));
91  
92              // Login again
93              rundata.getParameters().setString("username", username);
94              rundata.getParameters().setString("password", password);
95              String userRequestsRememberMe = rundata.getParameters().getString("rememberme");
96              rundata.getParameters().setString("rememberme", userRequestsRememberMe);
97              ActionLoader.getInstance().getInstance(
98                  JetspeedResources.getString(TurbineConstants.ACTION_LOGIN)
99                  ).doPerform(rundata);
100 
101             // Update the profile in rundata - not sure why this is not happening automatically?
102             JetspeedRunData jdata = (JetspeedRunData) rundata;
103             jdata.setProfile(Profiler.getProfile(jdata));
104 
105         }
106         catch (Exception e) 
107         {
108             logger.error("Exception", e);
109             rundata.setMessage(e.getMessage() != null ? e.getMessage() : e.toString());
110             rundata.setScreenTemplate(returnTemplate);
111         }
112     }
113 
114 }