Sha256: 49252d5f334379a9da26080df6d3743eeb79d52a8fbb9b76c6784cb30a93f062

Contents?: true

Size: 944 Bytes

Versions: 2

Compression:

Stored size: 944 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
      (test? ? Apimaster::Models::UserMock : Apimaster::Models::User).auth @access_token
    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

2 entries across 2 versions & 1 rubygems

Version Path
apimaster-0.0.3 lib/apimaster/helpers/session.rb
apimaster-0.0.2 lib/apimaster/helpers/session.rb