Class: PersonalAccessTokens::CreateService

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/personal_access_tokens/create_service.rb

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

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:, target_user:, params: {}) ⇒ CreateService

Returns a new instance of CreateService.


5
6
7
8
9
10
# File 'app/services/personal_access_tokens/create_service.rb', line 5

def initialize(current_user:, target_user:, params: {})
  @current_user = current_user
  @target_user = target_user
  @params = params.dup
  @ip_address = @params.delete(:ip_address)
end

Instance Method Details

#executeObject


12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/services/personal_access_tokens/create_service.rb', line 12

def execute
  return ServiceResponse.error(message: 'Not permitted to create') unless creation_permitted?

  token = target_user.personal_access_tokens.create(params.slice(*allowed_params))

  if token.persisted?
    log_event(token)
    notification_service.access_token_created(target_user, token.name)
    ServiceResponse.success(payload: { personal_access_token: token })
  else
    ServiceResponse.error(message: token.errors.full_messages.to_sentence, payload: { personal_access_token: token })
  end
end