2019-12-10 07:53:40 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module PersonalAccessTokens
|
2020-02-19 18:09:10 +00:00
|
|
|
class ExpiringWorker # rubocop:disable Scalability/IdempotentWorker
|
2019-12-10 07:53:40 +00:00
|
|
|
include ApplicationWorker
|
2021-04-30 18:10:09 +00:00
|
|
|
|
|
|
|
sidekiq_options retry: 3
|
2020-02-09 21:09:08 +00:00
|
|
|
include CronjobQueue
|
2019-12-10 07:53:40 +00:00
|
|
|
|
|
|
|
feature_category :authentication_and_authorization
|
|
|
|
|
2021-02-25 12:11:01 +00:00
|
|
|
MAX_TOKENS = 100
|
|
|
|
|
2019-12-10 07:53:40 +00:00
|
|
|
def perform(*args)
|
|
|
|
notification_service = NotificationService.new
|
|
|
|
limit_date = PersonalAccessToken::DAYS_TO_EXPIRE.days.from_now.to_date
|
|
|
|
|
|
|
|
User.with_expiring_and_not_notified_personal_access_tokens(limit_date).find_each do |user|
|
2020-02-09 21:09:08 +00:00
|
|
|
with_context(user: user) do
|
2021-02-25 12:11:01 +00:00
|
|
|
expiring_user_tokens = user.personal_access_tokens.without_impersonation.expiring_and_not_notified(limit_date)
|
|
|
|
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
# We never materialise the token instances. We need the names to mention them in the
|
|
|
|
# email. Later we trigger an update query on the entire relation, not on individual instances.
|
|
|
|
token_names = expiring_user_tokens.limit(MAX_TOKENS).pluck(:name)
|
|
|
|
# We're limiting to 100 tokens so we avoid loading too many tokens into memory.
|
|
|
|
# At the time of writing this would only affect 69 users on GitLab.com
|
|
|
|
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
|
|
|
notification_service.access_token_about_to_expire(user, token_names)
|
2019-12-10 07:53:40 +00:00
|
|
|
|
2020-05-27 21:08:05 +00:00
|
|
|
Gitlab::AppLogger.info "#{self.class}: Notifying User #{user.id} about expiring tokens"
|
2019-12-10 07:53:40 +00:00
|
|
|
|
2021-02-25 12:11:01 +00:00
|
|
|
expiring_user_tokens.each_batch do |expiring_tokens|
|
|
|
|
expiring_tokens.update_all(expire_notification_delivered: true)
|
|
|
|
end
|
2020-02-09 21:09:08 +00:00
|
|
|
end
|
2019-12-10 07:53:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|