Class: ResourceAccessTokens::RevokeService

Inherits:
BaseService
  • Object
show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
app/services/resource_access_tokens/revoke_service.rb

Constant Summary collapse

RevokeAccessTokenError =
Class.new(RuntimeError)

Instance Attribute Summary

Attributes inherited from BaseService

#params, #project

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?

Constructor Details

#initialize(current_user, resource, access_token) ⇒ RevokeService

Returns a new instance of RevokeService.


9
10
11
12
13
14
# File 'app/services/resource_access_tokens/revoke_service.rb', line 9

def initialize(current_user, resource, access_token)
  @current_user = current_user
  @access_token = access_token
  @bot_user = access_token.user
  @resource = resource
end

Instance Method Details

#executeObject


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/resource_access_tokens/revoke_service.rb', line 16

def execute
  return error("Failed to find bot user") unless find_member

  PersonalAccessToken.transaction do
    access_token.revoke!

    raise RevokeAccessTokenError, "Failed to remove #{bot_user.name} member from: #{resource.name}" unless remove_member

    raise RevokeAccessTokenError, "Migration to ghost user failed" unless migrate_to_ghost_user
  end

  success("Revoked access token: #{access_token.name}")
rescue ActiveRecord::ActiveRecordError, RevokeAccessTokenError => error
  log_error("Failed to revoke access token for #{bot_user.name}: #{error.message}")
  error(error.message)
end