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
|
# File 'lib/devise_yauth_token/strategy.rb', line 15
def authenticate!
@yauth_provider_error = nil
if token = params[:yauth_token]
result = RestClient.get("http://#{current_account.yauth_host}/api/auth/user.json?auth_host=#{request.host}¬rack=true&auth_token=#{token}"){|response, request, result| result }
result = JSON.parse(result.body) rescue {}
yauth_user_id = result['data']['user']['id'] if result['status'] == 200
if !yauth_user_id.blank? && !current_account.blank?
resource = mapping.to.find_for_yauth_authentication(yauth_user_id, current_account.id)
if resource.blank?
result['data']['user']['yauth_user_id'] = yauth_user_id
resource = mapping.to.new()
end
resource.email = result['data']['user']['email']
resource.omniauth_name = result['data']['user']['omniauth_name']
resource.yauth_user_id = result['data']['user']['id']
resource.yauth_account_id = result['data']['user']['account_id']
resource.admin = result['data']['user']['admin']
resource.need_sign_out = false
resource.account_id = current_account.id
resource.save(:validate => false)
current_account.code = result['data']['account']['code']
current_account.yauth_account_id = result['data']['account']['id']
current_account.name = result['data']['account']['name']
current_account.save(:validate => false)
Rails.logger.info "------------------#{result['data']}---------------------"
Rails.cache.write("yauth_user_#{result['data']['user']['id']}", result['data']['user'])
Rails.cache.write("yauth_account_#{result['data']['account']['id']}", result['data']['account'])
Rails.cache.write("user_permissions_#{resource.id}", result['data']['permissions'])
success!(resource)
else
@yauth_provider_error = I18n.t("devise.yauth_token.erorrs.token_invalid")
fail(:invalid_token)
end
else
@yauth_provider_error = I18n.t("devise.yauth_token.erorrs.token_blank")
fail(:invalid_token_blank)
end
end
|