Sha256: 6ad8a527f73c641e4c6e61b88aaa8853455b47e008c75c87fea1b35aa9bb329b

Contents?: true

Size: 965 Bytes

Versions: 8

Compression:

Stored size: 965 Bytes

Contents

# encoding: utf-8
#
# Copyright (C) 2011-2012  AdMaster, Inc.
#
# @author: sunxiqiu@admaster.com.cn

module Apimaster::Helpers
  module Session

    # Check logged in user is the owner
    def is_owner? owner_id
      !!current_user && current_user.id.to_i == owner_id.to_i
    end

    def authorize
      raise Apimaster::UnauthorizedError.new :user unless current_user
    end

    # Return current_user record if logged in
    def current_user
      @current_user ||= auth_user
    end

    def auth_user
      @access_token ||= params[:access_token] or header_token
      user_model.auth @access_token
    end

    def user_model
      @user_model ||= Apimaster::Models::User
    end

    def header_token
      keys = %w{HTTP_AUTHORIZATION X-HTTP_AUTHORIZATION X_HTTP_AUTHORIZATION}
      authorization ||= keys.inject(nil) { |auth, key| auth || request.env[key] }
      authorization.split[1] if authorization and authorization[/^token/i]
    end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
apimaster-0.1.4 lib/apimaster/helpers/session.rb
apimaster-0.1.3 lib/apimaster/helpers/session.rb
apimaster-0.1.2 lib/apimaster/helpers/session.rb
apimaster-0.1.1 lib/apimaster/helpers/session.rb
apimaster-0.0.7 lib/apimaster/helpers/session.rb
apimaster-0.0.6 lib/apimaster/helpers/session.rb
apimaster-0.0.5 lib/apimaster/helpers/session.rb
apimaster-0.0.4 lib/apimaster/helpers/session.rb