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

[BaseController] Localize OPTIONS (allow optional user auth) #1043

Merged
merged 1 commit into from
Jun 10, 2021

Conversation

NickLaMuro
Copy link
Member

@NickLaMuro NickLaMuro commented Jun 3, 2021

This allows translating of OPTION data if a user is logged in.

To achieve this, the commit does a few things:

  • Split up .require_api_user_or_token to pull out the "authentication" step into it's own method: .authenticate_user
  • Create a new method called .optional_api_user_or_token for only OPTIONS requests
    • Logs in a user in on OPTIONS calls, but no-op if it fails
    • in otherwords: if you don't pass auth, still work as we did, but allow a user to log in and retrieve settings

By doing this, we can make sure that :set_gettext_locale can have a user if one is provided, and allow translations to be processed further down the line.

FYI

There are places where we are using _N() instead of _(), which will cause some of the translations to still result in English:

https://github.com/ManageIQ/manageiq-providers-kubernetes/blob/9b5b107dfcf702901683f02ac19263e2730d217a/app/models/manageiq/providers/kubernetes/container_manager/options.rb#L5-L13

This probably should be addressed, but can be handled on a case by case basis.

Links

Testing/QA

The spec in spec/requests/providers_spec.rb is a pretty good framework for seeing if this works:

  • Update your user record to use a different locale ("es" for Spanish)

  • Get an API auth token

  • curl the OPTIONS endpoint:

    $ curl -X OPTIONS "localhost:3000?type=ManageIQ::Providers::Openstack::CloudManager"
    

(the curl above is missing some options for auth. I use miqperf from manageiq-performance myself so I don't have to worry about these options)

@NickLaMuro
Copy link
Member Author

@miq-bot add_label bug
@miq-bot assign @Fryguy

@Fryguy assigning you as you seem familiar with this code, but feel free to delegate to someone else.

@NickLaMuro
Copy link
Member Author

cc @kavyanekkalapu

@Fryguy
Copy link
Member

Fryguy commented Jun 3, 2021

wow this is cool...great work @NickLaMuro

@Fryguy
Copy link
Member

Fryguy commented Jun 3, 2021

Wondering if we should cross repo test this

@NickLaMuro
Copy link
Member Author

@Fryguy not opposed to it. Do you have some repos in mind where this makes sense? Not sure what might be using the API in their specs myself. Maybe the UI I guess?

@kavyanekkalapu
Copy link
Member

kavyanekkalapu commented Jun 4, 2021

@NickLaMuro I verified the fix, it is working , tomorrow i will try to do more testing on other pages, i am not sure if we are calling options in all pages or not, Thank you.

Screen Shot 2021-06-03 at 9 42 40 PM

@NickLaMuro
Copy link
Member Author

@miq-bot cross-repo-test ManageIQ/manageiq-ui-clasic

miq-bot pushed a commit to ManageIQ/manageiq-cross_repo-tests that referenced this pull request Jun 4, 2021
def optional_api_user_or_token
authenticate_user
rescue AuthenticationError => e
api_log_error("AuthenticationError: #{e.message} (on #{request.method}... ignoring)")
Copy link
Member

Choose a reason for hiding this comment

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

wondering if we should log this more as a warning, since it's not really an error.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I just threw something in there for getting something pushed up, but definitely wasn't satisfied when I wrote it.

We can absolutely change it to a warning. I will see if there is a method for this somewhere. Happy to change anything else with this if you can think of anything better as well.

Copy link
Member

@Fryguy Fryguy left a comment

Choose a reason for hiding this comment

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

One minor comment

This allows translating of OPTION data if a user is logged in.

To achieve this, the commit does a few things:

- Split up require_api_user_or_token to pull out the "authentication"
  step into it's own method:  `.authenticate_user`
- Create a new method called `.optional_api_user_or_token` for only
  OPTIONS requests
  - Logs in a user in on OPTIONS calls, but no-op if it fails
  - in otherwords:  if you don't pass auth, still work as we did, but
    allow a user to log in and retrieve settings

By doing this, we can make sure that `:set_gettext_locale` can have a
user if one is provided, and allow translations to be processed further
down the line.
@NickLaMuro NickLaMuro force-pushed the option-request-localization branch from 39e340e to e8de60d Compare June 8, 2021 20:40
@NickLaMuro
Copy link
Member Author

@Fryguy let me know if that phrasing change is also good.

@miq-bot
Copy link
Member

miq-bot commented Jun 8, 2021

Checked commit NickLaMuro@e8de60d with ruby 2.6.3, rubocop 1.13.0, haml-lint 0.35.0, and yamllint
3 files checked, 0 offenses detected
Everything looks fine. 🍰

@NickLaMuro NickLaMuro mentioned this pull request Jun 10, 2021
7 tasks
@NickLaMuro NickLaMuro changed the title [BaseController] Localize OPTIONS (optional auth) [BaseController] Localize OPTIONS (allow optional user auth) Jun 10, 2021
@Fryguy Fryguy merged commit b44f339 into ManageIQ:master Jun 10, 2021
@Fryguy
Copy link
Member

Fryguy commented Aug 25, 2021

Backported to morphy in commit 5e6b5e0.

commit 5e6b5e0c52c6593018c428bdee727e0e16d32f09
Author: Jason Frey <fryguy9@gmail.com>
Date:   Thu Jun 10 16:58:58 2021 -0400

    Merge pull request #1043 from NickLaMuro/option-request-localization
    
    [BaseController] Localize OPTIONS (allow optional user auth)
    
    (cherry picked from commit b44f339a6f099941781523860cb9375c5e49066b)

Fryguy added a commit that referenced this pull request Aug 25, 2021
[BaseController] Localize OPTIONS (allow optional user auth)

(cherry picked from commit b44f339)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants