Commit 68873eb155b401fb9d1890e3974422544a1a1cc5
1 parent
7d22740a
Exists in
master
and in
29 other branches
Adding environment option to ask captcha on comments to logged users too
(ActionItem2259)
Showing
4 changed files
with
41 additions
and
5 deletions
Show diff stats
app/controllers/public/content_viewer_controller.rb
@@ -119,7 +119,7 @@ class ContentViewerController < ApplicationController | @@ -119,7 +119,7 @@ class ContentViewerController < ApplicationController | ||
119 | def add_comment | 119 | def add_comment |
120 | @comment.author = user if logged_in? | 120 | @comment.author = user if logged_in? |
121 | @comment.article = @page | 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 | @page.touch | 123 | @page.touch |
124 | @comment = nil # clear the comment form | 124 | @comment = nil # clear the comment form |
125 | redirect_to :action => 'view_page', :profile => params[:profile], :page => @page.explode_path, :view => params[:view] | 125 | redirect_to :action => 'view_page', :profile => params[:profile], :page => @page.explode_path, :view => params[:view] |
@@ -128,6 +128,11 @@ class ContentViewerController < ApplicationController | @@ -128,6 +128,11 @@ class ContentViewerController < ApplicationController | ||
128 | end | 128 | end |
129 | end | 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 | def remove_comment | 136 | def remove_comment |
132 | @comment = @page.comments.find(params[:remove_comment]) | 137 | @comment = @page.comments.find(params[:remove_comment]) |
133 | if (user == @comment.author || user == @page.profile || user.has_permission?(:moderate_comments, @page.profile)) | 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,7 +120,8 @@ class Environment < ActiveRecord::Base | ||
120 | 'enterprises_are_validated_when_created' => __('Enterprises are validated when created'), | 120 | 'enterprises_are_validated_when_created' => __('Enterprises are validated when created'), |
121 | 'show_balloon_with_profile_links_when_clicked' => _('Show a balloon with profile links when a profile image is clicked'), | 121 | 'show_balloon_with_profile_links_when_clicked' => _('Show a balloon with profile links when a profile image is clicked'), |
122 | 'xmpp_chat' => _('XMPP/Jabber based chat'), | 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 | end | 126 | end |
126 | 127 |
app/views/content_viewer/_comment_form.rhtml
1 | <script type="text/javascript"> | 1 | <script type="text/javascript"> |
2 | function submit_comment_form(button) { | 2 | function submit_comment_form(button) { |
3 | - <% if logged_in? %> | 3 | + <% if pass_without_comment_captcha? %> |
4 | button.form.confirm.value = 'true'; | 4 | button.form.confirm.value = 'true'; |
5 | button.disabled = true; | 5 | button.disabled = true; |
6 | button.form.submit(); | 6 | button.form.submit(); |
@@ -42,7 +42,7 @@ function submit_comment_form(button) { | @@ -42,7 +42,7 @@ function submit_comment_form(button) { | ||
42 | <%= content_tag('a', '', :name => 'comment_form') + _('Post a comment') %> | 42 | <%= content_tag('a', '', :name => 'comment_form') + _('Post a comment') %> |
43 | </h4> | 43 | </h4> |
44 | 44 | ||
45 | -<% unless logged_in? %> | 45 | +<% unless pass_without_comment_captcha? %> |
46 | <div id="recaptcha-container" style="display: none"> | 46 | <div id="recaptcha-container" style="display: none"> |
47 | <h3><%= _('Please type the two words below') %></h3> | 47 | <h3><%= _('Please type the two words below') %></h3> |
48 | <%= recaptcha_tags(:display => { :theme => 'clean' }, :ajax => true) %> | 48 | <%= recaptcha_tags(:display => { :theme => 'clean' }, :ajax => true) %> |
@@ -72,9 +72,11 @@ function submit_comment_form(button) { | @@ -72,9 +72,11 @@ function submit_comment_form(button) { | ||
72 | <%= _('If you are a registered user, you can login and be automatically recognized.') %> | 72 | <%= _('If you are a registered user, you can login and be automatically recognized.') %> |
73 | </p> | 73 | </p> |
74 | 74 | ||
75 | + <% end %> | ||
76 | + | ||
77 | + <% unless pass_without_comment_captcha? %> | ||
75 | <%= hidden_field_tag(:recaptcha_response_field, nil, :id => nil) %> | 78 | <%= hidden_field_tag(:recaptcha_response_field, nil, :id => nil) %> |
76 | <%= hidden_field_tag(:recaptcha_challenge_field, nil, :id => nil) %> | 79 | <%= hidden_field_tag(:recaptcha_challenge_field, nil, :id => nil) %> |
77 | - | ||
78 | <% end %> | 80 | <% end %> |
79 | 81 | ||
80 | <%= labelled_form_field(_('Title'), text_field(:comment, :title)) %> | 82 | <%= labelled_form_field(_('Title'), text_field(:comment, :title)) %> |
test/functional/content_viewer_controller_test.rb
@@ -1371,4 +1371,32 @@ class ContentViewerControllerTest < Test::Unit::TestCase | @@ -1371,4 +1371,32 @@ class ContentViewerControllerTest < Test::Unit::TestCase | ||
1371 | assert_no_tag :tag => 'body', :attributes => { :class => /profile-homepage/ } | 1371 | assert_no_tag :tag => 'body', :attributes => { :class => /profile-homepage/ } |
1372 | end | 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 | end | 1402 | end |