2013-08-23 14:20:43 +08:00
|
|
|
class Auth::FacebookAuthenticator < Auth::Authenticator
|
|
|
|
|
2016-09-28 23:49:22 +08:00
|
|
|
AVATAR_SIZE = 480
|
2016-09-28 12:38:41 +08:00
|
|
|
|
2013-08-23 14:20:43 +08:00
|
|
|
def name
|
|
|
|
"facebook"
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_authenticate(auth_token)
|
|
|
|
|
|
|
|
result = Auth::Result.new
|
|
|
|
|
|
|
|
session_info = parse_auth_token(auth_token)
|
|
|
|
facebook_hash = session_info[:facebook]
|
|
|
|
|
|
|
|
result.email = email = session_info[:email]
|
2014-12-08 18:40:44 +08:00
|
|
|
result.email_valid = !email.blank?
|
2014-02-17 11:45:17 +08:00
|
|
|
result.name = facebook_hash[:name]
|
2013-08-23 14:20:43 +08:00
|
|
|
|
2013-08-26 15:36:20 +08:00
|
|
|
result.extra_data = facebook_hash
|
2013-08-23 14:20:43 +08:00
|
|
|
|
2014-05-06 21:41:59 +08:00
|
|
|
user_info = FacebookUserInfo.find_by(facebook_user_id: facebook_hash[:facebook_user_id])
|
2013-08-26 16:01:01 +08:00
|
|
|
result.user = user_info.try(:user)
|
2013-08-23 14:20:43 +08:00
|
|
|
|
2014-07-14 22:16:24 +08:00
|
|
|
if !result.user && !email.blank? && result.user = User.find_by_email(email)
|
2013-08-26 15:36:20 +08:00
|
|
|
FacebookUserInfo.create({user_id: result.user.id}.merge(facebook_hash))
|
2013-08-23 14:20:43 +08:00
|
|
|
end
|
|
|
|
|
2016-09-19 13:10:02 +08:00
|
|
|
if user_info
|
|
|
|
user_info.update_columns(facebook_hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
user = result.user
|
|
|
|
if user && (!user.user_avatar || user.user_avatar.custom_upload_id.nil?)
|
|
|
|
if (avatar_url = facebook_hash[:avatar_url]).present?
|
2016-09-28 12:38:41 +08:00
|
|
|
avatar_url_with_parameters = add_avatar_parameters(avatar_url)
|
|
|
|
UserAvatar.import_url_for_user(avatar_url_with_parameters, user, override_gravatar: false)
|
2016-09-19 13:10:02 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-19 14:14:11 +08:00
|
|
|
|
|
|
|
bio = facebook_hash[:about_me]
|
|
|
|
location = facebook_hash[:location]
|
|
|
|
website = facebook_hash[:website]
|
|
|
|
|
|
|
|
if user && (bio || location || website)
|
|
|
|
profile = user.user_profile
|
|
|
|
|
|
|
|
profile.bio_raw = bio unless profile.bio_raw.present?
|
|
|
|
profile.location = location unless profile.location.present?
|
|
|
|
profile.website = website unless profile.website.present?
|
|
|
|
profile.save
|
|
|
|
end
|
|
|
|
|
2014-03-20 01:31:17 +08:00
|
|
|
if email.blank?
|
|
|
|
UserHistory.create(
|
|
|
|
action: UserHistory.actions[:facebook_no_email],
|
|
|
|
details: "name: #{facebook_hash[:name]}, facebook_user_id: #{facebook_hash[:facebook_user_id]}"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-08-23 14:20:43 +08:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_create_account(user, auth)
|
|
|
|
data = auth[:extra_data]
|
|
|
|
FacebookUserInfo.create({user_id: user.id}.merge(data))
|
2016-09-19 13:10:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
if (avatar_url = data[:avatar_url]).present?
|
2016-09-28 12:38:41 +08:00
|
|
|
avatar_url_with_parameters = add_avatar_parameters(avatar_url)
|
|
|
|
UserAvatar.import_url_for_user(avatar_url_with_parameters, user)
|
2016-09-19 13:10:02 +08:00
|
|
|
user.save
|
|
|
|
end
|
|
|
|
|
2016-09-19 14:14:11 +08:00
|
|
|
bio = data[:about_me]
|
|
|
|
location = data[:location]
|
|
|
|
website = data[:website]
|
|
|
|
|
|
|
|
if bio || location || website
|
|
|
|
user.user_profile.bio_raw = bio
|
|
|
|
user.user_profile.location = location
|
|
|
|
user.user_profile.website = website
|
|
|
|
user.user_profile.save
|
|
|
|
end
|
|
|
|
|
2016-09-19 13:10:02 +08:00
|
|
|
true
|
2013-08-23 14:20:43 +08:00
|
|
|
end
|
|
|
|
|
2013-08-26 09:04:16 +08:00
|
|
|
def register_middleware(omniauth)
|
2016-09-19 13:10:02 +08:00
|
|
|
|
2013-08-26 09:04:16 +08:00
|
|
|
omniauth.provider :facebook,
|
|
|
|
:setup => lambda { |env|
|
|
|
|
strategy = env["omniauth.strategy"]
|
|
|
|
strategy.options[:client_id] = SiteSetting.facebook_app_id
|
|
|
|
strategy.options[:client_secret] = SiteSetting.facebook_app_secret
|
2016-09-19 14:14:11 +08:00
|
|
|
strategy.options[:info_fields] = 'gender,email,name,bio,first_name,link,last_name,website,location'
|
|
|
|
if SiteSetting.facebook_request_extra_profile_details
|
|
|
|
strategy.options[:scope] = 'email,user_about_me,user_location,user_website'
|
|
|
|
end
|
2013-08-26 09:04:16 +08:00
|
|
|
},
|
|
|
|
:scope => "email"
|
|
|
|
end
|
|
|
|
|
2013-08-23 14:20:43 +08:00
|
|
|
protected
|
|
|
|
|
|
|
|
def parse_auth_token(auth_token)
|
|
|
|
|
|
|
|
raw_info = auth_token["extra"]["raw_info"]
|
2016-09-19 13:10:02 +08:00
|
|
|
info = auth_token["info"]
|
|
|
|
|
2013-08-23 14:20:43 +08:00
|
|
|
email = auth_token["info"][:email]
|
|
|
|
|
2016-09-19 14:14:11 +08:00
|
|
|
website = (info["urls"] && info["urls"]["Website"]) || nil
|
|
|
|
|
2013-08-23 14:20:43 +08:00
|
|
|
{
|
|
|
|
facebook: {
|
|
|
|
facebook_user_id: auth_token["uid"],
|
|
|
|
link: raw_info["link"],
|
|
|
|
username: raw_info["username"],
|
|
|
|
first_name: raw_info["first_name"],
|
|
|
|
last_name: raw_info["last_name"],
|
|
|
|
email: email,
|
|
|
|
gender: raw_info["gender"],
|
2016-09-19 13:10:02 +08:00
|
|
|
name: raw_info["name"],
|
2016-09-19 14:14:11 +08:00
|
|
|
avatar_url: info["image"],
|
|
|
|
location: info["location"],
|
|
|
|
website: website,
|
|
|
|
about_me: info["description"]
|
2013-08-23 14:20:43 +08:00
|
|
|
},
|
|
|
|
email: email,
|
|
|
|
email_valid: true
|
|
|
|
}
|
|
|
|
|
|
|
|
end
|
2013-08-26 09:04:16 +08:00
|
|
|
|
2016-09-28 12:38:41 +08:00
|
|
|
def add_avatar_parameters(avatar_url)
|
2016-09-28 23:49:22 +08:00
|
|
|
"#{avatar_url}?height=#{AVATAR_SIZE}&width=#{AVATAR_SIZE}"
|
2016-09-28 12:38:41 +08:00
|
|
|
end
|
2013-08-26 09:04:16 +08:00
|
|
|
|
2013-08-23 14:20:43 +08:00
|
|
|
end
|