View Javadoc
1   package org.apache.turbine.modules.actions;
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.fulcrum.security.util.DataBackendException;
24  import org.apache.fulcrum.security.util.FulcrumSecurityException;
25  import org.apache.logging.log4j.LogManager;
26  import org.apache.logging.log4j.Logger;
27  import org.apache.turbine.TurbineConstants;
28  import org.apache.turbine.annotation.TurbineConfiguration;
29  import org.apache.turbine.annotation.TurbineService;
30  import org.apache.turbine.modules.Action;
31  import org.apache.turbine.om.security.User;
32  import org.apache.turbine.pipeline.PipelineData;
33  import org.apache.turbine.services.security.SecurityService;
34  import org.apache.turbine.util.RunData;
35  import org.apache.turbine.util.TurbineException;
36  
37  /**
38   * This is where we authenticate the user logging into the system
39   * against a user in the database. If the user exists in the database
40   * that users last login time will be updated.
41   *
42   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
43   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
44   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
45   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
46   * @version $Id$
47   */
48  public class LoginUser implements Action
49  {
50      /** CGI Parameter for the user name */
51      public static final String CGI_USERNAME = "username";
52  
53      /** CGI Parameter for the password */
54      public static final String CGI_PASSWORD = "password";
55  
56      /** Logging */
57      private static Logger log = LogManager.getLogger(LoginUser.class);
58  
59      /** Injected service instance */
60      @TurbineService
61      private SecurityService security;
62  
63      @TurbineConfiguration( TurbineConstants.LOGIN_ERROR )
64      private String loginError = "";
65  
66      @TurbineConfiguration( TurbineConstants.TEMPLATE_LOGIN )
67      private String templateLogin;
68  
69      @TurbineConfiguration( TurbineConstants.SCREEN_LOGIN )
70      private String screenLogin;
71  
72      /**
73       * Updates the user's LastLogin timestamp, sets their state to
74       * "logged in" and calls RunData.setUser() .  If the user cannot
75       * be authenticated (database error?) the user is assigned
76       * anonymous status and, if tr.props contains a TEMPLATE_LOGIN,
77       * the screenTemplate is set to this, otherwise the screen is set
78       * to SCREEN_LOGIN
79       *
80       * @param     pipelineData Turbine information.
81       * @throws FulcrumSecurityException could not get instance of the
82       *            anonymous user
83       */
84      @Override
85      public void doPerform(PipelineData pipelineData)
86              throws FulcrumSecurityException
87      {
88          RunData data = pipelineData.getRunData();
89          String username = data.getParameters().getString(CGI_USERNAME, "");
90          String password = data.getParameters().getString(CGI_PASSWORD, "");
91  
92          if (StringUtils.isEmpty(username))
93          {
94              return;
95          }
96  
97          try
98          {
99          	if (username.equals(security.getAnonymousUser().getName()))
100             {
101                 throw new TurbineException("Anonymous user cannot login");
102             }
103 
104             // Authenticate the user and get the object.
105             User user = security.getAuthenticatedUser(username, password);
106 
107             // Store the user object.
108             data.setUser(user);
109 
110             // Mark the user as being logged in.
111             user.setHasLoggedIn(Boolean.TRUE);
112 
113             // Set the last_login date in the database.
114             user.updateLastLogin();
115 
116             // This only happens if the user is valid; otherwise, we
117             // will get a valueBound in the User object when we don't
118             // want to because the username is not set yet.  Save the
119             // User object into the session.
120             data.save();
121 
122             /*
123              * If the setPage("template.vm") method has not
124              * been used in the template to authenticate the
125              * user (usually Login.vm), then the user will
126              * be forwarded to the template that is specified
127              * by the "template.home" property as listed in
128              * TR.props for the webapp.
129              */
130 
131         }
132         catch (Exception e)
133         {
134             if (e instanceof DataBackendException)
135             {
136                 log.error(e);
137             }
138 
139             // Set Error Message and clean out the user.
140             data.setMessage(loginError);
141 
142             User anonymousUser = security.getAnonymousUser();
143             data.setUser(anonymousUser);
144 
145             if (StringUtils.isNotEmpty(templateLogin))
146             {
147                 // We're running in a templating solution
148                 data.setScreenTemplate(templateLogin);
149             }
150             else
151             {
152                 data.setScreen(screenLogin);
153             }
154         }
155     }
156 
157 }