added endpoints to demo registry for accr results

This commit is contained in:
olegphenomenon 2021-12-10 16:53:25 +02:00
parent 487cbb1774
commit c07abaea59
20 changed files with 401 additions and 78 deletions

View file

@ -47,8 +47,46 @@ module Admin
redirect_to admin_registrar_path(@api_user.registrar), notice: t('.deleted')
end
def set_test_date_to_api_user
user_api = User.find(params[:user_api_id])
uri = URI.parse(ENV['registry_demo_registrar_api_user_url'] + "?username=#{user_api.username}&identity_code=#{user_api.identity_code}")
response = base_get_request(uri: uri, port: ENV['registry_demo_registrar_port'])
if response.code == "200"
result = JSON.parse(response.body)
demo_user_api = result['user_api']
Actions::RecordDateOfTest.record_result_to_api_user(
api_user:user_api,
date: demo_user_api['accreditation_date']) unless demo_user_api.empty?
return redirect_to request.referrer, notice: 'User Api found'
else
return redirect_to request.referrer, notice: 'User Api no found or not accriditated yet'
end
redirect_to request.referrer, notice: 'Something goes wrong'
end
def remove_test_date_to_api_user
user_api = User.find(params[:user_api_id])
user_api.accreditation_date = nil
user_api.accreditation_expire_date = nil
user_api.save
redirect_to request.referrer
end
private
def base_get_request(uri:, port:)
http = Net::HTTP.new(uri.host, port)
req = Net::HTTP::Get.new(uri.request_uri)
http.request(req)
end
def api_user_params
params.require(:api_user).permit(:username, :plain_text_password, :active,
:identity_code, { roles: [] })

View file

@ -1,3 +1,5 @@
require 'net/http'
module Admin
class RegistrarsController < BaseController # rubocop:disable Metrics/ClassLength
load_and_authorize_resource
@ -55,8 +57,56 @@ module Admin
end
end
def set_test_date
registrar = Registrar.find(params[:registrar_id])
uri = URI.parse(ENV['registry_demo_registrar_results_url'] + "?registrar_name=#{registrar.name}")
response = base_get_request(uri: uri, port: ENV['registry_demo_registrar_port'])
if response.code == "200"
return record_result_for_each_api_user(response: response)
else
return redirect_to request.referrer, notice: 'Registrar no found'
end
redirect_to request.referrer, notice: 'Something goes wrong'
end
def remove_test_date
registrar = Registrar.find(params[:registrar_id])
registrar.api_users.each do |api|
api.accreditation_date = nil
api.accreditation_expire_date = nil
api.save
end
redirect_to request.referrer
end
private
def record_result_for_each_api_user(response:)
result = JSON.parse(response.body)
registrar_users = result['registrar_users']
return redirect_to request.referrer, notice: 'Registrar found, but not accreditated yet' if registrar_users.empty?
registrar_users.each do |api|
a = ApiUser.find_by(username: api.username, identity_code: api.identity_code)
Actions::RecordDateOfTest.record_result_to_api_user(a, api.accreditation_date) unless a.nil?
end
redirect_to request.referrer, notice: 'Registrar found'
end
def base_get_request(uri:, port:)
http = Net::HTTP.new(uri.host, port)
req = Net::HTTP::Get.new(uri.request_uri)
http.request(req)
end
def filter_by_status
case params[:status]
when 'Active'

View file

@ -3,24 +3,26 @@ require 'auth_token/auth_token_decryptor'
module Api
module V1
module AccreditationCenter
class BaseController < ActionController::API
rescue_from ActiveRecord::RecordNotFound, with: :show_not_found_error
rescue_from ActiveRecord::RecordInvalid, with: :show_invalid_record_error
rescue_from(ActionController::ParameterMissing) do |parameter_missing_exception|
error = {}
error[parameter_missing_exception.param] = ['parameter is required']
response = { errors: [error] }
render json: response, status: :unprocessable_entity
end
if Rails.env.development? || Rails.env.staging? || Rails.env.test?
class BaseController < ActionController::API
rescue_from ActiveRecord::RecordNotFound, with: :show_not_found_error
rescue_from ActiveRecord::RecordInvalid, with: :show_invalid_record_error
rescue_from(ActionController::ParameterMissing) do |parameter_missing_exception|
error = {}
error[parameter_missing_exception.param] = ['parameter is required']
response = { errors: [error] }
render json: response, status: :unprocessable_entity
end
private
private
def show_not_found_error
render json: { errors: [{ base: ['Not found'] }] }, status: :not_found
end
def show_not_found_error
render json: { errors: [{ base: ['Not found'] }] }, status: :not_found
end
def show_invalid_record_error(exception)
render json: { errors: exception.record.errors }, status: :bad_request
def show_invalid_record_error(exception)
render json: { errors: exception.record.errors }, status: :bad_request
end
end
end
end

View file

