0002-Add-Mailer-security_notification.patch

Jan from Planio www.plan.io, 2015-12-11 12:33

Download (6.68 KB)

View differences:

app/models/mailer.rb
318 318
      :subject => l(:mail_subject_register, Setting.app_title)
319 319
  end
320 320

  
321
  def security_notification(recipients, options={})
322
    redmine_headers 'Sender' => User.current.login
323
    @user = Array(recipients).detect{|r| r.is_a? User }
324
    set_language_if_valid(@user.try :language)
325
    @message = l(options[:message],
326
      field: (options[:field] && l(options[:field])),
327
      value: options[:value]
328
    )
329
    @title = options[:title] && l(options[:title])
330
    @url = options[:url] && (options[:url].is_a?(Hash) ? url_for(options[:url]) : options[:url])
331
    mail :to => recipients,
332
      :subject => l(:mail_subject_security_notification)
333
  end
334

  
321 335
  def test_email(user)
322 336
    set_language_if_valid(user.language)
323 337
    @url = url_for(:controller => 'welcome')
app/views/mailer/security_notification.html.erb
1
<p><%= @message %><br />
2
<% if @url && @title -%>
3
<%= link_to @title, @url -%>
4
<% elsif @url -%>
5
<%= link_to @url  -%>
6
<% elsif @title -%>
7
<%= content_tag :h1, @title -%>
8
<% end %></p>
9

  
10
<p><%= l(:field_user) %>: <strong><%= User.current.login %></strong><br/>
11
<%= l(:field_remote_ip) %>: <strong><%= User.current.remote_ip %></strong><br/>
12
<%= l(:label_date) %>: <strong><%= format_time Time.now, true, @user %></strong></p>
13

  
app/views/mailer/security_notification.text.erb
1
<%= @message %>
2

  
3
<%= @url || @title %>
4

  
5
<%= l(:field_user) %>: <%= User.current.login %>
6
<%= l(:field_remote_ip) %>: <%= User.current.remote_ip %>
7
<%= l(:label_date) %>: <%= format_time Time.now, true, @user %>
8

  
config/locales/de.yml
848 848
  mail_subject_reminder: "%{count} Tickets müssen in den nächsten %{days} Tagen abgegeben werden"
849 849
  mail_subject_wiki_content_added: "Wiki-Seite '%{id}' hinzugefügt"
850 850
  mail_subject_wiki_content_updated: "Wiki-Seite '%{id}' erfolgreich aktualisiert"
851
  mail_subject_security_notification: "Sicherheitshinweis"
852
  mail_body_security_notification_change: "%{field} wurde geändert."
853
  mail_body_security_notification_change_to: "%{field} wurde geändert zu %{value}."
854
  mail_body_security_notification_add: "%{field} %{value} wurde hinzugefügt."
855
  mail_body_security_notification_remove: "%{field} %{value} wurde entfernt."
851 856

  
852 857
  notice_account_activated: Ihr Konto ist aktiviert. Sie können sich jetzt anmelden.
853 858
  notice_account_deleted: Ihr Benutzerkonto wurde unwiderruflich gelöscht.
config/locales/en.yml
227 227
  mail_body_wiki_content_added: "The '%{id}' wiki page has been added by %{author}."
228 228
  mail_subject_wiki_content_updated: "'%{id}' wiki page has been updated"
229 229
  mail_body_wiki_content_updated: "The '%{id}' wiki page has been updated by %{author}."
230
  mail_subject_security_notification: "Security notification"
231
  mail_body_security_notification_change: "%{field} was changed."
232
  mail_body_security_notification_change_to: "%{field} was changed to %{value}."
233
  mail_body_security_notification_add: "%{field} %{value} was added."
234
  mail_body_security_notification_remove: "%{field} %{value} was removed."
230 235

  
231 236
  field_name: Name
232 237
  field_description: Description
test/unit/mailer_test.rb
666 666
    end
667 667
  end
668 668

  
669
  def test_security_notification
670
    set_language_if_valid User.find(1).language
671
    with_settings :emails_footer => "footer without link" do
672
      User.current.remote_ip = '192.168.1.1'
673
      assert Mailer.security_notification(User.find(1), message: :notice_account_password_updated).deliver
674
      mail = last_email
675
      assert_not_nil mail
676
      assert_mail_body_match '192.168.1.1', mail
677
      assert_mail_body_match I18n.t(:notice_account_password_updated), mail
678
      assert_select_email do
679
        assert_select "h1", false
680
        assert_select "a", false
681
      end
682
    end
683
  end
684

  
685
  def test_security_notification_should_include_title
686
    set_language_if_valid User.find(2).language
687
    with_settings :emails_footer => "footer without link" do
688
      assert Mailer.security_notification(User.find(2),
689
        message: :notice_account_password_updated,
690
        title: :label_my_account
691
      ).deliver
692
      assert_select_email do
693
        assert_select "a", false
694
        assert_select "h1", :text => I18n.t(:label_my_account)
695
      end
696
    end
697
  end
698

  
699
  def test_security_notification_should_include_link
700
    set_language_if_valid User.find(3).language
701
    with_settings :emails_footer => "footer without link" do
702
      assert Mailer.security_notification(User.find(3),
703
      message: :notice_account_password_updated,
704
      title: :label_my_account,
705
      url: {controller: 'my', action: 'account'}
706
      ).deliver
707
      assert_select_email do
708
        assert_select "h1", false
709
        assert_select 'a[href=?]', 'http://mydomain.foo/my/account', :text => I18n.t(:label_my_account)
710
      end
711
    end
712
  end
713

  
669 714
  def test_mailer_should_not_change_locale
670 715
    # Set current language to italian
671 716
    set_language_if_valid 'it'
672
-