diff --git a/plugins/require_auth_to_comment/features/require_auth_to_comment.feature b/plugins/require_auth_to_comment/features/require_auth_to_comment.feature new file mode 100644 index 0000000..9a8ce8d --- /dev/null +++ b/plugins/require_auth_to_comment/features/require_auth_to_comment.feature @@ -0,0 +1,15 @@ +Feature: require authentication to comment + + Background: + Given plugin RequireAuthToCommentPlugin is enabled on environment + And the following users + | login | + | bozo | + + Scenario: enabling unauthenticated comment per profile + Given I am logged in as "bozo" + When I edit my profile + And I check "Accept comments from unauthenticated users" + And I press "Save" + Then I edit my profile + And the "Accept comments from unauthenticated users" checkbox should be checked diff --git a/plugins/require_auth_to_comment/lib/require_auth_to_comment_plugin.rb b/plugins/require_auth_to_comment/lib/require_auth_to_comment_plugin.rb new file mode 100644 index 0000000..9d333c5 --- /dev/null +++ b/plugins/require_auth_to_comment/lib/require_auth_to_comment_plugin.rb @@ -0,0 +1,45 @@ +require_dependency 'require_auth_to_comment_plugin/ext/profile' + +class RequireAuthToCommentPlugin < Noosfero::Plugin + + include ActionView::Helpers::TagHelper + include ActionView::Helpers::FormTagHelper + include ApplicationHelper + + def self.plugin_name + "RequireAuthToCommentPlugin" + end + + def self.plugin_description + _("Requires users to authenticate in order to post comments.") + end + + def filter_comment(c) + c.reject! unless logged_in? || allowed_by_profile + end + + def profile_editor_extras + expanded_template('profile-editor-extras.rhtml') + end + + def stylesheet? + true + end + + def js_files + ['hide_comment_form.js'] + end + + def body_beginning + "" if allowed_by_profile + end + + protected + + delegate :logged_in?, :to => :context + + def allowed_by_profile + context.profile && context.profile.allow_unauthenticated_comments + end + +end diff --git a/plugins/require_auth_to_comment/lib/require_auth_to_comment_plugin/ext/profile.rb b/plugins/require_auth_to_comment/lib/require_auth_to_comment_plugin/ext/profile.rb new file mode 100644 index 0000000..9847e7b --- /dev/null +++ b/plugins/require_auth_to_comment/lib/require_auth_to_comment_plugin/ext/profile.rb @@ -0,0 +1,5 @@ +require_dependency 'profile' + +class Profile + settings_items :allow_unauthenticated_comments, :type => :boolean +end diff --git a/plugins/require_auth_to_comment/public/hide_comment_form.js b/plugins/require_auth_to_comment/public/hide_comment_form.js new file mode 100644 index 0000000..eb92872 --- /dev/null +++ b/plugins/require_auth_to_comment/public/hide_comment_form.js @@ -0,0 +1,8 @@ +(function($) { + $(window).bind('userDataLoaded', function(event, data) { + if (data.login || $('meta[name=profile.allow_unauthenticated_comments]').length > 0) { + $('.post-comment-button').show(); + $('#page-comment-form').show(); + } + }); +})(jQuery); diff --git a/plugins/require_auth_to_comment/public/style.css b/plugins/require_auth_to_comment/public/style.css new file mode 100644 index 0000000..9efd5e2 --- /dev/null +++ b/plugins/require_auth_to_comment/public/style.css @@ -0,0 +1,3 @@ +.post-comment-button, #page-comment-form { + display: none; +} diff --git a/plugins/require_auth_to_comment/test/unit/require_auth_to_comment_plugin_test.rb b/plugins/require_auth_to_comment/test/unit/require_auth_to_comment_plugin_test.rb new file mode 100644 index 0000000..c807fd1 --- /dev/null +++ b/plugins/require_auth_to_comment/test/unit/require_auth_to_comment_plugin_test.rb @@ -0,0 +1,41 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' + +class RequireAuthToCommentPluginTest < ActiveSupport::TestCase + + def setup + @plugin = RequireAuthToCommentPlugin.new + @comment = Comment.new + end + + attr_reader :plugin, :comment + + should 'reject comments for unauthenticated users' do + plugin.context = logged_in(false) + plugin.filter_comment(comment) + assert comment.rejected? + end + + should 'allow comments from authenticated users' do + plugin.context = logged_in(true) + plugin.filter_comment(comment) + assert !comment.rejected? + end + + should 'allow comments from unauthenticated users if allowed by profile' do + plugin.context = logged_in(false) + plugin.context.profile.allow_unauthenticated_comments = true + + plugin.filter_comment(comment) + assert !comment.rejected? + end + + protected + + def logged_in(boolean) + controller = mock() + controller.stubs(:logged_in?).returns(boolean) + controller.stubs(:profile).returns(Profile.new) + Noosfero::Plugin::Context.new(controller) + end + +end diff --git a/plugins/require_auth_to_comment/views/profile-editor-extras.rhtml b/plugins/require_auth_to_comment/views/profile-editor-extras.rhtml new file mode 100644 index 0000000..4dcfb95 --- /dev/null +++ b/plugins/require_auth_to_comment/views/profile-editor-extras.rhtml @@ -0,0 +1,4 @@ +