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