add account deletion
This commit is contained in:
@@ -1,10 +1,18 @@
|
||||
module Maintenance
|
||||
module User
|
||||
class DeletionsController < ApplicationController
|
||||
rescue_from UserDeletion::ValidationError, :with => :rescue_exception
|
||||
|
||||
def show
|
||||
end
|
||||
|
||||
def destroy
|
||||
deletion = UserDeletion.new(CurrentUser.user, params[:password])
|
||||
deletion.delete!
|
||||
session.delete(:user_id)
|
||||
cookies.delete(:cookie_password_hash)
|
||||
cookies.delete(:user_name)
|
||||
redirect_to(posts_path, :notice => "You are now logged out")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
71
app/logical/user_deletion.rb
Normal file
71
app/logical/user_deletion.rb
Normal file
@@ -0,0 +1,71 @@
|
||||
class UserDeletion
|
||||
class ValidationError < Exception ; end
|
||||
|
||||
attr_reader :user, :password
|
||||
|
||||
def initialize(user, password)
|
||||
@user = user
|
||||
@password = password
|
||||
end
|
||||
|
||||
def delete!
|
||||
validate
|
||||
clear_user_settings
|
||||
remove_favorites
|
||||
rename
|
||||
reset_password
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def clear_user_settings
|
||||
user.email = nil
|
||||
user.last_logged_in_at = nil
|
||||
user.last_forum_read_at = nil
|
||||
user.recent_tags = nil
|
||||
user.favorite_tags = nil
|
||||
user.blacklisted_tags = nil
|
||||
user.hide_deleted_posts = false
|
||||
user.time_zone = "Eastern Time (US & Canada)"
|
||||
user.save!
|
||||
end
|
||||
|
||||
def reset_password
|
||||
random = SecureRandom.hex(16)
|
||||
user.password = random
|
||||
user.password_confirmation = random
|
||||
user.old_password = password
|
||||
user.save!
|
||||
end
|
||||
|
||||
def remove_favorites
|
||||
Post.tag_match("fav:#{user.name}").find_each do |post|
|
||||
Favorite.remove(post, user)
|
||||
end
|
||||
end
|
||||
|
||||
def rename
|
||||
name = "user_#{user.id}"
|
||||
n = 0
|
||||
while User.where(:name => name).exists? && (n < 10)
|
||||
name += "~"
|
||||
end
|
||||
|
||||
if n == 10
|
||||
raise ValidationError.new("New name could not be found")
|
||||
end
|
||||
|
||||
user.name = name
|
||||
user.save!
|
||||
end
|
||||
|
||||
def validate
|
||||
if !User.authenticate(user.name, password)
|
||||
raise ValidationError.new("Password is incorrect")
|
||||
end
|
||||
|
||||
if user.level >= User::Levels::ADMIN
|
||||
raise ValidationError.new("Admins cannot delete their account")
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -6,7 +6,18 @@
|
||||
<li>Rename your account to a generic string</li>
|
||||
<li>Scramble your password</li>
|
||||
<li>Remove all your favorites</li>
|
||||
<li>Blank out your settings (including email)</li>
|
||||
</ul>
|
||||
|
||||
<p>You must enter your password to delete your account.</p>
|
||||
|
||||
<%= form_tag(maintenance_user_deletion_path, :method => :delete, :class => "simple_form") do %>
|
||||
<div class="input">
|
||||
<label>Password</label>
|
||||
<%= password_field_tag :password %>
|
||||
</div>
|
||||
|
||||
<div class="input">
|
||||
<%= submit_tag %>
|
||||
</div>
|
||||
<% end %>
|
||||
@@ -25,8 +25,6 @@
|
||||
<%= f.input :per_page, :label => "Posts per page", :as => :select, :collection => (1..100), :include_blank => false %>
|
||||
<% end %>
|
||||
|
||||
<%= f.input :style_usernames, :as => :select, :label => "Colored usernames", :hint => "Color each user's name depending on their rank", :include_blank => false, :collection => [["Yes", "true"], ["No", "false"]] %>
|
||||
|
||||
<%= f.input :blacklisted_tags, :hint => "Put any tag combinations you never want to see here. Each combination should go on a separate line.", :input_html => {:size => "40x5"} %>
|
||||
<div class="input text optional field_with_hint">
|
||||
<label class="text optional" for="user_favorite_tags">Frequent tags</label>
|
||||
@@ -37,6 +35,7 @@
|
||||
|
||||
<fieldset>
|
||||
<legend>Advanced Settings</legend>
|
||||
<%= f.input :style_usernames, :as => :select, :label => "Colored usernames", :hint => "Color each user's name depending on their rank", :include_blank => false, :collection => [["Yes", "true"], ["No", "false"]] %>
|
||||
<%= f.input :always_resize_images, :as => :select, :include_blank => false, :label => "Fit images to window", :hint => "Use JavaScript to resize images to fit window" %>
|
||||
<%= f.input :enable_post_navigation, :as => :select, :include_blank => false, :label => "Enable keyboard shortcuts" %>
|
||||
<%= f.input :new_post_navigation_layout, :as => :select, :label => "Pool links", :include_blank => false, :collection => [["Bottom", "true"], ["Top", "false"]], :hint => "When browsing pools, where do you want the navigation links to be placed?" %>
|
||||
@@ -50,6 +49,11 @@
|
||||
<%= f.input :old_password, :as => :password, :input_html => {:autocomplete => "off"} %>
|
||||
</fieldset>
|
||||
|
||||
<fieldset>
|
||||
<legend>Delete Account</legend>
|
||||
<p><%= link_to "Click here to delete your account", maintenance_user_deletion_path %></p>
|
||||
</fieldset>
|
||||
|
||||
<%= f.button :submit, "Submit" %>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user