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.lang3.StringUtils;
023import org.apache.fulcrum.security.util.DataBackendException;
024import org.apache.fulcrum.security.util.FulcrumSecurityException;
025import org.apache.logging.log4j.LogManager;
026import org.apache.logging.log4j.Logger;
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$
047 */
048public class LoginUser implements Action
049{
050    /** CGI Parameter for the user name */
051    public static final String CGI_USERNAME = "username";
052
053    /** CGI Parameter for the password */
054    public static final String CGI_PASSWORD = "password";
055
056    /** Logging */
057    private static Logger log = LogManager.getLogger(LoginUser.class);
058
059    /** Injected service instance */
060    @TurbineService
061    private SecurityService security;
062
063    @TurbineConfiguration( TurbineConstants.LOGIN_ERROR )
064    private String loginError = "";
065
066    @TurbineConfiguration( TurbineConstants.TEMPLATE_LOGIN )
067    private String templateLogin;
068
069    @TurbineConfiguration( TurbineConstants.SCREEN_LOGIN )
070    private String screenLogin;
071
072    /**
073     * Updates the user's LastLogin timestamp, sets their state to
074     * "logged in" and calls RunData.setUser() .  If the user cannot
075     * be authenticated (database error?) the user is assigned
076     * anonymous status and, if tr.props contains a TEMPLATE_LOGIN,
077     * the screenTemplate is set to this, otherwise the screen is set
078     * to SCREEN_LOGIN
079     *
080     * @param     pipelineData Turbine information.
081     * @throws FulcrumSecurityException could not get instance of the
082     *            anonymous user
083     */
084    @Override
085    public void doPerform(PipelineData pipelineData)
086            throws FulcrumSecurityException
087    {
088        RunData data = pipelineData.getRunData();
089        String username = data.getParameters().getString(CGI_USERNAME, "");
090        String password = data.getParameters().getString(CGI_PASSWORD, "");
091
092        if (StringUtils.isEmpty(username))
093        {
094            return;
095        }
096
097        try
098        {
099                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}