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



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

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



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

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.



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

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