    001package org.apache.turbine.modules.actions;
    002
    003/*
    004 * Licensed to the Apache Software Foundation (ASF) under one
    005 * or more contributor license agreements.  See the NOTICE file
    006 * distributed with this work for additional information
    007 * regarding copyright ownership.  The ASF licenses this file
    008 * to you under the Apache License, Version 2.0 (the
    009 * "License"); you may not use this file except in compliance
    010 * with the License.  You may obtain a copy of the License at
    011 *
    012 *   http://www.apache.org/licenses/LICENSE-2.0
    013 *
    014 * Unless required by applicable law or agreed to in writing,
    015 * software distributed under the License is distributed on an
    016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    017 * KIND, either express or implied.  See the License for the
    018 * specific language governing permissions and limitations
    019 * under the License.
    020 */
    021
    022import org.apache.commons.configuration.Configuration;
    023import org.apache.commons.lang.StringUtils;
    024import org.apache.commons.logging.Log;
    025import org.apache.commons.logging.LogFactory;
    026import org.apache.fulcrum.security.util.DataBackendException;
    027import org.apache.fulcrum.security.util.FulcrumSecurityException;
    028import org.apache.turbine.TurbineConstants;
    029import org.apache.turbine.annotation.TurbineConfiguration;
    030import org.apache.turbine.annotation.TurbineService;
    031import org.apache.turbine.modules.Action;
    032import org.apache.turbine.om.security.User;
    033import org.apache.turbine.pipeline.PipelineData;
    034import org.apache.turbine.services.security.SecurityService;
    035import org.apache.turbine.util.RunData;
    036
    037/**
    038 * This is where we authenticate the user logging into the system
    039 * against a user in the database. If the user exists in the database
    040 * that users last login time will be updated.
    041 *
    042 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
    043 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
    044 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
    045 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
    046 * @version $Id: LoginUser.java 1695634 2015-08-13 00:35:47Z tv $
    047 */
    048public class LoginUser
    049        extends Action
    050{
    051    /** CGI Parameter for the user name */
    052    public static final String CGI_USERNAME = "username";
    053
    054    /** CGI Parameter for the password */
    055    public static final String CGI_PASSWORD = "password";
    056
    057    /** Logging */
    058    private static Log log = LogFactory.getLog(LoginUser.class);
    059
    060    /** Injected service instance */
    061    @TurbineService
    062    private SecurityService security;
    063
    064    /** Injected configuration instance */
    065    @TurbineConfiguration
    066    private Configuration conf;
    067
    068    /**
    069     * Updates the user's LastLogin timestamp, sets their state to
    070     * "logged in" and calls RunData.setUser() .  If the user cannot
    071     * be authenticated (database error?) the user is assigned
    072     * anonymous status and, if tr.props contains a TEMPLATE_LOGIN,
    073     * the screenTemplate is set to this, otherwise the screen is set
    074     * to SCREEN_LOGIN
    075     *
    076     * @param     pipelineData Turbine information.
    077     * @exception FulcrumSecurityException could not get instance of the
    078     *            anonymous user
    079     */
    080    @Override
    081    public void doPerform(PipelineData pipelineData)
    082            throws FulcrumSecurityException
    083    {
    084        RunData data = getRunData(pipelineData);
    085        String username = data.getParameters().getString(CGI_USERNAME, "");
    086        String password = data.getParameters().getString(CGI_PASSWORD, "");
    087
    088        if (StringUtils.isEmpty(username))
    089        {
    090            return;
    091        }
    092
    093        try
    094        {
    095            // Authenticate the user and get the object.
    096            User user = security.getAuthenticatedUser(username, password);
    097
    098            // Store the user object.
    099            data.setUser(user);
    100
    101            // Mark the user as being logged in.
    102            user.setHasLoggedIn(Boolean.TRUE);
    103
    104            // Set the last_login date in the database.
    105            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            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        catch (Exception e)
    124        {
    125            if (e instanceof DataBackendException)
    126            {
    127                log.error(e);
    128            }
    129
    130            // Set Error Message and clean out the user.
    131            data.setMessage(conf.getString(TurbineConstants.LOGIN_ERROR, ""));
    132            User anonymousUser = security.getAnonymousUser();
    133            data.setUser(anonymousUser);
    134
    135            String loginTemplate = conf.getString(
    136                    TurbineConstants.TEMPLATE_LOGIN);
    137
    138            if (StringUtils.isNotEmpty(loginTemplate))
    139            {
    140                // We're running in a templating solution
    141                data.setScreenTemplate(loginTemplate);
    142            }
    143            else
    144            {
    145                data.setScreen(conf.getString(TurbineConstants.SCREEN_LOGIN));
    146            }
    147        }
    148    }
    149
    150}