diff --git a/app/models/user.rb b/app/models/user.rb index 672cf47..3b20ad6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -50,6 +50,7 @@ include ValidationMessages include ValidationValues extend UniqueRandom + prepend Dmpopidor::Models::User ## # Devise @@ -384,22 +385,12 @@ notifications << notification if notification.dismissable? end - # Anonymize a user (by removing its personnal data and deactivating its account) - def anonymize - copy = dup - - update(firstname: 'anonymous', surname: 'user', email: "anonymous#{id}@opidor.fr", last_sign_in_at: nil, encrypted_password: nil, active: false) - - if save - Rails.logger.info "User #{id} anonymized" - p "User #{id} anonymized" - UserMailer.anonymization_notice(copy).deliver_now - end - end + # remove personal data from the user account and save # leave account in-place, with org for statistics (until we refactor those) # # Returns boolean + # SEE MODULE def archive self.firstname = 'Deleted' self.surname = 'User' diff --git a/app/views/user_mailer/anonymization_notice.html.erb b/app/views/user_mailer/anonymization_notice.html.erb index eb6ca8b..d4eaa54 100644 --- a/app/views/user_mailer/anonymization_notice.html.erb +++ b/app/views/user_mailer/anonymization_notice.html.erb @@ -2,7 +2,7 @@

<%= _('Hello ') %><%= @user.name %>

- <%= d_('dmpopidor', 'You have not signed in since %{last_sign_in_date} and your account has been deactivated') % { :last_sign_in_date => @user.last_sign_in_at.to_date } %>. + <%= d_('dmpopidor', 'Your account has been deactivated') %>. <%= d_('dmpopidor', 'All accounts inactive for 5 years are automaticaly deactivated') %>.

diff --git a/config/locale/dmpopidor.pot b/config/locale/dmpopidor.pot index 7aa16a5..ee3bd23 100644 --- a/config/locale/dmpopidor.pot +++ b/config/locale/dmpopidor.pot @@ -24,7 +24,7 @@ msgid "Account expired in %{tool_name}" msgstr "" -msgid "You have not signed in since %{last_sign_in_date} and your account has been deactivated" +msgid "Your account has been deactivated" msgstr "" msgid "All accounts inactive for 5 years are automaticaly deactivated" diff --git a/config/locale/en_GB/dmpopidor.po b/config/locale/en_GB/dmpopidor.po index fab994f..1f3367b 100644 --- a/config/locale/en_GB/dmpopidor.po +++ b/config/locale/en_GB/dmpopidor.po @@ -22,8 +22,8 @@ msgid "Account expired in %{tool_name}" msgstr "Account expired in %{tool_name}" -msgid "You have not signed in since %{last_sign_in_date} and your account has been deactivated" -msgstr "You have not signed in since %{last_sign_in_date} and your account has been deactivated" +msgid "Your account has been deactivated" +msgstr "Your account has been deactivated" msgid "All accounts inactive for 5 years are automaticaly deactivated" msgstr "All accounts inactive for 5 years are automaticaly deactivated" diff --git a/config/locale/fr_FR/dmpopidor.po b/config/locale/fr_FR/dmpopidor.po index bbc7b1a..bc6e9ee 100644 --- a/config/locale/fr_FR/dmpopidor.po +++ b/config/locale/fr_FR/dmpopidor.po @@ -20,8 +20,8 @@ msgid "Account expired in %{tool_name}" msgstr "Compte %{tool_name} expiré" -msgid "You have not signed in since %{last_sign_in_date} and your account has been deactivated" -msgstr "Vous ne vous êtes pas connecté depuis le %{last_sign_in_date}, votre compte a été désactivé" +msgid "Your account has been deactivated" +msgstr "Votre compte a été désactivé" msgid "All accounts inactive for 5 years are automaticaly deactivated" msgstr "Tous les comptes inactifs pendant 5 ans sont automatiquement désactivés" diff --git a/lib/dmpopidor/models/user.rb b/lib/dmpopidor/models/user.rb new file mode 100644 index 0000000..2d1825c --- /dev/null +++ b/lib/dmpopidor/models/user.rb @@ -0,0 +1,35 @@ +module Dmpopidor + module Models + module User + + # remove personal data from the user account and save + # leave account in-place, with org for statistics (until we refactor those) + # + # Returns boolean + def archive + copy = self.dup + self.firstname = 'Anonymous' + self.surname = 'User' + self.email = ::User.unique_random(field_name: 'email', + prefix: 'user_', + suffix: Rails.configuration.branding[:application].fetch(:archived_accounts_email_suffix, '@example.org'), + length: 5) + self.recovery_email = nil + self.api_token = nil + self.encrypted_password = nil + self.last_sign_in_ip = nil + self.current_sign_in_ip = nil + self.active = false + + self.user_identifiers.destroy_all + + Rails.logger.info "User #{self.id} anonymized" + p "User #{self.id} anonymized" + UserMailer.anonymization_notice(copy).deliver_now + + return self.save + end + + end + end +end diff --git a/lib/tasks/user_cleaning.rake b/lib/tasks/user_cleaning.rake index 4191afd..9158c72 100644 --- a/lib/tasks/user_cleaning.rake +++ b/lib/tasks/user_cleaning.rake @@ -14,16 +14,16 @@ task anonymize_users_after_5_years: :environment do Rails.logger.info 'Anonymizing users who have not connected for the last 5 years' - User.where('last_sign_in_at < ?', 5.years.ago + 1.month).each do |user| + User.where('last_sign_in_at < ?', 5.years.ago - 1.month).each do |user| case user.last_sign_in_at.to_date - when (5.years.ago + 1.month).to_date + when (5.years.ago - 1.month).to_date UserMailer.anonymization_warning(user).deliver_now - when (5.years.ago + 1.week).to_date + when (5.years.ago - 1.week).to_date UserMailer.anonymization_warning(user).deliver_now - when (5.years.ago + 1.day).to_date + when (5.years.ago - 1.day).to_date UserMailer.anonymization_warning(user).deliver_now when 5.years.ago.to_date - user.anonymize + user.archive end end end