1
0
Fork 0

Add action AsymmetricKeysController#create

This commit is contained in:
Alex Kotov 2019-09-15 14:31:47 +05:00
parent 08bcc41e30
commit cefced198a
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
8 changed files with 62 additions and 3 deletions

View file

@ -1,7 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
class AsymmetricKeysController < ApplicationController 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 # GET /asymmetric_keys
def index def index
@ -27,9 +27,31 @@ class AsymmetricKeysController < ApplicationController
authorize @asymmetric_key_form authorize @asymmetric_key_form
end 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 private
def set_asymmetric_key def set_asymmetric_key
@asymmetric_key = AsymmetricKey.find params[:id] @asymmetric_key = AsymmetricKey.find params[:id]
end 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 end

View file

@ -3,6 +3,7 @@
class ApplicationForm class ApplicationForm
include ActiveModel::Model include ActiveModel::Model
include ActiveModel::Attributes include ActiveModel::Attributes
include ActiveModel::Validations::Callbacks
include ActiveRecord::AttributeAssignment include ActiveRecord::AttributeAssignment
def has_attribute?(name) def has_attribute?(name)

View file

@ -1,8 +1,12 @@
# frozen_string_literal: true # frozen_string_literal: true
class AsymmetricKeyForm < ApplicationForm class AsymmetricKeyForm < ApplicationForm
attr_reader :public_key_openssl_pkey
attribute :public_key_pem, :string attribute :public_key_pem, :string
before_validation :set_public_key_openssl_pkey
validates :public_key_pem, presence: true validates :public_key_pem, presence: true
def self.model_name def self.model_name
@ -12,4 +16,15 @@ class AsymmetricKeyForm < ApplicationForm
def self.policy_class def self.policy_class
'AsymmetricKeyPolicy' 'AsymmetricKeyPolicy'
end 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 end

View file

@ -0,0 +1,7 @@
# frozen_string_literal: true
class ImportAsymmetricKey
include Interactor
def call; end
end

View file

@ -9,7 +9,7 @@ class AsymmetricKeyPolicy < ApplicationPolicy
true true
end end
def new? def create?
true true
end end

View file

@ -13,7 +13,7 @@ Rails.application.routes.draw do
resources :federal_subjects, param: :number, only: %i[index show] 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 resource :private_key, only: :show
end end

View 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

View 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