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;
021
022import org.ametys.cms.repository.Content;
023import org.ametys.core.observation.ObservationManager;
024import org.ametys.core.right.RightManager;
025import org.ametys.core.right.RightManager.RightResult;
026import org.ametys.core.user.CurrentUserProvider;
027import org.ametys.core.user.UserIdentity;
028import org.ametys.plugins.repository.AmetysObjectResolver;
029
030/**
031 * Abstract action on comments
032 *
033 */
034public abstract class AbstractCommentAction extends ServiceableAction
035{
036    /** The ametys object resolver */
037    protected AmetysObjectResolver _resolver;
038    /** The current user provider */
039    protected CurrentUserProvider _userProvider;
040    /** The rights manager */
041    protected RightManager _rightManager;
042    /** The observation manager */
043    protected ObservationManager _observationManager;
044    
045    @Override
046    public void service(ServiceManager smanager) throws ServiceException
047    {
048        super.service(smanager);
049        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
050        _rightManager = (RightManager) smanager.lookup(RightManager.ROLE);
051        _userProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
052        _observationManager = (ObservationManager) smanager.lookup(ObservationManager.ROLE);
053    }
054    
055    /**
056     * Get the current user
057     * @return The current user
058     */
059    protected UserIdentity getCurrentUser ()
060    {
061        return _userProvider.getUser();
062    }
063
064    /**
065     * Determines if connected user has right on content
066     * @param rightId The right id
067     * @param content The content
068     * @return true if user has right
069     */
070    protected boolean hasRight (String rightId, Content content)
071    {
072        UserIdentity user = null;
073        user = _userProvider.getUser();
074        
075        return _rightManager.hasRight(user, "CMS_Rights_CommentModerate", content) == RightResult.RIGHT_ALLOW;
076    }
077}