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.site.SiteConfigurationExtensionPoint;
040
041/**
042 * Extension point to handle comments in the web
043 */
044public class WebCommentManagerExtensionPoint extends DefaultCommentManagerExtensionPoint implements Serviceable
045{
046    private SiteConfigurationExtensionPoint _siteConfiguration;
047    private RightManager _rightManager;
048    private AmetysObjectResolver _resolver;
049    private PageHelper _pageHelper;
050    private CurrentUserProvider _currentUserProvider;
051
052    public void service(ServiceManager smanager) throws ServiceException
053    {
054        _siteConfiguration = (SiteConfigurationExtensionPoint) smanager.lookup(SiteConfigurationExtensionPoint.ROLE);
055        _rightManager = (RightManager) smanager.lookup(RightManager.ROLE);
056        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
057        _pageHelper = (PageHelper) smanager.lookup(PageHelper.ROLE);
058        _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
059    }
060    
061    @Override
062    public boolean isValidatedByDefault(Content content)
063    {
064        if (content instanceof WebContent)
065        {
066            String siteName = ((WebContent) content).getSiteName();
067            String validation = _siteConfiguration.getValueAsString(siteName, "site-contents-comments-postvalidation");
068            
069            if (StringUtils.isNotEmpty(validation) && validation.equals("post"))
070            {
071                return true;
072            }
073            else if (StringUtils.isNotEmpty(validation) && validation.equals("pre"))
074            {
075                return false;
076            }
077            else // global
078            {
079                return super.isValidatedByDefault(content);
080            }
081        }
082        
083        return super.isValidatedByDefault(content);
084        
085    }
086    
087    @Override
088    public boolean isCaptchaRequired(Content content, Map objectModel)
089    {
090        Request request = ObjectModelHelper.getRequest(objectModel);
091        String pageId = request.getParameter("page-id");
092        
093        if (StringUtils.isNotEmpty(pageId))
094        {
095            Page page = _resolver.resolveById(pageId);
096            return _pageHelper.isCaptchaRequired(page);
097        }
098        
099        // Global
100        return super.isCaptchaRequired(content, objectModel);
101    }
102    
103    @Override
104    public List<I18nizableText> getErrors(CommentableContent content, Map objectModel)
105    {
106        List<I18nizableText> errors = super.getErrors(content, objectModel);
107        
108        if (content instanceof WebContent)
109        {
110            WebContent wContent = (WebContent) content;
111            for (Page page : wContent.getReferencingPages())
112            {
113                if (!_rightManager.hasReadAccess(_currentUserProvider.getUser(), page))
114                {
115                    errors.add(new I18nizableText("plugin.web", "PLUGINS_WEB_CONTENT_COMMENTS_ADD_ERROR_RIGHTS"));
116                    return errors;
117                }
118            }
119        }
120        
121        return errors;
122    }
123}