1
0
mirror of https://github.com/meineerde/redmine.git synced 2026-02-05 00:23:24 +00:00

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
This commit is contained in:
Go MAEDA 2022-03-22 04:02:39 +00:00
parent d8d0f38a6b
commit 2e86acfa87
3 changed files with 30 additions and 0 deletions

View 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

View 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

View 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