Skip to content

Commit

Permalink
added prepend action to format foreign symbols to punicode
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegPhenomenon committed Jun 26, 2024
1 parent 2a64056 commit 47f72ce
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 15 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ RUN yarn install --frozen-lockfile

FROM base

ENV LAUNCHY_DRY_RUN=true
ENV BROWSER=/dev/null

RUN useradd rails
RUN mkdir -p /home/rails && chown rails:rails /home/rails

Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ group :development do
gem 'htmlbeautifier'
gem 'i18n-debug'
gem 'letter_opener', '~> 1.8'
gem 'letter_opener_web', '~> 3.0'
gem 'listen', '>= 3.0.5', '< 3.9'
gem 'ruby-lsp-rails'
gem 'web-console', '>= 3.3.0'
Expand Down
10 changes: 8 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,13 @@ GEM
language_server-protocol (3.17.0.3)
launchy (2.5.2)
addressable (~> 2.8)
letter_opener (1.8.1)
launchy (>= 2.2, < 3)
letter_opener (1.10.0)
launchy (>= 2.2, < 4)
letter_opener_web (3.0.0)
actionmailer (>= 6.1)
letter_opener (~> 1.9)
railties (>= 6.1)
rexml
listen (3.8.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
Expand Down Expand Up @@ -503,6 +508,7 @@ DEPENDENCIES
jsbundling-rails
jwt
letter_opener (~> 1.8)
letter_opener_web (~> 3.0)
listen (>= 3.0.5, < 3.9)
lograge
mimemagic (~> 0.4.3)
Expand Down
34 changes: 23 additions & 11 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
class ApplicationController < ActionController::Base
include Pagy::Backend

prepend_before_action :convert_punycode_params

helper_method :turbo_frame_request?

protect_from_forgery with: :exception
before_action :set_locale, :clear_flash
before_action :set_notifications
before_action :notifications_for_header

content_security_policy do |policy|
policy.style_src :self, 'www.gstatic.com', :unsafe_inline
end

rescue_from CanCan::AccessDenied do |exception|
rescue_from CanCan::AccessDenied do |_exception|
flash[:alert] = I18n.t('unauthorized.message')

if turbo_frame_request?
Expand All @@ -22,7 +24,9 @@ class ApplicationController < ActionController::Base
end

def store_location
session[:return_to] = request.referer.split('?').first if request.referer
return unless request.referer

session[:return_to] = request.referer.split('?').first
end

# If needed, add updated_by to the params hash. Updated by takes format of "123 - User Surname"
Expand All @@ -36,30 +40,38 @@ def merge_updated_by(update_params)
end
end

def after_sign_in_path_for(_resource)
root_path
end
def after_sign_in_path_for(_resource) = root_path

def set_notifications
def notifications_for_header
# don't change the name, it's used in the header and can be conflict with notification variable in notifications page
@notifications_for_header = current_user&.notifications&.unread&.order(created_at: :desc)&.limit(5)
@notifications_for_header ||= current_user&.notifications&.unread&.order(created_at: :desc)&.limit(5)
end

private

def set_locale
if params[:localize].present? && I18n.available_locales.include?(params[:localize].to_sym)
cookies[:locale] = params[:localize]
end
set_locale_to_cookies

I18n.locale = current_user&.locale || cookies[:locale] || I18n.default_locale
@pagy_locale = I18n.locale.to_s
end

def set_locale_to_cookies
return unless params[:localize].present? && I18n.available_locales.include?(params[:localize].to_sym)

cookies[:locale] = params[:localize]
end

def clear_flash
flash.clear if turbo_frame_request?
end

def convert_punycode_params
return unless email = request.params.dig(:user, :email)

request.params[:user][:email] = email.split('@').map { |val| SimpleIDN.to_ascii(val) }.join('@')
end

protected

def authenticate_user!
Expand Down
6 changes: 5 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def create

respond_to do |format|
if @user.save
flash[:notice] = t(:created)

format.html do
sign_in(User, @user)
redirect_to user_path(@user.uuid), notice: t(:created)
Expand All @@ -52,7 +54,9 @@ def create
render :show, status: :created, location: @user
end
else
format.html { render :new }
flash.now[:alert] = @user.errors.full_messages.join(', ')

format.html { render :new, status: :unprocessable_entity }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
Expand Down
2 changes: 1 addition & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
config.active_record.verbose_query_logs = true

config.log_formatter = ::Logger::Formatter.new
config.action_mailer.delivery_method = :letter_opener
config.action_mailer.delivery_method = :letter_opener_web
config.action_mailer.default_url_options = {
host: 'localhost:3000',
protocol: 'http'
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,5 @@
resources :autobider, param: :uuid, only: [:create, :update, :edit, :new]

mount OkComputer::Engine, at: '/healthcheck', as: :healthcheck
mount LetterOpenerWeb::Engine, at: "/letter_opener" if Rails.env.development?
end
27 changes: 27 additions & 0 deletions test/integration/users_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,31 @@ def test_user_is_notified_about_password_change
password_confirmation: new_password } }
end
end

def test_user_create_with_punicode
assert_enqueued_emails 1 do
post users_path, params: {
user: {
email: 'example@ää.eu',
given_names: 'John',
surname: 'Doe',
password: 'password123',
password_confirmation: 'password123',
terms_and_conditions_accepted_at: Time.now.utc,
locale: 'en',
mobile_phone: '+37269900366',
identity_code: '60001017793',
country_code: 'EE',
accepts_terms_and_conditions: true,
roles: ['participant'],
phone_number_confirmed: true,
provider: 'email'}
}
end

punicode = SimpleIDN.to_ascii('ää.eu')
assert User.find_by(email: "example@#{punicode}")

assert_redirected_to new_user_session_path
end
end

0 comments on commit 47f72ce

Please sign in to comment.