001/* 002 * Copyright 2020 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.web.repository.comment; 017 018import java.util.Arrays; 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.cocoon.components.ContextHelper; 025import org.apache.cocoon.environment.Request; 026import org.apache.commons.lang.StringUtils; 027 028import org.ametys.cms.repository.Content; 029import org.ametys.runtime.plugin.component.DeferredServiceable; 030import org.ametys.web.cache.PageHelper; 031import org.ametys.web.repository.content.WebContent; 032import org.ametys.web.repository.page.Page; 033import org.ametys.web.repository.site.Site; 034 035/** 036 * Comments DAO for web content 037 */ 038public class CommentsDAO extends org.ametys.cms.repository.comment.CommentsDAO implements DeferredServiceable 039{ 040 /** The page helper */ 041 protected PageHelper _pageHelper; 042 043 @Override 044 public void deferredService(ServiceManager smanager) throws ServiceException 045 { 046 _pageHelper = (PageHelper) smanager.lookup(PageHelper.ROLE); 047 } 048 049 @Override 050 public boolean isValidatedByDefault(Content content) 051 { 052 if (content instanceof WebContent) 053 { 054 Site site = ((WebContent) content).getSite(); 055 String validation = site.getValue("site-contents-comments-postvalidation"); 056 057 if (StringUtils.isNotEmpty(validation) && validation.equals("post")) 058 { 059 return true; 060 } 061 else if (StringUtils.isNotEmpty(validation) && validation.equals("pre")) 062 { 063 return false; 064 } 065 else // global 066 { 067 return super.isValidatedByDefault(content); 068 } 069 } 070 071 return super.isValidatedByDefault(content); 072 } 073 074 @Override 075 public boolean isCaptchaRequired(Content content, Map<String, Object> formValues) 076 { 077 Request request = ContextHelper.getRequest(_context); 078 String pageId = request.getParameter("pageId"); 079 if (StringUtils.isEmpty(pageId)) 080 { 081 pageId = (String) formValues.get("pageId"); 082 } 083 084 if (StringUtils.isNotEmpty(pageId)) 085 { 086 Page page = _resolver.resolveById(pageId); 087 return _pageHelper.isCaptchaRequired(page); 088 } 089 090 // Global 091 return super.isCaptchaRequired(content, formValues); 092 } 093 094 @Override 095 protected List<String> getUserPopulationsContexts(Request request, Map<String, Object> contextualParameters) 096 { 097 String siteName = (String) contextualParameters.get("siteName"); 098 return Arrays.asList("/sites/" + siteName, "/sites-fo/" + siteName); 099 } 100}