Class: Masks::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/masks/middleware.rb

Overview

Integrate masks with Rails, via middleware.

This middleware creates a new session for every request using Masks.request. If the session is passed? the middleware chain is allowed to continue.

The middleware uses the fail value on the session’s mask to determine what to do otherwise. If false the chain is allowed to continue regardless of pass/fail status. It will also accept a URI or path for redirects, an HTTP status code, or a controller action in the form of ControllerClass#action_name.

The session is stored in the rack environment under the name Mask::Middleware::SESSION_KEY, which is how it is made available to controllers in Masks::Controller#masked_session.

Constant Summary collapse

SESSION_KEY =
"masks.session"

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



26
27
28
# File 'lib/masks/middleware.rb', line 26

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/masks/middleware.rb', line 30

def call(env)
  request = ActionDispatch::Request.new(env)
  session = Masks.request(request)

  ::Rails.logger.info(
    [
      "Mask #{session.passed? ? "allowed" : "blocked"}",
      "by type=#{session.mask.type}",
      session.mask.name ? "name=#{session.mask.name}" : ""
    ].join(" ")
  )

  env[SESSION_KEY] = session
  app = @app
  app = (session.passed? ? app : error_app(session, app))

  app.call(env)
end