Description:
sends mass emails
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r309:5760cdcf6707 - - 3 files changed: 63 inserted, 1 deleted

@@ -0,0 +1,19
1 + %h1 Send mass e-mails
2 +
3 + - form_tag :action => 'bulk_mail' do
4 + %b List recipients' login below; one per line.
5 + %br/
6 + = text_area_tag 'login_list', nil, :rows => 7, :cols => 80
7 + %br/
8 + %b Subject:
9 + = text_field_tag 'subject', '', :size => 60
10 + %br/
11 + %b Email body:
12 + %br/
13 + = text_area_tag 'email_body', nil, :rows => 11, :cols => 80
14 + %br/
15 +
16 + = submit_tag "Send mails", :confirm => 'Are you sure?'
17 +
18 + %hr/
19 + = link_to '[go back to index]', :action => 'index'
@@ -1,111 +1,114
1 1 class UserAdminController < ApplicationController
2 2
3 3 include MailHelperMethods
4 4
5 5 before_filter :admin_authorization
6 6
7 7 def index
8 8 list
9 9 render :action => 'list'
10 10 end
11 11
12 12 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
13 13 verify :method => :post, :only => [ :destroy,
14 14 :create, :create_from_list,
15 - :update ],
15 + :update,
16 + :manage_contest,
17 + :bulk_mail
18 + ],
16 19 :redirect_to => { :action => :list }
17 20
18 21 def list
19 22 @user_count = User.count
20 23 if params[:page] == 'all'
21 24 @users = User.all
22 25 @paginated = false
23 26 else
24 27 @users = User.paginate :page => params[:page]
25 28 @paginated = true
26 29 end
27 30 @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at']
28 31 @contests = Contest.enabled
29 32 end
30 33
31 34 def active
32 35 sessions = ActiveRecord::SessionStore::Session.find(:all, :conditions => ["updated_at >= ?", 60.minutes.ago])
33 36 @users = []
34 37 sessions.each do |session|
35 38 if session.data[:user_id]
36 39 @users << User.find(session.data[:user_id])
37 40 end
38 41 end
39 42 end
40 43
41 44 def show
42 45 @user = User.find(params[:id])
43 46 end
44 47
45 48 def new
46 49 @user = User.new
47 50 end
48 51
49 52 def create
50 53 @user = User.new(params[:user])
51 54 @user.activated = true
52 55 if @user.save
53 56 flash[:notice] = 'User was successfully created.'
54 57 redirect_to :action => 'list'
55 58 else
56 59 render :action => 'new'
57 60 end
58 61 end
59 62
60 63 def create_from_list
61 64 lines = params[:user_list]
62 65
63 66 note = []
64 67
65 68 lines.split("\n").each do |line|
66 69 items = line.chomp.split(',')
67 70 if items.length>=2
68 71 login = items[0]
69 72 full_name = items[1]
70 73
71 74 added_random_password = false
72 75 if items.length>=3
73 76 password = items[2].chomp(" ")
74 77 user_alias = (items.length>=4) ? items[3] : login
75 78 else
76 79 password = random_password
77 80 user_alias = (items.length>=4) ? items[3] : login
78 81 added_random_password = true
79 82 end
80 83
81 84 user = User.new({:login => login,
82 85 :full_name => full_name,
83 86 :password => password,
84 87 :password_confirmation => password,
85 88 :alias => user_alias})
86 89 user.activated = true
87 90 user.save
88 91
89 92 if added_random_password
90 93 note << "'#{login}' (+)"
91 94 else
92 95 note << login
93 96 end
94 97 end
95 98 end
96 99 flash[:notice] = 'User(s) ' + note.join(', ') +
97 100 ' were successfully created. ' +
98 101 '( (+) - created with random passwords.)'
99 102 redirect_to :action => 'list'
100 103 end
101 104
102 105 def edit
103 106 @user = User.find(params[:id])
104 107 end
105 108
106 109 def update
107 110 @user = User.find(params[:id])
108 111 if @user.update_attributes(params[:user])
109 112 flash[:notice] = 'User was successfully updated.'
110 113 redirect_to :action => 'show', :id => @user
111 114 else
@@ -208,192 +211,231
208 211 def contest_management
209 212 end
210 213
211 214 def manage_contest
212 215 contest = Contest.find(params[:contest][:id])
213 216 if !contest
214 217 flash[:notice] = 'You did not choose the contest.'
215 218 redirect_to :action => 'contest_management' and return
216 219 end
217 220
218 221 operation = params[:operation]
219 222
220 223 if not ['add','remove','assign'].include? operation
221 224 flash[:notice] = 'You did not choose the operation to perform.'
222 225 redirect_to :action => 'contest_management' and return
223 226 end
224 227
225 228 lines = params[:login_list]
226 229 if !lines or lines.blank?
227 230 flash[:notice] = 'You entered an empty list.'
228 231 redirect_to :action => 'contest_management' and return
229 232 end
230 233
231 234 note = []
232 235 users = []
233 236 lines.split("\n").each do |line|
234 237 user = User.find_by_login(line.chomp)
235 238 if user
236 239 if operation=='add'
237 240 if ! user.contests.include? contest
238 241 user.contests << contest
239 242 end
240 243 elsif operation=='remove'
241 244 user.contests.delete(contest)
242 245 else
243 246 user.contests = [contest]
244 247 end
245 248
246 249 if params[:reset_timer]
247 250 user.contest_stat.forced_logout = true
248 251 user.contest_stat.reset_timer_and_save
249 252 end
250 253
251 254 if params[:notification_emails]
252 255 send_contest_update_notification_email(user, contest)
253 256 end
254 257
255 258 note << user.login
256 259 users << user
257 260 end
258 261 end
259 262
260 263 if params[:reset_timer]
261 264 logout_users(users)
262 265 end
263 266
264 267 flash[:notice] = 'User(s) ' + note.join(', ') +
265 268 ' were successfully modified. '
266 269 redirect_to :action => 'contest_management'
267 270 end
268 271
269 272 # admin management
270 273
271 274 def admin
272 275 @admins = User.find(:all).find_all {|user| user.admin? }
273 276 end
274 277
275 278 def grant_admin
276 279 login = params[:login]
277 280 user = User.find_by_login(login)
278 281 if user!=nil
279 282 admin_role = Role.find_by_name('admin')
280 283 user.roles << admin_role
281 284 else
282 285 flash[:notice] = 'Unknown user'
283 286 end
284 287 flash[:notice] = 'User added as admins'
285 288 redirect_to :action => 'admin'
286 289 end
287 290
288 291 def revoke_admin
289 292 user = User.find(params[:id])
290 293 if user==nil
291 294 flash[:notice] = 'Unknown user'
292 295 redirect_to :action => 'admin' and return
293 296 elsif user.login == 'root'
294 297 flash[:notice] = 'You cannot revoke admisnistrator permission from root.'
295 298 redirect_to :action => 'admin' and return
296 299 end
297 300
298 301 admin_role = Role.find_by_name('admin')
299 302 user.roles.delete(admin_role)
300 303 flash[:notice] = 'User permission revoked'
301 304 redirect_to :action => 'admin'
302 305 end
303 306
307 + # mass mailing
308 +
309 + def mass_mailing
310 + end
311 +
312 + def bulk_mail
313 + lines = params[:login_list]
314 + if !lines or lines.blank?
315 + flash[:notice] = 'You entered an empty list.'
316 + redirect_to :action => 'mass_mailing' and return
317 + end
318 +
319 + subject = params[:subject]
320 + if !subject or subject.blank?
321 + flash[:notice] = 'You entered an empty mail subject.'
322 + redirect_to :action => 'mass_mailing' and return
323 + end
324 +
325 + body = params[:email_body]
326 + if !body or body.blank?
327 + flash[:notice] = 'You entered an empty mail body.'
328 + redirect_to :action => 'mass_mailing' and return
329 + end
330 +
331 + note = []
332 + users = []
333 + lines.split("\n").each do |line|
334 + user = User.find_by_login(line.chomp)
335 + if user
336 + send_mail(user.email, subject, body)
337 + note << user.login
338 + end
339 + end
340 +
341 + flash[:notice] = 'User(s) ' + note.join(', ') +
342 + ' were successfully modified. '
343 + redirect_to :action => 'mass_mailing'
344 + end
345 +
304 346 protected
305 347
306 348 def random_password(length=5)
307 349 chars = 'abcdefghijkmnopqrstuvwxyz23456789'
308 350 newpass = ""
309 351 length.times { newpass << chars[rand(chars.size-1)] }
310 352 return newpass
311 353 end
312 354
313 355 def import_from_file(f)
314 356 data_hash = YAML.load(f)
315 357 @import_log = ""
316 358
317 359 country_data = data_hash[:countries]
318 360 site_data = data_hash[:sites]
319 361 user_data = data_hash[:users]
320 362
321 363 # import country
322 364 countries = {}
323 365 country_data.each_pair do |id,country|
324 366 c = Country.find_by_name(country[:name])
325 367 if c!=nil
326 368 countries[id] = c
327 369 @import_log << "Found #{country[:name]}\n"
328 370 else
329 371 countries[id] = Country.new(:name => country[:name])
330 372 countries[id].save
331 373 @import_log << "Created #{country[:name]}\n"
332 374 end
333 375 end
334 376
335 377 # import sites
336 378 sites = {}
337 379 site_data.each_pair do |id,site|
338 380 s = Site.find_by_name(site[:name])
339 381 if s!=nil
340 382 @import_log << "Found #{site[:name]}\n"
341 383 else
342 384 s = Site.new(:name => site[:name])
343 385 @import_log << "Created #{site[:name]}\n"
344 386 end
345 387 s.password = site[:password]
346 388 s.country = countries[site[:country_id]]
347 389 s.save
348 390 sites[id] = s
349 391 end
350 392
351 393 # import users
352 394 user_data.each_pair do |id,user|
353 395 u = User.find_by_login(user[:login])
354 396 if u!=nil
355 397 @import_log << "Found #{user[:login]}\n"
356 398 else
357 399 u = User.new(:login => user[:login])
358 400 @import_log << "Created #{user[:login]}\n"
359 401 end
360 402 u.full_name = user[:name]
361 403 u.password = user[:password]
362 404 u.country = countries[user[:country_id]]
363 405 u.site = sites[user[:site_id]]
364 406 u.activated = true
365 407 u.email = "empty-#{u.login}@none.com"
366 408 if not u.save
367 409 @import_log << "Errors\n"
368 410 u.errors.each { |attr,msg| @import_log << "#{attr} - #{msg}\n" }
369 411 end
370 412 end
371 413
372 414 end
373 415
374 416 def logout_users(users)
375 417 users.each do |user|
376 418 contest_stat = user.contest_stat(true)
377 419 if contest_stat and !contest_stat.forced_logout
378 420 contest_stat.forced_logout = true
379 421 contest_stat.save
380 422 end
381 423 end
382 424 end
383 425
384 426 def send_contest_update_notification_email(user, contest)
385 427 contest_title_name = Configuration['contest.name']
386 428 contest_name = contest.name
387 429 subject = t('contest.notification.email_subject', {
388 430 :contest_title_name => contest_title_name,
389 431 :contest_name => contest_name })
390 432 body = t('contest.notification.email_body', {
391 433 :full_name => user.full_name,
392 434 :contest_title_name => contest_title_name,
393 435 :contest_name => contest.name,
394 436 })
395 437
396 438 logger.info body
397 439 send_mail(user.email, subject, body)
398 440 end
399 441
@@ -1,85 +1,86
1 1 <h1>Listing users</h1>
2 2
3 3 <div class="submitbox">
4 4 <b>Quick add</b>
5 5 <% form_tag :action => 'create' do %>
6 6 <table border="0">
7 7 <tr>
8 8 <td><label for="user_login">Login</label></td>
9 9 <td><label for="user_full_name">Full name</label></td>
10 10 <td><label for="user_password">Password</label></td>
11 11 <td><label for="user_password_confirmation">Confirm</label></td>
12 12 <td><label for="user_email">Email</label></td>
13 13 </tr>
14 14 <tr>
15 15 <td><%= text_field 'user', 'login', :size => 10 %></td>
16 16 <td><%= text_field 'user', 'full_name', :size => 30 %></td>
17 17 <td><%= password_field 'user', 'password', :size => 10 %></td>
18 18 <td><%= password_field 'user', 'password_confirmation', :size => 10 %></td>
19 19 <td><%= text_field 'user', 'email', :size => 15 %></td>
20 20 <td><%= submit_tag "Create" %></td>
21 21 </tr>
22 22 </table>
23 23 <% end %>
24 24 <br/>
25 25 <b>Import from site management</b>
26 26 <% form_tag({:action => 'import'}, :multipart => true) do %>
27 27 File: <%= file_field_tag 'file' %> <%= submit_tag 'Import' %>
28 28 <% end %>
29 29 <br/>
30 30 <b>What else: </b>
31 31 <%= link_to '[New user]', :action => 'new' %>
32 32 <%= link_to '[New list of users]', :action => 'new_list' %>
33 33 <%= link_to '[View administrators]', :action => 'admin' %>
34 34 <%= link_to '[Random passwords]', :action => 'random_all_passwords' %>
35 35 <%= link_to '[View active users]', :action => 'active' %>
36 + <%= link_to '[Mass mailing]', :action => 'mass_mailing' %>
36 37 <% if Configuration.multicontests? %>
37 38 <br/><b>Multi-contest:</b>
38 39 <%= link_to '[Manage bulk users in contests]', :action => 'contest_management' %>
39 40 View users in:
40 41 <% @contests.each do |contest| %>
41 42 <%= link_to "[#{contest.name}]", :action => 'contests', :id => contest.id %>
42 43 <% end %>
43 44 <%= link_to "[no contest]", :action => 'contests', :id => 'none' %>
44 45 <% end %>
45 46 </div>
46 47
47 48 Total <%= @user_count %> users |
48 49 <% if !@paginated %>
49 50 Display all users.
50 51 <%= link_to '[show in pages]', :action => 'list', :page => '1' %>
51 52 <% else %>
52 53 Display in pages.
53 54 <%= link_to '[display all]', :action => 'list', :page => 'all' %> |
54 55 <%= will_paginate @users, :container => false %>
55 56 <% end %>
56 57 <table class="info">
57 58 <tr class="info-head">
58 59 <% for column in User.content_columns %>
59 60 <% if !@hidden_columns.index(column.name) %>
60 61 <th><%= column.human_name %></th>
61 62 <% end %>
62 63 <% end %>
63 64 <th></th>
64 65 <th></th>
65 66 <th></th>
66 67 </tr>
67 68
68 69 <% for user in @users %>
69 70 <tr class="info-<%= cycle("odd","even") %>">
70 71 <% for column in User.content_columns %>
71 72 <% if !@hidden_columns.index(column.name) %>
72 73 <td><%=h user.send(column.name) %></td>
73 74 <% end %>
74 75 <% end %>
75 76 <td><%= link_to 'Show', :action => 'show', :id => user %></td>
76 77 <td><%= link_to 'Edit', :action => 'edit', :id => user %></td>
77 78 <td><%= link_to 'Destroy', { :action => 'destroy', :id => user }, :confirm => 'Are you sure?', :method => :post %></td>
78 79 </tr>
79 80 <% end %>
80 81 </table>
81 82
82 83 <br />
83 84
84 85 <%= link_to '[New user]', :action => 'new' %>
85 86 <%= link_to '[New list of users]', :action => 'new_list' %>
You need to be logged in to leave comments. Login now