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.fulcrum.security.util.FulcrumSecurityException;
023import org.apache.turbine.TurbineConstants;
024import org.apache.turbine.annotation.TurbineConfiguration;
025import org.apache.turbine.annotation.TurbineService;
026import org.apache.turbine.modules.Action;
027import org.apache.turbine.om.security.User;
028import org.apache.turbine.pipeline.PipelineData;
029import org.apache.turbine.services.security.SecurityService;
030import org.apache.turbine.util.RunData;
031
032/**
033 * This action removes a user from the session. It makes sure to save
034 * the User object in the session.
035 *
036 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
037 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
038 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
039 * @version $Id$
040 */
041public class LogoutUser implements Action
042{
043    /** Injected service instance */
044    @TurbineService
045    private SecurityService security;
046
047    @TurbineConfiguration( TurbineConstants.LOGOUT_MESSAGE )
048    private String logoutMessage;
049
050    @TurbineConfiguration( TurbineConstants.ACTION_LOGOUT_KEY )
051    private String actionLogout = TurbineConstants.ACTION_LOGOUT_DEFAULT;
052
053    @TurbineConfiguration( TurbineConstants.SCREEN_HOMEPAGE )
054    private String screenHomepage;
055
056    /**
057     * Clears the PipelineData user object back to an anonymous status not
058     * logged in, and with a null ACL.  If the tr.props ACTION_LOGIN
059     * is anything except "LogoutUser", flow is transfered to the
060     * SCREEN_HOMEPAGE
061     *
062     * If this action name is the value of action.logout then we are
063     * being run before the session validator, so we don't need to
064     * set the screen (we assume that the session validator will handle
065     * that). This is basically still here simply to preserve old behavior
066     * - it is recommended that action.logout is set to "LogoutUser" and
067     * that the session validator does handle setting the screen/template
068     * for a logged out (read not-logged-in) user.
069     *
070     * @param pipelineData Turbine information.
071     * @throws FulcrumSecurityException a problem occurred in the security
072     *            service.
073     */
074    @Override
075    public void doPerform(PipelineData pipelineData)
076            throws FulcrumSecurityException
077    {
078        RunData data = pipelineData.getRunData();
079
080        // Session validator did not run, so RunData is not populated
081        User user = data.getUserFromSession();
082
083        if (!security.isAnonymousUser(user))
084        {
085            // Make sure that the user has really logged in...
086            if (!user.hasLoggedIn())
087            {
088                return;
089            }
090
091            user.setHasLoggedIn(Boolean.FALSE);
092            security.saveUser(user);
093        }
094
095        data.setMessage(logoutMessage);
096
097        // This will cause the acl to be removed from the session in
098        // the Turbine servlet code.
099        data.setACL(null);
100
101        // Retrieve an anonymous user.
102        User anonymousUser = security.getAnonymousUser();
103        data.setUser(anonymousUser);
104        data.save();
105
106        // In the event that the current screen or related navigations
107        // require acl info, we cannot wait for Turbine to handle
108        // regenerating acl.
109        data.getSession().removeAttribute(TurbineConstants.ACL_SESSION_KEY);
110
111        // If this action name is the value of action.logout then we are
112        // being run before the session validator, so we don't need to
113        // set the screen (we assume that the session validator will handle
114        // that). This is basically still here simply to preserve old behavior
115        // - it is recommended that action.logout is set to "LogoutUser" and
116        // that the session validator does handle setting the screen/template
117        // for a logged out (read not-logged-in) user.
118        if (!actionLogout.equals(TurbineConstants.ACTION_LOGOUT_DEFAULT))
119        {
120            data.setScreen(screenHomepage);
121        }
122    }
123}