2016-08-20 03:48:58 -04:00
|
|
|
desc "Change topic/post ownership of all the topics/posts by a specific user (without creating new revision)"
|
2017-07-27 21:20:09 -04:00
|
|
|
task "users:change_post_ownership", [:old_username, :new_username, :archetype] => [:environment] do |_, args|
|
2016-08-20 03:48:58 -04:00
|
|
|
old_username = args[:old_username]
|
|
|
|
new_username = args[:new_username]
|
|
|
|
archetype = args[:archetype]
|
|
|
|
archetype = archetype.downcase if archetype
|
2018-02-28 16:21:52 -05:00
|
|
|
|
2016-08-20 03:48:58 -04:00
|
|
|
if !old_username || !new_username
|
2018-05-29 16:29:06 -04:00
|
|
|
puts "ERROR: Expecting rake users:change_post_ownership[old_username,new_username,archetype]"
|
2016-08-20 03:48:58 -04:00
|
|
|
exit 1
|
|
|
|
end
|
2018-02-28 16:21:52 -05:00
|
|
|
|
|
|
|
old_user = find_user(old_username)
|
|
|
|
new_user = find_user(new_username)
|
2016-08-20 03:48:58 -04:00
|
|
|
|
|
|
|
if archetype == "private"
|
|
|
|
posts = Post.private_posts.where(user_id: old_user.id)
|
|
|
|
elsif archetype == "public" || !archetype
|
|
|
|
posts = Post.public_posts.where(user_id: old_user.id)
|
|
|
|
else
|
2018-05-29 16:29:06 -04:00
|
|
|
puts "ERROR: Expecting rake users:change_post_ownership[old_username,new_username,archetype] where archetype is public or private"
|
2016-08-20 03:48:58 -04:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "Changing post ownership"
|
|
|
|
i = 0
|
|
|
|
posts.each do |p|
|
|
|
|
PostOwnerChanger.new(post_ids: [p.id], topic_id: p.topic.id, new_owner: User.find_by(username_lower: new_user.username_lower), acting_user: User.find_by(username_lower: "system"), skip_revision: true).change_owner!
|
|
|
|
putc "."
|
|
|
|
i += 1
|
|
|
|
end
|
|
|
|
puts "", "#{i} posts ownership changed!", ""
|
|
|
|
end
|
2018-02-28 16:21:52 -05:00
|
|
|
|
2018-06-27 14:24:46 -04:00
|
|
|
desc "Merge the source user into the target user"
|
2018-02-28 16:21:52 -05:00
|
|
|
task "users:merge", [:source_username, :target_username] => [:environment] do |_, args|
|
|
|
|
source_username = args[:source_username]
|
|
|
|
target_username = args[:target_username]
|
|
|
|
|
|
|
|
if !source_username || !target_username
|
2018-05-29 16:29:06 -04:00
|
|
|
puts "ERROR: Expecting rake users:merge[source_username,target_username]"
|
2018-02-28 16:21:52 -05:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
source_user = find_user(source_username)
|
|
|
|
target_user = find_user(target_username)
|
|
|
|
|
|
|
|
UserMerger.new(source_user, target_user).merge!
|
|
|
|
puts "", "Users merged!", ""
|
|
|
|
end
|
|
|
|
|
2018-06-27 14:24:46 -04:00
|
|
|
desc "Rename a user"
|
2018-04-30 19:54:36 -04:00
|
|
|
task "users:rename", [:old_username, :new_username] => [:environment] do |_, args|
|
|
|
|
old_username = args[:old_username]
|
|
|
|
new_username = args[:new_username]
|
|
|
|
|
|
|
|
if !old_username || !new_username
|
2018-05-29 16:29:06 -04:00
|
|
|
puts "ERROR: Expecting rake users:rename[old_username,new_username]"
|
2018-04-30 19:54:36 -04:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
changer = UsernameChanger.new(find_user(old_username), new_username)
|
|
|
|
changer.change(asynchronous: false)
|
|
|
|
puts "", "User renamed!", ""
|
|
|
|
end
|
|
|
|
|
2018-06-27 14:24:46 -04:00
|
|
|
desc "Update username in quotes and mentions. Use this if the user was renamed before proper renaming existed."
|
2018-05-25 13:01:53 -04:00
|
|
|
task "users:update_posts", [:old_username, :current_username] => [:environment] do |_, args|
|
|
|
|
old_username = args[:old_username]
|
|
|
|
current_username = args[:current_username]
|
|
|
|
|
|
|
|
if !old_username || !current_username
|
2018-05-29 16:29:06 -04:00
|
|
|
puts "ERROR: Expecting rake users:update_posts[old_username,current_username]"
|
2018-05-25 13:01:53 -04:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
user = find_user(current_username)
|
2018-07-23 16:42:56 -04:00
|
|
|
UsernameChanger.update_username(user_id: user.id,
|
|
|
|
old_username: old_username,
|
|
|
|
new_username: user.username,
|
|
|
|
avatar_template: user.avatar_template,
|
|
|
|
asynchronous: false)
|
2018-05-25 13:01:53 -04:00
|
|
|
|
|
|
|
puts "", "Username updated!", ""
|
|
|
|
end
|
|
|
|
|
2018-07-05 10:38:35 -04:00
|
|
|
desc 'Recalculate post and topic counts in user stats'
|
|
|
|
task 'users:recalculate_post_counts' => :environment do
|
|
|
|
puts '', 'Updating user stats...'
|
|
|
|
|
|
|
|
filter_public_posts_and_topics = <<~SQL
|
|
|
|
p.deleted_at IS NULL
|
|
|
|
AND NOT COALESCE(p.hidden, 't')
|
|
|
|
AND p.post_type = 1
|
|
|
|
AND t.deleted_at IS NULL
|
|
|
|
AND COALESCE(t.visible, 't')
|
|
|
|
AND t.archetype <> 'private_message'
|
|
|
|
AND p.user_id > 0
|
|
|
|
SQL
|
|
|
|
|
|
|
|
puts 'post counts...'
|
|
|
|
|
|
|
|
# all public replies
|
|
|
|
DB.exec <<~SQL
|
|
|
|
WITH X AS (
|
|
|
|
SELECT p.user_id, COUNT(p.id) post_count
|
|
|
|
FROM posts p
|
|
|
|
JOIN topics t ON t.id = p.topic_id
|
|
|
|
WHERE #{filter_public_posts_and_topics}
|
|
|
|
AND p.post_number > 1
|
|
|
|
GROUP BY p.user_id
|
|
|
|
)
|
|
|
|
UPDATE user_stats
|
|
|
|
SET post_count = X.post_count
|
|
|
|
FROM X
|
|
|
|
WHERE user_stats.user_id = X.user_id
|
|
|
|
AND user_stats.post_count <> X.post_count
|
|
|
|
SQL
|
|
|
|
|
|
|
|
puts 'topic counts...'
|
|
|
|
|
|
|
|
# public topics
|
|
|
|
DB.exec <<~SQL
|
|
|
|
WITH X AS (
|
|
|
|
SELECT p.user_id, COUNT(p.id) topic_count
|
|
|
|
FROM posts p
|
|
|
|
JOIN topics t ON t.id = p.topic_id
|
|
|
|
WHERE #{filter_public_posts_and_topics}
|
|
|
|
AND p.post_number = 1
|
|
|
|
GROUP BY p.user_id
|
|
|
|
)
|
|
|
|
UPDATE user_stats
|
|
|
|
SET topic_count = X.topic_count
|
|
|
|
FROM X
|
|
|
|
WHERE user_stats.user_id = X.user_id
|
|
|
|
AND user_stats.topic_count <> X.topic_count
|
|
|
|
SQL
|
|
|
|
|
|
|
|
puts 'Done!', ''
|
|
|
|
end
|
|
|
|
|
2018-07-12 04:20:45 -04:00
|
|
|
desc "Disable 2FA for user with the given username"
|
|
|
|
task "users:disable_2fa", [:username] => [:environment] do |_, args|
|
|
|
|
username = args[:username]
|
|
|
|
user = find_user(username)
|
2018-08-10 17:45:40 -04:00
|
|
|
UserSecondFactor.where(user_id: user.id, method: UserSecondFactor.methods[:totp]).each(&:destroy!)
|
2018-07-12 04:20:45 -04:00
|
|
|
puts "2FA disabled for #{username}"
|
|
|
|
end
|
|
|
|
|
2018-10-15 06:49:25 -04:00
|
|
|
desc "Anonymize all users except staff"
|
|
|
|
task "users:anonymize_all" => :environment do
|
|
|
|
require 'highline/import'
|
|
|
|
|
|
|
|
non_staff_users = User.where('NOT admin AND NOT moderator')
|
|
|
|
total = non_staff_users.count
|
|
|
|
anonymized = 0
|
|
|
|
|
|
|
|
confirm_anonymize = ask("Are you sure you want to anonymize #{total} users? (Y/n)")
|
|
|
|
exit 1 unless (confirm_anonymize == "" || confirm_anonymize.downcase == 'y')
|
|
|
|
|
|
|
|
system_user = Discourse.system_user
|
|
|
|
non_staff_users.each do |user|
|
|
|
|
begin
|
|
|
|
UserAnonymizer.new(user, system_user).make_anonymous
|
|
|
|
print_status(anonymized += 1, total)
|
|
|
|
rescue
|
|
|
|
# skip
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "", "#{total} users anonymized.", ""
|
|
|
|
end
|
|
|
|
|
2018-02-28 16:21:52 -05:00
|
|
|
def find_user(username)
|
|
|
|
user = User.find_by_username(username)
|
|
|
|
|
|
|
|
if !user
|
|
|
|
puts "ERROR: User with username #{username} does not exist"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
user
|
|
|
|
end
|