users: allow mods to change the names of other users.

Allow moderators to forcibly change the username of other users. This is
so mods can change abusive or invalid usernames.

* A mod can only change the username of Builder-level users and below.
* The user can't change their own name again until one week has passed.
* A modaction is logged when a mod changes a user's name.
* A dmail is sent to the user notifying them of the change.
* The dmail does not send the user an email notification. This is so we
  don't spam users if their name is changed after they're banned, or if
  they haven't visited the site in a long time.

The rename button is on the user's profile page, and when you hover over
the user's name and open the "..." menu.
This commit is contained in:
evazion
2022-10-01 23:13:21 -05:00
parent 775326dc37
commit 99846b7e3d
16 changed files with 107 additions and 30 deletions

View File

@@ -1,7 +1,7 @@
# frozen_string_literal: true
class Dmail < ApplicationRecord
attr_accessor :creator_ip_addr
attr_accessor :creator_ip_addr, :disable_email_notifications
validate :validate_sender_is_not_limited, on: :create
validates :title, presence: true, length: { maximum: 200 }, if: :title_changed?
@@ -145,7 +145,7 @@ class Dmail < ApplicationRecord
end
def send_email
if is_recipient? && !is_deleted? && to.receive_email_notifications?
if is_recipient? && !is_deleted? && to.receive_email_notifications? && !disable_email_notifications
UserMailer.with(headers: { "X-Danbooru-Dmail": Routes.dmail_url(self) }).dmail_notice(self).deliver_later
end
end

View File

@@ -2,13 +2,16 @@
class UserNameChangeRequest < ApplicationRecord
belongs_to :user
belongs_to :approver, class_name: "User", optional: true
attr_accessor :updater
validate :not_limited, on: :create
validates :original_name, presence: true
validates :desired_name, user_name: true, presence: true, on: :create
after_create :update_name!
after_create :create_mod_action
after_create :send_dmail
def self.visible(user)
if user.is_moderator?
@@ -31,9 +34,22 @@ class UserNameChangeRequest < ApplicationRecord
def not_limited
return if user.name_invalid?
return if updater && updater != user
if UserNameChangeRequest.unscoped.where(user: user).exists?(["created_at >= ?", 1.week.ago])
if user.user_name_change_requests.exists?(created_at: (1.week.ago..))
errors.add(:base, "You can only submit one name change request per week")
end
end
def create_mod_action
return if updater.nil? || user == updater
ModAction.log("changed user ##{user.id}'s name from #{original_name} to #{desired_name}", :user_name_change, subject: user, user: updater)
end
def send_dmail
return if updater.nil? || user == updater
Dmail.create_automated(to: user, disable_email_notifications: true, title: "Your username has been changed", body: <<~EOS)
Your username has been changed from #{original_name} to #{desired_name}. Your old name was either no longer valid or it violated site rules. You can change it to something else after one week. Please make sure your name follows the [[help:community rules|community rules]].
EOS
end
end