    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.lang.StringUtils;
    023import org.apache.commons.logging.Log;
    024import org.apache.commons.logging.LogFactory;
    025import org.apache.fulcrum.security.util.DataBackendException;
    026import org.apache.fulcrum.security.util.FulcrumSecurityException;
    027import org.apache.turbine.TurbineConstants;
    028import org.apache.turbine.annotation.TurbineConfiguration;
    029import org.apache.turbine.annotation.TurbineService;
    030import org.apache.turbine.modules.Action;
    031import org.apache.turbine.om.security.User;
    032import org.apache.turbine.pipeline.PipelineData;
    033import org.apache.turbine.services.security.SecurityService;
    034import org.apache.turbine.util.RunData;
    035import org.apache.turbine.util.TurbineException;
    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 1812628 2017-10-19 12:34:25Z gk $
    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    @TurbineConfiguration( TurbineConstants.LOGIN_ERROR )
    065    private String loginError = "";
    066
    067    @TurbineConfiguration( TurbineConstants.TEMPLATE_LOGIN )
    068    private String templateLogin;
    069
    070    @TurbineConfiguration( TurbineConstants.SCREEN_LOGIN )
    071    private String screenLogin;
    072
    073    /**
    074     * Updates the user's LastLogin timestamp, sets their state to
    075     * "logged in" and calls RunData.setUser() .  If the user cannot
    076     * be authenticated (database error?) the user is assigned
    077     * anonymous status and, if tr.props contains a TEMPLATE_LOGIN,
    078     * the screenTemplate is set to this, otherwise the screen is set
    079     * to SCREEN_LOGIN
    080     *
    081     * @param     pipelineData Turbine information.
    082     * @throws FulcrumSecurityException could not get instance of the
    083     *            anonymous user
    084     */
    085    @Override
    086    public void doPerform(PipelineData pipelineData)
    087            throws FulcrumSecurityException
    088    {
    089        RunData data = getRunData(pipelineData);
    090        String username = data.getParameters().getString(CGI_USERNAME, "");
    091        String password = data.getParameters().getString(CGI_PASSWORD, "");
    092
    093        if (StringUtils.isEmpty(username))
    094        {
    095            return;
    096        }
    097
    098        try
    099        {
    100                if (username.equals(security.getAnonymousUser().getName()))
    101            {
    102                throw new TurbineException("Anonymous user cannot login");
    103            }
    104
    105            // Authenticate the user and get the object.
    106            User user = security.getAuthenticatedUser(username, password);
    107
    108            // Store the user object.
    109            data.setUser(user);
    110
    111            // Mark the user as being logged in.
    112            user.setHasLoggedIn(Boolean.TRUE);
    113
    114            // Set the last_login date in the database.
    115            user.updateLastLogin();
    116
    117            // This only happens if the user is valid; otherwise, we
    118            // will get a valueBound in the User object when we don't
    119            // want to because the username is not set yet.  Save the
    120            // User object into the session.
    121            data.save();
    122
    123            /*
    124             * If the setPage("template.vm") method has not
    125             * been used in the template to authenticate the
    126             * user (usually Login.vm), then the user will
    127             * be forwarded to the template that is specified
    128             * by the "template.home" property as listed in
    129             * TR.props for the webapp.
    130             */
    131
    132        }
    133        catch (Exception e)
    134        {
    135            if (e instanceof DataBackendException)
    136            {
    137                log.error(e);
    138            }
    139
    140            // Set Error Message and clean out the user.
    141            data.setMessage(loginError);
    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}