[noosfero/noosfero][master] 2 commits: forum: improved topic creation permissions configuration

Daniela Feitosa gitlab at gitlab.com
Fri May 22 10:56:14 BRT 2015


Daniela Feitosa pushed to branch master at Noosfero / noosfero


Commits:
7c9de6db by Rodrigo Souto at 2015-05-20T18:50:05Z
forum: improved topic creation permissions configuration

- - - - -
db6ca267 by Daniela Feitosa at 2015-05-22T10:39:54Z
Updated db schema

- - - - -


8 changed files:

- app/helpers/article_helper.rb
- app/models/forum.rb
- + db/migrate/20150513213939_update_topic_creation_configuration.rb
- db/schema.rb
- features/forum.feature
- + public/javascripts/topic-creation-config.js
- public/stylesheets/application.css
- test/unit/forum_test.rb


Changes:

=====================================
app/helpers/article_helper.rb
=====================================
--- a/app/helpers/article_helper.rb
+++ b/app/helpers/article_helper.rb
@@ -12,6 +12,7 @@ module ArticleHelper
     @article = article
 
     visibility_options(@article, tokenized_children) +
+    topic_creation(@article) +
     content_tag('h4', _('Options')) +
     content_tag('div',
       (article.profile.has_members? ?
@@ -55,14 +56,7 @@ module ArticleHelper
         'div',
         check_box(:article, :display_versions) +
         content_tag('label', _('I want this article to display a link to older versions'), :for => 'article_display_versions')
-      ) : '') +
-
-      (article.forum? && article.profile.community? ?
-      content_tag(
-        'div',
-        check_box(:article, :allows_members_to_create_topics) +
-        content_tag('label', _('Allow members to create topics'), :for => 'article_allows_members_to_create_topics')
-        ) : '')
+      ) : '')
     )
   end
 
@@ -81,6 +75,22 @@ module ArticleHelper
     )
   end
 
