From 97c11f5dfd2a32922c5cf30ac6af0637f3eefbf5 Mon Sep 17 00:00:00 2001 From: Sergei Tsoganov Date: Tue, 4 Jul 2023 11:33:06 +0300 Subject: [PATCH] Added error messages translations to white ips --- app/controllers/admin/base_controller.rb | 5 ++ app/controllers/repp/v1/base_controller.rb | 5 ++ .../concerns/white_ip/white_ip_concern.rb | 51 +++++++++++++++ app/models/white_ip.rb | 56 +++------------- config/locales/devise.et.yml | 64 +++++++++++++++++++ config/locales/en.yml | 15 ++++- config/locales/et.yml | 15 +++++ 7 files changed, 161 insertions(+), 50 deletions(-) create mode 100644 app/models/concerns/white_ip/white_ip_concern.rb create mode 100644 config/locales/devise.et.yml diff --git a/app/controllers/admin/base_controller.rb b/app/controllers/admin/base_controller.rb index 88afec72d..36de86a2c 100644 --- a/app/controllers/admin/base_controller.rb +++ b/app/controllers/admin/base_controller.rb @@ -1,6 +1,7 @@ module Admin class BaseController < ApplicationController before_action :authenticate_admin_user! + before_action :set_locale helper_method :head_title_sufix before_action :set_paper_trail_whodunnit @@ -33,5 +34,9 @@ module Admin end end end + + def set_locale + I18n.locale = params[:locale] || I18n.default_locale + end end end diff --git a/app/controllers/repp/v1/base_controller.rb b/app/controllers/repp/v1/base_controller.rb index 464be249a..b12addd0f 100644 --- a/app/controllers/repp/v1/base_controller.rb +++ b/app/controllers/repp/v1/base_controller.rb @@ -5,6 +5,7 @@ module Repp around_action :log_request before_action :authenticate_user + before_action :set_locale before_action :validate_webclient_ca before_action :validate_client_certs before_action :check_ip_restriction @@ -176,6 +177,10 @@ module Repp authorize!(:throttled_user, @domain) unless current_user || action_name == 'tara_callback' current_user end + + def set_locale + I18n.locale = current_user&.try(:locale) || params[:locale] || I18n.default_locale + end end end end diff --git a/app/models/concerns/white_ip/white_ip_concern.rb b/app/models/concerns/white_ip/white_ip_concern.rb new file mode 100644 index 000000000..73b17df19 --- /dev/null +++ b/app/models/concerns/white_ip/white_ip_concern.rb @@ -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 diff --git a/app/models/white_ip.rb b/app/models/white_ip.rb index f55a9482c..577faa565 100644 --- a/app/models/white_ip.rb +++ b/app/models/white_ip.rb @@ -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 diff --git a/config/locales/devise.et.yml b/config/locales/devise.et.yml new file mode 100644 index 000000000..051ad2ea2 --- /dev/null +++ b/config/locales/devise.et.yml @@ -0,0 +1,64 @@ +# Additional translations at https://github.com/plataformatec/devise/wiki/I18n + +et: + devise: + confirmations: + confirmed: "Your email address has been successfully confirmed." + send_instructions: "You will receive an email with instructions for how to confirm your email address in a few minutes." + send_paranoid_instructions: "If your email address exists in our database, you will receive an email with instructions for how to confirm your email address in a few minutes." + failure: + already_authenticated: "You are already signed in." + inactive: "Your account is not activated yet." + invalid: "Invalid %{authentication_keys} or password." + locked: "Your account is locked." + last_attempt: "You have one more attempt before your account is locked." + not_found_in_database: "Invalid %{authentication_keys} or password." + timeout: "Your session expired. Please sign in again to continue." + unauthenticated: "You need to sign in before continuing." + unconfirmed: "You have to confirm your email address before continuing." + mailer: + confirmation_instructions: + subject: "Confirmation instructions" + reset_password_instructions: + subject: "Reset password instructions" + unlock_instructions: + subject: "Unlock instructions" + email_changed: + subject: "Email Changed" + password_change: + subject: "Password Changed" + omniauth_callbacks: + failure: "Could not authenticate you from %{kind} because \"%{reason}\"." + success: "Successfully authenticated from %{kind} account." + passwords: + no_token: "You can't access this page without coming from a password reset email. If you do come from a password reset email, please make sure you used the full URL provided." + send_instructions: "You will receive an email with instructions on how to reset your password in a few minutes." + send_paranoid_instructions: "If your email address exists in our database, you will receive a password recovery link at your email address in a few minutes." + updated: "Your password has been changed successfully. You are now signed in." + updated_not_active: "Your password has been changed successfully." + registrations: + destroyed: "Bye! Your account has been successfully cancelled. We hope to see you again soon." + signed_up: "Welcome! You have signed up successfully." + signed_up_but_inactive: "You have signed up successfully. However, we could not sign you in because your account is not yet activated." + signed_up_but_locked: "You have signed up successfully. However, we could not sign you in because your account is locked." + signed_up_but_unconfirmed: "A message with a confirmation link has been sent to your email address. Please follow the link to activate your account." + update_needs_confirmation: "You updated your account successfully, but we need to verify your new email address. Please check your email and follow the confirm link to confirm your new email address." + updated: "Your account has been updated successfully." + sessions: + signed_in: "Signed in successfully." + signed_out: "Signed out successfully." + already_signed_out: "Signed out successfully." + unlocks: + send_instructions: "You will receive an email with instructions for how to unlock your account in a few minutes." + send_paranoid_instructions: "If your account exists, you will receive an email with instructions for how to unlock it in a few minutes." + unlocked: "Your account has been unlocked successfully. Please sign in to continue." + errors: + messages: + already_confirmed: "was already confirmed, please try signing in" + confirmation_period_expired: "needs to be confirmed within %{period}, please request a new one" + expired: "has expired, please request a new one" + not_found: "not found" + not_locked: "was not locked" + not_saved: + one: "1 error prohibited this %{resource} from being saved:" + other: "%{count} errors prohibited this %{resource} from being saved:" diff --git a/config/locales/en.yml b/config/locales/en.yml index 32f122924..edeef1852 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -147,6 +147,18 @@ en: length_more_than: 'Parameter value policy error: Legaldoc size is less than minimum allowed size of 3kB' length_less_than: 'Parameter value policy error: Legaldoc size exceeds maximum allowed size of 8mB' + white_ip: + attributes: + base: + address_invalid: 'IP Address is invalid' + ipv4_or_ipv6_must_be_present: 'IPv4 or IPv6 must be present' + ip_must_be_one: 'Please enter only one IP address' + ip_limit_exceeded: 'IP address limit exceeded. Total addresses: %{total}. Limit: %{limit}.' + ipv4: + taken: 'is already taken' + ipv6: + taken: 'is already taken' + attributes: epp_domain: &epp_domain_attributes @@ -548,9 +560,6 @@ en: not_valid_domain_verification_body: This could mean your verification has been expired or done already.

