##// END OF EJS Templates
Allow admins to edit user's email notifications and preferences. #3503...
Eric Davis -
r4109:437690119b0b
parent child
Show More
@@ -0,0 +1,12
1 <p>
2 <%= select_tag 'notification_option', options_for_select(@notification_options.collect {|o| [l(o.last), o.first]}, @notification_option.to_sym),
3 :onchange => 'if ($("notification_option").value == "selected") {Element.show("notified-projects")} else {Element.hide("notified-projects")}' %>
4 </p>
5 <% content_tag 'div', :id => 'notified-projects', :style => (@notification_option == 'selected' ? '' : 'display:none;') do %>
6 <p><% @user.projects.each do |project| %>
7 <label><%= check_box_tag 'notified_project_ids[]', project.id, @user.notified_projects_ids.include?(project.id) %> <%=h project.name %></label><br />
8 <% end %></p>
9 <p><em><%= l(:text_user_mail_option) %></em></p>
10 <% end %>
11 <p><label><%= l(:label_user_mail_no_self_notified) %></label><%= check_box_tag 'no_self_notified', 1, @user.pref[:no_self_notified] %></p>
12
@@ -0,0 +1,6
1 <% fields_for :pref, @user.pref, :builder => TabularFormBuilder, :lang => current_language do |pref_fields| %>
2 <p><%= pref_fields.check_box :hide_mail %></p>
3 <p><%= pref_fields.select :time_zone, ActiveSupport::TimeZone.all.collect {|z| [ z.to_s, z.name ]}, :include_blank => true %></p>
4 <p><%= pref_fields.select :comments_sorting, [[l(:label_chronological_order), 'asc'], [l(:label_reverse_chronological_order), 'desc']] %></p>
5 <% end %>
6
@@ -72,17 +72,40 class UsersController < ApplicationController
72 end
72 end
73
73
74 def add
74 def add
75 @notification_options = User::MAIL_NOTIFICATION_OPTIONS
76 @notification_option = Setting.default_notification_option
77
75 @user = User.new(:language => Setting.default_language)
78 @user = User.new(:language => Setting.default_language)
76 @auth_sources = AuthSource.find(:all)
79 @auth_sources = AuthSource.find(:all)
80
81 # TODO: Similar to My#account
82 # Only users that belong to more than 1 project can select projects for which they are notified
83 # Note that @user.membership.size would fail since AR ignores
84 # :include association option when doing a count
85 if @user.memberships.length < 1
86 @notification_options.delete_if {|option| option.first == :selected}
87 end
77 end
88 end
78
89
79 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
90 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
80 def create
91 def create
92 @notification_options = User::MAIL_NOTIFICATION_OPTIONS
93 @notification_option = Setting.default_notification_option
94
81 @user = User.new(params[:user])
95 @user = User.new(params[:user])
82 @user.admin = params[:user][:admin] || false
96 @user.admin = params[:user][:admin] || false
83 @user.login = params[:user][:login]
97 @user.login = params[:user][:login]
84 @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless @user.auth_source_id
98 @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless @user.auth_source_id
99
100 # TODO: Similar to My#account
101 @user.mail_notification = params[:notification_option] || 'only_my_events'
102 @user.pref.attributes = params[:pref]
103 @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
104
85 if @user.save
105 if @user.save
106 @user.pref.save
107 @user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : [])
108
86 Mailer.deliver_account_information(@user, params[:password]) if params[:send_information]
109 Mailer.deliver_account_information(@user, params[:password]) if params[:send_information]
87 flash[:notice] = l(:notice_successful_create)
110 flash[:notice] = l(:notice_successful_create)
88 redirect_to(params[:continue] ? {:controller => 'users', :action => 'add'} :
111 redirect_to(params[:continue] ? {:controller => 'users', :action => 'add'} :
@@ -90,12 +113,24 class UsersController < ApplicationController
90 return
113 return
91 else
114 else
92 @auth_sources = AuthSource.find(:all)
115 @auth_sources = AuthSource.find(:all)
116 @notification_option = @user.mail_notification
117
93 render :action => 'add'
118 render :action => 'add'
94 end
119 end
95 end
120 end
96
121
97 def edit
122 def edit
98 @user = User.find(params[:id])
123 @user = User.find(params[:id])
124 # TODO: Similar to My#account
125 @notification_options = User::MAIL_NOTIFICATION_OPTIONS
126 # Only users that belong to more than 1 project can select projects for which they are notified
127 # Note that @user.membership.size would fail since AR ignores
128 # :include association option when doing a count
129 if @user.memberships.length < 1
130 @notification_options.delete_if {|option| option.first == :selected}
131 end
132 @notification_option = @user.mail_notification
133
99 if request.post?
134 if request.post?
100 @user.admin = params[:user][:admin] if params[:user][:admin]
135 @user.admin = params[:user][:admin] if params[:user][:admin]
101 @user.login = params[:user][:login] if params[:user][:login]
136 @user.login = params[:user][:login] if params[:user][:login]
@@ -106,7 +141,15 class UsersController < ApplicationController
106 @user.attributes = params[:user]
141 @user.attributes = params[:user]
107 # Was the account actived ? (do it before User#save clears the change)
142 # Was the account actived ? (do it before User#save clears the change)
108 was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
143 was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
144 # TODO: Similar to My#account
145 @user.mail_notification = params[:notification_option] || 'only_my_events'
146 @user.pref.attributes = params[:pref]
147 @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
148
109 if @user.save
149 if @user.save
150 @user.pref.save
151 @user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : [])
152
110 if was_activated
153 if was_activated
111 Mailer.deliver_account_activated(@user)
154 Mailer.deliver_account_activated(@user)
112 elsif @user.active? && params[:send_information] && !params[:password].blank? && @user.auth_source_id.nil?
155 elsif @user.active? && params[:send_information] && !params[:password].blank? && @user.auth_source_id.nil?
@@ -74,7 +74,7 class User < Principal
74 validates_confirmation_of :password, :allow_nil => true
74 validates_confirmation_of :password, :allow_nil => true
75
75
76 def before_create
76 def before_create
77 self.mail_notification = Setting.default_notification_option
77 self.mail_notification = Setting.default_notification_option if self.mail_notification.blank?
78 true
78 true
79 end
79 end
80
80
@@ -32,24 +32,12
32 <div class="splitcontentright">
32 <div class="splitcontentright">
33 <h3><%=l(:field_mail_notification)%></h3>
33 <h3><%=l(:field_mail_notification)%></h3>
34 <div class="box">
34 <div class="box">
35 <%= select_tag 'notification_option', options_for_select(@notification_options.collect {|o| [l(o.last), o.first]}, @notification_option.to_sym),
35 <%= render :partial => 'users/mail_notifications' %>
36 :onchange => 'if ($("notification_option").value == "selected") {Element.show("notified-projects")} else {Element.hide("notified-projects")}' %>
37 <% content_tag 'div', :id => 'notified-projects', :style => (@notification_option == 'selected' ? '' : 'display:none;') do %>
38 <p><% User.current.projects.each do |project| %>
39 <label><%= check_box_tag 'notified_project_ids[]', project.id, @user.notified_projects_ids.include?(project.id) %> <%=h project.name %></label><br />
40 <% end %></p>
41 <p><em><%= l(:text_user_mail_option) %></em></p>
42 <% end %>
43 <p><label><%= check_box_tag 'no_self_notified', 1, @user.pref[:no_self_notified] %> <%= l(:label_user_mail_no_self_notified) %></label></p>
44 </div>
36 </div>
45
37
46 <h3><%=l(:label_preferences)%></h3>
38 <h3><%=l(:label_preferences)%></h3>
47 <div class="box tabular">
39 <div class="box tabular">
48 <% fields_for :pref, @user.pref, :builder => TabularFormBuilder, :lang => current_language do |pref_fields| %>
40 <%= render :partial => 'users/preferences' %>
49 <p><%= pref_fields.check_box :hide_mail %></p>
50 <p><%= pref_fields.select :time_zone, ActiveSupport::TimeZone.all.collect {|z| [ z.to_s, z.name ]}, :include_blank => true %></p>
51 <p><%= pref_fields.select :comments_sorting, [[l(:label_chronological_order), 'asc'], [l(:label_reverse_chronological_order), 'desc']] %></p>
52 <% end %>
53 </div>
41 </div>
54
42
55 </div>
43 </div>
@@ -32,4 +32,14
32 <%= password_field_tag 'password_confirmation', nil, :size => 25 %></p>
32 <%= password_field_tag 'password_confirmation', nil, :size => 25 %></p>
33 </div>
33 </div>
34 </div>
34 </div>
35
36 <div class="box">
37 <h3><%=l(:field_mail_notification)%></h3>
38 <%= render :partial => 'users/mail_notifications' %>
39 </div>
40
41 <div class="box tabular">
42 <h3><%=l(:label_preferences)%></h3>
43 <%= render :partial => 'users/preferences' %>
44 </div>
35 <!--[eoform:user]-->
45 <!--[eoform:user]-->
@@ -127,12 +127,18 class UsersControllerTest < ActionController::TestCase
127 :password => 'test',
127 :password => 'test',
128 :password_confirmation => 'test',
128 :password_confirmation => 'test',
129 :mail => 'jdoe@gmail.com'
129 :mail => 'jdoe@gmail.com'
130 }
130 },
131 :notification_option => 'none'
131 end
132 end
132
133
133 should_assign_to :user
134 should_assign_to :user
134 should_respond_with :redirect
135 should_respond_with :redirect
135 should_redirect_to('user edit') { {:controller => 'users', :action => 'edit', :id => User.find_by_login('jdoe')}}
136 should_redirect_to('user edit') { {:controller => 'users', :action => 'edit', :id => User.find_by_login('jdoe')}}
137
138 should 'set the users mail notification' do
139 user = User.last
140 assert_equal 'none', user.mail_notification
141 end
136 end
142 end
137
143
138 context "when unsuccessful" do
144 context "when unsuccessful" do
@@ -149,8 +155,13 class UsersControllerTest < ActionController::TestCase
149
155
150 def test_edit
156 def test_edit
151 ActionMailer::Base.deliveries.clear
157 ActionMailer::Base.deliveries.clear
152 post :edit, :id => 2, :user => {:firstname => 'Changed'}
158 post :edit, :id => 2, :user => {:firstname => 'Changed'}, :notification_option => 'all', :pref => {:hide_mail => '1', :comments_sorting => 'desc'}
153 assert_equal 'Changed', User.find(2).firstname
159
160 user = User.find(2)
161 assert_equal 'Changed', user.firstname
162 assert_equal 'all', user.mail_notification
163 assert_equal true, user.pref[:hide_mail]
164 assert_equal 'desc', user.pref[:comments_sorting]
154 assert ActionMailer::Base.deliveries.empty?
165 assert ActionMailer::Base.deliveries.empty?
155 end
166 end
156
167
General Comments 0
You need to be logged in to leave comments. Login now