FEATURE: optionally get extra profile info from facebook

This feature requires the application be approved by facebook, so it is
default off
This commit is contained in:
Sam 2016-09-19 16:14:11 +10:00
parent 5b3cd3fac9
commit 8dc4329094
6 changed files with 54 additions and 4 deletions

View file

@ -86,7 +86,6 @@ class UserAvatar < ActiveRecord::Base
rescue => e rescue => e
p e
# skip saving, we are not connected to the net # skip saving, we are not connected to the net
Rails.logger.warn "#{e}: Failed to download external avatar: #{avatar_url}, user id #{ user.id }" Rails.logger.warn "#{e}: Failed to download external avatar: #{avatar_url}, user id #{ user.id }"
ensure ensure

View file

@ -1019,6 +1019,7 @@ en:
enable_facebook_logins: "Enable Facebook authentication, requires facebook_app_id and facebook_app_secret" enable_facebook_logins: "Enable Facebook authentication, requires facebook_app_id and facebook_app_secret"
facebook_app_id: "App id for Facebook authentication, registered at https://developers.facebook.com/apps" facebook_app_id: "App id for Facebook authentication, registered at https://developers.facebook.com/apps"
facebook_app_secret: "App secret for Facebook authentication, registered at https://developers.facebook.com/apps" facebook_app_secret: "App secret for Facebook authentication, registered at https://developers.facebook.com/apps"
facebook_request_extra_profile_details: "Request about me, location and website from facebook. (requires that your auth application be approved by facebook)"
enable_github_logins: "Enable Github authentication, requires github_client_id and github_client_secret" enable_github_logins: "Enable Github authentication, requires github_client_id and github_client_secret"
github_client_id: "Client id for Github authentication, registered at https://github.com/settings/applications" github_client_id: "Client id for Github authentication, registered at https://github.com/settings/applications"

View file

@ -277,6 +277,8 @@ login:
facebook_app_secret: facebook_app_secret:
default: '' default: ''
regex: "^[a-f0-9]+$" regex: "^[a-f0-9]+$"
facebook_request_extra_profile_details:
default: false
enable_github_logins: enable_github_logins:
client: true client: true
default: false default: false

View file

@ -0,0 +1,7 @@
class AddFieldsToFacebookUserInfo < ActiveRecord::Migration
def change
add_column :facebook_user_infos, :about_me, :text
add_column :facebook_user_infos, :location, :string
add_column :facebook_user_infos, :website, :text
end
end

View file

@ -35,6 +35,20 @@ class Auth::FacebookAuthenticator < Auth::Authenticator
end end
end end
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
if email.blank? if email.blank?
UserHistory.create( UserHistory.create(
action: UserHistory.actions[:facebook_no_email], action: UserHistory.actions[:facebook_no_email],
@ -55,6 +69,17 @@ class Auth::FacebookAuthenticator < Auth::Authenticator
user.save user.save
end end
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
true true
end end
@ -65,7 +90,10 @@ class Auth::FacebookAuthenticator < Auth::Authenticator
strategy = env["omniauth.strategy"] strategy = env["omniauth.strategy"]
strategy.options[:client_id] = SiteSetting.facebook_app_id strategy.options[:client_id] = SiteSetting.facebook_app_id
strategy.options[:client_secret] = SiteSetting.facebook_app_secret strategy.options[:client_secret] = SiteSetting.facebook_app_secret
strategy.options[:info_fields] = 'gender,email,name,bio,first_name,link,last_name' 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
}, },
:scope => "email" :scope => "email"
end end
@ -79,6 +107,8 @@ class Auth::FacebookAuthenticator < Auth::Authenticator
email = auth_token["info"][:email] email = auth_token["info"][:email]
website = (info["urls"] && info["urls"]["Website"]) || nil
{ {
facebook: { facebook: {
facebook_user_id: auth_token["uid"], facebook_user_id: auth_token["uid"],
@ -89,7 +119,10 @@ class Auth::FacebookAuthenticator < Auth::Authenticator
email: email, email: email,
gender: raw_info["gender"], gender: raw_info["gender"],
name: raw_info["name"], name: raw_info["name"],
avatar_url: info["image"] avatar_url: info["image"],
location: info["location"],
website: website,
about_me: info["description"]
}, },
email: email, email: email,
email_valid: true email_valid: true

View file

@ -20,7 +20,12 @@ describe Auth::FacebookAuthenticator do
} }
}, },
"info" => { "info" => {
:email => user.email :email => user.email,
"location" => "America",
"description" => "bio",
"urls" => {
"Website" => "https://awesome.com"
}
}, },
"uid" => "100" "uid" => "100"
} }
@ -28,6 +33,9 @@ describe Auth::FacebookAuthenticator do
result = authenticator.after_authenticate(hash) result = authenticator.after_authenticate(hash)
expect(result.user.id).to eq(user.id) expect(result.user.id).to eq(user.id)
expect(result.user.user_profile.website).to eq("https://awesome.com")
expect(result.user.user_profile.bio_raw).to eq("bio")
expect(result.user.user_profile.location).to eq("America")
end end
it 'can create a proper result for non existing users' do it 'can create a proper result for non existing users' do