Sha256: 5e37299413b1d1c9b74280df61f932e43ee8bab37881317233c645cd56e3761b

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

module CurrentUserHelpers
  def login_required
    if !current_user
      return respond_to do |format|
        format.html { redirect_to login_path }
        format.json { render :json => { 'error' => 'Access Denied' }.to_json }
      end
    end
  end
  
  def login_path
    "#{SsoClyent.path}/auth/sso"
  end
  
  def logout_path
    "#{SsoClyent.path}/logout"
  end
  
  def signup_path
    SsoClyent.signup_url
  end

  def edit_account_path
    SsoClyent.edit_account_url
  end

  def current_user
    return nil unless session[:user_id]
    users = user_klass
    uid = userid
    if users.respond_to?(:"find_by_#{uid}")
      @current_user ||= users.send(:"find_by_#{uid}", session[:user_id]['uid'])
    end
  end
  
  def user_signed_in?
    !!current_user
  end
  
  def check_for_login_error api_response
    current_user_logout if !api_response.is_a? Array and api_response.try(:[],:error).try(:==,'401 Unauthorized')
  end

  def current_user_logout
    session[:user_id] = nil
  end

  def user_klass
    SsoClyent.user_class
  end

  def userid
    SsoClyent.unique_id
  end
  
  def check_if_from_sign_up
    if from_sign_up = params.delete(:from_sign_up)
      login_required
      begin process_params_from_sign_up(from_sign_up)
      rescue
      end
      redirect_to url_for(params)
    end
  end
end
ActionController::Base.send :include, CurrentUserHelpers
ActionController::Base.send :before_filter, :check_if_from_sign_up

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sso_clyent-0.0.7.27 lib/sso_clyent/controllers/current_user_helpers.rb