Commit c15a029cb596e47d9ecac86edcc59fdaa6a81155

Authored by MoisesMachado
1 parent dc74649b

ActionItem616: stop showing private content on seraches

  added the common method public? to trace public content and made it
not be shown in search results. Know issue: the numbers of total results
is wrong when private content would be retrived if it were public

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2398 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/article.rb
... ... @@ -42,6 +42,13 @@ class Article < ActiveRecord::Base
42 42 pending_categorizations.clear
43 43 end
44 44  
  45 + before_save do |article|
  46 + if article.parent
  47 + article.public_article = article.parent.public_article
  48 + end
  49 + true
  50 + end
  51 +
45 52 acts_as_taggable
46 53 N_('Tag list')
47 54  
... ... @@ -156,6 +163,10 @@ class Article < ActiveRecord::Base
156 163 !cat.is_a?(ProductCategory)
157 164 end
158 165  
  166 + def public?
  167 + profile.public? && public_article
  168 + end
  169 +
159 170 private
160 171  
161 172 def sanitize_tag_list
... ...
app/models/product.rb
... ... @@ -63,4 +63,8 @@ class Product < ActiveRecord::Base
63 63 enterprise.generate_url(:controller => 'catalog', :action => 'show', :id => id)
64 64 end
65 65  
  66 + def public?
  67 + enterprise.public_profile
  68 + end
  69 +
66 70 end
... ...
app/models/profile.rb
... ... @@ -304,16 +304,24 @@ class Profile < ActiveRecord::Base
304 304 false
305 305 end
306 306  
307   - after_create :insert_default_homepage_and_feed
308   - def insert_default_homepage_and_feed
  307 + after_create :insert_default_article_set
  308 + def insert_default_article_set
  309 + # a default homepage
309 310 hp = default_homepage(:name => _("%s's home page") % self.name, :body => _("<p>This is a default homepage created for %s. It can be changed though the control panel.</p>") % self.name, :advertise => false)
310 311 hp.profile = self
311 312 hp.save!
312 313 self.home_page = hp
313 314 self.save!
314 315  
  316 + # a default rss feed
315 317 feed = RssFeed.new(:name => 'feed')
316 318 self.articles << feed
  319 +
  320 + # a default private folder if public
  321 + if self.public?
  322 + folder = Folder.new(:name => _("Intranet"), :public_article => false)
  323 + self.articles << folder
  324 + end
317 325 end
318 326  
319 327 # Adds a person as member of this Profile.
... ... @@ -403,4 +411,7 @@ class Profile &lt; ActiveRecord::Base
403 411 self[:theme] || environment.theme
404 412 end
405 413  
  414 + def public?
  415 + public_profile
  416 + end
406 417 end
... ...
app/views/search/_display_results.rhtml
... ... @@ -38,7 +38,7 @@
38 38 <ul>
39 39 <% hit_pos = 0 %>
40 40 <% results.each do |hit| %>
41   - <% next if hit.respond_to?(:public_profile) && !hit.public_profile %>
  41 + <% next if hit.respond_to?(:public?) && !hit.public? %>
42 42 <%= render :partial => partial_for_class(hit.class),
43 43  
44 44 :object => hit,
... ...
test/functional/cms_controller_test.rb
... ... @@ -347,6 +347,7 @@ class CmsControllerTest &lt; Test::Unit::TestCase
347 347 end
348 348  
349 349 should 'list folders at top level' do
  350 + Folder.destroy_all
350 351 f1 = Folder.new(:name => 'f1'); profile.articles << f1; f1.save!
351 352 f2 = Folder.new(:name => 'f2'); profile.articles << f2; f2.save!
352 353  
... ... @@ -542,4 +543,15 @@ class CmsControllerTest &lt; Test::Unit::TestCase
542 543 assert_redirected_to @profile.articles.find_by_name('new-article-from-public-view').url
543 544 end
544 545  
  546 + should 'create a private article child of private folder' do
  547 + folder = Folder.new(:name => 'my intranet', :public_article => false); profile.articles << folder; folder.save!
  548 +
  549 + post :new, :profile => profile.identifier, :type => 'TextileArticle', :parent_id => folder.id, :article => { :name => 'new-private-article'}
  550 + folder.reload
  551 +
  552 + assert !assigns(:article).public?
  553 + assert_equal 'new-private-article', folder.children[0].name
  554 + assert !folder.children[0].public?
  555 + end
  556 +
