Files
danbooru/app/logical/user_deletion.rb
evazion 65adcd09c2 users: track logins, signups, and other user events.
Add tracking of certain important user actions. These events include:

* Logins
* Logouts
* Failed login attempts
* Account creations
* Account deletions
* Password reset requests
* Password changes
* Email address changes

This is similar to the mod actions log, except for account activity
related to a single user.

The information tracked includes the user, the event type (login,
logout, etc), the timestamp, the user's IP address, IP geolocation
information, the user's browser user agent, and the user's session ID
from their session cookie. This information is visible to mods only.

This is done with three models. The UserEvent model tracks the event
type (login, logout, password change, etc) and the user. The UserEvent
is tied to a UserSession, which contains the user's IP address and
browser metadata. Finally, the IpGeolocation model contains the
geolocation information for IPs, including the city, country, ISP, and
whether the IP is a proxy.

This tracking will be used for a few purposes:

* Letting users view their account history, to detect things like logins
  from unrecognized IPs, failed logins attempts, password changes, etc.
* Rate limiting failed login attempts.
* Detecting sockpuppet accounts using their login history.
* Detecting unauthorized account sharing.
2021-01-08 22:34:37 -06:00

79 lines
1.7 KiB
Ruby

class UserDeletion
include ActiveModel::Validations
attr_reader :user, :password, :request
validate :validate_deletion
def initialize(user, password, request)
@user = user
@password = password
@request = request
end
def delete!
return false if invalid?
clear_user_settings
remove_favorites
clear_saved_searches
rename
reset_password
create_mod_action
create_user_event
user
end
private
def create_mod_action
ModAction.log("user ##{user.id} deleted", :user_delete)
end
def create_user_event
UserEvent.create_from_request!(user, :user_deletion, request)
end
def clear_saved_searches
SavedSearch.where(user_id: user.id).destroy_all
end
def clear_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.hide_deleted_posts = false
user.show_deleted_children = false
user.time_zone = "Eastern Time (US & Canada)"
user.save!
end
def reset_password
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)
request = UserNameChangeRequest.new(user: user, desired_name: name, original_name: user.name)
request.save!(validate: false) # XXX don't validate so that the 1 name change per week rule doesn't interfere
end
def validate_deletion
if !user.authenticate_password(password)
errors.add(:base, "Password is incorrect")
end
if user.is_admin?
errors.add(:base, "Admins cannot delete their account")
end
end
end