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 stores with no authentication #4456

Conversation

waiting-for-dev
Copy link
Contributor

@waiting-for-dev waiting-for-dev commented Jul 15, 2022

We deprecated #try_spree_current_user in favor of
#spree_current_user in #3923. The former defaulted to nil when
no authentication library was present, but the latter was simply not
defined under those circumstances.

This commit restores the old behavior, as the code base works with the
assumption that current user is nil when no extension has defined it
(see one example or another).

The issue was raised in the support channel.

@waiting-for-dev waiting-for-dev marked this pull request as ready for review July 15, 2022 10:05
We deprecated `#try_spree_current_user` in favor of
`#spree_current_user` in solidusio#3923 [1]. The former defaulted to `nil` when
no authentication library was present, but the latter was simply not
defined under those circumstances.

This commit restores the old behavior, as the code base works with the
assumption that current user is `nil` when no extension has defined it
(e.g. [2] & [3]).

The issue was raised in the support channel [4].

[1] - solidusio#3923
[2] - https://github.com/solidusio/solidus/blob/fee8a860c068b6ad050f9b2ff9052aadf9d56174/backend/app/controllers/spree/admin/base_controller.rb#L36
[3] - https://github.com/solidusio/solidus/blob/fee8a860c068b6ad050f9b2ff9052aadf9d56174/backend/app/controllers/spree/admin/cancellations_controller.rb#L33
[4] - https://solidusio.slack.com/archives/C0JBKDF35/p1657030519259159?thread_ts=1657019907.668419&cid=C0JBKDF35
@waiting-for-dev waiting-for-dev force-pushed the waiting-for-dev/fix_when_no_auth_present branch from 219c059 to 5dc4094 Compare July 15, 2022 10:41
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.

Left a small comment, thanks!

# Auth extensions are expected to define it, otherwise it's a no-op
def spree_current_user
defined?(super) ? super : nil
end
Copy link
Member

@kennyadsl kennyadsl Jul 15, 2022

Choose a reason for hiding this comment

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

Some lines later, in try_spree_current_user, we have:

        def try_spree_current_user
          # This one will be defined by apps looking to hook into Spree
          # As per authentication_helpers.rb
          if respond_to?(:spree_current_user, true)
            spree_current_user
          # This one will be defined by Devise
          elsif respond_to?(:current_spree_user, true)
            current_spree_user
          end
        end

With the proposed change, will the respond_to?(:spree_current_user, true) always be true?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but I think that's ok. Then spree_current_user will delegate to the auth library, and solidus_auth_devise is already implementing it as noted in the linked PR.

My understanding is that #current_spree_user is never defined (solidus_auth_devise delegates to #current_user instead), so it's safe to forgo its support.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure to fully understand here. If we are 100% sure that the elsif will never be executed, why keep 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.

Yes, I guess we can remove it. Pinging @elia to be 100% sure.

Copy link
Member

Choose a reason for hiding this comment

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

@waiting-for-dev you're right, we shouldn't care about current_spree_user and this will work.
I'm just a bit hesitant on having a cover for a missing authentication implementation. Thoughts on having a sort of NoAuth module that we inject from within an initializer like we do for Spree::AuthenticationHelpers in auth-devise?

solidus_auth_devise/lib/spree/auth/engine.rb

module Spree
  module Auth
    class Engine < Rails::Engine
      
      config.to_prepare do
        Spree::Auth::Engine.prepare_backend if SolidusSupport.backend_available?
        Spree::Auth::Engine.prepare_frontend if SolidusSupport.frontend_available?

        ApplicationController.include Spree::AuthenticationHelpers
      end

in the no-auth case would be something like:

my-app/config/initializers/solidus_auth.rb

Rails.application.reloader.to_prepare do
  ApplicationController.include Spree::NoAuth
end

Copy link
Member

@aldesantis aldesantis Jul 18, 2022

Choose a reason for hiding this comment

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

@elia our new storefront customization strategy is never to inject anything, but rather to copy-paste code into the host application. We don't want to do anything weird with the storefront as that makes customization harder.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for looking into it, @elia! On top of what @aldesantis said, I think it's not worth it to make things complex. I agree that we don't have a clear separation of concerns for the authentication system. But extracting things to separated modules would give an apparent impression of orthogonality, but the truth is that all the components expect something to be there. Until we don't revisit it, I think it's better to have the no-op behavior as visible as possible.

@waiting-for-dev
Copy link
Contributor Author

cc @elia

@waiting-for-dev waiting-for-dev merged commit 72868a6 into solidusio:master Jul 20, 2022
@waiting-for-dev waiting-for-dev deleted the waiting-for-dev/fix_when_no_auth_present branch July 20, 2022 03:54
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.

5 participants