2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-12-13 04:32:14 +08:00
|
|
|
module Jobs
|
|
|
|
|
2019-10-02 12:01:53 +08:00
|
|
|
class InvalidateInactiveAdmins < ::Jobs::Scheduled
|
2018-12-13 04:32:14 +08:00
|
|
|
every 1.day
|
|
|
|
|
|
|
|
def execute(_)
|
|
|
|
return if SiteSetting.invalidate_inactive_admin_email_after_days == 0
|
|
|
|
|
2019-11-13 00:56:01 +08:00
|
|
|
timestamp = SiteSetting.invalidate_inactive_admin_email_after_days.days.ago
|
|
|
|
|
2018-12-13 04:32:14 +08:00
|
|
|
User.human_users
|
|
|
|
.where(admin: true)
|
2018-12-13 11:59:56 +08:00
|
|
|
.where(active: true)
|
2019-11-13 00:56:01 +08:00
|
|
|
.where('last_seen_at < ?', timestamp)
|
|
|
|
.where("NOT EXISTS ( SELECT 1 from api_keys WHERE api_keys.user_id = users.id AND COALESCE(last_used_at, updated_at) > ? )", timestamp)
|
|
|
|
.where("NOT EXISTS ( SELECT 1 from posts WHERE posts.user_id = users.id AND created_at > ?)", timestamp)
|
2018-12-13 04:32:14 +08:00
|
|
|
.each do |user|
|
|
|
|
|
2018-12-13 11:59:56 +08:00
|
|
|
User.transaction do
|
2019-04-04 00:04:05 +08:00
|
|
|
user.deactivate(Discourse.system_user)
|
2018-12-13 11:59:56 +08:00
|
|
|
user.email_tokens.update_all(confirmed: false, expired: true)
|
2018-12-13 04:32:14 +08:00
|
|
|
|
2020-01-28 20:16:24 +08:00
|
|
|
reason = I18n.t("user.deactivated_by_inactivity", count: SiteSetting.invalidate_inactive_admin_email_after_days)
|
|
|
|
StaffActionLogger.new(Discourse.system_user).log_user_deactivate(user, reason)
|
|
|
|
|
2018-12-13 11:59:56 +08:00
|
|
|
Discourse.authenticators.each do |authenticator|
|
|
|
|
if authenticator.can_revoke? && authenticator.description_for_user(user).present?
|
|
|
|
authenticator.revoke(user)
|
|
|
|
end
|
2018-12-13 04:32:14 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|