Please contact us if you think something is wrong. upload_crt: 'Upload CRT' crt_or_csr_must_be_present: 'CRT or CSR must be present' - ipv4_or_ipv6_must_be_present: 'IPv4 or IPv6 must be present' - ip_must_be_one: 'Please enter only one IP address' - ip_limit_exceeded: 'IP address limit exceeded. Total addresses: %{total}. Limit: %{limit}.' white_ip: 'White IP' edit_white_ip: 'Edit white IP' confirm_domain_delete: 'Confirm domain delete' diff --git a/config/locales/et.yml b/config/locales/et.yml index b381259a7..9dedcb86a 100644 --- a/config/locales/et.yml +++ b/config/locales/et.yml @@ -17,3 +17,18 @@ et: you_have_a_new_invoice: 'Teil on uus arve.' monthly_invoice: "Siit tuleb aruanne möödunud kuul ettemaksukontoga seotud tasuliste toimingutega." sincerely: 'Lugupidamisega' + + activerecord: + errors: + models: + white_ip: + attributes: + base: + address_invalid: 'IP-aadress on vale' + ipv4_or_ipv6_must_be_present: 'IPv4 või IPv6 peab olema olemas' + ip_must_be_one: 'Palun sisestage ainult üks IP-aadress' + ip_limit_exceeded: 'IP-aadressi piirang ületatud. Kokku aadresse: %{total}. Limiit: %{limit}.' + ipv4: + taken: 'on juba lisatud' + ipv6: + taken: 'on juba lisatud'