2020-10-24 23:57:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module PersonalAccessTokens
|
2022-11-25 23:54:43 +05:30
|
|
|
class RevokeService < BaseService
|
2021-01-03 14:25:43 +05:30
|
|
|
attr_reader :token, :current_user, :group
|
2020-10-24 23:57:45 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
def initialize(current_user = nil, token: nil, group: nil)
|
2020-10-24 23:57:45 +05:30
|
|
|
@current_user = current_user
|
2021-01-29 00:20:46 +05:30
|
|
|
@token = token
|
|
|
|
@group = group
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
return ServiceResponse.error(message: 'Not permitted to revoke') unless revocation_permitted?
|
|
|
|
|
|
|
|
if token.revoke!
|
2021-01-29 00:20:46 +05:30
|
|
|
log_event
|
2022-11-25 23:54:43 +05:30
|
|
|
notification_service.access_token_revoked(token.user, token.name)
|
2020-10-24 23:57:45 +05:30
|
|
|
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
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
def log_event
|
|
|
|
Gitlab::AppLogger.info("PAT REVOCATION: revoked_by: '#{current_user.username}', revoked_for: '#{token.user.username}', token_id: '#{token.id}'")
|
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
PersonalAccessTokens::RevokeService.prepend_mod_with('PersonalAccessTokens::RevokeService')
|