001/* 002 * Copyright 2018 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.cms.repository.comment.actions; 018 019import java.util.HashMap; 020import java.util.Map; 021 022import org.apache.avalon.framework.parameters.Parameters; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.cocoon.environment.ObjectModelHelper; 026import org.apache.cocoon.environment.Redirector; 027import org.apache.cocoon.environment.Request; 028import org.apache.cocoon.environment.SourceResolver; 029import org.apache.commons.lang.StringUtils; 030 031import org.ametys.cms.ObservationConstants; 032import org.ametys.cms.content.ContentHelper; 033import org.ametys.cms.repository.comment.Comment; 034import org.ametys.cms.repository.comment.CommentManagerExtensionPoint; 035import org.ametys.cms.repository.comment.CommentableContent; 036import org.ametys.core.cocoon.ActionResultGenerator; 037import org.ametys.core.observation.Event; 038import org.ametys.core.right.RightManager.RightResult; 039import org.ametys.core.user.User; 040import org.ametys.core.user.UserIdentity; 041import org.ametys.plugins.core.user.UserHelper; 042import org.ametys.plugins.repository.metadata.UnknownMetadataException; 043 044/** 045 * Delete a comment 046 */ 047public class DeleteCommentAction extends AbstractCommentAction 048{ 049 /** The listeners for this action */ 050 protected CommentManagerExtensionPoint _commentManager; 051 052 /** The content helper */ 053 protected ContentHelper _contentHelper; 054 055 /** The user helper */ 056 protected UserHelper _userHelper; 057 058 @Override 059 public void service(ServiceManager smanager) throws ServiceException 060 { 061 super.service(smanager); 062 _commentManager = (CommentManagerExtensionPoint) smanager.lookup(CommentManagerExtensionPoint.ROLE); 063 _contentHelper = (ContentHelper) smanager.lookup(ContentHelper.ROLE); 064 _userHelper = (UserHelper) smanager.lookup(UserHelper.ROLE); 065 } 066 067 @Override 068 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 069 { 070 Request request = ObjectModelHelper.getRequest(objectModel); 071 072 Map<String, Object> results = new HashMap<>(); 073 074 String contentId = request.getParameter(PARAMETER_CONTENTID); 075 if (StringUtils.isBlank(contentId)) 076 { 077 throw new IllegalArgumentException("Missing or empty 'content-id' parameter to like a comment"); 078 } 079 080 String commentId = request.getParameter(PARAMETER_COMMENTID); 081 if (StringUtils.isBlank(commentId)) 082 { 083 throw new IllegalArgumentException("Missing or empty 'comment-id' parameter to like a comment"); 084 } 085 086 CommentableContent content = getContent(request, contentId); 087 Comment comment = content.getComment(commentId); 088 089 UserIdentity currentUser = getCurrentUser(); 090 User user = _userHelper.getUser(currentUser); 091 Map<String, Object> commentParams = new HashMap<>(); 092 commentParams.put("id", commentId); 093 commentParams.put("contentId", contentId); 094 commentParams.put("contentTitle", _contentHelper.getTitle(content)); 095 096 String authorEmail = comment.getAuthorEmail(); 097 String userEmail = user.getEmail(); 098 boolean isUserOwnComment = StringUtils.isNotBlank(userEmail) && StringUtils.equals(authorEmail, userEmail); 099 100 if (isUserOwnComment || _rightManager.hasRight(currentUser, "CMS_Rights_CommentModerate", content).equals(RightResult.RIGHT_ALLOW)) 101 { 102 try 103 { 104 Map<String, Object> eventParams = new HashMap<>(); 105 eventParams.put(ObservationConstants.ARGS_CONTENT, content); 106 eventParams.put(ObservationConstants.ARGS_COMMENT_ID, comment.getId()); 107 eventParams.put(ObservationConstants.ARGS_COMMENT_AUTHOR, comment.getAuthorName()); 108 eventParams.put(ObservationConstants.ARGS_COMMENT_AUTHOR_EMAIL, comment.getAuthorEmail()); 109 eventParams.put(ObservationConstants.ARGS_COMMENT_VALIDATED, comment.isValidated()); 110 eventParams.put("comment.content", comment.getContent()); 111 112 _observationManager.notify(new Event(ObservationConstants.EVENT_CONTENT_COMMENT_DELETING, getCurrentUser(), eventParams)); 113 114 comment.remove(); 115 content.saveChanges(); 116 117 _observationManager.notify(new Event(ObservationConstants.EVENT_CONTENT_COMMENT_DELETED, getCurrentUser(), eventParams)); 118 119 results.put("deleted", true); 120 } 121 catch (UnknownMetadataException e) 122 { 123 getLogger().error("Can not remove a non existing comment", e); 124 results.put("deleted", false); 125 } 126 } 127 else 128 { 129 results.put("deleted", false); 130 } 131 132 results.put("contentId", content.getId()); 133 results.put("commentId", comment.getId()); 134 135 request.setAttribute(ActionResultGenerator.MAP_REQUEST_ATTR, results); 136 return EMPTY_MAP; 137 } 138}