Added error messages translations to white ips

This commit is contained in:
Sergei Tsoganov 2023-07-04 11:33:06 +03:00
parent 289159bff2
commit 97c11f5dfd
7 changed files with 161 additions and 50 deletions

View file

@ -1,5 +1,7 @@
class WhiteIp < ApplicationRecord # rubocop:disable Metrics/ClassLength
class WhiteIp < ApplicationRecord
include Versions
include WhiteIp::WhiteIpConcern
belongs_to :registrar
attr_accessor :address
@ -25,14 +27,14 @@ class WhiteIp < ApplicationRecord # rubocop:disable Metrics/ClassLength
assign_ip_attributes(ip_version)
rescue IPAddr::InvalidAddressError
errors.add(:address, :invalid)
errors.add(:base, :address_invalid)
end
def validate_only_one_ip
if ipv4.present? && ipv6.present?
errors.add(:base, I18n.t(:ip_must_be_one))
errors.add(:base, :ip_must_be_one)
elsif ipv4.blank? && ipv6.blank?
errors.add(:base, I18n.t(:ipv4_or_ipv6_must_be_present))
errors.add(:base, :ipv4_or_ipv6_must_be_present)
end
end
@ -45,7 +47,7 @@ class WhiteIp < ApplicationRecord # rubocop:disable Metrics/ClassLength
limit = Setting.ip_whitelist_max_count
return unless total >= limit
errors.add(:base, I18n.t(:ip_limit_exceeded, total: total, limit: limit))
errors.add(:base, :ip_limit_exceeded, total: total, limit: limit)
end
def valid_ipv4?
@ -75,49 +77,9 @@ class WhiteIp < ApplicationRecord # rubocop:disable Metrics/ClassLength
super(interfaces.reject(&:blank?))
end
class << self
# rubocop:disable Style/CaseEquality
# rubocop:disable Metrics/AbcSize
def include_ip?(ip)
return false if ip.blank?
where(id: ids_including(ip)).any?
end
def ids_including(ip)
ipv4 = ipv6 = []
ipv4 = select { |white_ip| check_ip4(white_ip.ipv4) === check_ip4(ip) } if check_ip4(ip).present?
ipv6 = select { |white_ip| check_ip6(white_ip.ipv6) === check_ip6(ip) } if check_ip6(ip).present?
(ipv4 + ipv6).pluck(:id).flatten.uniq
end
# rubocop:enable Style/CaseEquality
# rubocop:enable Metrics/AbcSize
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
def csv_header
%w[IPv4 IPv6 Interfaces Created Updated]
end
def ransackable_attributes(*)
authorizable_ransackable_attributes
end
end
def as_csv_row
[
ipv4,
ipv6,
ipv4, ipv6,
interfaces.join(', ').upcase,
created_at,
updated_at,
@ -142,7 +104,7 @@ class WhiteIp < ApplicationRecord # rubocop:disable Metrics/ClassLength
self.ipv6 = address
self.ipv4 = nil
else
errors.add(:address, :invalid)
errors.add(:base, :address_invalid)
end
end