Skip to content

Commit

Permalink
Add method to allow access for tenant quotas
Browse files Browse the repository at this point in the history
  • Loading branch information
lpichler committed Aug 31, 2018
1 parent 7550098 commit 4ec38c7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
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

0 comments on commit 4ec38c7

Please sign in to comment.