From 08373ae09c66f67616b3d7e2a122c8c1bd60c04e Mon Sep 17 00:00:00 2001 From: Jillian Tullo Date: Thu, 15 Feb 2018 13:38:02 -0500 Subject: [PATCH] Add sui_product_features method to miq_group The SUI needs the ability to see what SUI product features are available to the user before changing groups. --- app/models/miq_group.rb | 8 ++++++++ spec/models/miq_group_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/app/models/miq_group.rb b/app/models/miq_group.rb index 953674ca6d4..1090043bf40 100644 --- a/app/models/miq_group.rb +++ b/app/models/miq_group.rb @@ -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 @@ -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 diff --git a/spec/models/miq_group_spec.rb b/spec/models/miq_group_spec.rb index cc2199f79a6..991d3ea6ac6 100644 --- a/spec/models/miq_group_spec.rb +++ b/spec/models/miq_group_spec.rb @@ -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