@ -0,0 +1,40 @@
require 'serializers/repp/contact'
module Api
module V1
module AccreditationCenter
class ResultsController < ::Api::V1::AccreditationCenter::BaseController
def show
accr_users = []
registrar = Registrar.find_by(name: params[:registrar_name])
return render json: { errors: 'Registrar not found' }, status: :not_found if registrar.nil?
registrar.api_users.where.not(accreditation_date: nil).each do |u|
accr_users << u
end
render json: { code: 1000, registrar_users: accr_users }
end
def show_api_user
user_api = User.find_by(username: params[:username], identity_code: params[:identity_code])
return render json: { errors: 'User not found' }, status: :not_found if user_api.nil?
return render json: { errors: 'No accreditated yet' }, status: :not_found if user_api.accreditation_date.nil?
render json: { code: 1000, user_api: user_api }
end
def list_accreditated_api_users
users = User.where.not(accreditation_date: nil)
return render json: { errors: 'Accreditated users not found' }, status: :not_found if users.empty?
render json: { code: 1000, users: users }
end
end
end
end
end

View file

@ -1,36 +1,38 @@
module Repp
module V1
module Registrar
class AccreditationInfoController < BaseController
api :GET, 'repp/v1/registrar/accreditation/get_info'
desc 'check login user and return data'
if Rails.env.development? || Rails.env.staging?
class AccreditationInfoController < BaseController
api :GET, 'repp/v1/registrar/accreditation/get_info'
desc 'check login user and return data'
def index
login = current_user
registrar = current_user.registrar
def index
login = current_user
registrar = current_user.registrar
# rubocop:disable Style/AndOr
render_success(data: nil) and return unless login
# rubocop:enable Style/AndOr
# rubocop:disable Style/AndOr
render_success(data: nil) and return unless login
# rubocop:enable Style/AndOr
data = set_values_to_data(login: login, registrar: registrar)
data = set_values_to_data(login: login, registrar: registrar)
render_success(data: data)
end
render_success(data: data)
end
private
private
def set_values_to_data(login:, registrar:)
data = login.as_json(only: %i[id
username
name
uuid
roles
accreditation_date
accreditation_expire_date])
data[:registrar_name] = registrar.name
data[:registrar_reg_no] = registrar.reg_no
data
def set_values_to_data(login:, registrar:)
data = login.as_json(only: %i[id
username
name
uuid
roles
accreditation_date
accreditation_expire_date])
data[:registrar_name] = registrar.name
data[:registrar_reg_no] = registrar.reg_no
data
end
end
end
end

View file

@ -1,30 +1,31 @@
module Repp
module V1
module Registrar
class AccreditationResultsController < ActionController::API
before_action :authenticate_shared_key
if Rails.env.development? || Rails.env.staging?
class AccreditationResultsController < ActionController::API
before_action :authenticate_shared_key
TEMPORARY_SECRET_KEY = ENV['accreditation_secret'].freeze
EXPIRE_DEADLINE = 15.minutes.freeze
api :POST, 'repp/v1/registrar/accreditation/push_results'
desc 'added datetime results'
api :POST, 'repp/v1/registrar/accreditation/push_results'
desc 'added datetime results'
def create
username = params[:accreditation_result][:username]
result = params[:accreditation_result][:result]
def create
username = params[:accreditation_result][:username]
result = params[:accreditation_result][:result]
record_accreditation_result(username, result) if result
rescue ActiveRecord::RecordNotFound
record_not_found(username)
end
record_accreditation_result(username, result) if result
rescue ActiveRecord::RecordNotFound
record_not_found(username)
end
private
private
def record_accreditation_result(username, result)
user = ApiUser.find_by(username: username)
def record_accreditation_result(username, result)
user = ApiUser.find_by(username: username)
raise ActiveRecord::RecordNotFound if user.nil?
raise ActiveRecord::RecordNotFound if user.nil?
user.accreditation_date = DateTime.current
user.accreditation_expire_date = user.accreditation_date + EXPIRE_DEADLINE
@ -55,26 +56,27 @@ module Repp
end
end
def authenticate_shared_key
api_key = "Basic #{TEMPORARY_SECRET_KEY}"
render_failed unless api_key == request.authorization
end
def authenticate_shared_key
api_key = "Basic #{TEMPORARY_SECRET_KEY}"
render_failed unless api_key == request.authorization
end
def record_not_found(username)
@response = { code: 2303, message: "Object '#{username}' does not exist" }
render(json: @response)
end
def record_not_found(username)
@response = { code: 2303, message: "Object '#{username}' does not exist" }
render(json: @response)
end
def render_failed
@response = { code: 2202, message: 'Invalid authorization information' }
render(json: @response, status: :unauthorized)
end
def render_failed
@response = { code: 2202, message: 'Invalid authorization information' }
render(json: @response, status: :unauthorized)
end
def render_success(code: nil, message: nil, data: nil)
@response = { code: code || 1000, message: message || 'Command completed successfully',
data: data || {} }
def render_success(code: nil, message: nil, data: nil)
@response = { code: code || 1000, message: message || 'Command completed successfully',
data: data || {} }
render(json: @response, status: :ok)
render(json: @response, status: :ok)
end
end
end
end