1 package org.apache.turbine.modules.actions.sessionvalidator;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
85 data.populate();
86
87
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
96 if (!data.getUser().hasLoggedIn())
97 {
98 log.debug("User is not logged in!");
99
100
101
102 if (StringUtils.isEmpty(data.getMessage()))
103 {
104 data.setMessage(conf.getString(TurbineConstants.LOGIN_MESSAGE));
105 }
106
107
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
116
117 data.setAction(null);
118 }
119
120 log.debug("Login Check finished!");
121
122
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
141
142
143
144
145 if (data.getParameters().containsKey("_session_access_counter")
146 && !TurbineSecurity.isAnonymousUser(data.getUser()))
147 {
148
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
174
175 if (data.getTemplateInfo().getScreenTemplate() != null)
176 {
177 data.setScreen(null);
178 }
179 }
180 }