summaryrefslogtreecommitdiff
path: root/app/workers/personal_access_tokens/expired_notification_worker.rb
blob: b119957fa2c78c9107ec9a4b2c00f9de0ce64613 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# frozen_string_literal: true

module PersonalAccessTokens
  class ExpiredNotificationWorker # rubocop:disable Scalability/IdempotentWorker
    include ApplicationWorker

    data_consistency :always

    include CronjobQueue

    feature_category :authentication_and_authorization

    MAX_TOKENS = 100

    def perform(*args)
      notification_service = NotificationService.new

      User.with_personal_access_tokens_expired_today.find_each do |user|
        with_context(user: user) do
          expiring_user_tokens = user.personal_access_tokens.without_impersonation.expired_today_and_not_notified

          # 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)
          # rubocop: enable CodeReuse/ActiveRecord

          notification_service.access_token_expired(user, token_names)

          Gitlab::AppLogger.info "#{self.class}: Notifying User #{user.id} about expired tokens"

          expiring_user_tokens.each_batch do |expiring_tokens|
            expiring_tokens.update_all(after_expiry_notification_delivered: true)
          end
        end
      end
    end
  end
end