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