decidim-cleaner 2.0.0 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f54787d174b568ded865927e72ec935eda6e9133b7f0852bc9e00e7aa1a3d7ca
4
- data.tar.gz: 948c3bf4816c2ce34ce80d3d57196a5712c8fb286343662bfec2bfd3b4e3729a
3
+ metadata.gz: a54a64e4729007defe0803975b74c3e2d5d6fbcc66d30fc33c4534f79a496205
4
+ data.tar.gz: b3c8baca2de78e8536509be9d5ea87a96521c8b9376da7f1dd6cef97f211c240
5
5
  SHA512:
6
- metadata.gz: a6eac33bbda665f50c8e2e0ac84e7b5c7ee143e6adce9c624a31e9cb89ab9d2c31cb57c92cad167ee84420bf176ccf236a6a055bc288556cdfcc76cda940dcf5
7
- data.tar.gz: bac945ce0b0830278a2536242d0397e6bf8f78d448de866bf4ff23ebb047726c7c3d74cacd43e35b570ed469d39dcf189a6f025efc1dc36770c242d3bbd4a116
6
+ metadata.gz: 9c69c28fcc394795f6fbef9fd340cc35e4068e25983ba321d262416f8406c1b1b0769fc469fd9ab404203f963b10c8da8c8367f266c535fda771087575a2cddd
7
+ data.tar.gz: 2d7e6f95b56b232137ae8586d4562e59689f54b301360e3a899c5c25e3de237357c5562b442b96f09c018120d52ec47a717ec36b12aa61f51f713f78da1c3c3f
data/README.md CHANGED
@@ -22,6 +22,19 @@ bundle exec rails decidim_cleaner:install:migrations
22
22
  bundle exec rails db:migrate
