diff --git a/app/controllers/my_profile/cms_controller.rb b/app/controllers/my_profile/cms_controller.rb index a1e007e..199ccf7 100644 --- a/app/controllers/my_profile/cms_controller.rb +++ b/app/controllers/my_profile/cms_controller.rb @@ -74,6 +74,8 @@ class CmsController < MyProfileController record_coming if request.post? @article.image = nil if params[:remove_image] == 'true' + @article.users_with_agreement.clear if (@article.forum? and params[:article][:has_terms_of_use] == "0") + @article.users_with_agreement << user if (@article.forum? and params[:article][:has_terms_of_use] == "1" and !@article.users_with_agreement.include? user) @article.last_changed_by = user if @article.update_attributes(params[:article]) if !continue @@ -130,6 +132,7 @@ class CmsController < MyProfileController continue = params[:continue] if request.post? + @article.users_with_agreement << user if (@article.forum? and params[:article][:has_terms_of_use] == "1") if @article.save if continue redirect_to :action => 'edit', :id => @article @@ -387,4 +390,3 @@ class CmsController < MyProfileController end end - diff --git a/app/controllers/public/content_viewer_controller.rb b/app/controllers/public/content_viewer_controller.rb index b4d0190..4ba0cb1 100644 --- a/app/controllers/public/content_viewer_controller.rb +++ b/app/controllers/public/content_viewer_controller.rb @@ -50,6 +50,17 @@ class ContentViewerController < ApplicationController redirect_to_translation if @page.profile.redirect_l10n + if request.post? + if @page.forum? && @page.has_terms_of_use && params[:terms_accepted] == "true" + @page.add_agreed_user(user) + end + elsif !@page.parent.nil? && @page.parent.forum? + unless @page.parent.agrees_with_terms?(user) + params[:page].pop + redirect_to :profile => params[:profile], :page => params[:page], :action => 'view_page' + end + end + # At this point the page will be showed @page.hit diff --git a/app/models/forum.rb b/app/models/forum.rb index 0298487..a12cacc 100644 --- a/app/models/forum.rb +++ b/app/models/forum.rb @@ -3,6 +3,10 @@ class Forum < Folder acts_as_having_posts :order => 'updated_at DESC' include PostsLimit + settings_items :terms_of_use, :type => :string, :default => "" + settings_items :has_terms_of_use, :type => :boolean, :default => false + has_and_belongs_to_many :users_with_agreement, :class_name => 'Person', :join_table => 'terms_forum_people' + def self.type_name _('Forum') end @@ -39,4 +43,19 @@ class Forum < Folder paragraphs = Hpricot(body).search('p') paragraphs.empty? ? '' : paragraphs.first.to_html end + + def add_agreed_user(user) + self.users_with_agreement << user + self.save + end + + def agrees_with_terms?(user) + return true unless self.has_terms_of_use + if user + self.users_with_agreement.find_by_id user.id + else + false + end + end + end diff --git a/app/models/person.rb b/app/models/person.rb index ba9d0f0..269ad9c 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -63,6 +63,8 @@ class Person < Profile has_many :scraps_sent, :class_name => 'Scrap', :foreign_key => :sender_id, :dependent => :destroy + has_and_belongs_to_many :acepted_forums, :class_name => 'Forum', :join_table => 'terms_forum_people' + named_scope :more_popular, :select => "#{Profile.qualified_column_names}, count(friend_id) as total", :group => Profile.qualified_column_names, diff --git a/app/views/cms/_forum.rhtml b/app/views/cms/_forum.rhtml index e876af5..7c1e28f 100644 --- a/app/views/cms/_forum.rhtml +++ b/app/views/cms/_forum.rhtml @@ -11,3 +11,9 @@ <%= labelled_form_field(_('Description:'), text_area(:article, :body, :cols => 64, :rows => 10)) %> <%= labelled_form_field(_('Posts per page:'), f.select(:posts_per_page, Forum.posts_per_page_options)) %> + +<%= labelled_form_field(_('Has terms of use:'), check_box(:article, :has_terms_of_use))%> + +
<%= page.terms_of_use %>
\ No newline at end of file diff --git a/app/views/content_viewer/forum_page.rhtml b/app/views/content_viewer/forum_page.rhtml index 21938dd..fb15107 100644 --- a/app/views/content_viewer/forum_page.rhtml +++ b/app/views/content_viewer/forum_page.rhtml @@ -1,11 +1,28 @@ <% add_rss_feed_to_head(@page.name, @page.feed.url) if @page.forum? && @page.feed %> +<% if @page.agrees_with_terms?(user) %> -<%= @page.terms_of_use %>
+ + <% form_tag @page.url.merge(:terms_accepted => true) do %> + <% button_bar do %> + <% if user %> + <%= submit_button :save, _("Accept") %> + <% else %> + <%= button :save, _("Accept"), login_url %> + <% end %> + <%= button "cancel", _("Cancel"), profile.url %> + <% end %> + <% end %> +<% end %> \ No newline at end of file diff --git a/db/migrate/20131128161159_terms_forum_people.rb b/db/migrate/20131128161159_terms_forum_people.rb new file mode 100644 index 0000000..02186b1 --- /dev/null +++ b/db/migrate/20131128161159_terms_forum_people.rb @@ -0,0 +1,15 @@ +class TermsForumPeople < ActiveRecord::Migration + def self.up + create_table :terms_forum_people, :id => false do |t| + t.integer :forum_id + t.integer :person_id + end + add_index :terms_forum_people, [:forum_id, :person_id] + end + + def self.down + remove_index :terms_forum_people, [:forum_id, :person_id] + drop_table :terms_forum_people + end + +end \ No newline at end of file diff --git a/features/forum.feature b/features/forum.feature index 3ca8371..11a6327 100644 --- a/features/forum.feature +++ b/features/forum.feature @@ -70,28 +70,99 @@ Feature: forum When I follow "Configure forum" Then I should be on edit "Forum One" by joaosilva + @selenium + Scenario: show forum with terms of use for owner + Given the following forums + | owner | name | + | joaosilva | Forum One | + And I go to /joaosilva/forum-one + When I follow "Configure forum" + And I fill in "Description" with "My description" + And I check "Has terms of use:" + And I press "Save" + Then I should see "Forum One" + And I should see "My description" + + @selenium + Scenario: accept terms in topics page + Given the following forums + | owner | name | + | joaosilva | Forum One | + And the following users + | login | name | + | mariasilva | Maria Silva | + And I go to /joaosilva/forum-one + When I follow "Configure forum" + And I fill in "Description" with "My description" + And I check "Has terms of use:" + And I press "Save" + When I follow "New discussion topic" + And I follow "Text article with visual editor" + And I fill in "Title" with "Topic" + And I press "Save" + And I am logged in as "mariasilva" + And I go to /joaosilva/forum-one/topic + And I press "Accept" + Then I should see "Topic" + + @selenium + Scenario: accept terms of use of a forum for others users + Given the following forums + | owner | name | + | joaosilva | Forum One | + And the following users + | login | name | + | mariasilva | Maria Silva | + And I go to /joaosilva/forum-one + When I follow "Configure forum" + And I fill in "Description" with "My description" + And I check "Has terms of use:" + And I press "Save" + When I follow "Logout" + And I am logged in as "mariasilva" + And I go to /joaosilva/forum-one?terms=terms + When I press "Accept" + Then I should see "Forum One" + And I should see "My description" + + @selenium + Scenario: redirect user not logged + Given the following forums + | owner | name | + | joaosilva | Forum One | + And I go to /joaosilva/forum-one + When I follow "Configure forum" + And I fill in "Description" with "My description" + And I check "Has terms of use:" + And I press "Save" + When I follow "Logout" + And I go to /joaosilva/forum-one?terms=terms + When I follow "Accept" + Then I should see "Login" within ".login-box" + + @selenium Scenario: last topic update by unautenticated user should not link Given the following forums - | owner | name | + | owner | name | | joaosilva | Forum | And the following articles - | owner | name | parent | - | joaosilva | Post one | Forum | + | owner | name | parent | + | joaosilva | Post one | Forum | And the following comments - | article | name | email | title | body | + | article | name | email | title | body | | Post one | Joao | joao@example.com | Hi all | Hi all | When I go to /joaosilva/forum Then I should not see "Joao" link Scenario: last topic update by autenticated user should link to profile url Given the following forums - | owner | name | + | owner | name | | joaosilva | Forum | And the following articles - | owner | name | parent | - | joaosilva | Post one | Forum | + | owner | name | parent | + | joaosilva | Post one | Forum | And the following comments - | article | author | title | body | + | article | author | title | body | | Post one | joaosilva | Hi all | Hi all | When I go to /joaosilva/forum Then I should see "Joao" linking to "http://localhost/joaosilva" diff --git a/public/javascripts/application.js b/public/javascripts/application.js index 6c2ab93..1c7d4ef 100644 --- a/public/javascripts/application.js +++ b/public/javascripts/application.js @@ -1084,3 +1084,21 @@ jQuery(function($) { }); }); + +function showHideTermsOfUse() { + if( jQuery("#article_has_terms_of_use").attr("checked") ) + jQuery("#text_area_terms_of_use").show(); + else { + jQuery("#text_area_terms_of_use").hide(); + jQuery("#article_terms_of_use").val(""); + jQuery("#article_terms_of_use_ifr").contents().find("body").html(""); + } +} + +jQuery(document).ready(function(){ + showHideTermsOfUse(); + + jQuery("#article_has_terms_of_use").click(function(){ + showHideTermsOfUse(); + }); +}); \ No newline at end of file diff --git a/test/functional/cms_controller_test.rb b/test/functional/cms_controller_test.rb index 0acac68..7e3c948 100644 --- a/test/functional/cms_controller_test.rb +++ b/test/functional/cms_controller_test.rb @@ -1324,10 +1324,11 @@ class CmsControllerTest < ActionController::TestCase end should 'back to forum after config forum' do - profile.articles << Forum.new(:name => 'my-forum', :profile => profile) - post :edit, :profile => profile.identifier, :id => profile.forum.id - - assert_redirected_to @profile.articles.find_by_name('my-forum').view_url + assert_difference Forum, :count do + post :new, :type => Forum.name, :profile => profile.identifier, :article => { :name => 'my-forum' }, :back_to => 'control_panel' + end + post :edit, :type => Forum.name, :profile => profile.identifier, :article => { :name => 'my forum' }, :id => profile.forum.id + assert_redirected_to @profile.articles.find_by_name('my forum').view_url end should 'back to control panel if cancel create forum' do -- libgit2 0.21.2