Commit dcd002a1 authored by Douwe Maan's avatar Douwe Maan

Add username parameter to gravatar URL

parent bd259d6b
...@@ -794,7 +794,7 @@ class User < ActiveRecord::Base ...@@ -794,7 +794,7 @@ class User < ActiveRecord::Base
def avatar_url(size: nil, scale: 2, **args) def avatar_url(size: nil, scale: 2, **args)
# We use avatar_path instead of overriding avatar_url because of carrierwave. # We use avatar_path instead of overriding avatar_url because of carrierwave.
# See https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/11001/diffs#note_28659864 # See https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/11001/diffs#note_28659864
avatar_path(args) || GravatarService.new.execute(email, size, scale) avatar_path(args) || GravatarService.new.execute(email, size, scale, username: username)
end end
def all_emails def all_emails
......
class GravatarService class GravatarService
include Gitlab::CurrentSettings include Gitlab::CurrentSettings
def execute(email, size = nil, scale = 2) def execute(email, size = nil, scale = 2, username: nil)
if current_application_settings.gravatar_enabled? && email.present? return unless current_application_settings.gravatar_enabled?
size = 40 if size.nil? || size <= 0
sprintf gravatar_url, identifier = email.presence || username.presence
hash: Digest::MD5.hexdigest(email.strip.downcase), return unless identifier
size: size * scale,
email: email.strip hash = Digest::MD5.hexdigest(identifier.strip.downcase)
end size = 40 unless size && size > 0
sprintf gravatar_url,
hash: hash,
size: size * scale,
email: ERB::Util.url_encode(email&.strip || ''),
username: ERB::Util.url_encode(username&.strip || '')
end end
def gitlab_config def gitlab_config
......
---
title: Add username parameter to gravatar URL
merge_request:
author:
...@@ -169,7 +169,7 @@ production: &base ...@@ -169,7 +169,7 @@ production: &base
## Gravatar ## Gravatar
## For Libravatar see: http://doc.gitlab.com/ce/customization/libravatar.html ## For Libravatar see: http://doc.gitlab.com/ce/customization/libravatar.html
gravatar: gravatar:
# gravatar urls: possible placeholders: %{hash} %{size} %{email} # gravatar urls: possible placeholders: %{hash} %{size} %{email} %{username}
# plain_url: "http://..." # default: http://www.gravatar.com/avatar/%{hash}?s=%{size}&d=identicon # plain_url: "http://..." # default: http://www.gravatar.com/avatar/%{hash}?s=%{size}&d=identicon
# ssl_url: "https://..." # default: https://secure.gravatar.com/avatar/%{hash}?s=%{size}&d=identicon # ssl_url: "https://..." # default: https://secure.gravatar.com/avatar/%{hash}?s=%{size}&d=identicon
......
...@@ -16,7 +16,7 @@ the configuration options as follows: ...@@ -16,7 +16,7 @@ the configuration options as follows:
```yml ```yml
gravatar: gravatar:
enabled: true enabled: true
# gravatar URLs: possible placeholders: %{hash} %{size} %{email} # gravatar URLs: possible placeholders: %{hash} %{size} %{email} %{username}
plain_url: "http://cdn.libravatar.org/avatar/%{hash}?s=%{size}&d=identicon" plain_url: "http://cdn.libravatar.org/avatar/%{hash}?s=%{size}&d=identicon"
``` ```
...@@ -25,7 +25,7 @@ the configuration options as follows: ...@@ -25,7 +25,7 @@ the configuration options as follows:
```yml ```yml
gravatar: gravatar:
enabled: true enabled: true
# gravatar URLs: possible placeholders: %{hash} %{size} %{email} # gravatar URLs: possible placeholders: %{hash} %{size} %{email} %{username}
ssl_url: "https://seccdn.libravatar.org/avatar/%{hash}?s=%{size}&d=identicon" ssl_url: "https://seccdn.libravatar.org/avatar/%{hash}?s=%{size}&d=identicon"
``` ```
......
require 'spec_helper'
describe GravatarService, service: true do
describe '#execute' do
let(:url) { 'http://example.com/avatar?hash=%{hash}&size=%{size}&email=%{email}&username=%{username}' }
before do
allow(Gitlab.config.gravatar).to receive(:plain_url).and_return(url)
end
it 'replaces the placeholders' do
avatar_url = described_class.new.execute('user@example.com', 100, 2, username: 'user')
expect(avatar_url).to include("hash=#{Digest::MD5.hexdigest('user@example.com')}")
expect(avatar_url).to include("size=200")
expect(avatar_url).to include("email=user%40example.com")
expect(avatar_url).to include("username=user")
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment