mirror of
https://github.com/discourse/discourse.git
synced 2025-09-06 10:50:21 +08:00
FIX: Rename User#usernames
that clashes with Group#name
. (#6069)
This commit is contained in:
parent
96aca6d7e6
commit
4172e1dd52
2 changed files with 44 additions and 0 deletions
|
@ -0,0 +1,29 @@
|
||||||
|
module Jobs
|
||||||
|
class FixUserUsernamesAndGroupNamesClash < Jobs::Scheduled
|
||||||
|
every 1.week
|
||||||
|
|
||||||
|
def execute(args)
|
||||||
|
User.joins("LEFT JOIN groups ON lower(groups.name) = users.username_lower")
|
||||||
|
.where("groups.id IS NOT NULL")
|
||||||
|
.find_each do |user|
|
||||||
|
|
||||||
|
suffix = 1
|
||||||
|
old_username = user.username
|
||||||
|
|
||||||
|
loop do
|
||||||
|
user.username = "#{old_username}#{suffix}"
|
||||||
|
suffix += 1
|
||||||
|
break if user.valid?
|
||||||
|
end
|
||||||
|
|
||||||
|
new_username = user.username
|
||||||
|
user.username = old_username
|
||||||
|
|
||||||
|
UsernameChanger.new(
|
||||||
|
user,
|
||||||
|
new_username
|
||||||
|
).change(asynchronous: false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
15
spec/jobs/fix_user_usernames_and_group_names_clash_spec.rb
Normal file
15
spec/jobs/fix_user_usernames_and_group_names_clash_spec.rb
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe Jobs::FixUserUsernamesAndGroupNamesClash do
|
||||||
|
it 'update usernames of users that clashes with a group name' do
|
||||||
|
user = Fabricate(:user)
|
||||||
|
Fabricate(:user, username: 'test1')
|
||||||
|
group = Fabricate(:group, name: 'test')
|
||||||
|
user.update_columns(username: 'test', username_lower: 'test')
|
||||||
|
|
||||||
|
Jobs::FixUserUsernamesAndGroupNamesClash.new.execute({})
|
||||||
|
|
||||||
|
expect(user.reload.username).to eq('test2')
|
||||||
|
expect(group.reload.name).to eq('test')
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Add a link
Reference in a new issue