001/*
002 *  Copyright 2012 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.plugins.core.impl.user;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.context.Context;
021import org.apache.avalon.framework.context.ContextException;
022import org.apache.avalon.framework.context.Contextualizable;
023import org.apache.avalon.framework.thread.ThreadSafe;
024import org.apache.cocoon.ProcessingException;
025import org.apache.cocoon.components.ContextHelper;
026import org.apache.cocoon.environment.ObjectModelHelper;
027import org.apache.cocoon.environment.Redirector;
028import org.apache.cocoon.environment.Request;
029import org.apache.cocoon.environment.Session;
030
031import org.ametys.core.authentication.AuthenticateAction;
032import org.ametys.core.authentication.CredentialProvider;
033import org.ametys.core.authentication.LogoutCapable;
034import org.ametys.core.user.CurrentUserProvider;
035import org.ametys.core.user.UserIdentity;
036import org.ametys.runtime.plugin.component.AbstractLogEnabled;
037
038/**
039 * Provides the current user from session.<br>
040 */
041public class AvalonCurrentUserProvider extends AbstractLogEnabled implements CurrentUserProvider, Contextualizable, ThreadSafe
042{
043    /** Avalon context. */
044    protected Context _context;
045    
046    public void contextualize(Context context) throws ContextException
047    {
048        _context = context;        
049    }
050    
051    public UserIdentity getUser()
052    {
053        UserIdentity user = null;
054        
055        try
056        {
057            Request request = _getRequest();
058            user = AuthenticateAction.getUserIdentityFromSession(request);
059        }
060        catch (Exception e)
061        {
062            getLogger().info("Unable to retrieve current authenticated user", e);
063        }
064        
065        if (user == null)
066        {
067            getLogger().debug("There is no current user");
068        }
069        else
070        {
071            getLogger().debug("Providing current user as: {}", user);
072        }
073        
074        return user;
075    }
076    
077    private Request _getRequest()
078    {
079        Map objectModel = ContextHelper.getObjectModel(_context);
080        return ObjectModelHelper.getRequest(objectModel);
081    }
082    
083    @Override
084    public void logout(Redirector redirector) throws ProcessingException
085    {
086        Map objectModel = ContextHelper.getObjectModel(_context);
087        Request request = ObjectModelHelper.getRequest(objectModel);
088        Session session = request.getSession(false);
089        
090        if (session != null)
091        {
092            CredentialProvider cp = AuthenticateAction.getCredentialProviderFromSession(request);
093            
094            if (cp instanceof LogoutCapable)
095            {
096                // Logout process
097                ((LogoutCapable) cp).logout(redirector); 
098            }
099            
100            // Invalidate session
101            session.invalidate();
102        }
103    }
104}