545 557 end
... ...
test/unit/article_test.rb
... ... @@ -364,4 +364,45 @@ class ArticleTest &lt; Test::Unit::TestCase
364 364 assert !article.display_to?(person2)
365 365 assert article.display_to?(person1)
366 366 end
  367 +
  368 + should 'make new article private if created inside a private folder' do
  369 + profile = Profile.create!(:name => 'test profile', :identifier => 'test_profile')
  370 + folder = Folder.create!(:name => 'my_intranet', :profile => profile, :public_article => false)
  371 + article = Article.create!(:name => 'my private article', :profile => profile, :parent => folder)
  372 +
  373 + assert !article.public_article
  374 + end
  375 +
  376 + should 'respond to public? like public_article if profile is public' do
  377 + p = Profile.create!(:name => 'test profile', :identifier => 'test_profile')
  378 + a1 = Article.create!(:name => 'test public article', :profile => p)
  379 + a2 = Article.create!(:name => 'test private article', :profile => p, :public_article => false)
  380 +
  381 + assert a1.public?
  382 + assert !a2.public?
  383 + end
  384 +
  385 + should 'respond to public? as false if profile is private' do
  386 + p = Profile.create!(:name => 'test profile', :identifier => 'test_profile', :public_profile => false)
  387 + a1 = Article.create!(:name => 'test public article', :profile => p)
  388 + a2 = Article.create!(:name => 'test private article', :profile => p, :public_article => false)
  389 +
  390 + assert !a1.public?
  391 + assert !a2.public?
  392 + end
  393 +
  394 + should 'save as private' do
  395 + profile = Profile.create!(:name => 'test profile', :identifier => 'test_profile')
  396 + folder = Folder.create!(:name => 'my_intranet', :profile => profile, :public_article => false)
  397 + article = TextileArticle.new(:name => 'my private article')
  398 + article.profile = profile
  399 + article.parent = folder
  400 + article.save!
  401 + article.reload
  402 +
  403 + assert !article.public_article
  404 +
  405 +
  406 + end
  407 +
367 408 end
... ...
test/unit/product_test.rb
... ... @@ -168,4 +168,16 @@ class ProductTest &lt; Test::Unit::TestCase
168 168 assert !ProductCategorization.find(:first, :conditions => {:product_id => p, :category_id => cat})
169 169 end
170 170  
  171 + should 'respond to public? as its enterprise public?' do
  172 + e1 = Enterprise.create!(:name => 'test ent 1', :identifier => 'test_ent1')
  173 + p1 = Product.create!(:name => 'test product 1', :enterprise => e1)
  174 +
  175 + assert p1.public?
  176 +
  177 + e1.public_profile = false
  178 + e1.save!
  179 +
  180 + assert !p1.public?
  181 + end
  182 +
171 183 end
... ...
test/unit/profile_test.rb
... ... @@ -794,6 +794,22 @@ class ProfileTest &lt; Test::Unit::TestCase
794 794 assert_equal 'environment-stored-theme', p.theme
795 795 end
796 796  
  797 + should 'respond to public? as public_profile' do
  798 + p1 = Profile.create!(:name => 'test profile 1', :identifier => 'test_profile1')
  799 + p2 = Profile.create!(:name => 'test profile 2', :identifier => 'test_profile2', :public_profile => false)
  800 +
  801 + assert p1.public?
  802 + assert !p2.public?
  803 + end
  804 +
  805 + should 'create a initial private folder when a public profile is created' do
  806 + p1 = Profile.create!(:name => 'test profile 1', :identifier => 'test_profile1')
  807 + p2 = Profile.create!(:name => 'test profile 2', :identifier => 'test_profile2', :public_profile => false)
  808 +
  809 + assert p1.articles.find(:first, :conditions => {:public_article => false})
  810 + assert !p2.articles.find(:first, :conditions => {:public_article => false})
  811 + end
  812 +
797 813 private
798 814  
799 815 def assert_invalid_identifier(id)
... ...