]> source.dussan.org Git - redmine.git/commitdiff
Add tasks to prune registered users after a certain number of days (#30998).
authorGo MAEDA <maeda@farend.jp>
Tue, 22 Mar 2022 04:02:39 +0000 (04:02 +0000)
committerGo MAEDA <maeda@farend.jp>
Tue, 22 Mar 2022 04:02:39 +0000 (04:02 +0000)
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
lib/tasks/redmine.rake
test/unit/user_test.rb

index ffe147ba1c7d77c991c19560158ef427f24546a1..f898f9460c92459e9b8037d9f9673d22f6996f28 100644 (file)
@@ -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
index 112a9c5d34519c9871c17d9c01b8af099dcb8e1d..83740c86aca9761439d7c3b740cee77d46ffd23f 100644 (file)
@@ -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
index 9e2b9be1a665fb58b03f344d3d003efd1ac280e1..dde1cdd6226cb89f4d8e2d7ff735c62c5bbff9b4 100644 (file)
@@ -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