diff --git a/app/controllers/public/comment_controller.rb b/app/controllers/public/comment_controller.rb index 35b8b09..2760dbb 100644 --- a/app/controllers/public/comment_controller.rb +++ b/app/controllers/public/comment_controller.rb @@ -86,8 +86,7 @@ class CommentController < ApplicationController def destroy comment = profile.comments_received.find(params[:id]) - could_remove = (user == comment.author || user == comment.profile || user.has_permission?(:moderate_comments, comment.profile)) - if comment && could_remove && comment.destroy + if comment && comment.can_be_destroyed_by?(user) && comment.destroy render :text => {'ok' => true}.to_json, :content_type => 'application/json' else session[:notice] = _("The comment was not removed.") diff --git a/app/models/comment.rb b/app/models/comment.rb index 47920ae..387f413 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -259,4 +259,9 @@ class Comment < ActiveRecord::Base article.moderate_comments? && (author.nil? || article.author != author) end + def can_be_destroyed_by?(user) + return if user.nil? + user == author || user == profile || user.has_permission?(:moderate_comments, profile) + end + end diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index 3d727d4..ded8093 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -593,6 +593,48 @@ class CommentTest < ActiveSupport::TestCase assert comment.need_moderation? end + should 'not be able to destroy comment without user' do + comment = Comment.new + + assert !comment.can_be_destroyed_by?(nil) + end + + should 'not be able to destroy comment' do + user = Person.new + profile = Profile.new + article = Article.new(:profile => profile) + comment = Comment.new(:article => article) + user.expects(:has_permission?).with(:moderate_comments, profile).returns(false) + + assert !comment.can_be_destroyed_by?(user) + end + + should 'be able to destroy comment if is the author' do + user = Person.new + comment = Comment.new(:author => user) + + assert comment.can_be_destroyed_by?(user) + end + + should 'be able to destroy comment if is the profile' do + user = Person.new + article = Article.new(:profile => user) + comment = Comment.new(:article => article) + + assert comment.can_be_destroyed_by?(user) + end + + should 'be able to destroy comment if can moderate_comments on the profile' do + user = Person.new + profile = Profile.new + article = Article.new(:profile => profile) + comment = Comment.new(:article => article) + + user.expects(:has_permission?).with(:moderate_comments, profile).returns(true) + + assert comment.can_be_destroyed_by?(user) + end + private def create_comment(args = {}) -- libgit2 0.21.2