Commit d2879e8d3784f2f714a5519fc6f9ca012c54cacf

Authored by Rodrigo Souto
Committed by Antonio Terceiro
1 parent 8cf6fdcf

Refactoring the public/private article's logic

* This patch will centralize the logic of public/private article in
	  the 'published' attribute.
	* Removing the public_article attribute from the code replacing it
	  by published.
	* Creating two new migrations:
		1. Setting the published attribute to false when the
		   article has the public_article attribute false. This will grant
		   that all the articles that aren't public now continue
		   unpublished.
		2. Removing the public_article attribute from the schema.
app/controllers/public/content_viewer_controller.rb
... ... @@ -26,11 +26,6 @@ class ContentViewerController < ApplicationController
26 26 end
27 27 end
28 28  
29   - # only show unpublished articles to those who can edit then
30   - if @page && !@page.published && !@page.allow_post_content?(user)
31   - @page = nil
32   - end
33   -
34 29 # page not found, give error
35 30 if @page.nil?
36 31 render_not_found(@path)
... ...
app/models/article.rb
... ... @@ -84,13 +84,6 @@ class Article < ActiveRecord::Base
84 84 pending_categorizations.clear
85 85 end
86 86  
87   - before_save do |article|
88   - if article.parent
89   - article.public_article = article.parent.public_article
90   - end
91   - true
92   - end
93   -
94 87 acts_as_taggable
95 88 N_('Tag list')
96 89  
... ... @@ -123,11 +116,10 @@ class Article < ActiveRecord::Base
123 116 options = { :limit => limit,
124 117 :conditions => [
125 118 "advertise = ? AND
126   - public_article = ? AND
127 119 published = ? AND
128 120 profiles.visible = ? AND
129 121 profiles.public_profile = ? AND
130   - ((articles.type != ? and articles.type != ? and articles.type != ?) OR articles.type is NULL)", true, true, true, true, true, 'UploadedFile', 'RssFeed', 'Blog'
  122 + ((articles.type != ? and articles.type != ? and articles.type != ?) OR articles.type is NULL)", true, true, true, true, 'UploadedFile', 'RssFeed', 'Blog'
131 123 ],
132 124 :include => 'profile',
133 125 :order => 'articles.published_at desc, articles.id desc'
... ... @@ -221,16 +213,32 @@ class Article < ActiveRecord::Base
221 213 false
222 214 end
223 215  
  216 + def published?
  217 + if self.published
  218 + if self.parent && !self.parent.published?
  219 + return false
  220 + end
  221 + true
  222 + else
  223 + false
  224 + end
  225 + end
  226 +
224 227 named_scope :folders, :conditions => { :type => ['Folder', 'Blog'] }
225 228  
  229 + def display_unpublished_article_to?(user)
  230 + self.author == user || allow_view_private_content?(user) || user == self.profile ||
  231 + user.is_admin?(self.profile.environment) || user.is_admin?(self.profile)
  232 + end
  233 +
226 234 def display_to?(user)
227   - if self.public_article
  235 + if self.published?
228 236 self.profile.display_info_to?(user)
229 237 else
230 238 if user.nil?
231 239 false
232 240 else
233   - (user == self.profile) || user.has_permission?('view_private_content', self.profile)
  241 + self.display_unpublished_article_to?(user)
234 242 end
235 243 end
236 244 end
... ... @@ -243,6 +251,10 @@ class Article < ActiveRecord::Base
243 251 user && user.has_permission?('publish_content', profile)
244 252 end
245 253  
  254 + def allow_view_private_content?(user = nil)
  255 + user && user.has_permission?('view_private_content', profile)
  256 + end
  257 +
246 258 def comments_updated
247 259 ferret_update
248 260 end
... ... @@ -252,9 +264,10 @@ class Article < ActiveRecord::Base
252 264 end
253 265  
254 266 def public?
255   - profile.visible? && profile.public? && public_article
  267 + profile.visible? && profile.public? && published?
256 268 end
257 269  
  270 +
258 271 def copy(options)
259 272 attrs = attributes.reject! { |key, value| article_attr_blacklist.include?(key) }
260 273 attrs.merge!(options)
... ...
app/models/profile.rb
... ... @@ -447,7 +447,7 @@ private :generate_url, :url_options
447 447  
448 448 # a default private folder if public
449 449 if self.public?
450   - folder = Folder.new(:name => _("Intranet"), :public_article => false)
  450 + folder = Folder.new(:name => _("Intranet"), :published => false)
451 451 self.articles << folder
452 452 end
453 453 end
... ... @@ -692,7 +692,7 @@ private :generate_url, :url_options
692 692 if user.nil?
693 693 false
694 694 else
695   - (user == self) || (user.is_admin?(self.environment)) || (user.memberships.include?(self))
  695 + (user == self) || (user.is_admin?(self.environment)) || user.is_admin?(self) || user.memberships.include?(self)
696 696 end
697 697 end
698 698 end
... ...
db/migrate/084_set_public_article_into_published_attribute.rb 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +class SetPublicArticleIntoPublishedAttribute < ActiveRecord::Migration
  2 + def self.up
  3 + execute('update articles set published=(1!=1) where not public_article')
  4 + end
  5 +
  6 + def self.down
  7 + raise "this migration can't be reverted"
  8 + end
  9 +end
... ...
db/migrate/085_remove_public_article.rb 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +class RemovePublicArticle < ActiveRecord::Migration
  2 + def self.up
  3 + remove_column :articles, :public_article
  4 + end
  5 +
  6 + def self.down
  7 + add_column :articles, :public_article, :boolean, :default => true
  8 + execute('update articles set public_article = (1>0)')
  9 + end
  10 +end
... ...
script/apply-template
... ... @@ -8,7 +8,7 @@ env = Environment.default
8 8  
9 9 def move_articles_to_blog(profile)
10 10 profile.articles.each { |article|
11   - if !article.blog? && !article.is_a?(RssFeed) && article.public_article
  11 + if !article.blog? && !article.is_a?(RssFeed) && article.published?
12 12 puts 'including ' + article.path + ' in the blog'
13 13 article.parent = profile.blog
14 14 article.save!
... ...
test/functional/cms_controller_test.rb
... ... @@ -624,14 +624,14 @@ class CmsControllerTest &lt; Test::Unit::TestCase
624 624 end
625 625  
626 626 should 'create a private article child of private folder' do
627   - folder = Folder.new(:name => 'my intranet', :public_article => false); profile.articles << folder; folder.save!
  627 + folder = Folder.new(:name => 'my intranet', :published => false); profile.articles << folder; folder.save!
628 628  
629 629 post :new, :profile => profile.identifier, :type => 'TextileArticle', :parent_id => folder.id, :article => { :name => 'new-private-article'}
630 630 folder.reload
631 631  
632   - assert !assigns(:article).public?
  632 + assert !assigns(:article).published?
633 633 assert_equal 'new-private-article', folder.children[0].name
634   - assert !folder.children[0].public?
  634 + assert !folder.children[0].published?
635 635 end
636 636  
637 637 should 'load communities for that the user belongs' do
... ...
test/functional/content_viewer_controller_test.rb
... ... @@ -293,10 +293,10 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase
293 293 assert_response 404
294 294 end
295 295  
296   - should 'show unpublished articles as unexisting' do
  296 + should 'show access denied to unpublished articles' do
297 297 profile.articles.create!(:name => 'test', :published => false)
298 298 get :view_page, :profile => profile.identifier, :page => [ 'test' ]
299   - assert_response 404
  299 + assert_response 403
300 300 end
301 301  
302 302 should 'show unpublished articles to the user himself' do
... ... @@ -307,19 +307,9 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase
307 307 assert_response :success
308 308 end
309 309  
310   - should 'show unpublished articles to members' do
311   - community = Community.create!(:name => 'testcomm')
312   - community.articles.create!(:name => 'test', :published => false)
313   - community.add_member(profile)
314   -
315   - login_as(profile.identifier)
316   - get :view_page, :profile => community.identifier, :page => [ 'test' ]
317   - assert_response :success
318   - end
319   -
320 310 should 'not show private content to members' do
321 311 community = Community.create!(:name => 'testcomm')
322   - Folder.create!(:name => 'test', :profile => community, :public_article => false)
  312 + Folder.create!(:name => 'test', :profile => community, :published => false)
323 313 community.add_member(profile)
324 314  
325 315 login_as(profile.identifier)
... ... @@ -332,7 +322,7 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase
332 322  
333 323 should 'show private content to profile moderators' do
334 324 community = Community.create!(:name => 'testcomm')
335   - community.articles.create!(:name => 'test', :public_article => false)
  325 + community.articles.create!(:name => 'test', :published => false)
336 326 community.add_moderator(profile)
337 327  
338 328 login_as(profile.identifier)
... ... @@ -344,7 +334,7 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase
344 334  
345 335 should 'show private content to profile admins' do
346 336 community = Community.create!(:name => 'testcomm')
347   - community.articles.create!(:name => 'test', :public_article => false)
  337 + community.articles.create!(:name => 'test', :published => false)
348 338 community.add_admin(profile)
349 339  
350 340 login_as(profile.identifier)
... ... @@ -430,7 +420,7 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase
430 420  
431 421 should 'not give access to private articles if logged off' do
432 422 profile = Profile.create!(:name => 'test profile', :identifier => 'test_profile')
433   - intranet = Folder.create!(:name => 'my_intranet', :profile => profile, :public_article => false)
  423 + intranet = Folder.create!(:name => 'my_intranet', :profile => profile, :published => false)
434 424  
435 425 @request.stubs(:ssl?).returns(true)
436 426 get :view_page, :profile => 'test_profile', :page => [ 'my-intranet' ]
... ... @@ -441,7 +431,7 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase
441 431 should 'not give access to private articles if logged in but not member' do
442 432 login_as('testinguser')
443 433 profile = Profile.create!(:name => 'test profile', :identifier => 'test_profile')
444   - intranet = Folder.create!(:name => 'my_intranet', :profile => profile, :public_article => false)
  434 + intranet = Folder.create!(:name => 'my_intranet', :profile => profile, :published => false)
445 435  
446 436 @request.stubs(:ssl?).returns(true)
447 437 get :view_page, :profile => 'test_profile', :page => [ 'my-intranet' ]
... ... @@ -452,7 +442,7 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase
452 442 should 'not give access to private articles if logged in and only member' do
453 443 person = create_user('test_user').person
454 444 profile = Profile.create!(:name => 'test profile', :identifier => 'test_profile')
455   - intranet = Folder.create!(:name => 'my_intranet', :profile => profile, :public_article => false)
  445 + intranet = Folder.create!(:name => 'my_intranet', :profile => profile, :published => false)
456 446 profile.affiliate(person, Profile::Roles.member(profile.environment.id))
457 447 login_as('test_user')
458 448  
... ... @@ -465,7 +455,7 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase
465 455 should 'give access to private articles if logged in and moderator' do
466 456 person = create_user('test_user').person
467 457 profile = Profile.create!(:name => 'test profile', :identifier => 'test_profile')
468   - intranet = Folder.create!(:name => 'my_intranet', :profile => profile, :public_article => false)
  458 + intranet = Folder.create!(:name => 'my_intranet', :profile => profile, :published => false)
469 459 profile.affiliate(person, Profile::Roles.moderator(profile.environment.id))
470 460 login_as('test_user')
471 461  
... ... @@ -478,7 +468,7 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase
478 468 should 'give access to private articles if logged in and admin' do
479 469 person = create_user('test_user').person
480 470 profile = Profile.create!(:name => 'test profile', :identifier => 'test_profile')
481   - intranet = Folder.create!(:name => 'my_intranet', :profile => profile, :public_article => false)
  471 + intranet = Folder.create!(:name => 'my_intranet', :profile => profile, :published => false)
482 472 profile.affiliate(person, Profile::Roles.admin(profile.environment.id))
483 473 login_as('test_user')
484 474  
... ... @@ -507,21 +497,21 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase
507 497  
508 498 should 'require SSL for viewing non-public articles' do
509 499 Environment.default.update_attribute(:enable_ssl, true)
510   - page = profile.articles.create!(:name => 'myarticle', :body => 'top secret', :public_article => false)
  500 + page = profile.articles.create!(:name => 'myarticle', :body => 'top secret', :published => false)
511 501 get :view_page, :profile => 'testinguser', :page => [ 'myarticle' ]
512 502 assert_redirected_to :protocol => 'https://', :profile => 'testinguser', :page => [ 'myarticle' ]
513 503 end
514 504  
515 505 should 'avoid SSL for viewing public articles' do
516 506 @request.expects(:ssl?).returns(true).at_least_once
517   - page = profile.articles.create!(:name => 'myarticle', :body => 'top secret', :public_article => true)
  507 + page = profile.articles.create!(:name => 'myarticle', :body => 'top secret', :published => true)
518 508 get :view_page, :profile => 'testinguser', :page => [ 'myarticle' ]
519 509 assert_redirected_to :protocol => 'http://', :profile => 'testinguser', :page => [ 'myarticle' ]
520 510 end
521 511  
522 512 should 'not redirect to SSL if already on SSL' do
523 513 @request.expects(:ssl?).returns(true).at_least_once
524   - page = profile.articles.create!(:name => 'myarticle', :body => 'top secret', :public_article => false)
  514 + page = profile.articles.create!(:name => 'myarticle', :body => 'top secret', :published => false)
525 515 login_as('testinguser')
526 516 get :view_page, :profile => 'testinguser', :page => [ 'myarticle' ]
527 517 assert_response :success
... ...
test/unit/article_test.rb
... ... @@ -160,8 +160,8 @@ class ArticleTest &lt; Test::Unit::TestCase
160 160 p = create_user('usr1').person
161 161 Article.destroy_all
162 162  
163   - first = p.articles.build(:name => 'first', :public_article => true); first.save!
164   - second = p.articles.build(:name => 'second', :public_article => false); second.save!
  163 + first = p.articles.build(:name => 'first', :published => true); first.save!
  164 + second = p.articles.build(:name => 'second', :published => false); second.save!
165 165  
166 166 assert_equal [ first ], Article.recent(nil)
167 167 end
... ... @@ -202,8 +202,8 @@ class ArticleTest &lt; Test::Unit::TestCase
202 202  
203 203 now = Time.now
204 204  
205   - first = p.articles.build(:name => 'first', :public_article => true, :created_at => now, :published_at => now); first.save!
206   - second = p.articles.build(:name => 'second', :public_article => true, :updated_at => now, :published_at => now + 1.second); second.save!
  205 + first = p.articles.build(:name => 'first', :published => true, :created_at => now, :published_at => now); first.save!
  206 + second = p.articles.build(:name => 'second', :published => true, :updated_at => now, :published_at => now + 1.second); second.save!
207 207  
208 208 assert_equal [ second, first ], Article.recent(2)
209 209  
... ... @@ -443,21 +443,21 @@ class ArticleTest &lt; Test::Unit::TestCase
443 443 assert !Article.new.accept_category?(ProductCategory.new)
444 444 end
445 445  
446   - should 'accept public_article attribute' do
447   - assert_respond_to Article.new, :public_article
448   - assert_respond_to Article.new, :public_article=
  446 + should 'accept published attribute' do
  447 + assert_respond_to Article.new, :published
  448 + assert_respond_to Article.new, :published=
449 449 end
450 450  
451 451 should 'say that logged off user cannot see private article' do
452 452 profile = Profile.create!(:name => 'test profile', :identifier => 'test_profile')
453   - article = Article.create!(:name => 'test article', :profile => profile, :public_article => false)
  453 + article = Article.create!(:name => 'test article', :profile => profile, :published => false)
454 454  
455 455 assert !article.display_to?(nil)
456 456 end
457 457  
458 458 should 'say that not member of profile cannot see private article' do
459 459 profile = Profile.create!(:name => 'test profile', :identifier => 'test_profile')
460   - article = Article.create!(:name => 'test article', :profile => profile, :public_article => false)
  460 + article = Article.create!(:name => 'test article', :profile => profile, :published => false)
461 461 person = create_user('test_user').person
462 462  
463 463 assert !article.display_to?(person)
... ... @@ -465,7 +465,7 @@ class ArticleTest &lt; Test::Unit::TestCase
465 465  
466 466 should 'say that member user can not see private article' do
467 467 profile = Profile.create!(:name => 'test profile', :identifier => 'test_profile')
468   - article = Article.create!(:name => 'test article', :profile => profile, :public_article => false)
  468 + article = Article.create!(:name => 'test article', :profile => profile, :published => false)
469 469 person = create_user('test_user').person
470 470 profile.affiliate(person, Profile::Roles.member(profile.environment.id))
471 471  
... ... @@ -474,7 +474,7 @@ class ArticleTest &lt; Test::Unit::TestCase
474 474  
475 475 should 'say that profile admin can see private article' do
476 476 profile = Profile.create!(:name => 'test profile', :identifier => 'test_profile')
477   - article = Article.create!(:name => 'test article', :profile => profile, :public_article => false)
  477 + article = Article.create!(:name => 'test article', :profile => profile, :published => false)
478 478 person = create_user('test_user').person
479 479 profile.affiliate(person, Profile::Roles.admin(profile.environment.id))
480 480  
... ... @@ -483,7 +483,7 @@ class ArticleTest &lt; Test::Unit::TestCase
483 483  
484 484 should 'say that profile moderator can see private article' do
485 485 profile = Profile.create!(:name => 'test profile', :identifier => 'test_profile')
486   - article = Article.create!(:name => 'test article', :profile => profile, :public_article => false)
  486 + article = Article.create!(:name => 'test article', :profile => profile, :published => false)
487 487 person = create_user('test_user').person
488 488 profile.affiliate(person, Profile::Roles.moderator(profile.environment.id))
489 489  
... ... @@ -492,7 +492,7 @@ class ArticleTest &lt; Test::Unit::TestCase
492 492  
493 493 should 'not show article to non member if article public but profile private' do
494 494 profile = Profile.create!(:name => 'test profile', :identifier => 'test_profile', :public_profile => false)
495   - article = Article.create!(:name => 'test article', :profile => profile, :public_article => true)
  495 + article = Article.create!(:name => 'test article', :profile => profile, :published => true)
496 496 person1 = create_user('test_user1').person
497 497 profile.affiliate(person1, Profile::Roles.member(profile.environment.id))
498 498 person2 = create_user('test_user2').person
... ... @@ -504,54 +504,27 @@ class ArticleTest &lt; Test::Unit::TestCase
504 504  
505 505 should 'make new article private if created inside a private folder' do
506 506 profile = Profile.create!(:name => 'test profile', :identifier => 'test_profile')
507   - folder = Folder.create!(:name => 'my_intranet', :profile => profile, :public_article => false)
  507 + folder = Folder.create!(:name => 'my_intranet', :profile => profile, :published => false)
508 508 article = Article.create!(:name => 'my private article', :profile => profile, :parent => folder)
509 509  
510   - assert !article.public_article
511   - end
512   -
513   - should 'respond to public? like public_article if profile is public' do
514   - p = Profile.create!(:name => 'test profile', :identifier => 'test_profile')
515   - a1 = Article.create!(:name => 'test public article', :profile => p)
516   - a2 = Article.create!(:name => 'test private article', :profile => p, :public_article => false)
517   -
518   - assert a1.public?
519   - assert !a2.public?
520   - end
521   -
522   - should 'respond to public? as false if profile is private' do
523   - p = Profile.create!(:name => 'test profile', :identifier => 'test_profile', :public_profile => false)
524   - a1 = Article.create!(:name => 'test public article', :profile => p)
525   - a2 = Article.create!(:name => 'test private article', :profile => p, :public_article => false)
526   -
527   - assert !a1.public?
528   - assert !a2.public?
529   - end
530   -
531   - should 'respond to public? as false if profile is invisible' do
532   - profile = fast_create(Profile, :visible => false)
533   - article1 = fast_create(Article, :profile_id => profile.id)
534   - article2 = fast_create(Article, :profile_id => profile.id, :public_article => false)
535   -
536   - assert !article1.public?
537   - assert !article2.public?
  510 + assert !article.published?
538 511 end
539 512  
540 513 should 'save as private' do
541 514 profile = Profile.create!(:name => 'test profile', :identifier => 'test_profile')
542   - folder = Folder.create!(:name => 'my_intranet', :profile => profile, :public_article => false)
  515 + folder = Folder.create!(:name => 'my_intranet', :profile => profile, :published => false)
543 516 article = TextileArticle.new(:name => 'my private article')
544 517 article.profile = profile
545 518 article.parent = folder
546 519 article.save!
547 520 article.reload
548 521  
549   - assert !article.public_article
  522 + assert !article.published?
550 523 end
551 524  
552 525 should 'not allow friends of private person see the article' do
553 526 person = create_user('test_user').person
554   - article = Article.create!(:name => 'test article', :profile => person, :public_article => false)
  527 + article = Article.create!(:name => 'test article', :profile => person, :published => false)
555 528 friend = create_user('test_friend').person
556 529 person.add_friend(friend)
557 530 person.save!
... ... @@ -562,7 +535,7 @@ class ArticleTest &lt; Test::Unit::TestCase
562 535  
563 536 should 'display private articles to people who can view private content' do
564 537 person = create_user('test_user').person
565   - article = Article.create!(:name => 'test article', :profile => person, :public_article => false)
  538 + article = Article.create!(:name => 'test article', :profile => person, :published => false)
566 539  
567 540 admin_user = create_user('admin_user').person
568 541 admin_user.stubs(:has_permission?).with('view_private_content', article.profile).returns('true')
... ...
test/unit/profile_test.rb
... ... @@ -930,8 +930,8 @@ class ProfileTest &lt; Test::Unit::TestCase
930 930 p1 = create(Profile)
931 931 p2 = create(Profile, :public_profile => false)
932 932  
933   - assert p1.articles.find(:first, :conditions => {:public_article => false})
934   - assert !p2.articles.find(:first, :conditions => {:public_article => false})
  933 + assert p1.articles.find(:first, :conditions => {:published => false})
  934 + assert !p2.articles.find(:first, :conditions => {:published => false})
935 935 end
936 936  
937 937 should 'remove member with many roles' do
... ...