From cefced198a6e255979dc9cff1df7df0a61c22de8 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sun, 15 Sep 2019 14:31:47 +0500 Subject: [PATCH] Add action AsymmetricKeysController#create --- app/controllers/asymmetric_keys_controller.rb | 24 ++++++++++++++++++- app/forms/application_form.rb | 1 + app/forms/asymmetric_key_form.rb | 15 ++++++++++++ app/interactors/import_asymmetric_key.rb | 7 ++++++ app/policies/asymmetric_key_policy.rb | 2 +- config/routes.rb | 2 +- .../interactors/import_asymmetric_key_spec.rb | 7 ++++++ spec/requests/asymmetric_keys/create_spec.rb | 7 ++++++ 8 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 app/interactors/import_asymmetric_key.rb create mode 100644 spec/interactors/import_asymmetric_key_spec.rb create mode 100644 spec/requests/asymmetric_keys/create_spec.rb diff --git a/app/controllers/asymmetric_keys_controller.rb b/app/controllers/asymmetric_keys_controller.rb index 3c102c3..279bb28 100644 --- a/app/controllers/asymmetric_keys_controller.rb +++ b/app/controllers/asymmetric_keys_controller.rb @@ -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 diff --git a/app/forms/application_form.rb b/app/forms/application_form.rb index a219e15..f1f54ce 100644 --- a/app/forms/application_form.rb +++ b/app/forms/application_form.rb @@ -3,6 +3,7 @@ class ApplicationForm include ActiveModel::Model include ActiveModel::Attributes + include ActiveModel::Validations::Callbacks include ActiveRecord::AttributeAssignment def has_attribute?(name) diff --git a/app/forms/asymmetric_key_form.rb b/app/forms/asymmetric_key_form.rb index e87704c..a62027d 100644 --- a/app/forms/asymmetric_key_form.rb +++ b/app/forms/asymmetric_key_form.rb @@ -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 diff --git a/app/interactors/import_asymmetric_key.rb b/app/interactors/import_asymmetric_key.rb new file mode 100644 index 0000000..fb55def --- /dev/null +++ b/app/interactors/import_asymmetric_key.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class ImportAsymmetricKey + include Interactor + + def call; end +end diff --git a/app/policies/asymmetric_key_policy.rb b/app/policies/asymmetric_key_policy.rb index 199ba90..c9a766c 100644 --- a/app/policies/asymmetric_key_policy.rb +++ b/app/policies/asymmetric_key_policy.rb @@ -9,7 +9,7 @@ class AsymmetricKeyPolicy < ApplicationPolicy true end - def new? + def create? true end diff --git a/config/routes.rb b/config/routes.rb index 7a62f26..44c6783 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/interactors/import_asymmetric_key_spec.rb b/spec/interactors/import_asymmetric_key_spec.rb new file mode 100644 index 0000000..28c44ca --- /dev/null +++ b/spec/interactors/import_asymmetric_key_spec.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe ImportAsymmetricKey do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/requests/asymmetric_keys/create_spec.rb b/spec/requests/asymmetric_keys/create_spec.rb new file mode 100644 index 0000000..58d975a --- /dev/null +++ b/spec/requests/asymmetric_keys/create_spec.rb @@ -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