Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tenant admin should not be able to create groups in other tenants. #13483

Merged
merged 1 commit into from
Jan 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/models/tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,11 @@ def self.seed
# [["tenant/tenant2/project4", 4]]
# ]
def self.tenant_and_project_names
tenants_and_projects = Tenant.in_my_region.select(:id, :ancestry, :divisible, :use_config_for_attributes, :name)
all_tenants_and_projects = Tenant.in_my_region.select(:id, :ancestry, :divisible, :use_config_for_attributes, :name)
tenants_by_id = all_tenants_and_projects.index_by(&:id)

tenants_and_projects = Rbac.filtered(Tenant.in_my_region.select(:id, :ancestry, :divisible, :use_config_for_attributes, :name))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to pass the local variable into rbac (could you verify it is not worse?):

tenants_and_projects = Rbac.filtered(all_tenants_and_projects)

bummed about the double query here, but looks necessary

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear to me why all_tenants_and_projects can't be passed into Rbac.filtered. @martinpovolny, @kbrock?

.to_a.sort_by { |t| [t.ancestry || "", t.name] }
tenants_by_id = tenants_and_projects.index_by(&:id)

tenants_and_projects.partition(&:divisible?).map do |tenants|
tenants.map do |t|
Expand Down
31 changes: 21 additions & 10 deletions spec/models/tenant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
include_examples ".seed called multiple times"

let(:tenant) { described_class.new(:domain => 'x.com', :parent => default_tenant) }
let(:user_admin) {
user = FactoryGirl.create(:user_admin)
allow(user).to receive(:get_timezone).and_return("UTC")
user
}

let(:default_tenant) do
root_tenant
Expand Down Expand Up @@ -861,9 +866,11 @@
ten1 = FactoryGirl.create(:tenant, :name => "ten1", :parent => root_tenant)
ten2 = FactoryGirl.create(:tenant, :name => "ten2", :parent => ten1)

tenants, projects = Tenant.tenant_and_project_names
expect(tenants).to eq([["root", root_tenant.id], ["root/ten1", ten1.id], ["root/ten1/ten2", ten2.id]])
expect(projects).to be_empty
User.with_user(user_admin) do
tenants, projects = Tenant.tenant_and_project_names
expect(tenants).to eq([["root", root_tenant.id], ["root/ten1", ten1.id], ["root/ten1/ten2", ten2.id]])
expect(projects).to be_empty
end
end

# root
Expand All @@ -873,9 +880,11 @@
proj2 = FactoryGirl.create(:tenant, :name => "proj2", :divisible => false, :parent => root_tenant)
proj1 = FactoryGirl.create(:tenant, :name => "proj1", :divisible => false, :parent => root_tenant)

tenants, projects = Tenant.tenant_and_project_names
expect(tenants).to eq([["root", root_tenant.id]])
expect(projects).to eq([["root/proj1", proj1.id], ["root/proj2", proj2.id]])
User.with_user(user_admin) do
tenants, projects = Tenant.tenant_and_project_names
expect(tenants).to eq([["root", root_tenant.id]])
expect(projects).to eq([["root/proj1", proj1.id], ["root/proj2", proj2.id]])
end
end

# root
Expand All @@ -893,11 +902,13 @@
FactoryGirl.create(:tenant, :name => "proj1", :divisible => false, :parent => ten1)
FactoryGirl.create(:tenant, :name => "proj3", :divisible => false, :parent => root_tenant)

tenants, projects = Tenant.tenant_and_project_names
expect(tenants.map(&:first)).to eq(%w(root root/ten1 root/ten2 root/ten3))
expect(tenants.first.last).to eq(root_tenant.id)
User.with_user(user_admin) do
tenants, projects = Tenant.tenant_and_project_names
expect(tenants.map(&:first)).to eq(%w(root root/ten1 root/ten2 root/ten3))
expect(tenants.first.last).to eq(root_tenant.id)

expect(projects.map(&:first)).to eq(%w(root/proj3 root/ten1/proj1 root/ten2/proj2))
expect(projects.map(&:first)).to eq(%w(root/proj3 root/ten1/proj1 root/ten2/proj2))
end
end
end

Expand Down