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

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

Instance Method Summary collapse

Instance Method Details

#build_from_provider(attrs) ⇒ Object

NOTE: Should this build the authentication as well and return [user, auth]? Currently, users call this function for the user and call add_provider_to_user after saving



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sorcery/model/submodules/external.rb', line 80

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

  if block_given?
    return false unless yield user
  end

  user
end

#create_and_validate_from_provider(provider, uid, attrs) ⇒ Object



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

def create_and_validate_from_provider(provider, uid, attrs)
  user = new(attrs)
  user.send(sorcery_config.authentications_class.name.demodulize.underscore.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



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

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
45
# 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)
  # Return user if matching authentication found
  sorcery_adapter.find_by_id(authentication.send(config.authentications_user_id_attribute_name)) if authentication
end