+  def topic_creation(article)
+    return '' unless article.forum?
+
+    general_options = Forum::TopicCreation.general_options(article)
+    slider_options = {:id => 'topic-creation-slider'}
+    slider_options = general_options.keys.inject(slider_options) do |result, option|
+      result.merge!({'data-'+option => general_options[option]})
+    end
+
+    content_tag('h4', _('Topic creation')) +
+    content_tag( 'small', _('Who will be able to create new topics on this forum?')) +
+    content_tag('div', '', slider_options) +
+    hidden_field_tag('article[topic_creation]', article.topic_creation) +
+    javascript_include_tag('topic-creation-config')
+  end
+
   def privacity_exceptions(article, tokenized_children)
     content_tag('div',
       content_tag('div',


=====================================
app/models/forum.rb
=====================================
--- a/app/models/forum.rb
+++ b/app/models/forum.rb
@@ -3,11 +3,11 @@ class Forum < Folder
   acts_as_having_posts :order => 'updated_at DESC'
   include PostsLimit
 
-  attr_accessible :has_terms_of_use, :terms_of_use, :allows_members_to_create_topics
+  attr_accessible :has_terms_of_use, :terms_of_use, :topic_creation
 
   settings_items :terms_of_use, :type => :string, :default => ""
   settings_items :has_terms_of_use, :type => :boolean, :default => false
-  settings_items :allows_members_to_create_topics, :type => :boolean, :default => false
+  settings_items :topic_creation, :type => :string, :default => 'self'
   has_and_belongs_to_many :users_with_agreement, :class_name => 'Person', :join_table => 'terms_forum_people'
 
   before_save do |forum|
@@ -33,6 +33,23 @@ class Forum < Folder
     _('An internet forum, also called message board, where discussions can be held.')
   end
 
+  module TopicCreation
+    BASE = ActiveSupport::OrderedHash.new
+    BASE['users'] = _('Logged users')
+
+    PERSON = ActiveSupport::OrderedHash.new
+    PERSON['self'] = _('Me')
+    PERSON['related'] = _('Friends')
+
+    GROUP = ActiveSupport::OrderedHash.new
+    GROUP['self'] = _('Administrators')
+    GROUP['related'] = _('Members')
+
+    def self.general_options(forum)
+      forum.profile.person? ? PERSON.merge(BASE) : GROUP.merge(BASE)
+    end
+  end
+
   include ActionView::Helpers::TagHelper
   def to_html(options = {})
     proc do
@@ -69,11 +86,17 @@ class Forum < Folder
     self.users_with_agreement.exists? user
   end
 
-  def can_create_topic?(user, profile)
-    return profile.community? && profile.members.include?(user) && self.allows_members_to_create_topics
+  def can_create_topic?(user)
+    return true if user == profile || profile.admins.include?(user) || profile.environment.admins.include?(user)
+    case topic_creation
+    when 'related'
+      profile.person? ? profile.friends.include?(user) : profile.members.include?(user)
+    when 'users'
+      user.present?
+    end
   end
 
   def allow_create?(user)
-    super || can_create_topic?(user, profile)
+    super || can_create_topic?(user)
   end
 end


=====================================
db/migrate/20150513213939_update_topic_creation_configuration.rb
=====================================
--- /dev/null
+++ b/db/migrate/20150513213939_update_topic_creation_configuration.rb
@@ -0,0 +1,13 @@
+class UpdateTopicCreationConfiguration < ActiveRecord::Migration
+  def up
+    Forum.where("setting LIKE '%:allows_members_to_create_topics: true%'").find_each do |forum|
+      forum.setting.delete(:allows_members_to_create_topics)
+      forum.setting.merge!(:topic_creation => 'related')
+      forum.save
+    end
+  end
+
+  def down
+     say "this migration can't be reverted"
+  end
+end


=====================================
db/schema.rb
=====================================
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
 #
 # It's strongly recommended to check this file into your version control system.
 
-ActiveRecord::Schema.define(:version => 20150423144533) do
+ActiveRecord::Schema.define(:version => 20150513213939) do
 
   create_table "abuse_reports", :force => true do |t|
     t.integer  "reporter_id"


=====================================
features/forum.feature
=====================================
--- a/features/forum.feature
+++ b/features/forum.feature
@@ -169,124 +169,3 @@ Feature: forum
        | Post one | joaosilva | Hi all | Hi all |
    When I go to /joaosilva/forum
    Then I should see "Joao Silva" within ".forum-post-last-answer"
-
-   @selenium
-   Scenario: community member should be able to see the discussion topic button
-    Given the following community
-      | identifier       | name             | owner     |
-      | sample-community | Sample Community | joaosilva |
-    And the following forums
-      | owner            | name  |
-      | sample-community | Forum |
-    And the following users
-      | login      | name       |
-      | mariasilva | Maria Silva|
-    And "Maria Silva" is a member of "Sample Community"
-    And I am logged in as "joaosilva"
-    When I go to /sample-community/forum
-    And I follow "Configure forum"
-    And I check "Allow members to create topics"
-    And I press "Save"
-    And I am logged in as "mariasilva"
-    And I go to /sample-community/forum
-    Then I should see "New discussion topic"
-
-   @selenium
-   Scenario: a non community member should not be able to see the discussion topic button
-    Given the following community
-      | identifier       | name             | owner     |
-      | sample-community | Sample Community | joaosilva |
-    And the following forums
-      | owner            | name  |
-      | sample-community | Forum |
-    And the following users
-      | login      | name       |
-      | mariasilva | Maria Silva|
-    And I am logged in as "joaosilva"
-    When I go to /sample-community/forum
-    And I follow "Configure forum"
-    And I check "Allow members to create topics"
-    And I press "Save"
-    And I am logged in as "mariasilva"
-    And I go to /sample-community/forum
-    Then I should not see "New discussion topic"
-
-   @selenium
-   Scenario: community member should not be able to see the discussion topic button
-    Given the following community
-      | identifier       | name             | owner     |
-      | sample-community | Sample Community | joaosilva |
-    And the following forums
-      | owner            | name  |
-      | sample-community | Forum |
-    And the following users
-      | login      | name       |
-      | mariasilva | Maria Silva|
-    And "Maria Silva" is a member of "Sample Community"
-    And I am logged in as "joaosilva"
-    When I go to /sample-community/forum
-    And I follow "Configure forum"
-    And I uncheck "Allow members to create topics"
-    And I press "Save"
-    And I am logged in as "mariasilva"
-    And I go to /sample-community/forum
-    Then I should not see "New discussion topic"
-
-   @selenium
-   Scenario: community member should be able to create a topic with the discussion topic button
-    Given the following community
-      | identifier       | name             | owner     |
-      | sample-community | Sample Community | joaosilva |
-    And the following forums
-      | owner            | name  |
-      | sample-community | Forum |
-    And the following users
-      | login      | name       |
-      | mariasilva | Maria Silva|
-    And "Maria Silva" is a member of "Sample Community"
-    And I am logged in as "joaosilva"
-    When I go to /sample-community/forum
-    And I follow "Configure forum"
-    And I check "Allow members to create topics"
-    And I press "Save"
-    And I am logged in as "mariasilva"
-    And I go to /sample-community/forum
-    And I follow "New discussion topic"
-    And I should see "Text article with visual editor"
-    And I follow "Text article with visual editor"
-    And I fill in "Title" with "Test"
-    And I press "Save"
-    Then I should see "Test"
-
-   @selenium
-   Scenario: community member should be able to create a topic on a topic page
-    Given the following community
-      | identifier       | name             | owner     |
-      | sample-community | Sample Community | joaosilva |
-    And the following forums
-      | owner            | name  |
-      | sample-community | Forum |
-    And the following users
-      | login      | name       |
-      | mariasilva | Maria Silva|
-    And "Maria Silva" is a member of "Sample Community"
-    And I am logged in as "joaosilva"
-    When I go to /sample-community/forum
-    And I follow "Configure forum"
-    And I check "Allow members to create topics"
-    And I press "Save"
-    And I am logged in as "mariasilva"
-    And I go to /sample-community/forum
-    And I follow "New discussion topic"
-    And I should see "Text article with visual editor"
-    And I follow "Text article with visual editor"
-    And I fill in "Title" with "Test"
-    And I press "Save"
-    And I go to /sample-community/forum/test
-    And I follow "New discussion topic"
-    And I should see "Text article with visual editor"
-    And I follow "Text article with visual editor"
-    And I fill in "Title" with "Test inside the topic page"
-    And I press "Save"
-    And I go to /sample-community/forum
-    Then I should see "Test inside the topic page"


=====================================
public/javascripts/topic-creation-config.js
=====================================
--- /dev/null
+++ b/public/javascripts/topic-creation-config.js
@@ -0,0 +1,30 @@
+var values_map = {2: 'self', 1: 'related', 0: 'users'};
+var keys_map = {};
+Object.keys(values_map).forEach(function(value){
+  keys_map[values_map[value]] = value;
+});
+var s =  jQuery('#topic-creation-slider');
+
+function setValue(event, ui){
+  jQuery('#article_topic_creation').val(values_map[ui.value]);
+}
+
+s.slider({
+  orientation: 'vertical',
+  min: 0,
+  max: 2,
+  step: 1,
+  value: keys_map[jQuery('#article_topic_creation').val()],
+  range: 'max',
+  change: setValue
+}).each(function() {
+  var opt = jQuery(this).data()['ui-slider'].options;
+  var vals = opt.max - opt.min;
+
+  for (var i = 0; i <= vals; i++) {
+    var n = vals - i;
+    var el = jQuery('<label>' + s.data(values_map[i]) + '</label>').css('top', ((n/vals*100) - 7 - n) + '%');
+    s.append(el);
+  }
+});
+


=====================================
public/stylesheets/application.css
=====================================
--- a/public/stylesheets/application.css
+++ b/public/stylesheets/application.css
@@ -6239,6 +6239,20 @@ li.profile-activity-item.upload_image .activity-gallery-images-count-1 img {
 .forum-posts .pagination {
   margin-top: 20px;
 }
+
+#topic-creation-slider{
+  margin-top: 15px;
+}
+
+#topic-creation-slider .ui-slider-range {
+  background: #73D216;
+}
+
+#topic-creation-slider label {
+  left: 20px;
+  position: absolute;
+  width: 200px;
+}
 /* Task */
 
 #user a#pending-tasks-count {


