Add action AsymmentricKeysController#show
This commit is contained in:
parent
9660c47381
commit
1fac8e634e
10 changed files with 134 additions and 3 deletions
|
@ -1,9 +1,22 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class AsymmetricKeysController < ApplicationController
|
||||
before_action :set_asymmetric_key, except: :index
|
||||
|
||||
# GET /public_keys
|
||||
def index
|
||||
authorize AsymmetricKey
|
||||
@asymmetric_keys = policy_scope(AsymmetricKey).page(params[:page])
|
||||
end
|
||||
|
||||
# GET /public_keys/:id
|
||||
def show
|
||||
authorize @asymmetric_key
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_asymmetric_key
|
||||
@asymmetric_key = AsymmetricKey.find params[:id]
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module ApplicationHelper
|
||||
module ApplicationHelper # rubocop:disable Metrics/ModuleLength
|
||||
def federal_subjects_controller?
|
||||
controller_path == 'federal_subjects'
|
||||
end
|
||||
|
@ -13,6 +13,34 @@ module ApplicationHelper
|
|||
[*negative_timezones_collection, *positive_timezones_collection].freeze
|
||||
end
|
||||
|
||||
def display_sha1(str)
|
||||
str = String(str).upcase
|
||||
raise 'Invalid format for SHA-1' unless str.match?(/\A[A-F0-9]{40}\z/)
|
||||
|
||||
tag.small do
|
||||
concat display_fingerprint str[0...20]
|
||||
concat tag.br
|
||||
concat display_fingerprint str[20..-1]
|
||||
end
|
||||
end
|
||||
|
||||
def display_sha256(str)
|
||||
str = String(str).upcase
|
||||
raise 'Invalid format for SHA-256' unless str.match?(/\A[A-F0-9]{64}\z/)
|
||||
|
||||
tag.small do
|
||||
concat display_fingerprint str[0...32]
|
||||
concat tag.br
|
||||
concat display_fingerprint str[32..-1]
|
||||
end
|
||||
end
|
||||
|
||||
def display_fingerprint(str)
|
||||
tag.samp do
|
||||
String(str).strip.upcase.each_char.each_slice(2).map(&:join).join(':')
|
||||
end
|
||||
end
|
||||
|
||||
def positive_timezones_collection
|
||||
0.upto(11).flat_map do |n|
|
||||
s = n.to_s.rjust(2, '0')
|
||||
|
|
|
@ -44,6 +44,10 @@ class AsymmetricKey < ApplicationRecord
|
|||
# Methods #
|
||||
###########
|
||||
|
||||
def self.policy_class
|
||||
AsymmetricKeyPolicy
|
||||
end
|
||||
|
||||
def algo_class
|
||||
raise NotImplementedError, "#{self.class}#algo_class"
|
||||
end
|
||||
|
|
|
@ -5,6 +5,10 @@ class AsymmetricKeyPolicy < ApplicationPolicy
|
|||
true
|
||||
end
|
||||
|
||||
def show?
|
||||
true
|
||||
end
|
||||
|
||||
class Scope < Scope
|
||||
def resolve
|
||||
scope.all
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
<th scope="col">
|
||||
<%= AsymmetricKey.human_attribute_name :algo_variant %>
|
||||
</th>
|
||||
<th scope="col"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
|
@ -19,6 +20,11 @@
|
|||
<td scope="row"><%= asymmetric_key.id %></td>
|
||||
<td><%= asymmetric_key.algo_class %></td>
|
||||
<td><%= asymmetric_key.algo_variant %></td>
|
||||
<td>
|
||||
<% if policy(asymmetric_key).show? %>
|
||||
<%= open_action public_key_path(asymmetric_key) %>
|
||||
<% end %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
|
|
35
app/views/asymmetric_keys/show.html.erb
Normal file
35
app/views/asymmetric_keys/show.html.erb
Normal file
|
@ -0,0 +1,35 @@
|
|||
<div class="container">
|
||||
<%= nav_breadcrumb(
|
||||
[AsymmetricKey.model_name.human(count: 0), public_keys_path],
|
||||
AsymmetricKey.model_name.human(count: 1),
|
||||
) %>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<dl>
|
||||
<dt><%= AsymmetricKey.human_attribute_name :id %></dt>
|
||||
<dd><%= @asymmetric_key.id %></dd>
|
||||
|
||||
<dt><%= AsymmetricKey.human_attribute_name :algo_class %></dt>
|
||||
<dd><%= @asymmetric_key.algo_class %></dd>
|
||||
|
||||
<dt><%= AsymmetricKey.human_attribute_name :algo_variant %></dt>
|
||||
<dd><%= @asymmetric_key.algo_variant %></dd>
|
||||
|
||||
<dt><%= AsymmetricKey.human_attribute_name :sha1 %></dt>
|
||||
<dd><%= display_sha1 @asymmetric_key.sha1 %></dd>
|
||||
|
||||
<dt><%= AsymmetricKey.human_attribute_name :sha256 %></dt>
|
||||
<dd><%= display_sha256 @asymmetric_key.sha256 %></dd>
|
||||
</dl>
|
||||
</div>
|
||||
|
||||
<div class="col-md-6">
|
||||
<%= render partial: 'private_keys/alert',
|
||||
locals: {
|
||||
asymmetric_key: @asymmetric_key,
|
||||
}
|
||||
%>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
|
@ -57,6 +57,7 @@ en:
|
|||
id: ID
|
||||
algo_class: Algorithm class
|
||||
algo_variant: Variant
|
||||
sha1: SHA-1 fingerprint
|
||||
sha256: SHA-256 fingerprint
|
||||
contact:
|
||||
id: ID
|
||||
|
|
|
@ -57,6 +57,7 @@ ru:
|
|||
id: ID
|
||||
algo_class: Класс алгоритма
|
||||
algo_variant: Вариант
|
||||
sha1: Отпечаток SHA-1
|
||||
sha256: Отпечаток SHA-256
|
||||
contact:
|
||||
id: ID
|
||||
|
|
|
@ -14,9 +14,8 @@ Rails.application.routes.draw do
|
|||
resources :federal_subjects, param: :number, only: %i[index show]
|
||||
|
||||
resources :public_keys,
|
||||
as: :asymmetric_key,
|
||||
controller: 'asymmetric_keys',
|
||||
only: :index
|
||||
only: %i[index show]
|
||||
|
||||
resources :private_keys, only: :show
|
||||
|
||||
|
|
40
spec/requests/asymmetric_keys/show_spec.rb
Normal file
40
spec/requests/asymmetric_keys/show_spec.rb
Normal file
|
@ -0,0 +1,40 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'GET /public_keys/:id' do
|
||||
let(:current_account) { nil }
|
||||
|
||||
let(:asymmetric_key) { create %i[rsa_key ecurve_key].sample }
|
||||
|
||||
def make_request
|
||||
get "/public_keys/#{asymmetric_key.id}"
|
||||
end
|
||||
|
||||
before do
|
||||
sign_in current_account.user if current_account&.user
|
||||
make_request
|
||||
end
|
||||
|
||||
for_account_types nil, :usual, :superuser do
|
||||
specify do
|
||||
expect(response).to have_http_status :ok
|
||||
end
|
||||
end
|
||||
|
||||
context 'for RSA key' do
|
||||
let(:asymmetric_key) { create :rsa_key }
|
||||
|
||||
specify do
|
||||
expect(response).to have_http_status :ok
|
||||
end
|
||||
end
|
||||
|
||||
context 'for elliptic-curve key' do
|
||||
let(:asymmetric_key) { create :ecurve_key }
|
||||
|
||||
specify do
|
||||
expect(response).to have_http_status :ok
|
||||
end
|
||||
end
|
||||
end
|
Reference in a new issue