Module: Masks::Controller::ClassMethods

Defined in:
app/controllers/concerns/masks/controller.rb

Instance Method Summary collapse

Instance Method Details

#require_access(name, **opts) ⇒ Object

Builds an access object by the passed name, if allowed.

If this succeeds, the controller instance’s current_access will return the constructed access class. Otherwise, on failure the unathorized_access method is called.

Examples:

class MyController < ApplicationController
  include Masks::Controller

  require_access 'my.example', only: :index

  # this action is only called if the session is
  # able to build the "my.example" access class.
  def index
    render json: {
      foobar: current_access.foo_bar
    }
  end
end

Parameters:

  • name (Symbol|String)
  • opts (Hash)

    forwarded to before_action



48
49
50
51
52
53
54
# File 'app/controllers/concerns/masks/controller.rb', line 48

def require_access(name, **opts)
  before_action(
    :unauthorized_access,
    unless: -> { build_access(name) },
    **opts
  )
end

#require_mask(type: nil, **opts) ⇒ Object

Require a session masked by the type passed.

If this fails unathorized_access is called.

Parameters:

  • type (Symbol|String) (defaults to: nil)
  • opts (Hash)

    forwarded to before_action



17
18
19
20
21
22
23
# File 'app/controllers/concerns/masks/controller.rb', line 17

def require_mask(type: nil, **opts)
  before_action(
    :unauthorized_access,
    unless: -> { masked?(type:) },
    **opts
  )
end