=====================================
test/unit/forum_test.rb
=====================================
--- a/test/unit/forum_test.rb
+++ b/test/unit/forum_test.rb
@@ -174,4 +174,70 @@ class ForumTest < ActiveSupport::TestCase
     assert_equal true, Forum.find(forum.id).agrees_with_terms?(person)
   end
 
+  should 'always allow topic creation to the person himself' do
+    person = fast_create(Person)
+    someone = fast_create(Person)
+    forum = Forum.new(:profile => person)
+
+    assert forum.can_create_topic?(person)
+    assert !forum.can_create_topic?(someone)
+  end
+
+  should 'always allow topic creation to profile admins' do
+    admin = fast_create(Person)
+    someone = fast_create(Person)
+    profile = fast_create(Profile)
+    admins = [admin]
+    profile.stubs(:admins).returns(admins)
+    forum = Forum.new(:profile => profile)
+
+    assert forum.can_create_topic?(admin)
+    assert !forum.can_create_topic?(someone)
+  end
+
+  should 'always allow topic creation to environment admins' do
+    admin = fast_create(Person)
+    someone = fast_create(Person)
+    profile = fast_create(Profile)
+    admins = [admin]
+    environment = profile.environment
+    environment.stubs(:admins).returns(admins)
+    forum = Forum.new(:profile => profile)
+
+    assert forum.can_create_topic?(admin)
+    assert !forum.can_create_topic?(someone)
+  end
+
+  should 'allow only person friends to create topics when topic_creation is related' do
+    person = fast_create(Person)
+    friend = fast_create(Person)
+    someone = fast_create(Person)
+    friends = [friend]
+    person.stubs(:friends).returns(friends)
+    forum = Forum.new(:profile => person, :topic_creation => 'related')
+
+    assert forum.can_create_topic?(friend)
+    assert !forum.can_create_topic?(someone)
+  end
+
+  should 'allow only group members to create topics when topic_creation is related' do
+    organization = fast_create(Organization)
+    member = fast_create(Person)
+    someone = fast_create(Person)
+    members = [member]
+    organization.stubs(:members).returns(members)
+    forum = Forum.new(:profile => organization, :topic_creation => 'related')
+
+    assert forum.can_create_topic?(member)
+    assert !forum.can_create_topic?(someone)
+  end
+
+  should 'allow every user to create topics when topic_creation is users' do
+    profile = fast_create(Profile)
+    user = fast_create(Person)
+    forum = Forum.new(:profile => profile, :topic_creation => 'users')
+
+    assert forum.can_create_topic?(user)
+    assert !forum.can_create_topic?(nil)
+  end
 end



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/82270a3f1cd9eb73335e67b9893a53c5f5c47ca0...db6ca267b0488e03226b48fe489c626c454c30b2
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20150522/83b38a8b/attachment-0001.html>


More information about the Noosfero-dev mailing list