001/* 002 * Copyright 2015 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 */ 016 017package org.ametys.web.repository.comment; 018 019import java.util.List; 020import java.util.Map; 021 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024import org.apache.avalon.framework.service.Serviceable; 025import org.apache.cocoon.environment.ObjectModelHelper; 026import org.apache.cocoon.environment.Request; 027import org.apache.commons.lang.StringUtils; 028 029import org.ametys.cms.repository.Content; 030import org.ametys.cms.repository.comment.CommentableContent; 031import org.ametys.cms.repository.comment.DefaultCommentManagerExtensionPoint; 032import org.ametys.core.right.RightManager; 033import org.ametys.core.user.CurrentUserProvider; 034import org.ametys.plugins.repository.AmetysObjectResolver; 035import org.ametys.runtime.i18n.I18nizableText; 036import org.ametys.web.cache.PageHelper; 037import org.ametys.web.repository.content.WebContent; 038import org.ametys.web.repository.page.Page; 039import org.ametys.web.repository.site.Site; 040 041/** 042 * Extension point to handle comments in the web 043 */ 044public class WebCommentManagerExtensionPoint extends DefaultCommentManagerExtensionPoint implements Serviceable 045{ 046 private RightManager _rightManager; 047 private AmetysObjectResolver _resolver; 048 private PageHelper _pageHelper; 049 private CurrentUserProvider _currentUserProvider; 050 051 public void service(ServiceManager smanager) throws ServiceException 052 { 053 _rightManager = (RightManager) smanager.lookup(RightManager.ROLE); 054 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 055 _pageHelper = (PageHelper) smanager.lookup(PageHelper.ROLE); 056 _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE); 057 } 058 059 @Override 060 public boolean isValidatedByDefault(Content content) 061 { 062 if (content instanceof WebContent) 063 { 064 Site site = ((WebContent) content).getSite(); 065 String validation = site.getValue("site-contents-comments-postvalidation"); 066 067 if (StringUtils.isNotEmpty(validation) && validation.equals("post")) 068 { 069 return true; 070 } 071 else if (StringUtils.isNotEmpty(validation) && validation.equals("pre")) 072 { 073 return false; 074 } 075 else // global 076 { 077 return super.isValidatedByDefault(content); 078 } 079 } 080 081 return super.isValidatedByDefault(content); 082 083 } 084 085 @Override 086 public boolean isCaptchaRequired(Content content, Map objectModel) 087 { 088 Request request = ObjectModelHelper.getRequest(objectModel); 089 String pageId = request.getParameter("page-id"); 090 091 if (StringUtils.isNotEmpty(pageId)) 092 { 093 Page page = _resolver.resolveById(pageId); 094 return _pageHelper.isCaptchaRequired(page); 095 } 096 097 // Global 098 return super.isCaptchaRequired(content, objectModel); 099 } 100 101 @Override 102 public List<I18nizableText> getErrors(CommentableContent content, Map objectModel) 103 { 104 List<I18nizableText> errors = super.getErrors(content, objectModel); 105 106 if (content instanceof WebContent) 107 { 108 WebContent wContent = (WebContent) content; 109 for (Page page : wContent.getReferencingPages()) 110 { 111 if (!_rightManager.hasReadAccess(_currentUserProvider.getUser(), page)) 112 { 113 errors.add(new I18nizableText("plugin.web", "PLUGINS_WEB_CONTENT_COMMENTS_ADD_ERROR_RIGHTS")); 114 return errors; 115 } 116 } 117 } 118 119 return errors; 120 } 121}