diff --git a/lib/guardian.rb b/lib/guardian.rb index d03cd89fb82..cea13bc930b 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 41c8ff3a91c..84afc362af1 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