mirror of
https://github.com/internetee/registry.git
synced 2025-06-10 14:44:47 +02:00
Add domain index action (without pagination yet)
This commit is contained in:
parent
13562aeb06
commit
10d42a0d74
5 changed files with 155 additions and 4 deletions
38
app/controllers/api/v1/registrant/base_controller.rb
Normal file
38
app/controllers/api/v1/registrant/base_controller.rb
Normal file
|
@ -0,0 +1,38 @@
|
|||
require 'rails5_api_controller_backport'
|
||||
require 'auth_token/auth_token_decryptor'
|
||||
|
||||
module Api
|
||||
module V1
|
||||
module Registrant
|
||||
class BaseController < ActionController::API
|
||||
before_action :authenticate
|
||||
|
||||
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
|
||||
|
||||
def bearer_token
|
||||
pattern = /^Bearer /
|
||||
header = request.headers['Authorization']
|
||||
header.gsub(pattern, '') if header&.match(pattern)
|
||||
end
|
||||
|
||||
def authenticate
|
||||
decryptor = AuthTokenDecryptor.create_with_defaults(bearer_token)
|
||||
decryptor.decrypt_token
|
||||
|
||||
if decryptor.valid?
|
||||
sign_in decryptor.user
|
||||
else
|
||||
render json: { errors: ['Not authorized'] }, status: :unauthorized
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -3,10 +3,15 @@ require 'rails5_api_controller_backport'
|
|||
module Api
|
||||
module V1
|
||||
module Registrant
|
||||
class DomainsController < ActionController::API
|
||||
class DomainsController < BaseController
|
||||
def index
|
||||
@domains = associated_domains(current_user)
|
||||
render json: @domains
|
||||
end
|
||||
|
||||
def show
|
||||
@domain = Domain.find_by(uuid: params[:uuid])
|
||||
domain_pool = associated_domains(current_user)
|
||||
@domain = domain_pool.find_by(uuid: params[:uuid])
|
||||
|
||||
if @domain
|
||||
render json: @domain
|
||||
|
@ -14,6 +19,17 @@ module Api
|
|||
render json: { errors: ["Domain not found"] }, status: :not_found
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def associated_domains(user)
|
||||
country_code, ident = user.registrant_ident.split('-')
|
||||
|
||||
BusinessRegistryCache.fetch_associated_domains(ident, country_code)
|
||||
rescue Soap::Arireg::NotAvailableError => error
|
||||
Rails.logger.fatal("[EXCEPTION] #{error.to_s}")
|
||||
user.domains
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue