-
Notifications
You must be signed in to change notification settings - Fork 897
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 #17926 from lpichler/restrict_tenant_quotas
Add method to allow access for tenant quotas
- Loading branch information
Showing
3 changed files
with
58 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module TenantQuotasMixin | ||
extend ActiveSupport::Concern | ||
|
||
def tenant_quotas_allowed? | ||
current_user = User.current_user | ||
return true if current_user.super_admin_user? | ||
return true unless current_user.miq_user_role.tenant_admin_user? | ||
|
||
current_tenant = current_user.current_tenant | ||
# don't allow tenant quotas for current tenant and for ancestors | ||
!(current_tenant == self || current_tenant.ancestor_ids.include?(id)) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
describe TenantQuotasMixin do | ||
before do | ||
Tenant.seed | ||
end | ||
|
||
let(:root_tenant) do | ||
Tenant.root_tenant | ||
end | ||
|
||
let(:super_admin_role) { FactoryGirl.create(:miq_user_role, :features => MiqProductFeature::SUPER_ADMIN_FEATURE) } | ||
let(:tenant_admin_role) { FactoryGirl.create(:miq_user_role, :features => MiqProductFeature::TENANT_ADMIN_FEATURE) } | ||
|
||
let(:tenant_1) { FactoryGirl.create(:tenant, :parent => root_tenant) } | ||
let(:tenant_1_1) { FactoryGirl.create(:tenant, :parent => tenant_1) } | ||
let(:tenant_1_2) { FactoryGirl.create(:tenant, :parent => tenant_1, :divisible => false) } | ||
|
||
let(:group_tenant_1_tenant_admin) { FactoryGirl.create(:miq_group, :miq_user_role => tenant_admin_role, :tenant => tenant_1) } | ||
let(:user_tenant_1_tenant_admin) { FactoryGirl.create(:user, :miq_groups => [group_tenant_1_tenant_admin]) } | ||
|
||
let(:group_tenant_1_super_admin) { FactoryGirl.create(:miq_group, :miq_user_role => super_admin_role, :tenant => tenant_1) } | ||
let(:user_tenant_1_super_admin) { FactoryGirl.create(:user, :miq_groups => [group_tenant_1_super_admin]) } | ||
|
||
describe "#tenant_quotas_allowed?" do | ||
it "allows managing on all tenant quotas when user is super admin" do | ||
User.with_user(user_tenant_1_super_admin) do | ||
expect(root_tenant.tenant_quotas_allowed?).to be_truthy | ||
expect(tenant_1.tenant_quotas_allowed?).to be_truthy | ||
expect(tenant_1_1.tenant_quotas_allowed?).to be_truthy | ||
expect(tenant_1_2.tenant_quotas_allowed?).to be_truthy | ||
end | ||
end | ||
|
||
context "user has tenant-admin role" do | ||
it "allows managing on tenant quotas" do | ||
User.with_user(user_tenant_1_tenant_admin) do | ||
expect(root_tenant.tenant_quotas_allowed?).to be_falsey | ||
expect(tenant_1.tenant_quotas_allowed?).to be_falsey | ||
expect(tenant_1_1.tenant_quotas_allowed?).to be_truthy | ||
expect(tenant_1_2.tenant_quotas_allowed?).to be_truthy | ||
end | ||
end | ||
end | ||
end | ||
end |