Module: Sorcery::Model::Submodules::External::ClassMethods

Defined in:
lib/sorcery/model/submodules/external.rb

Instance Method Summary collapse

Instance Method Details

#create_and_validate_from_provider(provider, uid, attrs) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/sorcery/model/submodules/external.rb', line 48

def create_and_validate_from_provider(provider, uid, attrs)
  user = new(attrs)
  user.send(sorcery_config.authentications_class.to_s.downcase.pluralize).build(
    sorcery_config.provider_uid_attribute_name => uid,
    sorcery_config.provider_attribute_name => provider
  )
  saved = user.sorcery_adapter.save
  [user, saved]
end

#create_from_provider(provider, uid, attrs) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/sorcery/model/submodules/external.rb', line 58

def create_from_provider(provider, uid, attrs)
  user = new
  attrs.each do |k,v|
    user.send(:"#{k}=", v)
  end

  if block_given?
    return false unless yield user
  end

  sorcery_adapter.transaction do
    user.sorcery_adapter.save(:validate => false)
    sorcery_config.authentications_class.create!(
      sorcery_config.authentications_user_id_attribute_name => user.id,
      sorcery_config.provider_attribute_name => provider,
      sorcery_config.provider_uid_attribute_name => uid
    )
  end
  user
end

#load_from_provider(provider, uid) ⇒ Object

takes a provider and uid and finds a user by them.



42
43
44
45
46
# File 'lib/sorcery/model/submodules/external.rb', line 42

def load_from_provider(provider,uid)
  config = sorcery_config
  authentication = config.authentications_class.sorcery_adapter.find_by_oauth_credentials(provider, uid)
  user = sorcery_adapter.find_by_id(authentication.send(config.authentications_user_id_attribute_name)) if authentication
end