From 30e25cea3eb4e8f15126e8d3aa8309ca98a604b8 Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Tue, 26 Dec 2017 15:34:52 -0600 Subject: [PATCH] Allow users to delete their own account :( --- app/controllers/users_controller.rb | 34 +++++++++++ app/views/devise/registrations/edit.html.erb | 4 ++ .../devise/registrations/panes/_more.html.erb | 56 +++++++++++++++++++ config/routes.rb | 1 + 4 files changed, 95 insertions(+) create mode 100644 app/views/devise/registrations/panes/_more.html.erb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 31d0a7d2..15dc63bf 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -27,4 +27,38 @@ class UsersController < ApplicationController render :content_list end end + + def delete_my_account # :( + unless user_signed_in? + redirect_to root_path + return + end + + # Make sure the user is set to Starter on Stripe so we don't keep charging them + stripe_customer = Stripe::Customer.retrieve current_user.stripe_customer_id + stripe_subscription = stripe_customer.subscriptions.data[0] + stripe_subscription.plan = 'starter' + stripe_subscription.save + + report_user_deletion_to_slack current_user + + current_user.really_destroy! + redirect_to root_path, notice: 'Your account has been deleted. We will miss you greatly!' + end + + def report_user_deletion_to_slack user + return unless Rails.env == 'production' + slack_hook = ENV['SLACK_HOOK'] + return unless slack_hook + + notifier = Slack::Notifier.new slack_hook, + channel: '#analytics', + username: 'tristan' + + notifier.ping [ + ":bomb: :bomb: :bomb:", + "#{user.email.split('@').first}@ (##{user.id}) just deleted their account.", + ":bomb: :bomb: :bomb:", + ].join("\n") + end end diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/edit.html.erb index 84b8919f..0bb149f4 100644 --- a/app/views/devise/registrations/edit.html.erb +++ b/app/views/devise/registrations/edit.html.erb @@ -7,6 +7,7 @@
@@ -15,6 +16,9 @@
<%= render partial: 'devise/registrations/panes/preferences', locals: { f: f } %>
+
+ <%= render partial: 'devise/registrations/panes/more', locals: { f: f } %> +
diff --git a/app/views/devise/registrations/panes/_more.html.erb b/app/views/devise/registrations/panes/_more.html.erb new file mode 100644 index 00000000..1dab72c5 --- /dev/null +++ b/app/views/devise/registrations/panes/_more.html.erb @@ -0,0 +1,56 @@ +
+
+

To delete your account

+

+ This action is IRREVERSIBLE. Your account and all of your content + will be immediately and permanently deleted. We will miss you! +

+

+
+ Delete my account + +

+

+
+
diff --git a/config/routes.rb b/config/routes.rb index 82c490f3..8821696b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -66,6 +66,7 @@ Rails.application.routes.draw do end end resources :documents + delete 'delete_my_account', to: 'users#delete_my_account' # Lab apps scope '/app' do