2020-06-03 05:08:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ServiceFieldEntity < Grape::Entity
|
|
|
|
include RequestAwareEntity
|
2021-03-25 08:09:19 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2020-06-03 05:08:47 -04:00
|
|
|
|
2021-10-20 14:12:31 -04:00
|
|
|
expose :type, :name, :placeholder, :required, :choices, :checkbox_label
|
2021-03-25 08:09:19 -04:00
|
|
|
|
|
|
|
expose :title do |field|
|
|
|
|
non_empty_password?(field) ? field[:non_empty_password_title] : field[:title]
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :help do |field|
|
|
|
|
non_empty_password?(field) ? field[:non_empty_password_help] : field[:help]
|
|
|
|
end
|
2020-06-03 05:08:47 -04:00
|
|
|
|
|
|
|
expose :value do |field|
|
2021-03-25 08:09:19 -04:00
|
|
|
value = value_for(field)
|
2020-06-03 05:08:47 -04:00
|
|
|
|
2021-03-25 08:09:19 -04:00
|
|
|
if non_empty_password?(field)
|
2020-06-03 05:08:47 -04:00
|
|
|
'true'
|
2020-07-14 20:09:23 -04:00
|
|
|
elsif field[:type] == 'checkbox'
|
|
|
|
ActiveRecord::Type::Boolean.new.deserialize(value).to_s
|
2020-06-03 05:08:47 -04:00
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def service
|
|
|
|
request.service
|
|
|
|
end
|
2021-03-25 08:09:19 -04:00
|
|
|
|
|
|
|
def value_for(field)
|
|
|
|
strong_memoize(:value_for) do
|
|
|
|
# field[:name] is not user input and so can assume is safe
|
|
|
|
service.public_send(field[:name]) # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def non_empty_password?(field)
|
|
|
|
strong_memoize(:non_empty_password) do
|
|
|
|
field[:type] == 'password' && value_for(field).present?
|
|
|
|
end
|
|
|
|
end
|
2020-06-03 05:08:47 -04:00
|
|
|
end
|