decidim-cleaner 1.0.1 → 2.1.0

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: c9fd35840bd986218c10726232aa523b811331ec05cb78d6ae52b0352e434862
4
- data.tar.gz: 2edf1e583c38516e4d7eefd51f4116cb883e4f823643c605df4f40ae4718684f
3
+ metadata.gz: 7667514845e0dddc19580aa0762d5218b93e1a24b75e5053856c8d1b7d748e0e
4
+ data.tar.gz: e39fbda595e2082802936da1a446b6e9a943d6dbb3c611dd2d67539c2a72e700
5
5
  SHA512:
6
- metadata.gz: e0c59579a5fe21080c697d6553b2983b21f242d62687c7fb7e700e86c5a01617762cdd8019a0b9258719d1534f5be29a00faa0580208ae92c31dc6fedb6d536f
7
- data.tar.gz: aca024679460c2a7fd0896e426c3b2de5f6aee923595b572bdfae1f2cb419090ae5a5dba6d898d4e38a744b67a0caff17b2ed2eb583bd883156cad4c063df912
6
+ metadata.gz: 334ed4a155cbafd585244e53d6e080145c046894411ef9bec85d9fab00d9d9e89f8a7fc75b30c8eeef761a913183a3474543108b1e5ff6bbb624ab9a0dfbdad0
7
+ data.tar.gz: 76f39a3e96f7a152efc8420c2b352825f3d544826a7dac395f1f7d2025e82b3e81412b6b6144d93342f0c799c825a1f52c89548761362a45aa2306e511e56ca9
data/README.md CHANGED
@@ -22,6 +22,24 @@ 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
+
38
+ ### Sidekiq Scheduler
39
+ [Further documentation](https://github.com/sidekiq-scheduler/sidekiq-scheduler)
40
+
41
+ **Sidekiq scheduler uses a 6 columns format**
42
+
25
43
  You can then add to your 'config/sidekiq.yml' file:
26
44
 
27
45
  ```yaml
@@ -36,6 +54,15 @@ You can then add to your 'config/sidekiq.yml' file:
36
54
  queue: scheduled
37
55
  ```
38
56
 
57
+ ### Cronjob
58
+ ```
59
+ # Warns and deletes inactive users
60
+ 0 9 * * * cd /home/user/decidim_application && RAILS_ENV=production bundle exec rake decidim_cleaner:clean_inactive_users
61
+
62
+ # Deletes old admin logs
63
+ 0 9 * * * cd /home/user/decidim_application && RAILS_ENV=production bundle exec rake decidim_cleaner:clean_admin_logs
64
+ ```
65
+
39
66
  ## Available tasks
40
67
 
41
68
  - [ ] **Delete inactive users**
@@ -5,7 +5,7 @@ module Decidim
5
5
  module Admin
6
6
  # A command with all the business logic for updating the current
7
7
  # organization cleaner.
8
- class UpdateOrganizationCleaner < Decidim::Command
8
+ class UpdateOrganizationCleaner < Rectify::Command
9
9
  # Public: Initializes the command.
10
10
  #
11
11
  # organization - The Organization that will be updated.
@@ -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
@@ -10,25 +10,32 @@ module Decidim
10
10
  next unless organization.delete_inactive_users?
11
11
 
12
12
  send_warning(Decidim::User.where(organization: organization)
13
- .where("last_sign_in_at < ?", Time.zone.now - (organization.delete_inactive_users_email_after || 365).days)
14
- .where("last_sign_in_at > ?", Time.zone.now - (organization.delete_inactive_users_email_after || 365).days - 1.day))
13
+ .not_deleted
14
+ .where.not(email: "")
15
+ .where("last_sign_in_at < ?", email_inactive_before_date(organization)))
15
16
  delete_user_and_send_email(Decidim::User.where(organization: organization)
16
- .where("last_sign_in_at < ?", Time.zone.now - (organization.delete_inactive_users_after || 390).days))
17
+ .not_deleted
18
+ .where.not(email: "")
19
+ .where("warning_date < ?", delete_inactive_before_date(organization)))
17
20
  end
18
21
  end
19
22
 
20
23
  def send_warning(users)
21
24
  users.find_each do |user|
22
- next if user.deleted?
25
+ next if user.warning_date.present?
23
26
 
24
- InactiveUsersMailer.warning_inactive(user).deliver_now
27
+ user.update!(warning_date: Time.zone.now) if InactiveUsersMailer.warning_inactive(user).deliver_now
25
28
  Rails.logger.info "Inactive warning sent to #{user.email}"
26
29
  end
27
30
  end
28
31
 
29
32
  def delete_user_and_send_email(users)
30
33
  users.find_each do |user|
31
- next if user.deleted?
34
+ if user.last_sign_in_at > user.warning_date
35
+ user.update!(warning_date: nil)
36
+ Rails.logger.info "User with id #{user.id} has logged in again, warning date reset"
37
+ next
38
+ end
32
39
 
33
40
  InactiveUsersMailer.warning_deletion(user).deliver_now
34
41
  Rails.logger.info "Deletion warning sent to #{user.email}"
@@ -37,6 +44,16 @@ module Decidim
37
44
  Rails.logger.info "User with id #{user.id} destroyed"
38
45
  end
39
46
  end
47
+
48
+ private
49
+
50
+ def email_inactive_before_date(organization)
51
+ Time.zone.now - (organization.delete_inactive_users_email_after || Decidim::Cleaner.delete_inactive_users_email_after).days
52
+ end
53
+
54
+ def delete_inactive_before_date(organization)
55
+ Time.zone.now - (organization.delete_inactive_users_after || Decidim::Cleaner.delete_inactive_users_after).days
56
+ end
40
57
  end
41
58
  end
42
59
  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:
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class AddDeleteAdminLogsToOrganization < ActiveRecord::Migration[6.1]
3
+ class AddDeleteAdminLogsToOrganization < ActiveRecord::Migration[5.2]
4
4
  def change
5
5
  add_column :decidim_organizations, :delete_admin_logs, :boolean, default: false, null: false
6
6
  add_column :decidim_organizations, :delete_admin_logs_after, :integer
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class AddDeleteInactiveUsersToOrganization < ActiveRecord::Migration[6.1]
3
+ class AddDeleteInactiveUsersToOrganization < ActiveRecord::Migration[5.2]
4
4
  def change
5
5
  add_column :decidim_organizations, :delete_inactive_users, :boolean, default: false, null: false
6
6
  add_column :decidim_organizations, :delete_inactive_users_email_after, :integer
@@ -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,11 +5,11 @@ module Decidim
5
5
  # This holds the decidim-meetings version.
6
6
  module Cleaner
7
7
  def self.version
8
- "1.0.1"
8
+ "2.1.0"
9
9
  end
10
10
 
11
- def self.decidim_version
12
- "0.26"
11
+ def self.compatible_decidim_version
12
+ "0.26.0"
13
13
  end
14
14
  end
15
15
  end
@@ -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: 1.0.1
4
+ version: 2.1.0
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-09 00:00:00.000000000 Z
11
+ date: 2023-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: decidim-core
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.26'
19
+ version: 0.26.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.26'
26
+ version: 0.26.0
27
27
  description: Clean outdated data in Decidim's database.
28
28
  email:
29
29
  - 26109239+Quentinchampenois@users.noreply.github.com
@@ -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