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.lang3.StringUtils;
23 import org.apache.logging.log4j.Logger;
24 import org.apache.logging.log4j.LogManager;
25 import org.apache.turbine.Turbine;
26 import org.apache.turbine.TurbineConstants;
27 import org.apache.turbine.annotation.TurbineConfiguration;
28 import org.apache.turbine.om.security.User;
29 import org.apache.turbine.pipeline.PipelineData;
30 import org.apache.turbine.util.RunData;
31
32 /**
33 * The SessionValidator attempts to retrieve the User object from the
34 * Servlet API session that is associated with the request. If the
35 * data cannot be retrieved, it is handled here. If the user has not
36 * been marked as being logged into the system, the user is rejected
37 * and the screen is set to the screen.homepage value in
38 * TurbineResources.properties.
39 *
40 * <p>
41 *
42 * Other systems generally have a database table which stores this
43 * information, but we take advantage of the Servlet API here to save
44 * a hit to the database for each and every connection that a user
45 * makes.
46 *
47 * <p>
48 *
49 * This action is special in that it should only be executed by the
50 * Turbine servlet.
51 *
52 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
53 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
54 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
55 * @version $Id: DefaultSessionValidator.java 1854797 2019-03-04 20:41:39Z tv $
56 */
57 public class DefaultSessionValidator
58 extends SessionValidator
59 {
60 /** Logging */
61 private static Logger log = LogManager.getLogger(DefaultSessionValidator.class);
62
63 @TurbineConfiguration( TurbineConstants.LOGIN_MESSAGE )
64 private String loginMessage;
65
66 @TurbineConfiguration( TurbineConstants.SCREEN_LOGIN )
67 private String screenLogin;
68
69 @TurbineConfiguration( TurbineConstants.LOGIN_MESSAGE_NOSCREEN )
70 private String loginMessageNoScreen;
71
72
73 /**
74 * Execute the action. The default behavior is to populate the PipelineData
75 * object, and if the user is unknown, to then force a redirect
76 * to the login screen (as set in the tr.props).
77 *
78 * @see org.apache.turbine.modules.screens.error.InvalidState
79 * @param pipelineData Turbine PipelineData context information.
80 * @throws Exception The anonymous user could not be obtained
81 * from the security service
82 */
83 @Override
84 public void doPerform(PipelineData pipelineData)
85 throws Exception
86 {
87 RunData data = pipelineData.getRunData();
88 // Pull user from session.
89 data.populate();
90
91 // The user may have not logged in, so create a "guest/anonymous" user.
92 if (data.getUser() == null)
93 {
94 log.debug("Creating an anonymous user object!");
95 User anonymousUser = security.getAnonymousUser();
96 data.setUser(anonymousUser);
97 data.save();
98 }
99
100 // Make sure the User has logged into the system.
101 if (!data.getUser().hasLoggedIn())
102 {
103 // only set the message if nothing else has already set it
104 // (e.g. the LogoutUser action).
105 if (StringUtils.isEmpty(data.getMessage()))
106 {
107 data.setMessage(loginMessage);
108 }
109
110 // set the screen to be the login page
111 data.setScreen(screenLogin);
112
113 // We're not doing any actions buddy! (except action.login which
114 // will have been performed already)
115 data.setAction(null);
116 }
117
118 if (!data.hasScreen())
119 {
120 data.setMessage(loginMessageNoScreen);
121 data.setScreen(screenHomepage);
122 }
123
124 handleFormCounterToken(data,true);
125
126 // Comply with Turbine 4.0 standards
127 pipelineData.get(Turbine.class).put(User.class, data.getUser());
128 }
129 }