2020-06-23 00:09:42 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ServiceFieldEntity < Grape::Entity
|
|
|
|
include RequestAwareEntity
|
2021-04-29 21:17:54 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
expose :type, :name, :placeholder, :required, :choices
|
|
|
|
|
|
|
|
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-23 00:09:42 +05:30
|
|
|
|
|
|
|
expose :value do |field|
|
2021-04-29 21:17:54 +05:30
|
|
|
value = value_for(field)
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
if non_empty_password?(field)
|
2020-06-23 00:09:42 +05:30
|
|
|
'true'
|
2020-07-28 23:09:34 +05:30
|
|
|
elsif field[:type] == 'checkbox'
|
|
|
|
ActiveRecord::Type::Boolean.new.deserialize(value).to_s
|
2020-06-23 00:09:42 +05:30
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def service
|
|
|
|
request.service
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
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-23 00:09:42 +05:30
|
|
|
end
|