debian-mirror-gitlab/app/services/personal_access_tokens/revoke_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

45 lines
1.2 KiB
Ruby
Raw Normal View History

2020-10-24 23:57:45 +05:30
# frozen_string_literal: true
module PersonalAccessTokens
class RevokeService
2021-01-03 14:25:43 +05:30
attr_reader :token, :current_user, :group
2020-10-24 23:57:45 +05:30
2021-01-29 00:20:46 +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
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')