diff --git a/app/models/user_avatar.rb b/app/models/user_avatar.rb index 239f8a7dbd8..5773b0ffd15 100644 --- a/app/models/user_avatar.rb +++ b/app/models/user_avatar.rb @@ -86,7 +86,6 @@ class UserAvatar < ActiveRecord::Base rescue => e - p e # skip saving, we are not connected to the net Rails.logger.warn "#{e}: Failed to download external avatar: #{avatar_url}, user id #{ user.id }" ensure diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 09554bb945a..2e533645b75 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1019,6 +1019,7 @@ en: 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_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" github_client_id: "Client id for Github authentication, registered at https://github.com/settings/applications" diff --git a/config/site_settings.yml b/config/site_settings.yml index 457f94fe514..bf3289b2dfe 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -277,6 +277,8 @@ login: facebook_app_secret: default: '' regex: "^[a-f0-9]+$" + facebook_request_extra_profile_details: + default: false enable_github_logins: client: true default: false diff --git a/db/migrate/20160919054014_add_fields_to_facebook_user_info.rb b/db/migrate/20160919054014_add_fields_to_facebook_user_info.rb new file mode 100644 index 00000000000..8e50f8fb414 --- /dev/null +++ b/db/migrate/20160919054014_add_fields_to_facebook_user_info.rb @@ -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 diff --git a/lib/auth/facebook_authenticator.rb b/lib/auth/facebook_authenticator.rb index fb59d0a64bd..127ec938311 100644 --- a/lib/auth/facebook_authenticator.rb +++ b/lib/auth/facebook_authenticator.rb @@ -35,6 +35,20 @@ class Auth::FacebookAuthenticator < Auth::Authenticator 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? UserHistory.create( action: UserHistory.actions[:facebook_no_email], @@ -55,6 +69,17 @@ class Auth::FacebookAuthenticator < Auth::Authenticator user.save 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 end @@ -65,7 +90,10 @@ class Auth::FacebookAuthenticator < Auth::Authenticator strategy = env["omniauth.strategy"] strategy.options[:client_id] = SiteSetting.facebook_app_id 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" end @@ -79,6 +107,8 @@ class Auth::FacebookAuthenticator < Auth::Authenticator email = auth_token["info"][:email] + website = (info["urls"] && info["urls"]["Website"]) || nil + { facebook: { facebook_user_id: auth_token["uid"], @@ -89,7 +119,10 @@ class Auth::FacebookAuthenticator < Auth::Authenticator email: email, gender: raw_info["gender"], name: raw_info["name"], - avatar_url: info["image"] + avatar_url: info["image"], + location: info["location"], + website: website, + about_me: info["description"] }, email: email, email_valid: true diff --git a/spec/components/auth/facebook_authenticator_spec.rb b/spec/components/auth/facebook_authenticator_spec.rb index 5091ac56630..c5a91249b23 100644 --- a/spec/components/auth/facebook_authenticator_spec.rb +++ b/spec/components/auth/facebook_authenticator_spec.rb @@ -20,7 +20,12 @@ describe Auth::FacebookAuthenticator do } }, "info" => { - :email => user.email + :email => user.email, + "location" => "America", + "description" => "bio", + "urls" => { + "Website" => "https://awesome.com" + } }, "uid" => "100" } @@ -28,6 +33,9 @@ describe Auth::FacebookAuthenticator do result = authenticator.after_authenticate(hash) 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 it 'can create a proper result for non existing users' do