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

Backend: Extra cancancan validations for some URL resource links #2823

Closed
wants to merge 0 commits into from
Closed

Conversation

memotoro
Copy link
Contributor

@memotoro memotoro commented Aug 7, 2018

Hello

In the backend system, some resource link are accesible without any cancancan validations. These extra simple conditionals will help to avoid a leak on those resources for users without the right roles and permissions sets. These changes simply avoid rendering the "create new resource" links if the user has no permissions to do so, given a cleaner backend user experience.

Regards

Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution! I left some questions, also specs are failing, can you please take a look?

backend/app/views/spree/admin/images/index.html.erb Outdated Show resolved Hide resolved
@@ -7,7 +7,7 @@
<li>
<%= link_to t('spree.new_order'), new_admin_order_url, id: 'admin_new_order', class: 'btn btn-primary' %>
</li>
<% end if can? :create, Spree::Order %>
<% end if can? :manage, Spree::Order %>
Copy link
Member

@kennyadsl kennyadsl Aug 7, 2018

Choose a reason for hiding this comment

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

why manage if the link is for creating orders only?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hello @kennyadsl .

There are two classes to manage the permissions sets Spree::PermissionSets::OrderManagement and Spree::PermissionSets::OrderDisplay. The OrderManagement establishes the ':manage' permissions. The display establishes the ':display' and ':admin' permissions. If I want to restrict a user with only the permission OrderDisplay, I have to check for ':manage' permission as for some reasons, the ':create' permission on OrderDisplay is still allowing the button to be rendered, allowing the user to click the New Order button, and creating an empty order that it cannot be modified, not great user experience I guess. I'm not sure why the ':create' is still available on OrderDisplay. Do you know how to fix this issue? Maybe I misunderstood the OrderDisplay role. Let me know your thoughts about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hello @kennyadsl

Do you have any thoughts about my previous comments on PermissionSets?

Regards

Copy link
Member

Choose a reason for hiding this comment

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

By default only this permissions set is activated. I think that at line 10 it sets the create ability for Order. Are you setting the other permissions for a specific user in some way via code?

Copy link
Contributor

Choose a reason for hiding this comment

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

From what I can understand from @memotoro's comment and the proposed patch is that, OrderDisplay works only to create orders without any further actions, whereas OrderManagement allows full control over them. Since we're only talking about backend, this feels reasonable.

As @tvdeyen said on his comment below, if you can create an order, you probably should be able to update it as well.

backend/app/views/spree/admin/payments/index.html.erb Outdated Show resolved Hide resolved
@@ -3,7 +3,7 @@
<% content_for :page_actions do %>
<% if @order.shipments.any? &:shipped? %>
<li>
<% if can? :create, Spree::ReturnAuthorization %>
<% if can? :manage, Spree::ReturnAuthorization %>
Copy link
Member

Choose a reason for hiding this comment

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

same question as above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kennyadsl Same comment with the OrderDisplay issue mentioned above.

@tvdeyen
Copy link
Member

tvdeyen commented Sep 11, 2018

I think :manage is fine for orders and returns. It only makes sense to be able to create an order if you are also able to update it.

Please fix the specs. Thanks for the contribution

@memotoro
Copy link
Contributor Author

@kennyadsl @tvdeyen . Changes as requested. Let me know your thoughts. Regards

@ericsaupe
Copy link
Contributor

Looks good. Would you mind squashing your commits?

@memotoro
Copy link
Contributor Author

Hello @ericsaupe @tvdeyen @kennyadsl

Sorry for the late replay. I had issues merging last time that I've squashed my commits to make it a single commit. Have a look and it is passing the CI tests.

Regards

Copy link
Contributor

@ericsaupe ericsaupe left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Contributor

@aitbw aitbw left a comment

Choose a reason for hiding this comment

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

Hey @memotoro, thanks a lot for this PR! 🙌

Is it possible you can rebase it against current master and add some specs? To make sure this doesn't happen again in the future 🙂

@kennyadsl kennyadsl dismissed their stale review March 15, 2019 16:14

Dismissing my review since specs are not broken anymore

@memotoro
Copy link
Contributor Author

Hello @kennyadsl @aitbw . Rebase the commit against the current master. Specs were added before to test the feature. Regards

@kennyadsl
Copy link
Member

Thanks @memotoro!

Specs were added before to test the feature.

What do you mean?

@memotoro
Copy link
Contributor Author

Hello @kennyadsl. What I meant is that I already changed a spec to test this feature, and I did it before the rebase that I just did. This commit is only a rebase against the current master, so it will pass the test cases now. Sorry for the misunderstanding. The spec I modified is backend/spec/features/admin/orders/listing_spec.rb. Hopefully it gets merge into the next release. Regards

Copy link
Contributor

@aitbw aitbw left a comment

Choose a reason for hiding this comment

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

Thanks for rebasing, @memotoro 🙌 left two extra comments as further improvements

Regarding specs: I still think that you should add some tests that reflect your changes; I know that you fixed one spec associated to what you changed but these files were affected and they don't have any tests associated to reflect said changes:

  • backend/app/views/spree/admin/users/items.html.erb
  • backend/app/views/spree/admin/users/orders.html.erb
  • backend/app/views/spree/admin/images/index.html.erb
  • backend/app/views/spree/admin/payments/index.html.erb
  • backend/app/views/spree/admin/return_authorizations/index.html.erb

If I remove what you introduced (on those files) and run the backend spec suite, I still get 💯 green. We'd like to avoid these kind of problems in the future, and I think that's the best way to do so. What do you think?

@@ -13,29 +13,31 @@
<%= render 'new', product: @product, image: Spree::Image.new(viewable: @product) %>
</div>

<fieldset class="no-border-bottom">
<legend align="center"><%= t(".upload_images") %></legend>
<% if can?(:create, Spree::Image) %>
Copy link
Contributor

Choose a reason for hiding this comment

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

Following the same idea with orders, shouldn't we have :manage here instead of :create?

<% if can? :manage, Spree::Order %>
<%= render 'spree/admin/shared/no_objects_found',
resource: Spree::Order,
new_resource_url: spree.new_admin_order_path %>
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this change for new_resource_url? Shouldn't it stay as spree.new_admin_order_path(user_id: @user.id)?

@spaghetticode
Copy link
Member

@memotoro nice work! 🎉

I was wondering if there's some plan of adding the missing integration specs. I also see there are some unanswered questions left by @aitbw, I think we're pretty close to having this finally approved, it just needs a bit more TLC 🔧⚒... thank you!

@kennyadsl kennyadsl added this to the 2.11 milestone May 22, 2020
@kennyadsl
Copy link
Member

Hey @memotoro! Is there any chance for you to complete the PR. I think the last missing piece is this Angel concern, and are good to go. Thanks!

@spaghetticode
Copy link
Member

@memotoro I'll be pushing this PR forward, thanks for the contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants