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