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.plugins.frontedition;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.cocoon.environment.ObjectModelHelper;
023import org.apache.cocoon.environment.Request;
024import org.apache.commons.lang3.StringUtils;
025
026import org.ametys.core.user.CurrentUserProvider;
027import org.ametys.core.user.UserIdentity;
028import org.ametys.core.util.I18nizableSerializer;
029import org.ametys.plugins.repository.AmetysObjectResolver;
030import org.ametys.runtime.authentication.AccessDeniedException;
031import org.ametys.web.CheckNotFrontAction;
032import org.ametys.web.WebAuthenticateAction;
033import org.ametys.web.repository.page.Page;
034
035/**
036 * Dispatch generator for the front edition
037 */
038public class DispatchGenerator extends org.ametys.core.ui.dispatcher.DispatchGenerator
039{
040    private AmetysObjectResolver _resolver;
041    private CurrentUserProvider _currentUserProvider;
042
043    @Override
044    public void service(ServiceManager smanager) throws ServiceException
045    {
046        super.service(smanager);
047        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
048        _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
049    }
050    
051    @Override
052    protected void _setContextInRequestAttributes(Map<String, Object> contextAsMap)
053    {
054        super._setContextInRequestAttributes(contextAsMap);
055        
056        Request request = ObjectModelHelper.getRequest(objectModel);
057        if (contextAsMap.containsKey("pageId"))
058        {
059            String pageId = (String) contextAsMap.get("pageId");
060            if (!StringUtils.isBlank(pageId))
061            {
062                Page page = _resolver.resolveById(pageId);
063                request.setAttribute(Page.class.getName(), page);
064            }
065        }
066        
067        if (!AmetysFrontEditionHelper.hasFrontEditionRight())
068        {
069            throw new AccessDeniedException("User " + _currentUserProvider.getUser() + " is not allowed to access front edition");
070        }
071        
072        // Force locale to FO edition locale
073        request.setAttribute(I18nizableSerializer.REQUEST_ATTR_LOCALE, contextAsMap.get("locale"));
074        
075        request.setAttribute(CheckNotFrontAction.CAN_COME_FROM_FRONT_ATTRIBUTE, true);
076    }
077    
078    @Override
079    protected Map<String, Object> transmitAttributes(Map<String, Object> attributes)
080    {
081        Map<String, Object> transmitAttributes = super.transmitAttributes(attributes);
082        
083        if (attributes.containsKey(WebAuthenticateAction.REQUEST_ATTRIBUTE_FRONTOFFICE_USERIDENTITY))
084        {
085            UserIdentity frontUserIdentity = (UserIdentity) attributes.get(WebAuthenticateAction.REQUEST_ATTRIBUTE_FRONTOFFICE_USERIDENTITY);
086            transmitAttributes.put(WebAuthenticateAction.REQUEST_ATTRIBUTE_FRONTOFFICE_USERIDENTITY, frontUserIdentity);
087        }
088        
089        if (attributes.containsKey("rendering-context"))
090        {
091            transmitAttributes.put("rendering-context", attributes.get("rendering-context"));
092        }
093        
094        if (attributes.containsKey(CheckNotFrontAction.CAN_COME_FROM_FRONT_ATTRIBUTE))
095        {
096            transmitAttributes.put(CheckNotFrontAction.CAN_COME_FROM_FRONT_ATTRIBUTE, attributes.get(CheckNotFrontAction.CAN_COME_FROM_FRONT_ATTRIBUTE));
097        }
098
099        if (attributes.containsKey("site"))
100        {
101            transmitAttributes.put("site", attributes.get("site"));
102        }
103
104        return transmitAttributes;
105    }
106}