class Hello::RailsController::RestrictByRole

Public Class Methods

new(controller) click to toggle source
# File lib/hello/rails_controller/restrict_by_role.rb, line 5
def initialize(controller)
  @controller = controller
end

Public Instance Methods

dont_kick(*roles) click to toggle source
# File lib/hello/rails_controller/restrict_by_role.rb, line 13
def dont_kick(*roles)
  to_home_page if not current_user.in_any_role?(roles)
end
kick(*roles) click to toggle source
# File lib/hello/rails_controller/restrict_by_role.rb, line 9
def kick(*roles)
  to_home_page if current_user.in_any_role?(roles)
end

Private Instance Methods

current_user() click to toggle source
# File lib/hello/rails_controller/restrict_by_role.rb, line 19
def current_user
  @controller.current_user || ::User.new(role: 'guest')
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method Hello::RailsHelper#method_missing
# File lib/hello/rails_controller/restrict_by_role.rb, line 69
def method_missing(method, *args, &block)
  if @controller.respond_to?(method)
    @controller.send(method, *args, &block)
  else
    super
  end
end
to_home_page() click to toggle source
# File lib/hello/rails_controller/restrict_by_role.rb, line 23
def to_home_page
  if current_user.role_is? 'guest'
    to_sign_in
  elsif current_user.role_is? 'onboarding'
    to_onboarding
  else
    to_root
  end
end
to_onboarding() click to toggle source
# File lib/hello/rails_controller/restrict_by_role.rb, line 58
def to_onboarding
  respond_to do |format|
    format.html { redirect_to '/onboarding' }
    format.json do
      data   = { 'message' => 'Access Denied, visit /onboarding and complete your registration.' }
      status = :forbidden # 403
      render json: data, status: status
    end
  end
end
to_root() click to toggle source
# File lib/hello/rails_controller/restrict_by_role.rb, line 33
def to_root
  respond_to do |format|
    format.html { redirect_to '/' }
    format.json do
      data   = { 'message' => 'Access Denied.' }
      status = :forbidden # 403
      render json: data, status: status
    end
  end
end
to_sign_in() click to toggle source
# File lib/hello/rails_controller/restrict_by_role.rb, line 44
def to_sign_in
  respond_to do |format|
    format.html do
      hello_store_url_on_session!
      redirect_to hello.sign_in_path
    end
    format.json do
      data   = { 'message' => 'An active access token must be used to query information about the current user.' }
      status = :unauthorized # 401
      render json: data, status: status
    end
  end
end