From 2e86acfa8742651c47fe5719c6d458299a428003 Mon Sep 17 00:00:00 2001 From: Go MAEDA Date: Tue, 22 Mar 2022 04:02:39 +0000 Subject: [PATCH] Add tasks to prune registered users after a certain number of days (#30998). Patch by Yuichi HARADA and Marius BALTEANU. git-svn-id: https://svn.redmine.org/redmine/trunk@21490 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/user.rb | 4 ++++ lib/tasks/redmine.rake | 16 ++++++++++++++++ test/unit/user_test.rb | 10 ++++++++++ 3 files changed, 30 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index ffe147ba1..f898f9460 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -886,6 +886,10 @@ class User < Principal project_ids.map(&:to_i) end + def self.prune(age) + User.where("created_on < ? AND status = ?", Time.now - age, STATUS_REGISTERED).destroy_all + end + protected def validate_password_length diff --git a/lib/tasks/redmine.rake b/lib/tasks/redmine.rake index 112a9c5d3..83740c86a 100644 --- a/lib/tasks/redmine.rake +++ b/lib/tasks/redmine.rake @@ -40,6 +40,22 @@ namespace :redmine do end end + namespace :users do + desc 'Removes registered users that have not been activated after a number of days. Use DAYS to set the number of days, defaults to 30 days.' + task :prune => :environment do + days = 30 + env_days = ENV['DAYS'] + if env_days + if env_days.to_i <= 0 + abort "Invalid DAYS #{env_days} given. The value must be a integer." + else + days = env_days.to_i + end + end + User.prune(days.days) + end + end + namespace :watchers do desc 'Removes watchers from what they can no longer view.' task :prune => :environment do diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index 9e2b9be1a..dde1cdd62 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -1348,4 +1348,14 @@ class UserTest < ActiveSupport::TestCase cv2a.reload assert_equal @dlopper.id.to_s, cv2a.value end + + def test_prune_should_destroy_unactivated_old_users + User.generate!(:status => User::STATUS_REGISTERED, :created_on => 6.days.ago) + User.generate!(:status => User::STATUS_REGISTERED, :created_on => 7.days.ago) + User.generate!(:status => User::STATUS_REGISTERED) + + assert_difference 'User.count', -2 do + User.prune(7) + end + end end -- 2.39.5