Use interactor validation
This commit is contained in:
parent
ec1d9c216c
commit
557e892e44
|
@ -32,10 +32,13 @@ class AsymmetricKeysController < ApplicationController
|
|||
@asymmetric_key_form = AsymmetricKeyForm.new asymmetric_key_form_params
|
||||
authorize @asymmetric_key_form
|
||||
|
||||
return render :new unless @asymmetric_key_form.valid?
|
||||
|
||||
result = ImportAsymmetricKey.call @asymmetric_key_form.attributes
|
||||
|
||||
if result.failure?
|
||||
@asymmetric_key_form.errors.add :public_key_pem
|
||||
return render :new
|
||||
end
|
||||
|
||||
redirect_to after_create_url result.asymmetric_key
|
||||
end
|
||||
|
||||
|
|
|
@ -1,14 +1,8 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class AsymmetricKeyForm < ApplicationForm
|
||||
attr_reader :public_key_openssl_pkey
|
||||
|
||||
attribute :public_key_pem, :string
|
||||
|
||||
before_validation :set_public_key_openssl_pkey
|
||||
|
||||
validates :public_key_pem, presence: true
|
||||
|
||||
def self.model_name
|
||||
ActiveModel::Name.new(self, nil, AsymmetricKey.name)
|
||||
end
|
||||
|
@ -16,15 +10,4 @@ class AsymmetricKeyForm < ApplicationForm
|
|||
def self.policy_class
|
||||
'AsymmetricKeyPolicy'
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_public_key_openssl_pkey
|
||||
return @public_key_openssl_pkey = nil if public_key_pem.blank?
|
||||
|
||||
@public_key_openssl_pkey = OpenSSL::PKey.read public_key_pem
|
||||
rescue OpenSSL::OpenSSLError
|
||||
@public_key_openssl_pkey = nil
|
||||
errors.add :public_key_pem
|
||||
end
|
||||
end
|
||||
|
|
Reference in New Issue