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

Fix permissions to see admin menu items #3840

Merged
merged 1 commit into from
Nov 18, 2020
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
10 changes: 5 additions & 5 deletions backend/app/views/spree/admin/shared/_settings_sub_menu.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
<%= tab :stores, label: :stores, url: spree.admin_stores_path %>
<% end %>

<% if can?(:show, Spree::PaymentMethod) %>
<% if can?(:admin, Spree::PaymentMethod) %>
<%= tab :payments, url: spree.admin_payment_methods_path %>
<% end %>

<% if can?(:show, Spree::TaxCategory) || can?(:show, Spree::TaxRate) %>
<% if can?(:admin, Spree::TaxCategory) || can?(:admin, Spree::TaxRate) %>
<%= tab :taxes, url: spree.admin_tax_categories_path, match_path: %r(tax_categories|tax_rates) %>
<% end %>

<% if can?(:show, Spree::RefundReason) || can?(:show, Spree::ReimbursementType) ||
<% if can?(:admin, Spree::RefundReason) || can?(:admin, Spree::ReimbursementType) ||
can?(:show, Spree::ReturnReason) || can?(:show, Spree::AdjustmentReason) %>
<%= tab :checkout, url: spree.admin_refund_reasons_path, match_path: %r(refund_reasons|reimbursement_types|return_reasons|adjustment_reasons|store_credit_reasons) %>
<% end %>

<% if can?(:show, Spree::ShippingMethod) || can?(:show, Spree::ShippingCategory) || can?(:show, Spree::StockLocation) %>
<% if can?(:admin, Spree::ShippingMethod) || can?(:admin, Spree::ShippingCategory) || can?(:admin, Spree::StockLocation) %>
<%= tab :shipping, url: spree.admin_shipping_methods_path, match_path: %r(shipping_methods|shipping_categories|stock_locations) %>
<% end %>

<% if can?(:show, Spree::Zone) %>
<% if can?(:admin, Spree::Zone) %>
<%= tab :zones, url: spree.admin_zones_path %>
<% end %>
</ul>
22 changes: 11 additions & 11 deletions backend/lib/spree/backend_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,17 @@ def menu_items
'wrench',
condition: -> {
can?(:admin, Spree::Store) ||
can?(:show, Spree::AdjustmentReason) ||
can?(:show, Spree::PaymentMethod) ||
can?(:show, Spree::RefundReason) ||
can?(:show, Spree::ReimbursementType) ||
can?(:show, Spree::ShippingCategory) ||
can?(:show, Spree::ShippingMethod) ||
can?(:show, Spree::StockLocation) ||
can?(:show, Spree::TaxCategory) ||
can?(:show, Spree::TaxRate) ||
can?(:show, Spree::ReturnReason) ||
can?(:show, Spree::Zone)
can?(:admin, Spree::AdjustmentReason) ||
can?(:admin, Spree::PaymentMethod) ||
can?(:admin, Spree::RefundReason) ||
can?(:admin, Spree::ReimbursementType) ||
can?(:admin, Spree::ShippingCategory) ||
can?(:admin, Spree::ShippingMethod) ||
can?(:admin, Spree::StockLocation) ||
can?(:admin, Spree::TaxCategory) ||
can?(:admin, Spree::TaxRate) ||
can?(:admin, Spree::ReturnReason) ||
can?(:admin, Spree::Zone)
},
label: :settings,
partial: 'spree/admin/shared/settings_sub_menu',
Expand Down
8 changes: 5 additions & 3 deletions backend/spec/features/admin/homepage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,18 @@
custom_authorization! do |_user|
can [:admin, :home], :dashboards
can [:admin, :edit, :index, :show], Spree::Order
cannot [:show], Spree::StockLocation
cannot [:show], Spree::Zone
Comment on lines -76 to -77
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these two be changed to:

      cannot [:admin], Spree::StockLocation
      cannot [:admin], Spree::Zone

So we can be sure that the settings tab is still visible even when the user is not able to access some of its sub-resources?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even with the suggested change, the settings tab is not visible. I could change it this way to make sure the settings tab is displayed if one of the inner tab can be accessed but this won't test that the rest of the settings are excluded:

context 'as fakedispatch user' do
    before do
      allow_any_instance_of(Spree::Admin::BaseController).to receive(:try_spree_current_user).and_return(nil)
    end

    custom_authorization! do |_user|
      can [:admin, :home], :dashboards
      can [:admin, :edit, :index, :show], Spree::Order
      can [:admin], Spree::Zone
    end

    it 'should only display tabs fakedispatch has access to' do
      visit spree.admin_path
      expect(page).to have_link('Orders')
      expect(page).not_to have_link('Products')
      expect(page).not_to have_link('Promotions')
      expect(page).to have_link('Settings')
    end
  end

Let me think about something to test that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elia maybe I've got a possible test, let me know your thoughts!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @kennyadsl this look like a nice canary to catch if anything is going wrong with permissions without checking all the possible combinations 👍

cannot [:admin], Spree::StockLocation
can [:admin], Spree::Zone
end

it 'should only display tabs fakedispatch has access to' do
visit spree.admin_path
expect(page).to have_link('Orders')
expect(page).not_to have_link('Products')
expect(page).not_to have_link('Promotions')
expect(page).not_to have_link('Settings')
expect(page).to have_link('Settings')
expect(page).not_to have_link('Stock Locations', visible: false)
expect(page).to have_link('Zones', visible: false)
end
end
end