Sha256: da53b49a47b67a0513bd290779a2db4359342a4bdbb36ad79775aaf81c1bf03a

Contents?: true

Size: 1.98 KB

Versions: 3

Compression:

Stored size: 1.98 KB

Contents

module PandaPal::Helpers::ControllerHelper
  def save_session
    current_session.try(:save)
  end

  def current_session
    @current_session ||= PandaPal::Session.find_by(session_key: session_key) if session_key
    @current_session ||= PandaPal::Session.new
  end

  def current_organization
    @organization ||= PandaPal::Organization.find_by!(key: organization_key) if organization_key
    @organization ||= PandaPal::Organization.find_by(id: params[:organization_id]) if params[:organization_id]
    @organization ||= PandaPal::Organization.find_by_name(Apartment::Tenant.current)
  end

  def current_session_data
    current_session.data
  end

  def session_changed?
    current_session.changed? && current_session.changes[:data].present?
  end

  def validate_launch!
    authorized = false
    if @organization = params['oauth_consumer_key'] && PandaPal::Organization.find_by_key(params['oauth_consumer_key'])
      @tp = IMS::LTI::ToolProvider.new(@organization.key, @organization.secret, params)
      authorized = @tp.valid_request?(request)
    end
    render :text => 'Invalid Credentials, please contact your Administrator.', :status => :unauthorized unless authorized
    authorized
  end

  def switch_tenant(organization = current_organization, &block)
    return unless organization
    raise 'This method should be called in an around_action callback' unless block_given?

    Apartment::Tenant.switch(organization.name) do
      yield
    end
  end

  def forbid_access_if_lacking_session
    render text: 'You should do an LTI Tool Launch.', status: :unauthorized unless current_session.persisted?
  end

  private
  def organization_key
    params[:oauth_consumer_key] || current_session_data[:organization_key] || session[:organization_key]
  end

  def session_key
    params[:session_key] || session_key_header || flash[:session_key] || session[:session_key]
  end

  def session_key_header
    if match = request.headers['Authorization'].try(:match, /token=(.+)/)
      match[1]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
panda_pal-0.0.4 lib/panda_pal/helpers/controller_helper.rb
panda_pal-0.0.3 lib/panda_pal/helpers/controller_helper.rb
panda_pal-0.0.2 lib/panda_pal/helpers/controller_helper.rb