001/*
002 *  Copyright 2016 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.runtime.workspaces.admin.authentication;
017
018import java.util.Collections;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.parameters.Parameters;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Redirector;
027import org.apache.cocoon.environment.Request;
028import org.apache.cocoon.environment.SourceResolver;
029
030import org.ametys.core.authentication.AuthenticateAction;
031import org.ametys.core.right.RightManager;
032import org.ametys.runtime.plugin.PluginsManager;
033
034/**
035 * Cocoon action for authenticating users in the administration workspace. 
036 */
037public class AdminAuthenticateAction extends AuthenticateAction
038{
039    /** The runtime rights manager */
040    protected RightManager _rightManager;
041    
042    @Override
043    public void service(ServiceManager smanager) throws ServiceException
044    {
045        super.service(smanager);
046        _rightManager = (RightManager) manager.lookup(RightManager.ROLE);
047    }
048    
049    @Override
050    protected List<String> _getAvailableUserPopulationsIds(Request request, List<String> contexts)
051    {
052        if (PluginsManager.getInstance().isSafeMode() || _userPopulationDAO.getEnabledUserPopulations(false).isEmpty())
053        {
054            return Collections.singletonList(_userPopulationDAO.getAdminPopulation().getId());
055        }
056        else
057        {
058            return super._getAvailableUserPopulationsIds(request, contexts);
059        }
060    }
061
062    @Override
063    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
064    {
065        boolean wasConnected = _currentUserProvider.getUser() != null;
066        
067        Map act = super.act(redirector, resolver, objectModel, source, parameters);
068
069        // When the user just connected, letting the HasNotAdminRightAction throw an AccessDeniedException will clear cookie, and the user will not be really connected
070        // So we do a redirect to here that will store the cookie and then we would let the AccessDeniedException plays
071        if (_currentUserProvider.getUser() != null && !wasConnected)
072        {
073            Request request = ObjectModelHelper.getRequest(objectModel);
074            String queryString = request.getQueryString();
075            redirector.globalRedirect(true, request.getRequestURI() + (queryString != null ? "?" + queryString : ""));
076        }
077        
078        return act;
079    }
080}