summaryrefslogtreecommitdiff
path: root/app/services/clusters/agent_tokens/revoke_service.rb
blob: 5d89b4059696c3b916d15ef06726dbaa0a1798ad (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
40
41
42
43
44
45
46
# frozen_string_literal: true

module Clusters
  module AgentTokens
    class RevokeService
      attr_reader :current_project, :current_user, :token

      def initialize(token:, current_user:)
        @token = token
        @current_user = current_user
      end

      def execute
        return error_no_permissions unless current_user.can?(:create_cluster, token.agent.project)

        if token.update(status: token.class.statuses[:revoked])
          log_activity_event(token)

          ServiceResponse.success
        else
          ServiceResponse.error(message: token.errors.full_messages)
        end
      end

      private

      def error_no_permissions
        ServiceResponse.error(
          message: s_('ClusterAgent|User has insufficient permissions to revoke the token for this project'))
      end

      def log_activity_event(token)
        Clusters::Agents::CreateActivityEventService.new(
          token.agent,
          kind: :token_revoked,
          level: :info,
          recorded_at: token.updated_at,
          user: current_user,
          agent_token: token
        ).execute
      end
    end
  end
end

Clusters::AgentTokens::RevokeService.prepend_mod