Coverage Report - org.apache.turbine.modules.actions.LoginUser
 
Classes in this File Line Coverage Branch Coverage Complexity
LoginUser
54%
13/24
16%
1/6
6
 
 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.configuration.Configuration;
 23  
 import org.apache.commons.lang.StringUtils;
 24  
 import org.apache.commons.logging.Log;
 25  
 import org.apache.commons.logging.LogFactory;
 26  
 import org.apache.fulcrum.security.util.DataBackendException;
 27  
 import org.apache.fulcrum.security.util.FulcrumSecurityException;
 28  
 import org.apache.turbine.TurbineConstants;
 29  
 import org.apache.turbine.annotation.TurbineConfiguration;
 30  
 import org.apache.turbine.annotation.TurbineService;
 31  
 import org.apache.turbine.modules.Action;
 32  
 import org.apache.turbine.om.security.User;
 33  
 import org.apache.turbine.pipeline.PipelineData;
 34  
 import org.apache.turbine.services.security.SecurityService;
 35  
 import org.apache.turbine.util.RunData;
 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: LoginUser.java 1695634 2015-08-13 00:35:47Z tv $
 47  
  */
 48  1
 public class LoginUser
 49  
         extends Action
 50  
 {
 51  
     /** CGI Parameter for the user name */
 52  
     public static final String CGI_USERNAME = "username";
 53  
 
 54  
     /** CGI Parameter for the password */
 55  
     public static final String CGI_PASSWORD = "password";
 56  
 
 57  
     /** Logging */
 58  1
     private static Log log = LogFactory.getLog(LoginUser.class);
 59  
 
 60  
     /** Injected service instance */
 61  
     @TurbineService
 62  
     private SecurityService security;
 63  
 
 64  
     /** Injected configuration instance */
 65  
     @TurbineConfiguration
 66  
     private Configuration conf;
 67  
 
 68  
     /**
 69  
      * Updates the user's LastLogin timestamp, sets their state to
 70  
      * "logged in" and calls RunData.setUser() .  If the user cannot
 71  
      * be authenticated (database error?) the user is assigned
 72  
      * anonymous status and, if tr.props contains a TEMPLATE_LOGIN,
 73  
      * the screenTemplate is set to this, otherwise the screen is set
 74  
      * to SCREEN_LOGIN
 75  
      *
 76  
      * @param     pipelineData Turbine information.
 77  
      * @exception FulcrumSecurityException could not get instance of the
 78  
      *            anonymous user
 79  
      */
 80  
     @Override
 81  
     public void doPerform(PipelineData pipelineData)
 82  
             throws FulcrumSecurityException
 83  
     {
 84  1
         RunData data = getRunData(pipelineData);
 85  1
         String username = data.getParameters().getString(CGI_USERNAME, "");
 86  1
         String password = data.getParameters().getString(CGI_PASSWORD, "");
 87  
 
 88  1
         if (StringUtils.isEmpty(username))
 89  
         {
 90  0
             return;
 91  
         }
 92  
 
 93  
         try
 94  
         {
 95  
             // Authenticate the user and get the object.
 96  1
             User user = security.getAuthenticatedUser(username, password);
 97  
 
 98  
             // Store the user object.
 99  1
             data.setUser(user);
 100  
 
 101  
             // Mark the user as being logged in.
 102  1
             user.setHasLoggedIn(Boolean.TRUE);
 103  
 
 104  
             // Set the last_login date in the database.
 105  1
             user.updateLastLogin();
 106  
 
 107  
             // This only happens if the user is valid; otherwise, we
 108  
             // will get a valueBound in the User object when we don't
 109  
             // want to because the username is not set yet.  Save the
 110  
             // User object into the session.
 111  1
             data.save();
 112  
 
 113  
             /*
 114  
              * If the setPage("template.vm") method has not
 115  
              * been used in the template to authenticate the
 116  
              * user (usually Login.vm), then the user will
 117  
              * be forwarded to the template that is specified
 118  
              * by the "template.home" property as listed in
 119  
              * TR.props for the webapp.
 120  
              */
 121  
 
 122  
         }
 123  0
         catch (Exception e)
 124  
         {
 125  0
             if (e instanceof DataBackendException)
 126  
             {
 127  0
                 log.error(e);
 128  
             }
 129  
 
 130  
             // Set Error Message and clean out the user.
 131  0
             data.setMessage(conf.getString(TurbineConstants.LOGIN_ERROR, ""));
 132  0
             User anonymousUser = security.getAnonymousUser();
 133  0
             data.setUser(anonymousUser);
 134  
 
 135  0
             String loginTemplate = conf.getString(
 136  
                     TurbineConstants.TEMPLATE_LOGIN);
 137  
 
 138  0
             if (StringUtils.isNotEmpty(loginTemplate))
 139  
             {
 140  
                 // We're running in a templating solution
 141  0
                 data.setScreenTemplate(loginTemplate);
 142  
             }
 143  
             else
 144  
             {
 145  0
                 data.setScreen(conf.getString(TurbineConstants.SCREEN_LOGIN));
 146  
             }
 147  1
         }
 148  1
     }
 149  
 
 150  
 }