From f395ee12665aff63e50552b6318b26806b0b7c49 Mon Sep 17 00:00:00 2001 From: Jyri-Petteri Paloposki Date: Tue, 22 Feb 2022 22:31:39 +0200 Subject: [PATCH] Fix code style issues and the sender map logic to handle the empty pref case correctly. Also fixed the rails script to use the correct directory always. --- app/models/message_gateway.rb | 31 +++++++++++++------------------ app/models/preference.rb | 2 +- bin/rails | 2 +- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/app/models/message_gateway.rb b/app/models/message_gateway.rb index 187a97f0c..3c9e6e22c 100644 --- a/app/models/message_gateway.rb +++ b/app/models/message_gateway.rb @@ -2,7 +2,7 @@ class MessageGateway < ActionMailer::Base def receive(email) user = get_receiving_user_from_email_address(email) return false if user.nil? - return false unless check_sender_is_in_mailmap(user, email) + return false unless check_sender_is_in_mailmap(user, email.from[0]) context = user.prefs.sms_context todo_params = get_todo_params(email) @@ -90,29 +90,24 @@ def get_receiving_user_from_sms_email(address) return user end - def check_sender_is_in_mailmap(user, email) - if user.present? && !sender_is_in_mailmap?(user, email) - Rails.logger.warn "#{email.from[0]} not found in mailmap for #{user.login}" + def check_sender_is_in_mailmap(user, from) + if user.present? && SITE_CONFIG['email_dispatch'] == 'to' && !sender_is_in_mailmap?(user, from) + Rails.logger.warn "#{from} not found in mailmap for #{user.login}" return false end return true end - def sender_is_in_mailmap?(user, email) - if SITE_CONFIG['email_dispatch'] == 'to' - if SITE_CONFIG['mailmap'].is_a? Hash - # Look for the sender in the map of allowed senders - SITE_CONFIG['mailmap'][user.preference.sms_email].include? email.from[0] - else - # If the config mailmap isn't defined, use the values provided by the users. - pref_senders = user.prefs.sms_permitted_senders.split(',').collect(&:strip) - pref_senders.include? email.from[0] - end - else - # We can't check the map if it's not defined, or if the lookup is the - # wrong way round, so just allow it - true + def sender_is_in_mailmap?(user, from) + if SITE_CONFIG['mailmap'].is_a? Hash + # Look for the sender in the map of allowed senders + SITE_CONFIG['mailmap'][user.preference.sms_email].include? from + elsif !(pref_senders = user.prefs.sms_permitted_senders).empty? + # If the config mailmap isn't defined, use the values provided by the users. + pref_senders.split(',').collect(&:strip).include? from end + # We can't check the map if it's not defined so just allow it + true end def get_text_or_nil(text) diff --git a/app/models/preference.rb b/app/models/preference.rb index 8e3d23ad9..f7cd7405f 100644 --- a/app/models/preference.rb +++ b/app/models/preference.rb @@ -2,7 +2,7 @@ class Preference < ApplicationRecord belongs_to :user belongs_to :sms_context, :class_name => 'Context' - validates_uniqueness_of :sms_email, :case_sensitive => false + validates :sms_email, uniqueness: { case_sensitive: false } def self.themes { :black => 'black', :light_blue => 'light_blue' } diff --git a/bin/rails b/bin/rails index 9d8f5bd1c..f5cab3d60 100755 --- a/bin/rails +++ b/bin/rails @@ -5,7 +5,7 @@ SCRIPT=$(readlink -f "$0") # Absolute path this script is in, thus /home/user/bin SCRIPTPATH=$(dirname "$SCRIPT") -if [ -e ../.use-docker -a ! -e /etc/app-env ]; +if [ -e $SCRIPTPATH/../.use-docker -a ! -e /etc/app-env ]; then $SCRIPTPATH/../script/docker-environment $0 "$@" else