mirror of
https://github.com/internetee/registry.git
synced 2025-07-30 06:26:15 +02:00
Added error messages translations to white ips
This commit is contained in:
parent
289159bff2
commit
97c11f5dfd
7 changed files with 161 additions and 50 deletions
51
app/models/concerns/white_ip/white_ip_concern.rb
Normal file
51
app/models/concerns/white_ip/white_ip_concern.rb
Normal file
|
@ -0,0 +1,51 @@
|
|||
# app/models/concerns/white_ip_concern.rb
|
||||
module WhiteIp::WhiteIpConcern
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
class_methods do # rubocop:disable Metrics/BlockLength
|
||||
def include_ip?(ip)
|
||||
return false if ip.blank?
|
||||
|
||||
where(id: ids_including(ip)).any?
|
||||
end
|
||||
|
||||
def ids_including(ip)
|
||||
ipv4 = select_ipv4(ip)
|
||||
ipv6 = select_ipv6(ip)
|
||||
|
||||
(ipv4 + ipv6).pluck(:id).flatten.uniq
|
||||
end
|
||||
|
||||
def select_ipv4(ip)
|
||||
return [] unless check_ip4(ip).present?
|
||||
|
||||
select { |white_ip| check_ip4(white_ip.ipv4) == check_ip4(ip) }
|
||||
end
|
||||
|
||||
def select_ipv6(ip)
|
||||
return [] unless check_ip6(ip).present?
|
||||
|
||||
select { |white_ip| check_ip6(white_ip.ipv6) == check_ip6(ip) }
|
||||
end
|
||||
|
||||
def csv_header
|
||||
%w[IPv4 IPv6 Interfaces Created Updated]
|
||||
end
|
||||
|
||||
def ransackable_attributes(*)
|
||||
authorizable_ransackable_attributes
|
||||
end
|
||||
|
||||
def check_ip4(ip)
|
||||
IPAddr.new(ip, Socket::AF_INET)
|
||||
rescue StandardError => _e
|
||||
nil
|
||||
end
|
||||
|
||||
def check_ip6(ip)
|
||||
IPAddr.new(ip, Socket::AF_INET6)
|
||||
rescue StandardError => _e
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue