From 663adde90e1a3b1d23bf698271fd6ba77c7281a1 Mon Sep 17 00:00:00 2001
From: Neil Lalonde <neillalonde@gmail.com>
Date: Fri, 23 Aug 2013 11:23:00 -0400
Subject: [PATCH] Users can change their own username at any time if they have
 no posts

---
 lib/guardian.rb                  |  2 +-
 spec/components/guardian_spec.rb | 15 ++++++++++++---
 2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/lib/guardian.rb b/lib/guardian.rb
index d03cd89fb..cea13bc93 100644
--- a/lib/guardian.rb
+++ b/lib/guardian.rb
@@ -273,7 +273,7 @@ class Guardian
   end
 
   def can_edit_username?(user)
-    is_staff? || (is_me?(user) && user.created_at > SiteSetting.username_change_period.days.ago)
+    is_staff? || (is_me?(user) && (user.post_count == 0 || user.created_at > SiteSetting.username_change_period.days.ago))
   end
 
   # Deleting Methods
diff --git a/spec/components/guardian_spec.rb b/spec/components/guardian_spec.rb
index 41c8ff3a9..84afc362a 100644
--- a/spec/components/guardian_spec.rb
+++ b/spec/components/guardian_spec.rb
@@ -1162,10 +1162,19 @@ describe Guardian do
 
       let(:target_user) { build(:user, created_at: 4.days.ago) }
 
-      include_examples "staff can always change usernames"
+      context 'with no posts' do
+        include_examples "staff can always change usernames"
+        it "is true for the user to change his own username" do
+          Guardian.new(target_user).can_edit_username?(target_user).should be_true
+        end
+      end
 
-      it "is false for the user to change his own username" do
-        Guardian.new(target_user).can_edit_username?(target_user).should be_false
+      context 'with posts' do
+        before { target_user.stubs(:post_count).returns(1) }
+        include_examples "staff can always change usernames"
+        it "is false for the user to change his own username" do
+          Guardian.new(target_user).can_edit_username?(target_user).should be_false
+        end
       end
     end
   end