Project

General

Profile

Lock inactive users in Group

Added by Mohamed El Habchi about 1 year ago

I'm trying to create a rake file which will be linked to crontab, and will have the ability to lock users which are inactive for 90 days in the group ID 600, but something is not correct in the file

namespace :redmine do
  desc "Lock inactive users" 
  task lock_inactive_users: :environment do
    inactive_users = User.joins(:groups)
                         .where('groups.id = ?', 600)
                         .where(type: 'User')
                         .where('last_login_on < ?', 90.days.ago)

    inactive_users.find_each(batch_size: 500) do |user|
      user.update(status: User::STATUS_LOCKED)
      puts "User #{user.login} (ID: #{user.id}) has been locked." 
    end
  end
end