diff --git a/app/models/feature_flags.rb b/app/models/feature_flags.rb index 096ef44fcc..70dcf1c04d 100644 --- a/app/models/feature_flags.rb +++ b/app/models/feature_flags.rb @@ -11,25 +11,17 @@ class FeatureFlags # # @return [Boolean] def product_import_enabled? - superadmin? + user.superadmin? end # Checks whether the "Enterprise Fee Summary" is enabled for the specified user # # @return [Boolean] def enterprise_fee_summary_enabled? - superadmin? + user.superadmin? end private attr_reader :user - - # Checks whether the specified user is a superadmin, with full control of the - # instance - # - # @return [Boolean] - def superadmin? - user.has_spree_role?('admin') - end end diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb index dec0309d2b..7b43d39b90 100644 --- a/app/models/spree/user_decorator.rb +++ b/app/models/spree/user_decorator.rb @@ -74,6 +74,14 @@ Spree.user_class.class_eval do credit_cards.where(is_default: true).first end + # Checks whether the specified user is a superadmin, with full control of the + # instance + # + # @return [Boolean] + def superadmin? + has_spree_role?('admin') + end + private def limit_owned_enterprises diff --git a/config/routes/admin.rb b/config/routes/admin.rb index f79a3eb742..e46ef5917a 100644 --- a/config/routes/admin.rb +++ b/config/routes/admin.rb @@ -1,7 +1,7 @@ Openfoodnetwork::Application.routes.draw do namespace :admin do - authenticated :spree_user, -> user { user.has_spree_role?('admin') } do + authenticated :spree_user, -> user { user.superadmin? } do mount DelayedJobWeb, at: '/delayed_job' end diff --git a/spec/models/feature_flags_spec.rb b/spec/models/feature_flags_spec.rb index 6b9380b879..b2ca275648 100644 --- a/spec/models/feature_flags_spec.rb +++ b/spec/models/feature_flags_spec.rb @@ -7,7 +7,7 @@ describe FeatureFlags do describe '#product_import_enabled?' do context 'when the user is superadmin' do before do - allow(user).to receive(:has_spree_role?).with('admin') { true } + allow(user).to receive(:superadmin?) { true } end it 'returns true' do @@ -17,7 +17,7 @@ describe FeatureFlags do context 'when the user is not superadmin' do before do - allow(user).to receive(:has_spree_role?).with('admin') { false } + allow(user).to receive(:superadmin?) { false } end it 'returns false' do diff --git a/spec/models/spree/user_spec.rb b/spec/models/spree/user_spec.rb index 7169930489..801d9ef9a7 100644 --- a/spec/models/spree/user_spec.rb +++ b/spec/models/spree/user_spec.rb @@ -162,4 +162,22 @@ describe Spree.user_class do end end end + + describe '#superadmin?' do + let(:user) { create(:user) } + + context 'when the user has an admin spree role' do + before { user.spree_roles << Spree::Role.create(name: 'admin') } + + it 'returns true' do + expect(user.superadmin?).to eq(true) + end + end + + context 'when the user does not have an admin spree role' do + it 'returns false' do + expect(user.superadmin?).to eq(false) + end + end + end end