Class: RightSharding::Rack::Middleware::CurrentAccount
- Inherits:
-
Object
- Object
- RightSharding::Rack::Middleware::CurrentAccount
- Defined in:
- lib/right_sharding/current_account.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#config_object ⇒ Object
readonly
Returns the value of attribute config_object.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
-
#call(env) ⇒ Object
TODO Current this replaces the account id set in env[“account”] ultimately, we want this middleware to simply determine the correct account and set it in an env variable such as request.env.
-
#initialize(app, params = {}) ⇒ CurrentAccount
constructor
———————————————————————– Rack Middleware ———————————————————————–.
-
#reset_global_session_account(env) ⇒ Object
Passing a X_ACCOUNT header or account query parameter can override the account provided in the global_session cookie.
Constructor Details
#initialize(app, params = {}) ⇒ CurrentAccount
Rack Middleware
14 15 16 17 18 19 |
# File 'lib/right_sharding/current_account.rb', line 14 def initialize(app, params = {}) @app = app @config_object = Module.const_get(params[:config_object]) @logger = Module.const_get(params[:logger]) end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
8 9 10 |
# File 'lib/right_sharding/current_account.rb', line 8 def app @app end |
#config_object ⇒ Object (readonly)
Returns the value of attribute config_object.
9 10 11 |
# File 'lib/right_sharding/current_account.rb', line 9 def config_object @config_object end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
9 10 11 |
# File 'lib/right_sharding/current_account.rb', line 9 def logger @logger end |
Instance Method Details
#call(env) ⇒ Object
TODO Current this replaces the account id set in env[“account”] ultimately, we want this middleware to simply determine the correct account and set it in an env variable such as request.env. Once we refactor all the apps to use that instead, we can remove the use of global_session
25 26 27 28 |
# File 'lib/right_sharding/current_account.rb', line 25 def call(env) reset_global_session_account(env) return @app.call(env) end |
#reset_global_session_account(env) ⇒ Object
Passing a X_ACCOUNT header or account query parameter can override the account provided in the global_session cookie. The X_ACCOUNT header or account query parameter must be the id of the account, and not the href.
Order of precedence: “X_ACCOUNT” header > “account” query parameter > global_session
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/right_sharding/current_account.rb', line 37 def reset_global_session_account(env) if env['HTTP_X_ACCOUNT'] account_id = env['HTTP_X_ACCOUNT'] from = 'header' elsif env['QUERY_STRING'] query_params = ::Rack::Utils.parse_query(env['QUERY_STRING']) if query_params['account'] account_id = query_params['account'] from = 'query string' end end # Only reset the account_id if it exists, is numeric, and is different from what's # currently in the global session if account_id && account_id =~ /\A\d+\Z/ && account_id.to_i != env["global_session"]["account"] logger.info("RightSharding::CurrentAccount: Resetting global_session account: " + "#{env["global_session"]["account"]} -> #{account_id} (#{from})") env["global_session"]["account"] = account_id.to_i end true end |