Module: Authorizme::ActsAsAuthorizme::ClassMethods

Defined in:
lib/authorizme/acts_as_authorizme.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/authorizme/acts_as_authorizme.rb', line 45

def method_missing(meth, *args, &block)
  if meth.to_s =~ /^authenticate_with_(.+)$/
    run_authenticate_with_provider($1, *args, &block)
  else
    super
  end
end

Instance Method Details

#acts_as_authorizmeObject



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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/authorizme/acts_as_authorizme.rb', line 9

def acts_as_authorizme
  # Load bcrypt-ruby only when acts_as_authorizme is called. Need for password digest
  gem 'bcrypt-ruby', '~> 3.0.0'
  require 'bcrypt'
  
  # Relations
  belongs_to :role, :class_name => "Authorizme::UserRole", :foreign_key => "user_role_id"
  belongs_to :origin_provider, :class_name => "Authorizme::UserProvider"
  has_many :providers, :class_name => "Authorizme::UserProvider"
  has_many :synchronize_requests, :class_name => "Authorizme::SynchronizeRequest"
  
  attr_reader :password
  attr_accessor :password_will_be_update
  
  attr_accessible :first_name, :last_name, :image_url, :email, :password, :password_confirmation, :password_will_be_update
  
  # Validations
  validates_confirmation_of :password
  validates_length_of :password, :minimum => 3, :if => :need_check_password?
  validates_presence_of :password_digest, :if => :has_not_provider?
  validates_presence_of :email, :on => :create, :if => :has_not_provider?
  validates_uniqueness_of :email, :if => :has_not_provider?

  # Filters
  before_create :set_default_role

  scope :with_role, joins(:role)

  include InstanceMethodsOnActivation

  if respond_to?(:attributes_protected_by_default)
    def self.attributes_protected_by_default
      super + ['password_digest']
    end
  end
  
  def method_missing(meth, *args, &block)
    if meth.to_s =~ /^authenticate_with_(.+)$/
      run_authenticate_with_provider($1, *args, &block)
    else
      super
    end
  end
  
  protected

    # authorize
    # Finds or creates user provider and creates or updates user with social data
    # => Attributes
    # *provider* provider name, e.g. facebook. Default in gem draugiem, twitter, facebook
    # From args: 
    # *social_id* social network user identity number
    # *attributes* attributes from social nettwork. Can be set: first_name, last_name, image_url 
    # *token* token
    # *secret* secret
    #
    def run_authenticate_with_provider provider, *args, &block          
      social_id = args[0]
      attributes = args[1]
      token = args[2]
      secret = args[3]

      user_provider = Authorizme::UserProvider.find_or_initialize_by_social_id_and_provider_type(social_id.to_s, provider)
      user_provider.token = token
      user_provider.secret = secret if secret
      user_provider.save!
      self.create_or_update_by_provider user_provider, attributes
    end
    
    
    def create_or_update_by_provider provider, attributes
      unless provider.user
        provider.user = User.new
        provider.user.origin_provider = provider
        provider.user.has_provider = true
        provider.user.save!
        provider.origin_user = provider.user
        provider.user
      end
      provider.user.has_provider = true
      provider.user.attributes = attributes
      provider.user.save!
      provider.save!
      provider.user
    end
end

#create_or_update_by_provider(provider, attributes) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/authorizme/acts_as_authorizme.rb', line 79

def create_or_update_by_provider provider, attributes
  unless provider.user
    provider.user = User.new
    provider.user.origin_provider = provider
    provider.user.has_provider = true
    provider.user.save!
    provider.origin_user = provider.user
    provider.user
  end
  provider.user.has_provider = true
  provider.user.attributes = attributes
  provider.user.save!
  provider.save!
  provider.user
end

#run_authenticate_with_provider(provider, *args, &block) ⇒ Object

authorize Finds or creates user provider and creates or updates user with social data

> Attributes

provider provider name, e.g. facebook. Default in gem draugiem, twitter, facebook From args: social_id social network user identity number attributes attributes from social nettwork. Can be set: first_name, last_name, image_url token token secret secret



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/authorizme/acts_as_authorizme.rb', line 65

def run_authenticate_with_provider provider, *args, &block          
  social_id = args[0]
  attributes = args[1]
  token = args[2]
  secret = args[3]

  user_provider = Authorizme::UserProvider.find_or_initialize_by_social_id_and_provider_type(social_id.to_s, provider)
  user_provider.token = token
  user_provider.secret = secret if secret
  user_provider.save!
  self.create_or_update_by_provider user_provider, attributes
end