Skip to content

Commit

Permalink
Add sui_product_features method to miq_group
Browse files Browse the repository at this point in the history
The SUI needs the ability to see what SUI product features are available to the user before changing groups.
  • Loading branch information
Jillian Tullo committed Feb 15, 2018
1 parent f5bc79d commit 08373ae
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/models/miq_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class MiqGroup < ApplicationRecord

virtual_column :miq_user_role_name, :type => :string, :uses => :miq_user_role
virtual_column :read_only, :type => :boolean
virtual_has_one :sui_product_features, :class_name => "Array"

delegate :self_service?, :limited_self_service?, :disallowed_roles, :to => :miq_user_role, :allow_nil => true

Expand Down Expand Up @@ -261,6 +262,13 @@ def single_group_users?
users.includes(:miq_groups).where(:id => group_user_ids).where.not(:miq_groups => {:id => id}).count != group_user_ids.size
end

def sui_product_features
return [] unless miq_user_role.allows?(:identifier => 'sui')
MiqProductFeature.feature_all_children('sui').each_with_object([]) do |sui_feature, sui_features|
sui_features << sui_feature if miq_user_role.allows?(:identifier => sui_feature)
end
end

private

# if this tenant is changing, make sure this is not a default group
Expand Down
24 changes: 24 additions & 0 deletions spec/models/miq_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -546,4 +546,28 @@
expect(User.find_by(:id => user2).current_group.id).to eq(testgroup3.id)
end
end

describe "#sui_product_features" do
let(:role) { double }

before do
allow(subject).to receive(:miq_user_role).and_return(role)
end

it "Returns an empty array for roles without sui support" do
allow(role).to receive(:allows?).with(:identifier => 'sui').and_return(false)

expect(subject.sui_product_features).to be_empty
end

it "Returns the expected sui roles" do
allow(MiqProductFeature).to receive(:feature_all_children).with('sui').and_return(%w(sui_role_a sui_role_b sui_role_c))
%w(sui sui_role_a sui_role_c).each do |ident|
allow(role).to receive(:allows?).with(:identifier => ident).and_return(true)
end
allow(role).to receive(:allows?).with(:identifier => 'sui_role_b').and_return(false)

expect(subject.sui_product_features).to eq(%w(sui_role_a sui_role_c))
end
end
end

0 comments on commit 08373ae

Please sign in to comment.