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

Add method to allow access for tenant quotas #17926

Merged
merged 1 commit into from
Sep 6, 2018
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
13 changes: 13 additions & 0 deletions app/models/mixins/tenant_quotas_mixin.rb
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
1 change: 1 addition & 0 deletions app/models/tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Tenant < ApplicationRecord

include ActiveVmAggregationMixin
include CustomActionsMixin
include TenantQuotasMixin

acts_as_miq_taggable

Expand Down
44 changes: 44 additions & 0 deletions spec/models/mixins/tenant_quotas_mixin_spec.rb
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