Module: Oauned::ControllerMethods

Defined in:
lib/oauned/controller_methods.rb

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/oauned/controller_methods.rb', line 3

def self.included(klass)
  klass.class_eval do
    cattr_accessor :oauth_options, :oauth_options_proc
      
    protected
    def self.deny_oauth(options = {}, &block)
      raise 'options cannot contain both :only and :except' if options[:only] && options[:except]
    
      [:only, :except].each do |k|
        if values = options[k]
          options[k] = Array(values).map(&:to_s).to_set
        end
      end
      self.oauth_options = options
      self.oauth_options_proc = block
    end
      
    def oauth_user
      @oauth_user ||= oauth_allowed? ? user_from_oauth : nil
    end          
      
    alias :normal_user :current_user
    def current_user
      normal_user || oauth_user
    end
    
    private
    def user_from_oauth
      token = Connection.where(['access_token LIKE ?', params[:access_token]]).first
      token.user if (token && !token.expired?)
    end
    
    def oauth_allowed?
      return true if (oauth_options_proc && !oauth_options_proc.call(self)) || oauth_options.nil?
      return false if oauth_options.empty?
      return true if oauth_options[:only] && !oauth_options[:only].include?(action_name)
      return true if oauth_options[:except] && oauth_options[:except].include?(action_name)
      false
    end
  end
end