From 437690119b0b1bd462d45f2367c1fe805e529972 Mon Sep 17 00:00:00 2001
From: Eric Davis
Date: Tue, 28 Sep 2010 22:13:06 +0000
Subject: [PATCH] Allow admins to edit user's email notifications and
preferences. #3503
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4223 e93f8b46-1217-0410-a6f0-8f06a7374b81
---
app/controllers/users_controller.rb | 43 ++++++++++++++++++++
app/models/user.rb | 2 +-
app/views/my/account.rhtml | 16 +-------
app/views/users/_form.rhtml | 10 +++++
app/views/users/_mail_notifications.html.erb | 12 ++++++
app/views/users/_preferences.html.erb | 6 +++
test/functional/users_controller_test.rb | 17 ++++++--
7 files changed, 88 insertions(+), 18 deletions(-)
create mode 100644 app/views/users/_mail_notifications.html.erb
create mode 100644 app/views/users/_preferences.html.erb
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index f22427a66..fb7a0e10d 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -72,17 +72,40 @@ class UsersController < ApplicationController
end
def add
+ @notification_options = User::MAIL_NOTIFICATION_OPTIONS
+ @notification_option = Setting.default_notification_option
+
@user = User.new(:language => Setting.default_language)
@auth_sources = AuthSource.find(:all)
+
+ # TODO: Similar to My#account
+ # Only users that belong to more than 1 project can select projects for which they are notified
+ # Note that @user.membership.size would fail since AR ignores
+ # :include association option when doing a count
+ if @user.memberships.length < 1
+ @notification_options.delete_if {|option| option.first == :selected}
+ end
end
verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
def create
+ @notification_options = User::MAIL_NOTIFICATION_OPTIONS
+ @notification_option = Setting.default_notification_option
+
@user = User.new(params[:user])
@user.admin = params[:user][:admin] || false
@user.login = params[:user][:login]
@user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless @user.auth_source_id
+
+ # TODO: Similar to My#account
+ @user.mail_notification = params[:notification_option] || 'only_my_events'
+ @user.pref.attributes = params[:pref]
+ @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
+
if @user.save
+ @user.pref.save
+ @user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : [])
+
Mailer.deliver_account_information(@user, params[:password]) if params[:send_information]
flash[:notice] = l(:notice_successful_create)
redirect_to(params[:continue] ? {:controller => 'users', :action => 'add'} :
@@ -90,12 +113,24 @@ class UsersController < ApplicationController
return
else
@auth_sources = AuthSource.find(:all)
+ @notification_option = @user.mail_notification
+
render :action => 'add'
end
end
def edit
@user = User.find(params[:id])
+ # TODO: Similar to My#account
+ @notification_options = User::MAIL_NOTIFICATION_OPTIONS
+ # Only users that belong to more than 1 project can select projects for which they are notified
+ # Note that @user.membership.size would fail since AR ignores
+ # :include association option when doing a count
+ if @user.memberships.length < 1
+ @notification_options.delete_if {|option| option.first == :selected}
+ end
+ @notification_option = @user.mail_notification
+
if request.post?
@user.admin = params[:user][:admin] if params[:user][:admin]
@user.login = params[:user][:login] if params[:user][:login]
@@ -106,7 +141,15 @@ class UsersController < ApplicationController
@user.attributes = params[:user]
# Was the account actived ? (do it before User#save clears the change)
was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
+ # TODO: Similar to My#account
+ @user.mail_notification = params[:notification_option] || 'only_my_events'
+ @user.pref.attributes = params[:pref]
+ @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
+
if @user.save
+ @user.pref.save
+ @user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : [])
+
if was_activated
Mailer.deliver_account_activated(@user)
elsif @user.active? && params[:send_information] && !params[:password].blank? && @user.auth_source_id.nil?
diff --git a/app/models/user.rb b/app/models/user.rb
index 0ac5e7d63..481c832b4 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -74,7 +74,7 @@ class User < Principal
validates_confirmation_of :password, :allow_nil => true
def before_create
- self.mail_notification = Setting.default_notification_option
+ self.mail_notification = Setting.default_notification_option if self.mail_notification.blank?
true
end
diff --git a/app/views/my/account.rhtml b/app/views/my/account.rhtml
index b173b2b0d..99b58ffe7 100644
--- a/app/views/my/account.rhtml
+++ b/app/views/my/account.rhtml
@@ -32,24 +32,12 @@
<%=l(:field_mail_notification)%>
-<%= select_tag 'notification_option', options_for_select(@notification_options.collect {|o| [l(o.last), o.first]}, @notification_option.to_sym),
- :onchange => 'if ($("notification_option").value == "selected") {Element.show("notified-projects")} else {Element.hide("notified-projects")}' %>
-<% content_tag 'div', :id => 'notified-projects', :style => (@notification_option == 'selected' ? '' : 'display:none;') do %>
-
<% User.current.projects.each do |project| %>
-
-<% end %>
-
<%= l(:text_user_mail_option) %>
-<% end %>
-
+<%= render :partial => 'users/mail_notifications' %>
<%=l(:label_preferences)%>
-<% fields_for :pref, @user.pref, :builder => TabularFormBuilder, :lang => current_language do |pref_fields| %>
-
<%= pref_fields.check_box :hide_mail %>
-
<%= pref_fields.select :time_zone, ActiveSupport::TimeZone.all.collect {|z| [ z.to_s, z.name ]}, :include_blank => true %>
-
<%= pref_fields.select :comments_sorting, [[l(:label_chronological_order), 'asc'], [l(:label_reverse_chronological_order), 'desc']] %>
-<% end %>
+<%= render :partial => 'users/preferences' %>
diff --git a/app/views/users/_form.rhtml b/app/views/users/_form.rhtml
index 4f9a0ff21..7e50fcdc3 100644
--- a/app/views/users/_form.rhtml
+++ b/app/views/users/_form.rhtml
@@ -32,4 +32,14 @@
<%= password_field_tag 'password_confirmation', nil, :size => 25 %>
+
+
+
<%=l(:field_mail_notification)%>
+<%= render :partial => 'users/mail_notifications' %>
+
+
+
+
<%=l(:label_preferences)%>
+<%= render :partial => 'users/preferences' %>
+
diff --git a/app/views/users/_mail_notifications.html.erb b/app/views/users/_mail_notifications.html.erb
new file mode 100644
index 000000000..d29250893
--- /dev/null
+++ b/app/views/users/_mail_notifications.html.erb
@@ -0,0 +1,12 @@
+
+<%= select_tag 'notification_option', options_for_select(@notification_options.collect {|o| [l(o.last), o.first]}, @notification_option.to_sym),
+ :onchange => 'if ($("notification_option").value == "selected") {Element.show("notified-projects")} else {Element.hide("notified-projects")}' %>
+
+<% content_tag 'div', :id => 'notified-projects', :style => (@notification_option == 'selected' ? '' : 'display:none;') do %>
+<% @user.projects.each do |project| %>
+
+<% end %>
+<%= l(:text_user_mail_option) %>
+<% end %>
+<%= check_box_tag 'no_self_notified', 1, @user.pref[:no_self_notified] %>
+
diff --git a/app/views/users/_preferences.html.erb b/app/views/users/_preferences.html.erb
new file mode 100644
index 000000000..85b5990e3
--- /dev/null
+++ b/app/views/users/_preferences.html.erb
@@ -0,0 +1,6 @@
+<% fields_for :pref, @user.pref, :builder => TabularFormBuilder, :lang => current_language do |pref_fields| %>
+<%= pref_fields.check_box :hide_mail %>
+<%= pref_fields.select :time_zone, ActiveSupport::TimeZone.all.collect {|z| [ z.to_s, z.name ]}, :include_blank => true %>
+<%= pref_fields.select :comments_sorting, [[l(:label_chronological_order), 'asc'], [l(:label_reverse_chronological_order), 'desc']] %>
+<% end %>
+
diff --git a/test/functional/users_controller_test.rb b/test/functional/users_controller_test.rb
index 8e115eac6..6131c7e7d 100644
--- a/test/functional/users_controller_test.rb
+++ b/test/functional/users_controller_test.rb
@@ -127,12 +127,18 @@ class UsersControllerTest < ActionController::TestCase
:password => 'test',
:password_confirmation => 'test',
:mail => 'jdoe@gmail.com'
- }
+ },
+ :notification_option => 'none'
end
should_assign_to :user
should_respond_with :redirect
should_redirect_to('user edit') { {:controller => 'users', :action => 'edit', :id => User.find_by_login('jdoe')}}
+
+ should 'set the users mail notification' do
+ user = User.last
+ assert_equal 'none', user.mail_notification
+ end
end
context "when unsuccessful" do
@@ -149,8 +155,13 @@ class UsersControllerTest < ActionController::TestCase
def test_edit
ActionMailer::Base.deliveries.clear
- post :edit, :id => 2, :user => {:firstname => 'Changed'}
- assert_equal 'Changed', User.find(2).firstname
+ post :edit, :id => 2, :user => {:firstname => 'Changed'}, :notification_option => 'all', :pref => {:hide_mail => '1', :comments_sorting => 'desc'}
+
+ user = User.find(2)
+ assert_equal 'Changed', user.firstname
+ assert_equal 'all', user.mail_notification
+ assert_equal true, user.pref[:hide_mail]
+ assert_equal 'desc', user.pref[:comments_sorting]
assert ActionMailer::Base.deliveries.empty?
end