View Javadoc

1   package org.apache.turbine.modules.actions.sessionvalidator;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.commons.configuration.Configuration;
23  
24  import org.apache.commons.lang.StringUtils;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  import org.apache.turbine.Turbine;
30  import org.apache.turbine.TurbineConstants;
31  
32  import org.apache.turbine.services.security.TurbineSecurity;
33  
34  import org.apache.turbine.util.RunData;
35  import org.apache.turbine.util.TurbineException;
36  
37  /***
38   * SessionValidator that requires login for use with Template Services
39   * like Velocity or WebMacro.
40   *
41   * <br>
42   *
43   * Templating services requires a different Session Validator
44   * because of the way it handles screens.  If you use the WebMacro or
45   * Velocity Service with the DefaultSessionValidator, users will be able to
46   * bypass login by directly addressing the template using
47   * template/index.wm.  This is because the Page class looks for the
48   * keyword "template" in the Path information and if it finds it will
49   * reset the screen using it's lookup mechanism and thereby bypass
50   * Login.
51   *
52   * Note that you will need to set the template.login property to the
53   * login template.
54   *
55   * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
56   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
57   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
58   * @version $Id: TemplateSecureSessionValidator.java 534527 2007-05-02 16:10:59Z tv $
59   */
60  public class TemplateSecureSessionValidator
61      extends SessionValidator
62  {
63      /*** Logging */
64      private static Log log = LogFactory.getLog(
65              TemplateSecureSessionValidator.class);
66  
67      /***
68       * doPerform is virtually identical to DefaultSessionValidator
69       * except that it calls template methods instead of bare screen
70       * methods. For example, it uses <code>setScreenTemplate</code> to
71       * load the tr.props TEMPLATE_LOGIN instead of the default's
72       * setScreen to TurbineConstants.SCREEN_LOGIN.
73       *
74       * @see DefaultSessionValidator
75       * @param data Turbine information.
76       * @throws TurbineException The anonymous user could not be obtained
77       *         from the security service
78       */
79      public void doPerform(RunData data)
80              throws TurbineException
81      {
82          Configuration conf = Turbine.getConfiguration();
83  
84          // Pull user from session.
85          data.populate();
86  
87          // The user may have not logged in, so create a "guest/anonymous" user.
88          if (data.getUser() == null)
89          {
90              log.debug("Fixing up empty User Object!");
91              data.setUser(TurbineSecurity.getAnonymousUser());
92              data.save();
93          }
94  
95          // This is the secure sessionvalidator, so user must be logged in.
96          if (!data.getUser().hasLoggedIn())
97          {
98              log.debug("User is not logged in!");
99  
100             // only set the message if nothing else has already set it
101             // (e.g. the LogoutUser action).
102             if (StringUtils.isEmpty(data.getMessage()))
103             {
104                 data.setMessage(conf.getString(TurbineConstants.LOGIN_MESSAGE));
105             }
106 
107             // Set the screen template to the login page.
108             String loginTemplate =
109                 conf.getString(TurbineConstants.TEMPLATE_LOGIN);
110 
111             log.debug("Sending User to the Login Screen ("
112                     + loginTemplate + ")");
113             data.getTemplateInfo().setScreenTemplate(loginTemplate);
114 
115             // We're not doing any actions buddy! (except action.login which
116             // will have been performed already)
117             data.setAction(null);
118         }
119 
120         log.debug("Login Check finished!");
121 
122         // Make sure we have some way to return a response.
123         if (!data.hasScreen() && StringUtils.isEmpty(
124                 data.getTemplateInfo().getScreenTemplate()))
125         {
126             String template = conf.getString(
127                     TurbineConstants.TEMPLATE_HOMEPAGE);
128 
129             if (StringUtils.isNotEmpty(template))
130             {
131                 data.getTemplateInfo().setScreenTemplate(template);
132             }
133             else
134             {
135                 data.setScreen(conf.getString(
136                         TurbineConstants.SCREEN_HOMEPAGE));
137             }
138         }
139 
140         // The session_access_counter can be placed as a hidden field in
141         // forms.  This can be used to prevent a user from using the
142         // browsers back button and submitting stale data.
143         // FIXME!! a template needs to be written to use this with templates.
144 
145         if (data.getParameters().containsKey("_session_access_counter")
146                 && !TurbineSecurity.isAnonymousUser(data.getUser()))
147         {
148             // See comments in screens.error.InvalidState.
149             if (data.getParameters().getInt("_session_access_counter")
150                     < (((Integer) data.getUser().getTemp(
151                     "_session_access_counter")).intValue() - 1))
152             {
153                 if (data.getTemplateInfo().getScreenTemplate() != null)
154                 {
155                     data.getUser().setTemp("prev_template",
156                             data.getTemplateInfo().getScreenTemplate()
157                             .replace('/', ','));
158                     data.getTemplateInfo().setScreenTemplate(conf.getString(
159                             TurbineConstants.TEMPLATE_INVALID_STATE));
160                 }
161                 else
162                 {
163                     data.getUser().setTemp("prev_screen",
164                                            data.getScreen().replace('/', ','));
165                     data.setScreen(conf.getString(
166                             TurbineConstants.SCREEN_INVALID_STATE));
167                 }
168                 data.getUser().setTemp("prev_parameters", data.getParameters());
169                 data.setAction("");
170             }
171         }
172 
173         // We do not want to allow both a screen and template parameter.
174         // The template parameter is dominant.
175         if (data.getTemplateInfo().getScreenTemplate() != null)
176         {
177             data.setScreen(null);
178         }
179     }
180 }