-
Notifications
You must be signed in to change notification settings - Fork 900
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
Introduces CloudTenancyMixin to fix RBAC for cloud_tenant based models #13535
Changes from all commits
fc32e7e
936097c
381a0ba
43d4b72
3a86234
4e9b56e
530df45
33a3327
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just question, why it is moved to method ? is called on other place then on line 17? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To share lines 14-15 between TenancyCommonMixin and CloudTenancyMixin. CloudTenancyMixin has a different set of conditionals for the tenant_id_clause. |
||
{table_name => {:tenant_id => tenant_ids}} | ||
end | ||
end |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about to use
delegate :tenant_mapping_enabled, :to => :parent_manager
for example on class
StorageManager < ManageIQ::Providers::BaseManager
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, I guess no, now I understand why we need it