Commit c366e6ecb18b9aa7add7bc3f2a3a93051babef28

Authored by Larissa Reis
1 parent 6b9d32eb

api: only search visible organization in current model

  scopes are defined as class methods on the singleton of the class they
  were named instead of the class that calls them, so regardless if they
  are called by Enterprise or Community, they were defined on
  Organization, so #visible_for_person() always gives us
    `WHERE "profiles"."type" IN ('Organization', 'Enterprise', 'Community')`

  This wouldn't be a problem if rails 3.2 didn't merge the WHERE clauses
  of nested scopes, which causes the previous filtering by type of
  Enterprise/Community to be thrown away in favor of the broader one.

  This was fixed in later rails versions but we want to be able to
  search only for Enterprises or Communities, so I translated the scope
  to a class method on the parent class Organization, so it can be
  inherited by the other.
app/models/organization.rb
... ... @@ -16,7 +16,7 @@ class Organization < Profile
16 16 # visible.
17 17 # 4) The user is not a member of the organization but the organization is
18 18 # visible, public and enabled.
19   - scope :visible_for_person, lambda { |person|
  19 + def self.visible_for_person(person)
20 20 joins('LEFT JOIN "role_assignments" ON ("role_assignments"."resource_id" = "profiles"."id"
21 21 AND "role_assignments"."resource_type" = \'Profile\') OR (
22 22 "role_assignments"."resource_id" = "profiles"."environment_id" AND
... ... @@ -31,7 +31,7 @@ class Organization < Profile
31 31 'profile_admin', 'environment_administrator', Profile.name, person.id,
32 32 Profile.name, person.id, true, true, true]
33 33 ).uniq
34   - }
  34 + end
35 35  
36 36 settings_items :closed, :type => :boolean, :default => false
37 37 def closed?
... ...
test/unit/api/communities_test.rb
... ... @@ -7,6 +7,15 @@ class CommunitiesTest < ActiveSupport::TestCase
7 7 login_api
8 8 end
9 9  
  10 + should 'list only communities' do
  11 + community = fast_create(Community)
  12 + enterprise = fast_create(Enterprise) # should not list this enterprise
  13 + get "/api/v1/communities?#{params.to_query}"
  14 + json = JSON.parse(last_response.body)
  15 + assert_not_includes json['communities'].map {|c| c['id']}, enterprise.id
  16 + assert_includes json['communities'].map {|c| c['id']}, community.id
  17 + end
  18 +
10 19 should 'list all communities' do
11 20 community1 = fast_create(Community, :public_profile => true)
12 21 community2 = fast_create(Community)
... ...
test/unit/api/enterprises_test.rb
... ... @@ -7,6 +7,15 @@ class EnterprisesTest < ActiveSupport::TestCase
7 7 login_api
8 8 end
9 9  
  10 + should 'list only enterprises' do
  11 + community = fast_create(Community) # should not list this community
  12 + enterprise = fast_create(Enterprise, :public_profile => true)
  13 + get "/api/v1/enterprises?#{params.to_query}"
  14 + json = JSON.parse(last_response.body)
  15 + assert_includes json['enterprises'].map {|c| c['id']}, enterprise.id
  16 + assert_not_includes json['enterprises'].map {|c| c['id']}, community.id
  17 + end
  18 +
10 19 should 'list all enterprises' do
11 20 enterprise1 = fast_create(Enterprise, :public_profile => true)
12 21 enterprise2 = fast_create(Enterprise)
... ...