Skip to content

Commit 663adde

Browse files
committed
Users can change their own username at any time if they have no posts
1 parent 6a3c849 commit 663adde

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

lib/guardian.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def can_edit_topic?(topic)
273273
end
274274

275275
def can_edit_username?(user)
276-
is_staff? || (is_me?(user) && user.created_at > SiteSetting.username_change_period.days.ago)
276+
is_staff? || (is_me?(user) && (user.post_count == 0 || user.created_at > SiteSetting.username_change_period.days.ago))
277277
end
278278

279279
# Deleting Methods

spec/components/guardian_spec.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,10 +1162,19 @@
11621162

11631163
let(:target_user) { build(:user, created_at: 4.days.ago) }
11641164

1165-
include_examples "staff can always change usernames"
1165+
context 'with no posts' do
1166+
include_examples "staff can always change usernames"
1167+
it "is true for the user to change his own username" do
1168+
Guardian.new(target_user).can_edit_username?(target_user).should be_true
1169+
end
1170+
end
11661171

1167-
it "is false for the user to change his own username" do
1168-
Guardian.new(target_user).can_edit_username?(target_user).should be_false
1172+
context 'with posts' do
1173+
before { target_user.stubs(:post_count).returns(1) }
1174+
include_examples "staff can always change usernames"
1175+
it "is false for the user to change his own username" do
1176+
Guardian.new(target_user).can_edit_username?(target_user).should be_false
1177+
end
11691178
end
11701179
end
11711180
end

0 commit comments

Comments
 (0)