Commit 1b6854864b958b1a34dab970ed88c81709ac796d

Authored by JoenioCosta
1 parent 929da83d

ActionItem529: moderator/admin can remove comments from articles

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2194 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/public/content_viewer_controller.rb
@@ -72,7 +72,7 @@ class ContentViewerController < PublicController @@ -72,7 +72,7 @@ class ContentViewerController < PublicController
72 72
73 def remove_comment 73 def remove_comment
74 @comment = @page.comments.find(params[:remove_comment]) 74 @comment = @page.comments.find(params[:remove_comment])
75 - if (user == @comment.author) || (user == @page.profile) 75 + if (user == @comment.author || user == @page.profile || user.has_permission?(:moderate_comments, @page.profile))
76 @comment.destroy 76 @comment.destroy
77 flash[:notice] = _('Comment succesfully deleted') 77 flash[:notice] = _('Comment succesfully deleted')
78 end 78 end
app/models/profile.rb
@@ -34,6 +34,7 @@ class Profile < ActiveRecord::Base @@ -34,6 +34,7 @@ class Profile < ActiveRecord::Base
34 'manage_friends' => N_('Manage friends'), 34 'manage_friends' => N_('Manage friends'),
35 'validate_enterprise' => N_('Validate enterprise'), 35 'validate_enterprise' => N_('Validate enterprise'),
36 'perform_task' => N_('Perform task'), 36 'perform_task' => N_('Perform task'),
  37 + 'moderate_comments' => N_('Moderate comments'),
37 } 38 }
38 39
39 acts_as_accessible 40 acts_as_accessible
app/views/content_viewer/_comment.rhtml
1 <%= content_tag('a', '', :name => comment.anchor) %> 1 <%= content_tag('a', '', :name => comment.anchor) %>
2 <div class="article-comment<%= ' comment-from-owner' if ( comment.author && (@page.profile.name == comment.author.name) ) %> comment-logged-<%= comment.author ? 'in' : 'out' %>"> 2 <div class="article-comment<%= ' comment-from-owner' if ( comment.author && (@page.profile.name == comment.author.name) ) %> comment-logged-<%= comment.author ? 'in' : 'out' %>">
3 - <% if logged_in? && (user == @page.profile || user == comment.author) %> 3 + <% if logged_in? && (user == @page.profile || user == comment.author || user.has_permission?(:moderate_comments, @page.profile)) %>
4 <% button_bar(:style => 'float: right; margin-top: 0;') do %> 4 <% button_bar(:style => 'float: right; margin-top: 0;') do %>
5 <%= icon_button(:delete, _('Remove'), { :remove_comment => comment.id }, :method => :post, :confirm => _('Are you sure you want to remove this comment?')) %> 5 <%= icon_button(:delete, _('Remove'), { :remove_comment => comment.id }, :method => :post, :confirm => _('Are you sure you want to remove this comment?')) %>
6 <% end %> 6 <% end %>
db/migrate/045_more_new_permissions.rb 0 → 100644
@@ -0,0 +1,21 @@ @@ -0,0 +1,21 @@
  1 +class MoreNewPermissions < ActiveRecord::Migration
  2 + def self.up
  3 + admin = Profile::Roles.admin
  4 + admin.permissions += ['moderate_comments']
  5 + admin.save
  6 +
  7 + moderator = Profile::Roles.moderator
  8 + moderator.permissions += ['moderate_comments']
  9 + moderator.save
  10 + end
  11 +
  12 + def self.down
  13 + admin = Profile::Roles.admin
  14 + admin.permissions -= ['moderate_comments']
  15 + admin.save
  16 +
  17 + moderator = Profile::Roles.moderator
  18 + moderator.permissions -= ['moderate_comments']
  19 + moderator.save
  20 + end
  21 +end
@@ -9,7 +9,7 @@ @@ -9,7 +9,7 @@
9 # 9 #
10 # It's strongly recommended to check this file into your version control system. 10 # It's strongly recommended to check this file into your version control system.
11 11
12 -ActiveRecord::Schema.define(:version => 44) do 12 +ActiveRecord::Schema.define(:version => 45) do
13 13
14 create_table "article_versions", :force => true do |t| 14 create_table "article_versions", :force => true do |t|
15 t.integer "article_id" 15 t.integer "article_id"
test/fixtures/roles.yml
@@ -3,7 +3,7 @@ one: @@ -3,7 +3,7 @@ one:
3 id: 1 3 id: 1
4 name: 'member' 4 name: 'member'
5 permissions: 5 permissions:
6 - - post_content 6 + - post_content
7 two: 7 two:
8 id: 2 8 id: 2
9 name: 'owner' 9 name: 'owner'
@@ -16,7 +16,7 @@ three: @@ -16,7 +16,7 @@ three:
16 id: 3 16 id: 3
17 name: 'moderator' 17 name: 'moderator'
18 permissions: 18 permissions:
19 - - manage_memberships 19 + - manage_memberships
20 four: 20 four:
21 id: 4 21 id: 4
22 name: 'admin' 22 name: 'admin'
@@ -27,6 +27,7 @@ four: @@ -27,6 +27,7 @@ four:
27 - manage_environment_categories 27 - manage_environment_categories
28 - manage_environment_roles 28 - manage_environment_roles
29 - manage_environment_validators 29 - manage_environment_validators
  30 + - moderate_comments
