diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 337c92e43..826eaf399 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -113,6 +113,18 @@ def set_current_user end end + def current_user_country + remote_ip = ENV.fetch('MOCK_IP') { Rails.env.production? ? request.remote_ip : '130.88.0.0' } + Locator.instance.lookup(remote_ip)&.dig('country') + end + + def from_blocked_country? + TeSS::Config.blocked_countries.present? && + TeSS::Config.blocked_countries.include?(current_user_country['iso_code'].downcase) + end + + helper_method :current_user_country, :from_blocked_country? + protected def configure_permitted_parameters diff --git a/app/controllers/tess_devise/registrations_controller.rb b/app/controllers/tess_devise/registrations_controller.rb index b9ef521cb..80fcca377 100644 --- a/app/controllers/tess_devise/registrations_controller.rb +++ b/app/controllers/tess_devise/registrations_controller.rb @@ -1,6 +1,7 @@ class TessDevise::RegistrationsController < Devise::RegistrationsController # Inspired by http://stackoverflow.com/questions/3546289/override-devise-registrations-controller before_action :check_captcha, only: :create + before_action :check_country_blocked, only: [:create, :new] before_action :set_breadcrumbs, only: :edit # Set the after update path to be user's show page @@ -30,6 +31,10 @@ def check_captcha end end + def check_country_blocked + head :forbidden if from_blocked_country? + end + def set_breadcrumbs add_base_breadcrumbs('users') @breadcrumbs += [{ name: @user.name, url: user_path(@user) }, { name: 'Manage Account' }] diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index fe260bb77..de1592cc4 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -691,9 +691,4 @@ def unverified_notice(resource) content_tag('div', t('warnings.unverified', resource_type: resource.model_name.human.downcase), class: 'alert alert-warning mb-4 unverified-notice') end - - def current_user_country - remote_ip = ENV.fetch('MOCK_IP') { Rails.env.production? ? request.remote_ip : '130.88.0.0' } - Locator.instance.lookup(remote_ip)&.dig('country') - end end diff --git a/app/views/layouts/_login_menu.html.erb b/app/views/layouts/_login_menu.html.erb index 70a06870e..55a26b63e 100644 --- a/app/views/layouts/_login_menu.html.erb +++ b/app/views/layouts/_login_menu.html.erb @@ -14,7 +14,7 @@ <%= link_to t('authentication.password.title'), new_user_session_path %> - <% if TeSS::Config.feature['registration'] %> + <% if TeSS::Config.feature['registration'] && !from_blocked_country? %>