Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegPhenomenon committed Aug 27, 2021
1 parent 5344f98 commit 7be0212
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 45 deletions.
84 changes: 42 additions & 42 deletions app/controllers/api/v1/accreditation_center/auth_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,58 @@ class AuthController < ::Api::V1::AccreditationCenter::BaseController
before_action :authenticate_user

def index
login = @current_user
registrar = @current_user.registrar
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 authenticate_user
username, password = Base64.urlsafe_decode64(basic_token).split(':')
@current_user ||= ApiUser.find_by(username: username, plain_text_password: password)
def authenticate_user
username, password = Base64.urlsafe_decode64(basic_token).split(':')
@current_user ||= ApiUser.find_by(username: username, plain_text_password: password)

return if @current_user
return if @current_user

raise(ArgumentError)
rescue NoMethodError, ArgumentError
@response = { code: 2202, message: 'Invalid authorization information' }
render(json: @response, status: :unauthorized)
end
raise(ArgumentError)
rescue NoMethodError, ArgumentError
@response = { code: 2202, message: 'Invalid authorization information' }
render(json: @response, status: :unauthorized)
end

def basic_token
pattern = /^Basic /
header = request.headers['Authorization']
header = header.gsub(pattern, '') if header&.match(pattern)
header.strip
end
def basic_token
pattern = /^Basic /
header = request.headers['Authorization']
header = header.gsub(pattern, '') if header&.match(pattern)
header.strip
end

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

def render_success(code: nil, message: nil, data: nil)
@response = { code: code || 1000, message: message || 'Command completed successfully',
data: 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

render(json: @response, status: :ok)
end
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)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class BaseController < ActionController::API

private

def check_ip_whitelist
def check_ip_whitelist
allowed_ips = ENV['accr_center_api_auth_allowed_ips'].to_s.split(',').map(&:strip)
return if allowed_ips.include?(request.ip) || Rails.env.development?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def show

if @contact
render json: { contact: Serializers::Repp::Contact.new(@contact,
show_address: false).to_json }, status: :found
show_address: false).to_json },
status: :found
else
render json: { errors: 'Contact not found' }, status: :not_found
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def show

if @domain
render json: { domain: Serializers::Repp::Domain.new(@domain,
sponsored: true).to_json }, status: :found
sponsored: true).to_json },
status: :found
else
render json: { errors: 'Domain not found' }, status: :not_found
end
Expand Down

0 comments on commit 7be0212

Please sign in to comment.