001/*
002 *  Copyright 2013 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.cms.repository.comment.actions;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020import org.apache.cocoon.acting.ServiceableAction;
021import org.apache.cocoon.environment.Request;
022
023import org.ametys.cms.repository.Content;
024import org.ametys.cms.repository.comment.CommentableContent;
025import org.ametys.core.observation.ObservationManager;
026import org.ametys.core.right.RightManager;
027import org.ametys.core.right.RightManager.RightResult;
028import org.ametys.core.user.CurrentUserProvider;
029import org.ametys.core.user.UserIdentity;
030import org.ametys.plugins.repository.AmetysObjectResolver;
031import org.ametys.plugins.repository.provider.RequestAttributeWorkspaceSelector;
032
033/**
034 * Abstract action on comments
035 *
036 */
037public abstract class AbstractCommentAction extends ServiceableAction
038{
039    /** The request parameter name for content id */
040    public static final String PARAMETER_CONTENTID = "content-id";
041    /** The request parameter name for content id */
042    public static final String PARAMETER_COMMENTID = "comment-id";
043
044    /** The ametys object resolver */
045    protected AmetysObjectResolver _resolver;
046    /** The current user provider */
047    protected CurrentUserProvider _userProvider;
048    /** The rights manager */
049    protected RightManager _rightManager;
050    /** The observation manager */
051    protected ObservationManager _observationManager;
052    
053    @Override
054    public void service(ServiceManager smanager) throws ServiceException
055    {
056        super.service(smanager);
057        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
058        _rightManager = (RightManager) smanager.lookup(RightManager.ROLE);
059        _userProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
060        _observationManager = (ObservationManager) smanager.lookup(ObservationManager.ROLE);
061    }
062    
063    /**
064     * Get the current user
065     * @return The current user
066     */
067    protected UserIdentity getCurrentUser ()
068    {
069        return _userProvider.getUser();
070    }
071
072    /**
073     * Determines if connected user has right on content
074     * @param rightId The right id
075     * @param content The content
076     * @return true if user has right
077     */
078    protected boolean hasRight (String rightId, Content content)
079    {
080        UserIdentity user = null;
081        user = _userProvider.getUser();
082        
083        return _rightManager.hasRight(user, "CMS_Rights_CommentModerate", content) == RightResult.RIGHT_ALLOW;
084    }
085    
086    /**
087     * Get the commentable content
088     * @param request The request 
089     * @param contentId The content id
090     * @return The content
091     */
092    protected CommentableContent getContent (Request request, String contentId)
093    {
094        String currentWorkspace = RequestAttributeWorkspaceSelector.getForcedWorkspace(request);
095
096        try
097        {
098            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, "default");
099            return _resolver.resolveById(contentId);
100        }
101        finally
102        {
103            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWorkspace);
104        }
105    }
106}