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.avalon.framework.context.ContextException;
021import org.apache.cocoon.ProcessingException;
022import org.apache.cocoon.components.ContextHelper;
023import org.apache.cocoon.environment.ObjectModelHelper;
024import org.apache.cocoon.environment.Redirector;
025import org.apache.cocoon.environment.Request;
026import org.apache.cocoon.environment.Session;
027
028import org.ametys.core.authentication.AuthenticateAction;
029import org.ametys.core.authentication.CredentialProvider;
030import org.ametys.core.authentication.LogoutCapable;
031import org.ametys.core.user.CurrentUserProvider;
032import org.ametys.core.user.UserIdentity;
033import org.ametys.plugins.core.impl.user.AvalonCurrentUserProvider;
034import org.ametys.plugins.site.Site;
035
036/**
037 * {@link CurrentUserProvider} able to logout from a site.
038 */
039public class FrontCurrentUserProvider extends AvalonCurrentUserProvider
040{
041    @Override
042    public void logout(Redirector redirector) throws ProcessingException
043    {
044        Map objectModel = ContextHelper.getObjectModel(_context);
045        Request request = ObjectModelHelper.getRequest(objectModel);
046        Session session = request.getSession(false);
047        
048        if (session != null)
049        {
050            // First check for sites' user
051            CredentialProvider cp = FrontAuthenticateAction.getCredentialProviderFromSession(request);
052            
053            if (cp == null)
054            {
055                // then check for application user
056                cp = AuthenticateAction.getCredentialProviderFromSession(request);
057            }
058            
059            if (cp instanceof LogoutCapable)
060            {
061                // Logout process
062                ((LogoutCapable) cp).logout(redirector); 
063            }
064            
065            // Invalidate session
066            session.invalidate();
067        }
068    }
069    
070    @Override
071    public UserIdentity getUser()
072    {
073        UserIdentity siteUser = _getUserInCurrentSite();
074        if (siteUser != null)
075        {
076            return siteUser;
077        }
078        else
079        {
080            return super.getUser();
081        }
082        
083    }
084    
085    private UserIdentity _getUserInCurrentSite()
086    {
087        try 
088        {
089            Map objectModel = (Map) _context.get(ContextHelper.CONTEXT_OBJECT_MODEL);
090            Request request = ObjectModelHelper.getRequest(objectModel);
091            if (request != null)
092            {
093                Site site = (Site) request.getAttribute("site");
094                if (site != null)
095                {
096                    return FrontAuthenticateAction.getUserIdentityFromSession(request, site.getName());
097                }
098            }
099        } 
100        catch (ContextException ce) 
101        {
102            // No context => no current site
103        }
104        
105        return null;
106    }
107}