-
Notifications
You must be signed in to change notification settings - Fork 900
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13535 from rwsu/cloud_tenancy_mixin
Introduces CloudTenancyMixin to fix RBAC for cloud_tenant based models
- Loading branch information
Showing
8 changed files
with
123 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module CloudTenancyMixin | ||
extend ActiveSupport::Concern | ||
|
||
module ClassMethods | ||
include TenancyCommonMixin | ||
|
||
def scope_by_cloud_tenant? | ||
true | ||
end | ||
|
||
def tenant_id_clause_format(tenant_ids) | ||
["(tenants.id IN (?) AND ext_management_systems.tenant_mapping_enabled IS TRUE) OR ext_management_systems.tenant_mapping_enabled IS FALSE OR ext_management_systems.tenant_mapping_enabled IS NULL", tenant_ids] | ||
end | ||
|
||
def tenant_joins_clause(scope) | ||
scope.includes(:cloud_tenant => "source_tenant").includes(:ext_management_system) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module TenancyCommonMixin | ||
def accessible_tenant_ids(user_or_group, strategy) | ||
tenant = user_or_group.try(:current_tenant) | ||
return [] if tenant.nil? || tenant.root? | ||
|
||
tenant.accessible_tenant_ids(strategy) | ||
end | ||
|
||
def tenant_id_clause(user_or_group) | ||
tenant_ids = accessible_tenant_ids(user_or_group, Rbac.accessible_tenant_ids_strategy(self)) | ||
return if tenant_ids.empty? | ||
|
||
tenant_id_clause_format(tenant_ids) | ||
end | ||
|
||
def tenant_id_clause_format(tenant_ids) | ||
{table_name => {:tenant_id => tenant_ids}} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
describe CloudTenancyMixin do | ||
let(:root_tenant) do | ||
Tenant.seed | ||
end | ||
|
||
let(:default_tenant) do | ||
root_tenant | ||
Tenant.default_tenant | ||
end | ||
|
||
describe "miq_group" do | ||
let(:user) { FactoryGirl.create(:user, :userid => 'user', :miq_groups => [tenant_group]) } | ||
let(:tenant) { FactoryGirl.build(:tenant, :parent => default_tenant) } | ||
let(:tenant_users) { FactoryGirl.create(:miq_user_role, :name => "tenant-users") } | ||
let(:tenant_group) { FactoryGirl.create(:miq_group, :miq_user_role => tenant_users, :tenant => tenant) } | ||
|
||
it "finds correct tenant id clause for regular tenants" do | ||
expect(VmOrTemplate.tenant_id_clause(user)).to eql ["(vms.template = true AND vms.tenant_id IN (?)) OR (vms.template = false AND vms.tenant_id IN (?))", [default_tenant.id, tenant.id], [tenant.id]] | ||
end | ||
|
||
it "finds correct tenant id clause for cloud tenants" do | ||
expect(CloudVolume.tenant_id_clause(user)).to eql ["(tenants.id IN (?) AND ext_management_systems.tenant_mapping_enabled IS TRUE) OR ext_management_systems.tenant_mapping_enabled IS FALSE OR ext_management_systems.tenant_mapping_enabled IS NULL", [tenant.id]] | ||
end | ||
end | ||
end |