001/*
002 *  Copyright 2017 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.site;
017
018import java.util.Map;
019
020import org.apache.cocoon.ProcessingException;
021import org.apache.cocoon.components.ContextHelper;
022import org.apache.cocoon.environment.ObjectModelHelper;
023import org.apache.cocoon.environment.Request;
024import org.apache.cocoon.environment.Session;
025
026import org.ametys.core.authentication.AuthenticateAction;
027import org.ametys.core.authentication.CredentialProvider;
028import org.ametys.core.authentication.LogoutCapable;
029import org.ametys.core.user.CurrentUserProvider;
030import org.ametys.plugins.core.impl.user.AvalonCurrentUserProvider;
031
032/**
033 * {@link CurrentUserProvider} able to logout from a site.
034 */
035public class FrontCurrentUserProvider extends AvalonCurrentUserProvider
036{
037    @Override
038    public void logout() throws ProcessingException
039    {
040        Map objectModel = ContextHelper.getObjectModel(_context);
041        Request request = ObjectModelHelper.getRequest(objectModel);
042        Session session = request.getSession(false);
043        
044        if (session != null)
045        {
046            // First check for sites' user
047            CredentialProvider cp = FrontAuthenticateAction.getCredentialProviderFromSession(request);
048            
049            if (cp == null)
050            {
051                // then check for application user
052                cp = AuthenticateAction.getCredentialProviderFromSession(request);
053            }
054            
055            // Invalidate session
056            session.invalidate();
057            
058            if (cp instanceof LogoutCapable)
059            {
060                // Logout process
061                ((LogoutCapable) cp).logout(); 
062            }
063        }
064    }
065}