23
23
  ```
24
24
 
25
+ You can then modify the default values of the cleaner in your .ENV with the following variables:
26
+
27
+ ```bash
28
+ # Delay until a user is considered inactive and receive a warning email (in days)
29
+ DECIDIM_CLEANER_INACTIVE_USERS_MAIL=
30
+
31
+ # Delay until a user is deleted after receiving an email (in days)
32
+ DECIDIM_CLEANER_DELETE_INACTIVE_USERS=
33
+
34
+ # Delay until an admin log is deleted (in days)
35
+ DECIDIM_CLEANER_DELETE_ADMIN_LOGS=
36
+ ```
37
+
25
38
  ### Sidekiq Scheduler
26
39
  [Further documentation](https://github.com/sidekiq-scheduler/sidekiq-scheduler)
27
40
 
@@ -53,7 +66,7 @@ You can then add to your 'config/sidekiq.yml' file:
53
66
  ## Available tasks
54
67
 
55
68
  - [ ] **Delete inactive users**
56
- - Cron task that checks for user accounts where `last_sign_in_at` is superior to environment variable `CLEANER_USER_INACTIVITY_LIMIT`. If true, deletes inactive user from the database.
69
+ - Cron task that checks for user accounts where `current_sign_in_at` is superior to environment variable `CLEANER_USER_INACTIVITY_LIMIT`. If true, deletes inactive user from the database.
57
70
 
58
71
  - [ ] **Delete old admin logs**
59
72
  - Cron task that checks for admin logs where `created_at` is anterior to the time you configured in the back office. If true, deletes old admin logs from the database.
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Cleaner
5
+ module DelaysHelper
6
+ def email_inactive_after(organization)
7
+ organization.delete_inactive_users_email_after || Decidim::Cleaner.delete_inactive_users_email_after
8
+ end
9
+
10
+ def delete_inactive_after(organization)
11
+ organization.delete_inactive_users_after || Decidim::Cleaner.delete_inactive_users_after
12
+ end
13
+ end
14
+ end
15
+ end
@@ -10,10 +10,16 @@ module Decidim
10
10
  next unless organization.delete_admin_logs?
11
11
 
12
12
  Decidim::ActionLog.where(organization: organization)
13
- .where("created_at < ?", Time.zone.now - (organization.delete_admin_logs_after || 365).days)
13
+ .where("created_at < ?", delete_admin_logs_before_date(organization))
14
14
  .delete_all
15
15
  end
16
16
  end
17
+
18
+ private
19
+
20
+ def delete_admin_logs_before_date(organization)
21
+ Time.zone.now - (organization.delete_admin_logs_after || Decidim::Cleaner.delete_admin_logs_after).days
22
+ end
17
23
  end
18
24
  end
19
25
  end
@@ -12,25 +12,32 @@ module Decidim
12
12
  send_warning(Decidim::User.where(organization: organization)
13
13
  .not_deleted
14
14
  .where.not(email: "")
15
- .where("last_sign_in_at < ?", Time.zone.now - (organization.delete_inactive_users_email_after || 365).days)
16
- .where("last_sign_in_at > ?", Time.zone.now - (organization.delete_inactive_users_email_after || 365).days - 1.day))
15
+ .where("current_sign_in_at < ?", email_inactive_before_date(organization)))
17
16
 
18
17
  delete_user_and_send_email(Decidim::User.where(organization: organization)
19
18
  .not_deleted
20
19
  .where.not(email: "")
21
- .where("last_sign_in_at < ?", Time.zone.now - (organization.delete_inactive_users_after || 390).days))
20
+ .where("warning_date < ?", delete_inactive_before_date(organization)))
22
21
  end
23
22
  end
24
23
 
25
24
  def send_warning(users)
26
25
  users.find_each do |user|
27
- InactiveUsersMailer.warning_inactive(user).deliver_now
26
+ next if user.warning_date.present?
27
+
28
+ user.update!(warning_date: Time.zone.now) if InactiveUsersMailer.warning_inactive(user).deliver_now
28
29
  Rails.logger.info "Inactive warning sent to #{user.email}"
29
30
  end
30
31
  end
31
32
 
32
33
  def delete_user_and_send_email(users)
33
34
  users.find_each do |user|
35
+ if user.current_sign_in_at > user.warning_date
36
+ user.update!(warning_date: nil)
37
+ Rails.logger.info "User with id #{user.id} has logged in again, warning date reset"
38
+ next
39
+ end
40
+
34
41
  InactiveUsersMailer.warning_deletion(user).deliver_now
35
42
  Rails.logger.info "Deletion warning sent to #{user.email}"
36
43
 
@@ -38,6 +45,16 @@ module Decidim
38
45
  Rails.logger.info "User with id #{user.id} destroyed"
39
46
  end
40
47
  end
48
+
49
+ private
50
+
51
+ def email_inactive_before_date(organization)
52
+ Time.zone.now - (organization.delete_inactive_users_email_after || Decidim::Cleaner.delete_inactive_users_email_after).days
53
+ end
54
+
55
+ def delete_inactive_before_date(organization)
56
+ Time.zone.now - (organization.delete_inactive_users_after || Decidim::Cleaner.delete_inactive_users_after).days
57
+ end
41
58
  end
42
59
  end
43
60
  end
@@ -5,6 +5,7 @@ module Decidim
5
5
  # A custom mailer for Decidim so we can notify users
6
6
  # when their account was blocked
7
7
  class InactiveUsersMailer < Decidim::ApplicationMailer
8
+ helper Decidim::Cleaner::DelaysHelper
8
9
  def warning_inactive(user)
9
10
  with_user(user) do
10
11
  @user = user
@@ -1,6 +1,6 @@
1
1
  <p class="email-greeting"><%= t ".hello" %></p>
2
2
 
3
- <p class="email-instructions"><%= t(".body_1", organization_name: h(@organization.name), organization_url: decidim.root_url(host: @organization.host), days: @organization.delete_inactive_users_after||390).html_safe %></p>
3
+ <p class="email-instructions"><%= t(".body_1", organization_name: h(@organization.name), organization_url: decidim.root_url(host: @organization.host), days: email_inactive_after(@organization) + delete_inactive_after(@organization)).html_safe %></p>
4
4
 
5
5
  <p class="email-instructions"><%= t ".body_2" %></p>
6
6
 
@@ -1,7 +1,7 @@
1
1
  <p class="email-greeting"><%= t ".hello" %></p>
2
2
 
3
- <p class="email-instructions"><%= t ".body_1", organization_name: h(@organization.name), days: @organization.delete_inactive_users_email_after %></p>
3
+ <p class="email-instructions"><%= t ".body_1", organization_name: h(@organization.name), days: email_inactive_after(@organization) %></p>
4
4
 
5
- <p class="email-instructions"><%= t(".body_2", remaining_days: (@organization.delete_inactive_users_after||390) - (@organization.delete_inactive_users_email_after||365), organization_url: decidim.root_url(host: @organization.host)).html_safe %></p>
5
+ <p class="email-instructions"><%= t(".body_2", remaining_days: delete_inactive_after(@organization), organization_url: decidim.root_url(host: @organization.host)).html_safe %></p>
6
6
 
7
7
  <p class="email-closing"><%= t(".greetings", organization_name: h(@organization.name), organization_url: decidim.root_url(host: @organization.host)).html_safe %></p>
@@ -6,7 +6,8 @@ en:
6
6
  delete_admin_logs: Enable admin logs deletion
7
7
  delete_admin_logs_after: Delete admin logs after (days, default 365)
8
8
  delete_inactive_users: Enable inactive users deletion
9
- delete_inactive_users_after: Delete inactive users after (days, default 390)
9
+ delete_inactive_users_after: Delete inactive users x days after the mail(days,
10
+ default 30)
10
11
  delete_inactive_users_email_after: Send email to inactive users before deletion
11
12
  (days, default 365)
12
13
  decidim:
@@ -7,8 +7,8 @@ fr:
7
7
  delete_admin_logs_after: Supprimer l'historique d'administration après (jours,
8
8
  par défaut 365)
9
9
  delete_inactive_users: Activer la suppression des utilisateurs inactifs
10
- delete_inactive_users_after: Supprimer les utilisateurs inactifs après (jours,
11
- par défaut 390)
10
+ delete_inactive_users_after: Supprimer les utilisateurs inactifs au bout de
11
+ x jours après le courriel (jours, par défaut 30)
12
12
  delete_inactive_users_email_after: Envoyer un courriel aux utilisateurs inactifs
13
13
  avant la suppression (jours, par défaut 365)
14
14
  decidim:
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddWarningDateToUsers < ActiveRecord::Migration[5.2]
4
+ def change
5
+ add_column :decidim_users, :warning_date, :datetime
6
+ end
7
+ end
@@ -5,7 +5,7 @@ module Decidim
5
5
  # This holds the decidim-meetings version.
6
6
  module Cleaner
7
7
  def self.version
8
- "2.0.0"
8
+ "2.1.1"
9
9
  end
10
10
 
11
11
  def self.compatible_decidim_version
@@ -7,5 +7,18 @@ require "decidim/cleaner/admin_engine"
7
7
  module Decidim
8
8
  # This namespace holds the logic of the `Cleaner` module.
9
9
  module Cleaner
10
+ include ActiveSupport::Configurable
11
+
12
+ config_accessor :delete_admin_logs_after do
13
+ ENV.fetch("DECIDIM_CLEANER_DELETE_ADMIN_LOGS", "365").to_i
14
+ end
15
+
16
+ config_accessor :delete_inactive_users_after do
17
+ ENV.fetch("DECIDIM_CLEANER_DELETE_INACTIVE_USERS", "30").to_i
18
+ end
19
+
20
+ config_accessor :delete_inactive_users_email_after do
21
+ ENV.fetch("DECIDIM_CLEANER_INACTIVE_USERS_MAIL", "365").to_i
22
+ end
10
23
  end
11
24
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decidim-cleaner
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Quentinchampenois
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-29 00:00:00.000000000 Z
11
+ date: 2023-05-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: decidim-core
@@ -38,6 +38,7 @@ files:
38
38
  - app/controllers/decidim/cleaner/admin/application_controller.rb
39
39
  - app/controllers/decidim/cleaner/admin/organization_cleaner_controller.rb
40
40
  - app/forms/decidim/cleaner/admin/organization_cleaner_form.rb
41
+ - app/helpers/decidim/cleaner/delays_helper.rb
41
42
  - app/jobs/decidim/cleaner/clean_admin_logs_job.rb
42
43
  - app/jobs/decidim/cleaner/clean_inactive_users_job.rb
43
44
  - app/mailers/decidim/cleaner/inactive_users_mailer.rb
@@ -52,6 +53,7 @@ files:
52
53
  - config/routes.rb
53
54
  - db/migrate/20230106105014_add_delete_admin_logs_to_organization.rb
54
55
  - db/migrate/20230110150032_add_delete_inactive_users_to_organization.rb
56
+ - db/migrate/20230328094652_add_warning_date_to_users.rb
55
57
  - lib/decidim/cleaner.rb
56
58
  - lib/decidim/cleaner/admin.rb
57
59
  - lib/decidim/cleaner/admin_engine.rb