Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a user preference that can be used as a fallback for the sender a… #2744

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/preferences_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def prefs_params
:staleness_starts, :due_style, :locale, :title_date_format, :time_zone,
:show_hidden_projects_in_sidebar, :show_project_on_todo_done,
:review_period, :refresh, :verbose_action_descriptors,
:mobile_todos_per_page, :sms_email, :sms_context_id, :theme)
:mobile_todos_per_page, :sms_email, :sms_context_id, :sms_permitted_senders, :theme)
end

def user_params
Expand Down
23 changes: 12 additions & 11 deletions app/models/message_gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -90,23 +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['mailmap'].is_a? Hash) && SITE_CONFIG['email_dispatch'] == 'to'
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? email.from[0]
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
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)
Expand Down
2 changes: 2 additions & 0 deletions app/models/preference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class Preference < ApplicationRecord
belongs_to :user
belongs_to :sms_context, :class_name => 'Context'

validates :sms_email, uniqueness: { case_sensitive: false }

def self.themes
{ :black => 'black', :light_blue => 'light_blue' }
end
Expand Down
3 changes: 3 additions & 0 deletions app/views/preferences/_tracks_behavior.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
<div class="form-group">
<%= pref('prefs', "sms_context") { select('prefs', 'sms_context_id', current_user.contexts.map{|c| [c.name, c.id]}, {}, class: "form-control") } %>
</div>
<div class="form-group">
<%= pref_with_text_field('prefs', "sms_permitted_senders") %>
</div>
<div class="form-group">
<%= pref_with_select_field('prefs', 'theme', [[t('models.preference.themes.black'), Preference.themes[:black]], [t('models.preference.themes.light_blue'), Preference.themes[:light_blue]]]) %>
</div>
2 changes: 1 addition & 1 deletion bin/rails
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ en:
show_project_on_todo_done: Go to project page on completing todo
sms_context: Default email context
sms_email: From email
sms_permitted_senders: Permitted email senders (use comma as separator)
staleness_starts: Start of staleness
theme: Theme
time_zone: Time zone
Expand Down
1 change: 1 addition & 0 deletions config/locales/fi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fi:
show_project_on_todo_done: Siirry projektinäkymään toimenpiteen valmistuessa
sms_context: Sähköpostin oletusasiayhteys
sms_email: Lähettäjän sähköpostiosoite
sms_permitted_senders: Sallitut lähettäjät (käytä pilkkua erottimena)
staleness_starts: Vanhentumisen alkaminen
time_zone: Aikavyöhyke
title_date_format: Otsakkeen päivämäärämuoto
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddSmspermittedsendersToPreference < ActiveRecord::Migration[5.2]
def change
add_column :preferences, :sms_permitted_senders, :string
end
end
21 changes: 11 additions & 10 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
# This file is the source Rails uses to define your schema when running `rails
# db:schema:load`. When creating a new database, `rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_08_20_000743) do
ActiveRecord::Schema.define(version: 2022_02_22_184000) do

create_table "attachments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.integer "todo_id"
Expand Down Expand Up @@ -88,14 +88,15 @@
t.string "locale"
t.integer "review_period", default: 14, null: false
t.string "theme"
t.string "sms_permitted_senders"
t.index ["user_id"], name: "index_preferences_on_user_id"
end

create_table "projects", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.string "name", null: false
t.integer "position", default: 0
t.integer "user_id", default: 1
t.text "description", limit: 16777215
t.text "description", size: :medium
t.string "state", limit: 20, null: false
t.datetime "created_at"
t.datetime "updated_at"
Expand All @@ -114,7 +115,7 @@
t.integer "context_id", null: false
t.integer "project_id"
t.string "description", null: false
t.text "notes", limit: 16777215
t.text "notes", size: :medium
t.string "state", limit: 20, null: false
t.datetime "start_from"
t.string "ends_on"
Expand Down Expand Up @@ -168,7 +169,7 @@
t.integer "context_id", null: false
t.integer "project_id"
t.text "description", null: false
t.text "notes", limit: 16777215
t.text "notes", size: :medium
t.datetime "created_at"
t.datetime "due"
t.datetime "completed_at"
Expand All @@ -177,7 +178,7 @@
t.string "state", limit: 20, null: false
t.integer "recurring_todo_id"
t.datetime "updated_at"
t.text "rendered_notes", limit: 16777215
t.text "rendered_notes", size: :medium
t.index ["context_id"], name: "index_todos_on_context_id"
t.index ["project_id"], name: "index_todos_on_project_id"
t.index ["state"], name: "index_todos_on_state"
Expand Down