From 608647d02fdadf240bdf1170006fdcd0f2f09bca Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Fri, 6 Mar 2015 16:44:54 -0500 Subject: [PATCH] FEATURE: Anonymize User. A way to remove a user but keep their topics and posts. --- .../javascripts/admin/models/admin_user.js | 42 ++++++++ .../admin/templates/user_index.hbs | 5 + app/controllers/admin/users_controller.rb | 12 ++- app/controllers/users_controller.rb | 2 +- app/models/user.rb | 15 +-- app/models/user_history.rb | 6 +- .../admin_detailed_user_serializer.rb | 5 + app/services/user_anonymizer.rb | 63 ++++++++++++ app/services/username_changer.rb | 23 +++++ config/locales/client.en.yml | 6 ++ config/routes.rb | 1 + lib/guardian/user_guardian.rb | 4 + spec/components/guardian_spec.rb | 42 ++++++++ spec/models/user_spec.rb | 86 ---------------- spec/services/user_anonymizer_spec.rb | 99 +++++++++++++++++++ spec/services/username_changer_spec.rb | 90 +++++++++++++++++ 16 files changed, 401 insertions(+), 100 deletions(-) create mode 100644 app/services/user_anonymizer.rb create mode 100644 app/services/username_changer.rb create mode 100644 spec/services/user_anonymizer_spec.rb create mode 100644 spec/services/username_changer_spec.rb diff --git a/app/assets/javascripts/admin/models/admin_user.js b/app/assets/javascripts/admin/models/admin_user.js index d005c14523c..7e66e278ab8 100644 --- a/app/assets/javascripts/admin/models/admin_user.js +++ b/app/assets/javascripts/admin/models/admin_user.js @@ -328,6 +328,48 @@ Discourse.AdminUser = Discourse.User.extend({ }); }, + anonymizeForbidden: Em.computed.not("can_be_anonymized"), + + anonymize: function() { + var user = this; + + var performAnonymize = function() { + Discourse.ajax("/admin/users/" + user.get('id') + '/anonymize.json', {type: 'PUT'}).then(function(data) { + if (data.success) { + bootbox.alert(I18n.t("admin.user.anonymize_successful")); + if (data.username) { + document.location = "/admin/users/" + data.username; + } else { + document.location = "/admin/users/list/active"; + } + } else { + bootbox.alert(I18n.t("admin.user.anonymize_failed")); + if (data.user) { + user.setProperties(data.user); + } + } + }, function() { + bootbox.alert(I18n.t("admin.user.anonymize_failed")); + }); + }; + + var message = I18n.t("admin.user.anonymize_confirm"); + + var buttons = [{ + "label": I18n.t("composer.cancel"), + "class": "cancel", + "link": true + }, { + "label": '' + I18n.t('admin.user.anonymize_yes'), + "class": "btn btn-danger", + "callback": function(){ + performAnonymize(); + } + }]; + + bootbox.dialog(message, buttons, {"classes": "delete-user-modal"}); + }, + deleteForbidden: Em.computed.not("canBeDeleted"), deleteExplanation: function() { diff --git a/app/assets/javascripts/admin/templates/user_index.hbs b/app/assets/javascripts/admin/templates/user_index.hbs index 6c85c515ebd..bd25465e137 100644 --- a/app/assets/javascripts/admin/templates/user_index.hbs +++ b/app/assets/javascripts/admin/templates/user_index.hbs @@ -450,6 +450,11 @@

+ +