Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement SSO overriding avatars. #2670

Merged
merged 1 commit into from
Aug 22, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default ObjectController.extend({

allowAvatarUpload: Discourse.computed.setting('allow_uploaded_avatars'),
allowUserLocale: Discourse.computed.setting('allow_user_locale'),
ssoOverridesAvatar: Discourse.computed.setting('sso_overrides_avatar'),

selectedCategories: function(){
return [].concat(this.get("watchedCategories"), this.get("trackedCategories"), this.get("mutedCategories"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@
{{bound-avatar model "large"}}
{{#if allowAvatarUpload}}
<button {{action showAvatarSelector}} class="btn pad-left no-text"><i class="fa fa-pencil"></i></button>
{{else}}
{{else}} {{#unless ssoOverridesAvatar}}
<a href="//gravatar.com/emails" target="_blank" title="{{i18n user.change_avatar.gravatar_title}}" class="btn no-text"><i class="fa fa-pencil"></i></a>
{{/if}}
{{/unless}} {{/if}}
</div>
</div>

Expand Down
23 changes: 23 additions & 0 deletions app/models/discourse_single_sign_on.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,32 @@ def change_external_attributes_and_override(sso_record, user)
user.name = User.suggest_name(name || username || email)
end

if SiteSetting.sso_overrides_avatar && (
avatar_force_update == "true" ||
avatar_force_update.to_i != 0 ||
sso_record.external_avatar_url != avatar_url)
begin
tempfile = FileHelper.download(avatar_url, 1.megabyte, "sso-avatar")

upload = Upload.create_for(user.id, tempfile, "external-avatar", File.size(tempfile.path), { origin: avatar_url })

user.uploaded_avatar_id = upload.id

if !user.user_avatar.contains_upload?(upload.id)
user.user_avatar.custom_upload_id = upload.id
end
rescue SocketError
# skip saving, we are not connected to the net
Rails.logger.warn "Failed to download external avatar: #{avatar_url}, socket error - user id #{ user.id }"
ensure
tempfile.close! if tempfile && tempfile.respond_to?(:close!)
end
end

# change external attributes for sso record
sso_record.external_username = username
sso_record.external_email = email
sso_record.external_name = name
sso_record.external_avatar_url = avatar_url
end
end
19 changes: 10 additions & 9 deletions app/models/single_sign_on_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ class SingleSignOnRecord < ActiveRecord::Base
#
# Table name: single_sign_on_records
#
# id :integer not null, primary key
# user_id :integer not null
# external_id :string(255) not null
# last_payload :text not null
# created_at :datetime
# updated_at :datetime
# external_username :string(255)
# external_email :string(255)
# external_name :string(255)
# id :integer not null, primary key
# user_id :integer not null
# external_id :string(255) not null
# last_payload :text not null
# created_at :datetime
# updated_at :datetime
# external_username :string(255)
# external_email :string(255)
# external_name :string(255)
# external_avatar_url :string(255)
#
# Indexes
#
Expand Down
1 change: 1 addition & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@ en:
sso_overrides_email: "Overrides local email with external site email from SSO payload (WARNING: discrepancies can occur due to normalization of local emails)"
sso_overrides_username: "Overrides local username with external site username from SSO payload (WARNING: discrepancies can occur due to differences in username length/requirements)"
sso_overrides_name: "Overrides local name with external site name from SSO payload (WARNING: discrepancies can occur due to normalization of local names)"
sso_overrides_avatar: "Overrides user avatar with external site avatar from SSO payload. If enabled, disabling allow_uploaded_avatars is highly recommended"

enable_local_logins: "Enable local username and password login based accounts. (Note: this must be enabled for invites to work)"
allow_new_registrations: "Allow new user registrations. Uncheck this to prevent anyone from creating a new account."
Expand Down
3 changes: 3 additions & 0 deletions config/site_settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ login:
sso_overrides_email: false
sso_overrides_username: false
sso_overrides_name: false
sso_overrides_avatar:
default: false
client: true


users:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddExternalAvatarUrlToSingleSignOnRecord < ActiveRecord::Migration
def change
add_column :single_sign_on_records, :external_avatar_url, :string
end
end
2 changes: 1 addition & 1 deletion lib/single_sign_on.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class SingleSignOn
ACCESSORS = [:nonce, :name, :username, :email,
ACCESSORS = [:nonce, :name, :username, :email, :avatar_url, :avatar_force_update,
:about_me, :external_id]
FIXNUMS = []
NONCE_EXPIRY_TIME = 10.minutes
Expand Down