mirror of
https://github.com/internetee/registry.git
synced 2025-05-17 01:47:18 +02:00
39 lines
1.7 KiB
Ruby
39 lines
1.7 KiB
Ruby
class WhiteIp < ActiveRecord::Base
|
|
include Versions
|
|
belongs_to :registrar
|
|
|
|
# rubocop: disable Metrics/LineLength
|
|
validates :ipv4, format: { with: /\A(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\z/, allow_blank: true }
|
|
validates :ipv6, format: { with: /(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/, allow_blank: true }
|
|
# rubocop: enable Metrics/LineLength
|
|
|
|
validate :validate_ipv4_and_ipv6
|
|
def validate_ipv4_and_ipv6
|
|
return if ipv4.present? || ipv6.present?
|
|
errors.add(:base, I18n.t(:ipv4_or_ipv6_must_be_present))
|
|
end
|
|
|
|
API = 'api'
|
|
REGISTRAR = 'registrar'
|
|
GLOBAL = 'global'
|
|
INTERFACES = [GLOBAL, API, REGISTRAR]
|
|
|
|
scope :api, -> { where(interface: API) }
|
|
scope :registrar, -> { where(interface: REGISTRAR) }
|
|
scope :global, -> { where(interface: GLOBAL) }
|
|
|
|
class << self
|
|
def registrar_ip_white?(ip)
|
|
return true unless Setting.registrar_ip_whitelist_enabled
|
|
|
|
at = WhiteIp.arel_table
|
|
WhiteIp.where(
|
|
at[:interface].eq(REGISTRAR).or(
|
|
at[:interface].eq(GLOBAL)
|
|
).and(
|
|
at[:ipv4].eq(ip)
|
|
)
|
|
).any?
|
|
end
|
|
end
|
|
end
|