From dacab152882618207e1052b61b301435a06b9287 Mon Sep 17 00:00:00 2001 From: kikito Date: Fri, 16 Oct 2015 14:02:02 +0200 Subject: [PATCH 01/12] adds erase_reason to users --- db/migrate/20151016110703_add_erase_reason_to_users.rb | 5 +++++ db/schema.rb | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20151016110703_add_erase_reason_to_users.rb diff --git a/db/migrate/20151016110703_add_erase_reason_to_users.rb b/db/migrate/20151016110703_add_erase_reason_to_users.rb new file mode 100644 index 000000000..be88869b5 --- /dev/null +++ b/db/migrate/20151016110703_add_erase_reason_to_users.rb @@ -0,0 +1,5 @@ +class AddEraseReasonToUsers < ActiveRecord::Migration + def change + add_column :users, :erase_reason, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 3dbea57a8..c576eb1b4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20151013145757) do +ActiveRecord::Schema.define(version: 20151016110703) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -304,6 +304,7 @@ ActiveRecord::Schema.define(version: 20151013145757) do t.string "letter_verification_code" t.integer "failed_census_calls_count", default: 0 t.datetime "level_two_verified_at" + t.string "erase_reason" end add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree From 1511f811fd60cfb711ae75b2a6faa1dcb8efa1a3 Mon Sep 17 00:00:00 2001 From: kikito Date: Fri, 16 Oct 2015 14:25:05 +0200 Subject: [PATCH 02/12] implements first version of user.erase --- app/models/user.rb | 17 +++++++++++++++++ spec/models/user_spec.rb | 31 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index f652a8f9c..eb7fdc5ab 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -26,6 +26,7 @@ class User < ActiveRecord::Base validates :username, presence: true, unless: :organization? validates :username, uniqueness: true, unless: :organization? validates :document_number, uniqueness: { scope: :document_type }, allow_nil: true + validate :validate_username_length validates :official_level, inclusion: {in: 0..5} @@ -145,6 +146,22 @@ class User < ActiveRecord::Base Proposal.hide_all proposal_ids end + def erase(erase_reason = nil) + self.update( + erase_reason: erase_reason, + username: nil, + email: "", + unconfirmed_email: nil, + document_number: nil, + phone_number: nil, + encrypted_password: "", + confirmation_token: nil, + reset_password_token: nil, + email_verification_token: nil + ) + + self.hide + end def email_provided? !!(email && email !~ OMNIAUTH_EMAIL_REGEX) || diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 14f20ad1d..be093c85e 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -302,4 +302,35 @@ describe User do end + describe "#erase" do + it "anonymizes a user and marks him as hidden" do + user = create(:user, + username: "manolo", + unconfirmed_email: "a@a.com", + document_number: "1234", + phone_number: "5678", + encrypted_password: "foobar", + confirmation_token: "token1", + reset_password_token: "token2", + email_verification_token: "token3") + user.erase('a test') + user.reload + + expect(user.erase_reason).to eq('a test') + + expect(user.username).to be_nil + + expect(user.email).to be_empty + expect(user.unconfirmed_email).to be_nil + expect(user.document_number).to be_nil + expect(user.phone_number).to be_nil + + expect(user.encrypted_password).to be_empty + + expect(user.confirmation_token).to be_nil + expect(user.reset_password_token).to be_nil + expect(user.email_verification_token).to be_nil + end + end + end From 9cc158540efaf91a2b40d705288055ed052c777c Mon Sep 17 00:00:00 2001 From: kikito Date: Fri, 16 Oct 2015 20:04:16 +0200 Subject: [PATCH 03/12] implements a first version of the forms. Missing: a) tests and b) check db constraints (duplicate email "", etc) --- .../users/registrations_controller.rb | 20 ++++++++++++++ app/views/account/show.html.erb | 5 +++- .../users/registrations/delete_form.html.erb | 27 +++++++++++++++++++ config/locales/devise_views.en.yml | 5 ++++ config/locales/devise_views.es.yml | 5 ++++ config/locales/en.yml | 1 + config/locales/es.yml | 1 + config/routes.rb | 12 +++++---- 8 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 app/views/users/registrations/delete_form.html.erb diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb index 8e04c0d61..9257488ee 100644 --- a/app/controllers/users/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -10,6 +10,22 @@ class Users::RegistrationsController < Devise::RegistrationsController end end + def delete_form + build_resource({}) + end + + def delete + # The only difference between this version of delete and the original are the following two lines + # (we build the resource differently and we also call erase instead of destroy) + build_resource(erase_params) + resource.erase(params[:erase_reason]) + + yield resource if block_given? + Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name) + set_flash_message :notice, :destroyed if is_flashing_format? + respond_with_navigational(resource){ redirect_to after_sign_out_path_for(resource_name) } + end + def success end @@ -32,6 +48,10 @@ class Users::RegistrationsController < Devise::RegistrationsController params.require(:user).permit(:username, :email, :password, :password_confirmation, :captcha, :captcha_key, :terms_of_service) end + def erase_params + params.require(:user).permit(:erase_reason) + end + def after_inactive_sign_up_path_for(resource_or_scope) users_sign_up_success_path end diff --git a/app/views/account/show.html.erb b/app/views/account/show.html.erb index c52219955..8c9be93c7 100644 --- a/app/views/account/show.html.erb +++ b/app/views/account/show.html.erb @@ -1,6 +1,9 @@