2020-08-07 11:10:17 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module PersonalAccessTokens
|
|
|
|
class RevokeService
|
2020-10-14 02:08:33 -04:00
|
|
|
attr_reader :token, :current_user, :group
|
2020-08-07 11:10:17 -04:00
|
|
|
|
2020-11-09 07:09:24 -05:00
|
|
|
def initialize(current_user = nil, token: nil, group: nil )
|
2020-08-07 11:10:17 -04:00
|
|
|
@current_user = current_user
|
2020-11-09 07:09:24 -05:00
|
|
|
@token = token
|
|
|
|
@group = group
|
2020-08-07 11:10:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
return ServiceResponse.error(message: 'Not permitted to revoke') unless revocation_permitted?
|
|
|
|
|
|
|
|
if token.revoke!
|
2020-11-09 07:09:24 -05:00
|
|
|
log_event
|
2020-08-07 11:10:17 -04:00
|
|
|
ServiceResponse.success(message: success_message)
|
|
|
|
else
|
|
|
|
ServiceResponse.error(message: error_message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def error_message
|
|
|
|
_("Could not revoke personal access token %{personal_access_token_name}.") % { personal_access_token_name: token.name }
|
|
|
|
end
|
|
|
|
|
|
|
|
def success_message
|
|
|
|
_("Revoked personal access token %{personal_access_token_name}!") % { personal_access_token_name: token.name }
|
|
|
|
end
|
|
|
|
|
|
|
|
def revocation_permitted?
|
|
|
|
Ability.allowed?(current_user, :revoke_token, token)
|
|
|
|
end
|
2020-11-09 07:09:24 -05:00
|
|
|
|
|
|
|
def log_event
|
|
|
|
Gitlab::AppLogger.info("PAT REVOCATION: revoked_by: '#{current_user.username}', revoked_for: '#{token.user.username}', token_id: '#{token.id}'")
|
|
|
|
end
|
2020-08-07 11:10:17 -04:00
|
|
|
end
|
|
|
|
end
|
2020-10-14 02:08:33 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
PersonalAccessTokens::RevokeService.prepend_mod_with('PersonalAccessTokens::RevokeService')
|