users: delete more data when user deactivates their account.

* Don't delete the user's favorites unless private favorites are enabled. The general rule is that
  public account activity is kept and private account activity is deleted.
* Delete the user's API keys, forum topics visits, private favgroups, downvotes, and upvotes (if
  privacy is enabled).
* Reset all of the user's account settings to default. This means custom CSS is deleted, where it
  wasn't before.
* Delete everything but the user's name and password asynchronously.
* Don't log the current user out if it's the owner deleting another user's account.
* Fix #5067 (Mod actions sometimes not created for user deletions) by wrapping the deletion process
  in a transaction.
This commit is contained in:
evazion
2022-11-05 23:26:13 -05:00
parent 3ffde5b23d
commit b43a913ad7
7 changed files with 143 additions and 53 deletions

View File

@@ -131,7 +131,6 @@ class UsersController < ApplicationController
user_deletion.delete!
if user_deletion.errors.none?
session.delete(:user_id)
flash[:notice] = "Your account has been deactivated"
respond_with(user_deletion, location: posts_path)
else

View File

@@ -0,0 +1,11 @@
# frozen_string_literal: true
# A job that deletes a user's settings and other personal data when they delete their account.
class DeleteUserJob < ApplicationJob
queue_as :default
queue_with_priority 20
def perform(user)
UserDeletion.new(user: user).delete_user
end
end

View File

@@ -46,11 +46,11 @@ class SessionLoader
end
# Logs the current user out. Deletes their session cookie and records a logout event.
def logout
def logout(user = CurrentUser.user)
session.delete(:user_id)
session.delete(:last_authenticated_at)
return if CurrentUser.user.is_anonymous?
UserEvent.create_from_request!(CurrentUser.user, :logout, request)
return if user.is_anonymous?
UserEvent.create_from_request!(user, :logout, request)
end
# Sets the current user. Runs on each HTTP request. The user is set based on

View File

@@ -12,8 +12,9 @@ class UserDeletion
validate :validate_deletion
# Initialize a user deletion.
#
# @param user [User] the user to delete
# @param user [User] the user performing the deletion
# @param deleter [User] the user performing the deletion
# @param password [String] the user's password (for confirmation)
# @param request the HTTP request (for logging the deletion in the user event log)
def initialize(user:, deleter: user, password: nil, request: nil)
@@ -24,43 +25,64 @@ class UserDeletion
end
# Delete the account, if the deletion is allowed.
# @return [Boolean] if the deletion failed
# @return [User] if the deletion succeeded
#
# @return [Boolean] True if the deletion was successful, false otherwise.
def delete!
return false if invalid?
clear_user_settings
remove_favorites
clear_saved_searches
rename
reset_password
create_mod_action
create_user_event
user
user.with_lock do
rename
reset_password
async_delete_user
ModAction.log("deleted user ##{user.id}", :user_delete, subject: user, user: deleter)
UserEvent.create_from_request!(user, :user_deletion, request) if request.present?
SessionLoader.new(request).logout(user) if user == deleter
end
true
end
private
def create_mod_action
ModAction.log("deleted user ##{user.id}", :user_delete, subject: user, user: deleter)
# Calls `delete_user`.
def async_delete_user
DeleteUserJob.perform_later(user)
end
def create_user_event
UserEvent.create_from_request!(user, :user_deletion, request) if request.present?
def delete_user
delete_user_data
delete_user_settings
end
def clear_saved_searches
SavedSearch.where(user_id: user.id).destroy_all
def delete_user_data
user.api_keys.destroy_all
user.forum_topic_visits.destroy_all
user.saved_searches.destroy_all
user.favorite_groups.is_private.destroy_all
user.post_votes.active.negative.find_each do |vote|
vote.soft_delete!(updater: user)
end
if user.enable_private_favorites
user.favorites.destroy_all
user.post_votes.active.positive.find_each do |vote|
vote.soft_delete!(updater: user)
end
end
end
def clear_user_settings
def delete_user_settings
user.email_address = nil
user.last_logged_in_at = nil
user.last_forum_read_at = nil
user.favorite_tags = ""
user.blacklisted_tags = ""
user.show_deleted_children = false
user.time_zone = "Eastern Time (US & Canada)"
User::USER_PREFERENCE_BOOLEAN_ATTRIBUTES.each do |attribute|
user.send("#{attribute}=", false)
end
%w[time_zone comment_threshold default_image_size favorite_tags blacklisted_tags custom_style per_page theme].each do |attribute|
user[attribute] = User.column_defaults[attribute]
end
user.save!
end
@@ -68,10 +90,6 @@ class UserDeletion
user.update!(password: SecureRandom.hex(16))
end
def remove_favorites
DeleteFavoritesJob.perform_later(user)
end
def rename
name = "user_#{user.id}"
name += "~" while User.exists?(name: name)

View File

@@ -58,6 +58,9 @@ class User < ApplicationRecord
ACTIVE_BOOLEAN_ATTRIBUTES = BOOLEAN_ATTRIBUTES.grep_v(/unused/)
# Personal preferences that are editable by the user, rather than internal flags. These will be cleared when the user deactivates their account.
USER_PREFERENCE_BOOLEAN_ATTRIBUTES = ACTIVE_BOOLEAN_ATTRIBUTES - %w[is_banned requires_verification is_verified]
DEFAULT_BLACKLIST = ["guro", "scat", "furry -rating:g"].join("\n")
attribute :id

View File

@@ -9,17 +9,19 @@
<h1>Deactivate Account: <%= link_to_user @user %></h1>
<% end %>
<div class="prose mb-4">
<div class="prose mb-4 fixed-width-container">
<p>
You can deactivate your <%= Danbooru.config.app_name %> account by entering your password below. Deactivating
your account will do the following things:
</p>
You can deactivate your account by entering your password below. Deactivating your account will delete your
private account information, but it will not delete your contributions to the site.
</p>Deactivating your account will do the following things: </p>
<ul>
<li>Change your username to a generic username (<i>user_<%= @user.id %></i>).</li>
<li>Delete your password, email address, and account settings.</li>
<li>Delete your favorites.</li>
<li>Delete your saved searches.</li>
<li>Delete your password, email address, <%= link_to_wiki "API keys", "help:api" %>, and account settings.</li>
<li>Delete your <%= link_to_wiki "saved searches", "help:saved_searches" %>.</li>
<li>Delete your <%= link_to_wiki "private favorite groups", "help:favorite_groups" %>.</li>
<li>Delete your private favorites and upvotes (only if <%= link_to_wiki "privacy mode", "help:privacy_mode" %> is enabled).</li>
</ul>
<p>
@@ -33,6 +35,8 @@
<li>Your login history, including your IP address and geographic location. This is kept for moderation purposes.</li>
</ul>
<p>If you just want to change your username, you can <%= link_to "change your name here", change_name_user_path(@user) %>.</p>
<p>
Enter your password below to deactivate your account. This cannot be
undone. Your account cannot be recovered after it is deactivated.