Feature #2722 » 2722_new_user_notification.patch
| app/controllers/account_controller.rb | ||
|---|---|---|
|
user.activate
|
||
|
if user.save
|
||
|
token.destroy
|
||
|
# Send notification to admins.
|
||
|
Mailer.deliver_activated_to_admins(user)
|
||
|
flash[:notice] = l(:notice_account_activated)
|
||
|
end
|
||
|
redirect_to signin_path
|
||
| ... | ... | |
|
user.activate
|
||
|
user.last_login_on = Time.now
|
||
|
if user.save
|
||
|
# Send notification to admins.
|
||
|
Mailer.deliver_activated_to_admins(user)
|
||
|
self.logged_user = user
|
||
|
flash[:notice] = l(:notice_account_activated)
|
||
|
redirect_to my_account_path
|
||
| app/models/mailer.rb | ||
|---|---|---|
|
# The email will be sent to the email address specifiedby recipient if provided.
|
||
|
#
|
||
|
# Exemple:
|
||
|
# Mailer.deliver_account_activated(user, token)
|
||
|
# Mailer.deliver_account_activated(user, token, 'foo@example.net')
|
||
|
# Mailer.deliver_lost_password(user, token)
|
||
|
# Mailer.deliver_lost_password(user, token, 'foo@example.net')
|
||
|
def self.deliver_lost_password(user, token, recipient=nil)
|
||
|
lost_password(user, token, recipient).deliver_later
|
||
|
end
|
||
| ... | ... | |
|
register(user, token).deliver_later
|
||
|
end
|
||
|
# Builds a mail to admin about user account has activated.
|
||
|
def account_activated_to_admins(admin, user)
|
||
|
@user = user
|
||
|
@url = url_for(:controller => :users, :action => :edit, :id => user.id, :tab => :memberships)
|
||
|
mail :to => admin,
|
||
|
:subject => l(:mail_subject_account_activated, Setting.app_title)
|
||
|
end
|
||
|
# Sends notification to admins about user account has activated.
|
||
|
#
|
||
|
# Exemple:
|
||
|
# Mailer.deliver_activated_to_admins(user)
|
||
|
def self.deliver_activated_to_admins(user)
|
||
|
return unless user.active?
|
||
|
return unless Setting.send_notification_to_admins_when_activated?
|
||
|
# Send the email to all active administrators
|
||
|
admins = User.active.where(:admin => true)
|
||
|
admins.each do |admin|
|
||
|
account_activated_to_admins(admin, user).deliver_later
|
||
|
end
|
||
|
end
|
||
|
# Build a mail to user and the additional recipients given in
|
||
|
# options[:recipients] about a security related event made by sender.
|
||
|
#
|
||
| app/views/mailer/account_activated_to_admins.html.erb | ||
|---|---|---|
|
<p><%= l(:mail_body_account_activated, h(@user.login)) %></p>
|
||
|
<p><%= link_to @url, @url %></p>
|
||
| app/views/mailer/account_activated_to_admins.text.erb | ||
|---|---|---|
|
<%= l(:mail_body_account_activated, @user.login) %>
|
||
|
<%= @url %>
|
||
| app/views/settings/_authentication.html.erb | ||
|---|---|---|
|
[l(:label_registration_manual_activation), "2"],
|
||
|
[l(:label_registration_automatic_activation), "3"]],
|
||
|
:onchange =>
|
||
|
"if (this.value != '0') { $('#settings_show_custom_fields_on_registration').removeAttr('disabled'); } else { $('#settings_show_custom_fields_on_registration').attr('disabled', true); }" %></p>
|
||
|
"$('#settings_show_custom_fields_on_registration').prop('disabled', (this.value == '0')); $('#settings_send_notification_to_admins_when_activated').prop('disabled', (this.value == '0' || this.value == '2'));"
|
||
|
%></p>
|
||
|
<p><%= setting_check_box :show_custom_fields_on_registration,
|
||
|
:disabled => !Setting.self_registration? %></p>
|
||
|
<p><%= setting_check_box :send_notification_to_admins_when_activated,
|
||
|
:disabled => [0, 2].include?(Setting.self_registration.to_i) %></p>
|
||
|
<p><%= setting_text_field :password_min_length, :size => 6 %></p>
|
||
|
<p><%= setting_multiselect :password_required_char_classes, Setting::PASSWORD_CHAR_CLASSES.keys.collect {|c| [l("label_password_char_class_#{c}"), c]} , :inline => true %></p>
|
||
| ... | ... | |
|
<p><%= setting_select :session_lifetime, session_lifetime_options %></p>
|
||
|
<p><%= setting_select :session_timeout, session_timeout_options %></p>
|
||
|
</div>
|
||
|
|
||
|
<p><em class="info"><%= l(:text_session_expiration_settings) %></em></p>
|
||
|
</fieldset>
|
||
| config/locales/en.yml | ||
|---|---|---|
|
mail_body_account_information: Your account information
|
||
|
mail_subject_account_activation_request: "%{value} account activation request"
|
||
|
mail_body_account_activation_request: "A new user (%{value}) has registered. The account is pending your approval:"
|
||
|
mail_subject_account_activated: "%{value} account activated"
|
||
|
mail_body_account_activated: "A new user (%{value}) has been activated. Please set the roles of the projects:"
|
||
|
mail_subject_reminder: "%{count} issue(s) due in the next %{days} days"
|
||
|
mail_body_reminder: "%{count} issue(s) that are assigned to you are due in the next %{days} days:"
|
||
|
mail_subject_wiki_content_added: "'%{id}' wiki page has been added"
|
||
| ... | ... | |
|
setting_login_required: Authentication required
|
||
|
setting_self_registration: Self-registration
|
||
|
setting_show_custom_fields_on_registration: Show custom fields on registration
|
||
|
setting_send_notification_to_admins_when_activated: Send notification to admins when activated
|
||
|
setting_attachment_max_size: Maximum attachment size
|
||
|
setting_issues_export_limit: Issues export limit
|
||
|
setting_mail_from: Emission email address
|
||
| config/settings.yml | ||
|---|---|---|
|
security_notifications: 1
|
||
|
show_custom_fields_on_registration:
|
||
|
default: 1
|
||
|
send_notification_to_admins_when_activated:
|
||
|
default: 0
|
||
|
lost_password:
|
||
|
default: 1
|
||
|
security_notifications: 1
|
||
| test/functional/account_controller_test.rb | ||
|---|---|---|
|
require File.expand_path('../../test_helper', __FILE__)
|
||
|
class AccountControllerTest < Redmine::ControllerTest
|
||
|
fixtures :users, :email_addresses, :roles
|
||
|
fixtures :users, :email_addresses, :roles, :auth_sources, :tokens
|
||
|
def setup
|
||
|
User.current = nil
|
||
| ... | ... | |
|
end
|
||
|
end
|
||
|
def test_get_activate_with_token_should_send_notification_to_admins
|
||
|
with_settings :send_notification_to_admins_when_activated => '1' do
|
||
|
user = User.generate!(:status => User::STATUS_REGISTERED)
|
||
|
assert !user.active?
|
||
|
token = Token.create(:user => user, :action => 'register')
|
||
|
ActionMailer::Base.deliveries.clear
|
||
|
get :activate, :params => {
|
||
|
:token => token.value
|
||
|
}
|
||
|
assert_redirected_to '/login'
|
||
|
user.reload
|
||
|
assert user.active?
|
||
|
assert_equal 1, ActionMailer::Base.deliveries.size
|
||
|
mail = ActionMailer::Base.deliveries.last
|
||
|
assert_match /\saccount\sactivated\z/, mail.subject
|
||
|
end
|
||
|
end
|
||
|
def test_get_activate_with_token_should_not_send_notification_to_admins
|
||
|
with_settings :send_notification_to_admins_when_activated => '0' do
|
||
|
user = User.generate!(:status => User::STATUS_REGISTERED)
|
||
|
assert !user.active?
|
||
|
token = Token.create(:user => user, :action => 'register')
|
||
|
ActionMailer::Base.deliveries.clear
|
||
|
get :activate, :params => {
|
||
|
:token => token.value
|
||
|
}
|
||
|
assert_redirected_to '/login'
|
||
|
user.reload
|
||
|
assert user.active?
|
||
|
assert_equal 0, ActionMailer::Base.deliveries.size
|
||
|
end
|
||
|
end
|
||
|
# See integration/account_test.rb for the full test
|
||
|
def test_post_register_with_registration_on
|
||
|
with_settings :self_registration => '3' do
|
||
|
with_settings :self_registration => '3', :send_notification_to_admins_when_activated => '1' do
|
||
|
ActionMailer::Base.deliveries.clear
|
||
|
assert_difference 'User.count' do
|
||
|
post :register, :params => {
|
||
|
:user => {
|
||
| ... | ... | |
|
:firstname => 'John',
|
||
|
:lastname => 'Doe',
|
||
|
:mail => 'register@example.com'
|
||
|
}
|
||
|
}
|
||
|
assert_redirected_to '/my/account'
|
||
|
end
|
||
|
assert_equal 1, ActionMailer::Base.deliveries.size
|
||
|
mail = ActionMailer::Base.deliveries.last
|
||
|
assert_match /\saccount\sactivated\z/, mail.subject
|
||
|
user = User.order('id DESC').first
|
||
|
assert_equal 'register', user.login
|
||
|
assert_equal 'John', user.firstname
|
||
| test/unit/mailer_test.rb | ||
|---|---|---|
|
end
|
||
|
end
|
||
|
def test_activated_account_should_send_notification_to_admins
|
||
|
with_settings :send_notification_to_admins_when_activated => '1' do
|
||
|
user = User.generate!(:login => 'foobar', :status => User::STATUS_ACTIVE)
|
||
|
Mailer.deliver_activated_to_admins(user)
|
||
|
assert_equal 1, ActionMailer::Base.deliveries.size
|
||
|
mail = last_email
|
||
|
assert_match /\saccount\sactivated\z/, mail.subject
|
||
|
assert_equal [User.find_by_login('admin').mail], mail.bcc
|
||
|
assert_select_email do
|
||
|
assert_select 'p', :text => 'A new user (foobar) has been activated. Please set the roles of the projects:'
|
||
|
url = "http://localhost:3000/users/#{user.id}/edit?tab=memberships"
|
||
|
assert_select 'a[href=?]', url, :text => url
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
def test_activated_account_should_not_send_notification_to_admins
|
||
|
with_settings :send_notification_to_admins_when_activated => '0' do
|
||
|
user = User.generate!(:login => 'foobar', :status => User::STATUS_ACTIVE)
|
||
|
Mailer.deliver_activated_to_admins(user)
|
||
|
assert_equal 0, ActionMailer::Base.deliveries.size
|
||
|
end
|
||
|
end
|
||
|
def test_test_email_later
|
||
|
user = User.find(1)
|
||
|
assert Mailer.test_email(user).deliver_later
|
||