Sha256: 921543a27b9125490b73327e9a23b350836a6c75c28e7eaf691284bdfd24133d

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

class SwitchUserController < ApplicationController
  before_action :developer_modes_only, :switch_back

  def set_current_user
    handle_request(params)

    redirect_path = SwitchUser.redirect_path.call(request, params)
    if Rails.version.to_i >= 5 && redirect_path == :back
      redirect_back(fallback_location: root_path)
    else
      redirect_to(redirect_path)
    end
  end

  def remember_user
    redirect_path = SwitchUser.redirect_path.call(request, params)
    if Rails.version.to_i >= 5 && redirect_path == :back
      redirect_back(fallback_location: root_path)
    else
      redirect_to(redirect_path)
    end
  end

  private

  def switch_back
    if SwitchUser.switch_back
      provider.remember_current_user(true) if params[:remember] == "true"
      provider.remember_current_user(false) if params[:remember] == "false"
    end
  end

  def developer_modes_only
    raise ActionController::RoutingError.new('Do not try to hack us.') unless available?
  end

  def available?
    SwitchUser.guard_class.new(self, provider).controller_available?
  end

  def handle_request(params)
    if params[:scope_identifier].blank?
      provider.logout_all
    else
      record = SwitchUser.data_sources.find_scope_id(params[:scope_identifier])
      unless record
        provider.logout_all
        return
      end
      if SwitchUser.login_exclusive
        provider.login_exclusive(record.user, scope: record.scope)
      else
        provider.login_inclusive(record.user, scope: record.scope)
      end
    end
  end

  # TODO make helper methods, so this can be eliminated from the
  # SwitchUserHelper
  def provider
    SwitchUser::Provider.init(self)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
switch_user-1.5.0 app/controllers/switch_user_controller.rb