diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -41,6 +41,7 @@ if Configuration.multicontests? user = User.find(session[:user_id]) + return true if user.admin? begin if user.contest_stat(true).forced_logout flash[:notice] = 'You have been automatically logged out.' diff --git a/app/controllers/user_admin_controller.rb b/app/controllers/user_admin_controller.rb --- a/app/controllers/user_admin_controller.rb +++ b/app/controllers/user_admin_controller.rb @@ -1,5 +1,7 @@ class UserAdminController < ApplicationController + include MailHelperMethods + before_filter :admin_authorization def index @@ -215,6 +217,10 @@ user.contest_stat.reset_timer_and_save end + if params[:notification_emails] + send_contest_update_notification_email(user, contest) + end + note << user.login users << user end @@ -344,4 +350,19 @@ end end + def send_contest_update_notification_email(user, contest) + contest_title_name = Configuration['contest.name'] + contest_name = contest.name + subject = t('contest.notification.email_subject', { + :contest_title_name => contest_title_name, + :contest_name => contest_name }) + body = t('contest.notification.email_body', { + :full_name => user.full_name, + :contest_title_name => contest_title_name, + :contest_name => contest.name, + }) + + logger.info body + send_mail(user.email, subject, body) + end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -3,6 +3,8 @@ class UsersController < ApplicationController + include MailHelperMethods + before_filter :authenticate, :except => [:new, :register, :confirm, @@ -122,64 +124,35 @@ :login => user.login, :activation => user.activation_key) home_url = url_for(:controller => 'main', :action => 'index') - mail = TMail::Mail.new - mail.to = user.email - mail.from = Configuration['system.online_registration.from'] - mail.subject = "[#{contest_name}] Confirmation" - mail.body = t('registration.email_body', { - :full_name => user.full_name, - :contest_name => contest_name, - :login => user.login, - :password => user.password, - :activation_url => activation_url, - :admin_email => admin_email - }) + subject = "[#{contest_name}] Confirmation" + body = t('registration.email_body', { + :full_name => user.full_name, + :contest_name => contest_name, + :login => user.login, + :password => user.password, + :activation_url => activation_url, + :admin_email => admin_email + }) - logger.info mail.body - - smtp_server = Configuration['system.online_registration.smtp'] + logger.info body - begin - Net::SMTP.start(smtp_server) do |smtp| - smtp.send_message(mail.to_s, mail.from, mail.to) - end - result = true - rescue - result = false - end - - return result + send_mail(user.email, subject, body) end def send_new_password_email(user) contest_name = Configuration['contest.name'] admin_email = Configuration['system.admin_email'] - mail = TMail::Mail.new - mail.to = user.email - mail.from = Configuration['system.online_registration.from'] - mail.subject = "[#{contest_name}] Password recovery" - mail.body = t('registration.password_retrieval.email_body', { - :full_name => user.full_name, - :contest_name => contest_name, - :login => user.login, - :password => user.password, - :admin_email => admin_email - }) + subject = "[#{contest_name}] Password recovery" + body = t('registration.password_retrieval.email_body', { + :full_name => user.full_name, + :contest_name => contest_name, + :login => user.login, + :password => user.password, + :admin_email => admin_email + }) - logger.info mail.body - - smtp_server = Configuration['system.online_registration.smtp'] - - begin - Net::SMTP.start(smtp_server) do |smtp| - smtp.send_message(mail.to_s, mail.from, mail.to) - end - result = true - rescue - result = false - end - - return result + logger.info body + send_mail(user.email, subject, body) end end diff --git a/app/views/user_admin/contest_management.html.haml b/app/views/user_admin/contest_management.html.haml --- a/app/views/user_admin/contest_management.html.haml +++ b/app/views/user_admin/contest_management.html.haml @@ -5,13 +5,20 @@ %br/ = text_area_tag 'login_list', nil, :rows => 23, :cols => 80 %br/ - You want to - = select(nil,"operation",[['assign users to','assign'],['add users to','add'],['remove users from','remove']]) - contest - = select("contest","id",Contest.all.collect {|c| [c.title, c.id]}) - = check_box_tag 'reset_timer' - Auto-reset current contest timer. - %br/ + %table + %tr + %td{:valign => 'top'} + You want to + = select(nil,"operation",[['assign users to','assign'],['add users to','add'],['remove users from','remove']]) + contest + = select("contest","id",Contest.all.collect {|c| [c.title, c.id]}) + and also + %td + = check_box_tag 'reset_timer' + Auto-reset current contest timer. + %br/ + = check_box_tag 'notification_emails' + Send notification email(s). = submit_tag "Perform action!", :confirm => 'Are you sure?' diff --git a/config/locales/en.yml b/config/locales/en.yml --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -119,6 +119,18 @@ Thanks!" + contest: + notification: + email_subject: "[{{contest_title_name}}] You have been upgraded to {{contest_name}}" + email_body: "Congratulations {{full_name}}, + +You have advanced to contest {{contest_name}} in {{contest_title_name}}. + +You can now log-in and start participating in that contest. + +Cheers! +-{{contest_title_name}} Admin" + help: how_to_submit: "How to submit" must_specify_language: "You must specify the language you are using in your program header. You can optionally specify the task you are submitting to." diff --git a/config/locales/th.yml b/config/locales/th.yml --- a/config/locales/th.yml +++ b/config/locales/th.yml @@ -78,8 +78,8 @@ เพื่อเปิดใช้งานบัญชีของคุณ -ถ้าคุณไม่ใช้คนที่ลงทะเบียน กรุณาละทิ้ง e-mail ฉบับนี้ -และแจ้งความผิดพลาดนี้กับ {{admin_email}} +ถ้าคุณไม่ใช่คนที่ลงทะเบียน กรุณาละทิ้ง e-mail ฉบับนี้ +และแจ้งความผิดพลาดนี้กับ {{admin_email}} ด้วย ขอบคุณมาก!" @@ -114,9 +114,21 @@ รหัสผ่านคือ: {{password}} ถ้าคุณไม่ได้ขอรหัสผ่านใหม่ หรือไม่ใช่คนที่ลงทะเบียน กรุณาละทิ้ง e-mail ฉบับนี้ -และแจ้งความผิดพลาดนี้กับ {{admin_email}} +และแจ้งความผิดพลาดนี้กับ {{admin_email}} ด้วย ขอบคุณมาก!" + + contest: + notification: + email_subject: "[{{contest_title_name}}] คุณได้รับการเลื่อนขั้นสู่ระดับ {{contest_name}}" + email_body: "ขอแสดงความยินดีด้วย คุณ{{full_name}} + +คุณได้รับการเลื่อนขั้นให้สามารถแข่งขันในการแข่งขันระดับ {{contest_name}} ของ {{contest_title_name}} แล้ว + +คุณสามารถเข้าสู่ระบบเพื่อทำโจทย์ได้ทันที คุณสามารถแข่งในระดับ {{contest_name}} ได้จนกระทั่งการแข่งขันรอบนี้สิ้นสุดลง + +-ทีมงาน {{contest_title_name}}" + help: how_to_submit: "วิธีการส่งโปรแกรม" diff --git a/lib/mail_helper_methods.rb b/lib/mail_helper_methods.rb new file mode 100644 --- /dev/null +++ b/lib/mail_helper_methods.rb @@ -0,0 +1,36 @@ +module MailHelperMethods + + def send_mail(to, subject, body) + mail = TMail::Mail.new + mail.to = to + mail.from = Configuration['system.online_registration.from'] + mail.subject = subject + mail.body = body + + smtp_server = Configuration['system.online_registration.smtp'] + + if ['fake', 'debug'].include? smtp_server + puts "------------------------- +To: #{mail.to} +From: #{mail.from} +Subject: #{mail.subject} +#{mail.body} +-------------------------- +" + return true + end + + begin + Net::SMTP.start(smtp_server) do |smtp| + smtp.send_message(mail.to_s, mail.from, mail.to) + end + result = true + rescue + result = false + end + + result + end + +end + diff --git a/spec/integration/contest_managements_spec.rb b/spec/integration/contest_managements_spec.rb --- a/spec/integration/contest_managements_spec.rb +++ b/spec/integration/contest_managements_spec.rb @@ -22,51 +22,70 @@ end it "should reset users' timer when their contests change" do - james_session = open_session - james_session.extend(MainSessionMethods) + session = open_session + session.extend(MainSessionMethods) - james_login_and_get_main_list(james_session) - james_session.response.should_not have_text(/OVER/) + james_login_and_get_main_list(session) + session.response.should_not have_text(/OVER/) Delorean.time_travel_to(190.minutes.since) do - james_session.get_main_list - james_session.response.should have_text(/OVER/) + session.get_main_list + session.response.should have_text(/OVER/) - james_session.get '/' # logout - james_session.get '/main/list' # clearly log out - james_session.response.should_not render_template 'main/list' + session.get '/' # logout + session.get '/main/list' # clearly log out + session.response.should_not render_template 'main/list' admin_change_users_contest_to("james", @contest_b, true) - james_login_and_get_main_list(james_session) - james_session.response.should_not have_text(/OVER/) + james_login_and_get_main_list(session) + session.response.should_not have_text(/OVER/) end end it "should force users to log out when their contests change" do - james_session = open_session - james_session.extend(MainSessionMethods) + session = open_session + session.extend(MainSessionMethods) - james_login_and_get_main_list(james_session) - james_session.response.should_not have_text(/OVER/) + james_login_and_get_main_list(session) + session.response.should_not have_text(/OVER/) Delorean.time_travel_to(190.minutes.since) do - james_session.get_main_list - james_session.response.should have_text(/OVER/) + session.get_main_list + session.response.should have_text(/OVER/) admin_change_users_contest_to("james", @contest_b, true) - james_session.get '/main/list' - james_session.response.should_not render_template 'main/list' - james_session.should be_redirect + session.get '/main/list' + session.response.should_not render_template 'main/list' + session.should be_redirect Delorean.time_travel_to(200.minutes.since) do - james_login_and_get_main_list(james_session) - james_session.response.should_not have_text(/OVER/) + james_login_and_get_main_list(session) + session.response.should_not have_text(/OVER/) end end end + it "should maintain admin logged-in sessions even when their contests change" do + session = open_session + session.extend(MainSessionMethods) + + session.login('mary', 'goodbye') + session.get_main_list + session.response.should_not have_text(/OVER/) + + Delorean.time_travel_to(190.minutes.since) do + session.get_main_list + session.response.should have_text(/OVER/) + + admin_change_users_contest_to("mary", @contest_b, true) + + session.get '/main/list' + session.response.should render_template 'main/list' + end + end + private module MainSessionMethods