Class: Groups::SshCertificates::CreateService

Inherits:
Object
  • Object
show all
Defined in:
app/services/groups/ssh_certificates/create_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(group, params) ⇒ CreateService

Returns a new instance of CreateService.



6
7
8
9
# File 'app/services/groups/ssh_certificates/create_service.rb', line 6

def initialize(group, params)
  @group = group
  @params = params
end

Instance Method Details

#executeObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/services/groups/ssh_certificates/create_service.rb', line 11

def execute
  key = params[:key]
  fingerprint = generate_fingerprint(key)

  return ServiceResponse.error(message: 'Group', reason: :forbidden) if group.has_parent?

  # return a key error instead of fingerprint error, as the user has no knowledge of fingerprint.
  unless fingerprint
    return ServiceResponse.error(message: 'Validation failed: Invalid key',
      reason: :unprocessable_entity)
  end

  result = group.ssh_certificates.create!(
    key: key,
    title: params[:title],
    fingerprint: fingerprint
  )

  # title and key attributes are returned as [FILTERED]
  # by config/application.rb#L181-233
  # make attributes unfiltered by running find
  ssh_certificate = group.ssh_certificates.find(result.id)
  ServiceResponse.success(payload: ssh_certificate)

rescue ActiveRecord::RecordInvalid, ArgumentError => e
  ServiceResponse.error(
    message: e.message,
    reason: :unprocessable_entity
  )
end