Skip to content

Commit

Permalink
[BaseController] Localize OPTIONS (optional auth)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
NickLaMuro committed Jun 3, 2021
1 parent ee146eb commit 39e340e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/controllers/api/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class BaseController < ActionController::API
before_action :log_request_initiated
before_action :clear_cached_current_user
before_action :require_api_user_or_token, :except => [:options, :product_info]
before_action :optional_api_user_or_token, :only => [:options]
before_action :set_gettext_locale, :set_access_control_headers, :parse_api_request, :log_api_request
before_action :validate_api_request, :except => [:product_info]
before_action :validate_api_action, :except => [:options, :product_info]
Expand Down
21 changes: 17 additions & 4 deletions app/controllers/api/base_controller/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ def auth_mechanism
end
end

#
# REST APIs Authenticator and Redirector
#
def require_api_user_or_token
def authenticate_user
case auth_mechanism
when :system
authenticate_with_system_token(request.headers[HttpHeaders::MIQ_TOKEN])
Expand All @@ -44,6 +41,13 @@ def require_api_user_or_token
raise AuthenticationError unless success
end
log_api_auth
end

#
# REST APIs Authenticator and Redirector
#
def require_api_user_or_token
authenticate_user
rescue AuthenticationError => e
api_log_error("AuthenticationError: #{e.message}")
response.headers["Content-Type"] = "application/json"
Expand All @@ -52,6 +56,15 @@ def require_api_user_or_token
log_api_response
end

#
# Attempt auth if desired, but flow through on failure
#
def optional_api_user_or_token
authenticate_user
rescue AuthenticationError => e
api_log_error("AuthenticationError: #{e.message} (on #{request.method}... ignoring)")
end

def user_settings
{
:locale => I18n.locale.to_s.sub('-', '_'),
Expand Down
9 changes: 9 additions & 0 deletions spec/requests/providers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1561,6 +1561,15 @@ def gen_import_request
expect(response.parsed_body["data"]["provider_settings"]["kubernetes"]["proxy_settings"]["settings"]["http_proxy"]["label"]).to eq('HTTP Proxy')
end

it "translates options using user settings if auth passed" do
api_basic_authorize
@user.update(:settings => {:display => {:locale => "es"}})
options("#{api_providers_url}?type=ManageIQ::Providers::Openstack::CloudManager")

field = response.parsed_body["data"]["provider_form_schema"]["fields"].detect { |f| f["id"] == "provider_region" }
expect(field["label"]).to eq("Regi\u00f3n del proveedor")
end

it "returns options for supported providers only" do
allow(Vmdb::PermissionStores.instance).to receive(:supported_ems_type?).and_return(false)
allow(Vmdb::PermissionStores.instance).to receive(:supported_ems_type?).with("vmwarews").and_return(true)
Expand Down

0 comments on commit 39e340e

Please sign in to comment.