OSDN Git Service

Allow disabling Gravatars in gitlab.yml settings
authorRobert Speicher <rspeicher@gmail.com>
Thu, 16 Aug 2012 01:06:08 +0000 (21:06 -0400)
committerRobert Speicher <rspeicher@gmail.com>
Thu, 16 Aug 2012 01:06:08 +0000 (21:06 -0400)
Closes #1237

app/helpers/application_helper.rb
config/gitlab.yml.example
config/initializers/1_settings.rb
spec/helpers/application_helper_spec.rb [new file with mode: 0644]

index 7a9f0e9..2d7e4fd 100644 (file)
@@ -2,10 +2,13 @@ require 'digest/md5'
 module ApplicationHelper
 
   def gravatar_icon(user_email = '', size = 40)
-    return unless user_email
-    gravatar_host = request.ssl? ? "https://secure.gravatar.com" :  "http://www.gravatar.com"
-    user_email.strip!
-    "#{gravatar_host}/avatar/#{Digest::MD5.hexdigest(user_email.downcase)}?s=#{size}&d=identicon"
+    if Gitlab.config.disable_gravatar? || user_email.blank?
+      'no_avatar.png'
+    else
+      gravatar_prefix = request.ssl? ? "https://secure" : "http://www"
+      user_email.strip!
+      "#{gravatar_prefix}.gravatar.com/avatar/#{Digest::MD5.hexdigest(user_email.downcase)}?s=#{size}&d=identicon"
+    end
   end
 
   def request_protocol
index 1818f2c..be36ee6 100644 (file)
@@ -1,4 +1,4 @@
-# # # # # # # # # # # # # # # # # # 
+# # # # # # # # # # # # # # # # # #
 # Gitlab application config file  #
 # # # # # # # # # # # # # # # # # #
 
@@ -19,14 +19,14 @@ email:
 
 # Application specific settings
 # Like default project limit for user etc
-app: 
-  default_projects_limit: 10 
+app:
+  default_projects_limit: 10
   # backup_path: "/vol/backups"   # default: Rails.root + backups/
   # backup_keep_time: 604800      # default: 0 (forever) (in seconds)
+  # disable_gravatar: true        # default: false - Disable user avatars from Gravatar.com
 
-
-# 
-# 2. Advanced settings: 
+#
+# 2. Advanced settings:
 # ==========================
 
 # Git Hosting configuration
@@ -39,7 +39,6 @@ git_host:
   receive_pack: true
   # port: 22
 
-
 # Git settings
 # Use default values unless you understand it
 git:
index 5c5987a..8165d6c 100644 (file)
@@ -111,5 +111,9 @@ class Settings < Settingslogic
     def backup_keep_time
       app['backup_keep_time'] || 0
     end
+
+    def disable_gravatar?
+      app['disable_gravatar'] || false
+    end
   end
 end
diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb
new file mode 100644 (file)
index 0000000..9a2df31
--- /dev/null
@@ -0,0 +1,26 @@
+require 'spec_helper'
+
+describe ApplicationHelper do
+  describe "gravatar_icon" do
+    let(:user_email) { 'user@email.com' }
+
+    it "should return a generic avatar path when Gravatar is disabled" do
+      Gitlab.config.stub(:disable_gravatar?).and_return(true)
+      gravatar_icon(user_email).should == 'no_avatar.png'
+    end
+
+    it "should return a generic avatar path when email is blank" do
+      gravatar_icon('').should == 'no_avatar.png'
+    end
+
+    it "should use SSL when appropriate" do
+      stub!(:request).and_return(double(:ssl? => true))
+      gravatar_icon(user_email).should match('https://secure.gravatar.com')
+    end
+
+    it "should accept a custom size" do
+      stub!(:request).and_return(double(:ssl? => false))
+      gravatar_icon(user_email, 64).should match(/\?s=64/)
+    end
+  end
+end