Commit 68873eb155b401fb9d1890e3974422544a1a1cc5

Authored by Rodrigo Souto
1 parent 7d22740a

Adding environment option to ask captcha on comments to logged users too

(ActionItem2259)
app/controllers/public/content_viewer_controller.rb
... ... @@ -119,7 +119,7 @@ class ContentViewerController < ApplicationController
119 119 def add_comment
120 120 @comment.author = user if logged_in?
121 121 @comment.article = @page
122   - if (logged_in? || verify_recaptcha(:model => @comment, :message => _('Please type the words correctly'))) && @comment.save
  122 + if (pass_without_comment_captcha? || verify_recaptcha(:model => @comment, :message => _('Please type the words correctly'))) && @comment.save
123 123 @page.touch
124 124 @comment = nil # clear the comment form
125 125 redirect_to :action => 'view_page', :profile => params[:profile], :page => @page.explode_path, :view => params[:view]
... ... @@ -128,6 +128,11 @@ class ContentViewerController < ApplicationController
128 128 end
129 129 end
130 130  
  131 + def pass_without_comment_captcha?
  132 + logged_in? && !environment.enabled?('captcha_for_logged_users')
  133 + end
  134 + helper_method :pass_without_comment_captcha?
  135 +
131 136 def remove_comment
132 137 @comment = @page.comments.find(params[:remove_comment])
133 138 if (user == @comment.author || user == @page.profile || user.has_permission?(:moderate_comments, @page.profile))
... ...
app/models/environment.rb
... ... @@ -120,7 +120,8 @@ class Environment < ActiveRecord::Base
120 120 'enterprises_are_validated_when_created' => __('Enterprises are validated when created'),
121 121 'show_balloon_with_profile_links_when_clicked' => _('Show a balloon with profile links when a profile image is clicked'),
122 122 'xmpp_chat' => _('XMPP/Jabber based chat'),
123   - 'show_zoom_button_on_article_images' => _('Show a zoom link on all article images')
  123 + 'show_zoom_button_on_article_images' => _('Show a zoom link on all article images'),
  124 + 'captcha_for_logged_users' => _('Ask captcha to comment for logged users too'),
124 125 }
125 126 end
126 127  
... ...
app/views/content_viewer/_comment_form.rhtml
1 1 <script type="text/javascript">
2 2 function submit_comment_form(button) {
3   - <% if logged_in? %>
  3 + <% if pass_without_comment_captcha? %>
4 4 button.form.confirm.value = 'true';
5 5 button.disabled = true;
6 6 button.form.submit();
... ... @@ -42,7 +42,7 @@ function submit_comment_form(button) {
42 42 <%= content_tag('a', '', :name => 'comment_form') + _('Post a comment') %>
43 43 </h4>
44 44  
45   -<% unless logged_in? %>
  45 +<% unless pass_without_comment_captcha? %>
46 46 <div id="recaptcha-container" style="display: none">
47 47 <h3><%= _('Please type the two words below') %></h3>
48 48 <%= recaptcha_tags(:display => { :theme => 'clean' }, :ajax => true) %>
... ... @@ -72,9 +72,11 @@ function submit_comment_form(button) {
72 72 <%= _('If you are a registered user, you can login and be automatically recognized.') %>
73 73 </p>
74 74  
  75 + <% end %>
  76 +
  77 + <% unless pass_without_comment_captcha? %>
75 78 <%= hidden_field_tag(:recaptcha_response_field, nil, :id => nil) %>
76 79 <%= hidden_field_tag(:recaptcha_challenge_field, nil, :id => nil) %>
77   -
78 80 <% end %>
79 81  
80 82 <%= labelled_form_field(_('Title'), text_field(:comment, :title)) %>
... ...
test/functional/content_viewer_controller_test.rb
... ... @@ -1371,4 +1371,32 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase
1371 1371 assert_no_tag :tag => 'body', :attributes => { :class => /profile-homepage/ }
1372 1372 end
1373 1373  
  1374 + should 'ask for captcha if user not logged' do
  1375 + article = profile.articles.build(:name => 'test')
  1376 + article.save!
  1377 +
  1378 + @controller.stubs(:verify_recaptcha).returns(false)
  1379 + post :view_page, :profile => profile.identifier, :page => ['test'], :comment => {:body => "Some comment...", :author => profile}, :confirm => 'true'
  1380 + assert_not_nil assigns(:comment)
  1381 +
  1382 + @controller.stubs(:verify_recaptcha).returns(true)
  1383 + post :view_page, :profile => profile.identifier, :page => ['test'], :comment => {:body => "Some comment...", :author => profile}, :confirm => 'true'
  1384 + assert_nil assigns(:comment)
  1385 + end
  1386 +
  1387 + should 'ask for captcha if environment defines even with logged user' do
  1388 + article = profile.articles.build(:name => 'test')
  1389 + article.save!
  1390 + login_as('testinguser')
  1391 + @controller.stubs(:verify_recaptcha).returns(false)
  1392 +
  1393 + post :view_page, :profile => profile.identifier, :page => ['test'], :comment => {:body => "Some comment...", :author => profile}, :confirm => 'true'
  1394 + assert_nil assigns(:comment)
  1395 +
  1396 + environment.enable('captcha_for_logged_users')
  1397 + environment.save!
  1398 +
  1399 + post :view_page, :profile => profile.identifier, :page => ['test'], :comment => {:body => "Some comment...", :author => profile}, :confirm => 'true'
  1400 + assert_not_nil assigns(:comment)
  1401 + end
1374 1402 end
... ...