Class: RightSharding::Rack::Middleware::CurrentAccount

Inherits:
Object
  • Object
show all
Defined in:
lib/right_sharding/current_account.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#appObject (readonly)

Returns the value of attribute app.



8
9
10
# File 'lib/right_sharding/current_account.rb', line 8

def app
  @app
end

#config_objectObject (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

#loggerObject (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)
  (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 (env)
  if env['HTTP_X_ACCOUNT']
     = env['HTTP_X_ACCOUNT']
    from = 'header'
  elsif env['QUERY_STRING']
    query_params = ::Rack::Utils.parse_query(env['QUERY_STRING'])
    if query_params['account']
       = 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  &&  =~ /\A\d+\Z/ && .to_i != env["global_session"]["account"]
    logger.info("RightSharding::CurrentAccount: Resetting global_session account: " +
                "#{env["global_session"]["account"]} ->  #{} (#{from})")
    env["global_session"]["account"] = .to_i
  end
  true
end