user.rb
1.93 KB
1
2
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require_dependency 'user'
class User
has_many :oauth_auths, through: :person
has_many :oauth_providers, through: :oauth_auths, source: :provider
after_create :activate_oauth_user
after_create :store_oauth_providers
def initialize_with_oauth_client(attributes = {}, options = {})
attributes ||= {}
@oauth_providers = attributes.delete(:oauth_providers) || []
initialize_without_oauth_client(attributes, options)
end
alias_method_chain :initialize, :oauth_client
def store_oauth_providers
@oauth_providers.each do |provider|
self.person.oauth_auths.create!(profile: self.person, provider: provider, enabled: true, oauth_data: oauth_data)
end
end
def activate_oauth_user
self.activate if oauth_providers.present? || @oauth_providers.present?
end
def password_required_with_oauth?
# user creation through api does not set oauth_providers
check_providers
password_required_without_oauth? && oauth_providers.empty? && @oauth_providers.blank?
end
def oauth_data
@oauth_data
end
def oauth_signup_token= value
@oauth_signup_token = value
end
def oauth_signup_token
@oauth_signup_token
end
# user creation through api does not set oauth_providers
# so it is being shared through a distributed cache
def check_providers
if oauth_providers.empty? && oauth_signup_token.present?
#check if is oauth user, reading oauth_data recorded at cache store
@oauth_data = OauthClientPlugin::SignupDataStore.get_oauth_data(self.email, self.oauth_signup_token)
provider_id = (@oauth_data || {}).delete(:provider_id)
@oauth_providers = [OauthClientPlugin::Provider.find(provider_id)] if provider_id.present?
end
end
alias_method_chain :password_required?, :oauth
def make_activation_code_with_oauth
@oauth_providers.blank? && oauth_providers.blank? ? make_activation_code_without_oauth : nil
end
alias_method_chain :make_activation_code, :oauth
end