Add action AsymmetricKeysController#create
This commit is contained in:
parent
08bcc41e30
commit
cefced198a
8 changed files with 62 additions and 3 deletions
|
@ -1,7 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class AsymmetricKeysController < ApplicationController
|
||||
before_action :set_asymmetric_key, except: %i[index new]
|
||||
before_action :set_asymmetric_key, except: %i[index new create]
|
||||
|
||||
# GET /asymmetric_keys
|
||||
def index
|
||||
|
@ -27,9 +27,31 @@ class AsymmetricKeysController < ApplicationController
|
|||
authorize @asymmetric_key_form
|
||||
end
|
||||
|
||||
# POST /asymmetric_keys
|
||||
def create
|
||||
@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
|
||||
|
||||
redirect_to after_create_url result.asymmetric_key
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_asymmetric_key
|
||||
@asymmetric_key = AsymmetricKey.find params[:id]
|
||||
end
|
||||
|
||||
def asymmetric_key_form_params
|
||||
params.require(:asymmetric_key).permit(
|
||||
:public_key_pem,
|
||||
)
|
||||
end
|
||||
|
||||
def after_create_url(asymmetric_key)
|
||||
asymmetric_key_url(asymmetric_key)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
class ApplicationForm
|
||||
include ActiveModel::Model
|
||||
include ActiveModel::Attributes
|
||||
include ActiveModel::Validations::Callbacks
|
||||
include ActiveRecord::AttributeAssignment
|
||||
|
||||
def has_attribute?(name)
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
# 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
|
||||
|
@ -12,4 +16,15 @@ 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
|
||||
|
|
7
app/interactors/import_asymmetric_key.rb
Normal file
7
app/interactors/import_asymmetric_key.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class ImportAsymmetricKey
|
||||
include Interactor
|
||||
|
||||
def call; end
|
||||
end
|
|
@ -9,7 +9,7 @@ class AsymmetricKeyPolicy < ApplicationPolicy
|
|||
true
|
||||
end
|
||||
|
||||
def new?
|
||||
def create?
|
||||
true
|
||||
end
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ Rails.application.routes.draw do
|
|||
|
||||
resources :federal_subjects, param: :number, only: %i[index show]
|
||||
|
||||
resources :asymmetric_keys, only: %i[index show new] do
|
||||
resources :asymmetric_keys, only: %i[index show new create] do
|
||||
resource :private_key, only: :show
|
||||
end
|
||||
|
||||
|
|
7
spec/interactors/import_asymmetric_key_spec.rb
Normal file
7
spec/interactors/import_asymmetric_key_spec.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe ImportAsymmetricKey do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
7
spec/requests/asymmetric_keys/create_spec.rb
Normal file
7
spec/requests/asymmetric_keys/create_spec.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'POST /asymmetric_keys' do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
Reference in a new issue