30 profile_admin: 31 profile_admin:
31 id: 5 32 id: 5
32 key: 'profile_admin' 33 key: 'profile_admin'
@@ -34,6 +35,7 @@ profile_admin: @@ -34,6 +35,7 @@ profile_admin:
34 system: true 35 system: true
35 permissions: 36 permissions:
36 - edit_profile_design 37 - edit_profile_design
  38 + - moderate_comments
37 profile_member: 39 profile_member:
38 id: 6 40 id: 6
39 key: 'profile_member' 41 key: 'profile_member'
@@ -48,3 +50,5 @@ profile_moderator: @@ -48,3 +50,5 @@ profile_moderator:
48 key: 'profile_moderator' 50 key: 'profile_moderator'
49 name: 'Profile Moderator' 51 name: 'Profile Moderator'
50 system: true 52 system: true
  53 + permissions:
  54 + - moderate_comments
test/functional/content_viewer_controller_test.rb
@@ -160,7 +160,6 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase @@ -160,7 +160,6 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase
160 post :view_page, :profile => profile.identifier, :page => [ 'test' ], :remove_comment => comment.id 160 post :view_page, :profile => profile.identifier, :page => [ 'test' ], :remove_comment => comment.id
161 assert_response :redirect 161 assert_response :redirect
162 end 162 end
163 -  
164 end 163 end
165 164
166 should 'not be able to post comment while inverse captcha field filled' do 165 should 'not be able to post comment while inverse captcha field filled' do
@@ -174,6 +173,19 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase @@ -174,6 +173,19 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase
174 end 173 end
175 end 174 end
176 175
  176 + should 'be able to remove comments if is moderator' do
  177 + commenter = create_user('commenter_user').person
  178 + community = Community.create!(:name => 'Community test', :identifier => 'community-test')
  179 + article = community.articles.create!(:name => 'test')
  180 + comment = article.comments.create!(:author => commenter, :title => 'a comment', :body => 'lalala')
  181 + community.add_moderator(profile)
  182 + login_as profile.identifier
  183 + assert_difference Comment, :count, -1 do
  184 + post :view_page, :profile => community.identifier, :page => [ 'test' ], :remove_comment => comment.id
  185 + assert_response :redirect
  186 + end
  187 + end
  188 +
177 should 'render inverse captcha field' do 189 should 'render inverse captcha field' do
178 profile = create_user('popstar').person 190 profile = create_user('popstar').person
179 page = profile.articles.build(:name => 'myarticle', :body => 'the body of the text') 191 page = profile.articles.build(:name => 'myarticle', :body => 'the body of the text')
test/unit/organization_test.rb
@@ -188,7 +188,6 @@ class OrganizationTest &lt; Test::Unit::TestCase @@ -188,7 +188,6 @@ class OrganizationTest &lt; Test::Unit::TestCase
188 assert_not_includes c.members, p 188 assert_not_includes c.members, p
189 end 189 end
190 190
191 - # FIXME why members dont return moderators???  
192 should 'allow to add new moderator' do 191 should 'allow to add new moderator' do
193 o = Organization.create!(:name => 'my test profile', :identifier => 'mytestprofile') 192 o = Organization.create!(:name => 'my test profile', :identifier => 'mytestprofile')
194 p = create_user('myanothertestuser').person 193 p = create_user('myanothertestuser').person
@@ -198,4 +197,11 @@ class OrganizationTest &lt; Test::Unit::TestCase @@ -198,4 +197,11 @@ class OrganizationTest &lt; Test::Unit::TestCase
198 assert o.members.include?(p), "Organization should add the new moderator" 197 assert o.members.include?(p), "Organization should add the new moderator"
199 end 198 end
200 199
  200 + should 'moderator has moderate_comments permission' do
  201 + o = Organization.create!(:name => 'my test profile', :identifier => 'mytestprofile')
  202 + p = create_user('myanothertestuser').person
  203 + o.add_moderator(p)
  204 + assert p.has_permission?(:moderate_comments, o)
  205 + end
  206 +
201 end 207 end