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,9 +3,16 @@ class UsersController < ApplicationController - before_filter :authenticate, :except => [:new, :register, :confirm] + before_filter :authenticate, :except => [:new, + :register, + :confirm, + :forget, + :retrieve_password] - before_filter :verify_online_registration, :only => [:new, :register] + before_filter :verify_online_registration, :only => [:new, + :register, + :forget, + :retrieve_password] verify :method => :post, :only => [:chg_passwd], :redirect_to => { :action => :index } @@ -77,6 +84,28 @@ render :action => 'confirm', :layout => 'empty' end + def forget + render :action => 'forget', :layout => 'empty' + end + + def retrieve_password + email = params[:email] + user = User.find_by_email(email) + if user + last_updated_time = user.updated_at || user.created_at || (Time.now.gmtime - 1.hour) + if last_updated_time > Time.now.gmtime - 5.minutes + flash[:notice] = 'The account has recently created or new password has recently been requested. Please wait for 5 minutes' + else + user.password = user.password_confirmation = User.random_password + send_new_password_email(user) + flash[:notice] = 'New password has been mailed to you.' + end + else + flash[:notice] = I18n.t 'registration.password_retrieval.no_email' + end + redirect_to :action => 'forget' + end + protected def verify_online_registration @@ -121,4 +150,35 @@ return result 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}] Confirmation" + 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 + }) + + 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 + end + end diff --git a/app/views/main/login.html.haml b/app/views/main/login.html.haml --- a/app/views/main/login.html.haml +++ b/app/views/main/login.html.haml @@ -39,4 +39,4 @@ = "#{t 'login.please'} " = link_to "#{t 'login.register'}", :controller => :users, :action => :new %br/ - + = link_to "#{t 'login.forget_password'}", :controller => :users, :action => :forget diff --git a/app/views/users/forget.html.haml b/app/views/users/forget.html.haml new file mode 100644 --- /dev/null +++ b/app/views/users/forget.html.haml @@ -0,0 +1,18 @@ +.contest-title + %h1 + = "#{Configuration['contest.name']}: #{t 'registration.password_retrieval.header'}" + +- if flash[:notice] + %hr/ + %b= flash[:notice] + %hr/ + +%br/ + +- form_tag :action => 'retrieve_password' do + =t 'registration.password_retrieval.instructions' + = text_field 'email', nil, :size => 20 + %br/ + = submit_tag(t 'registration.password_retrieval.button_label') + += link_to "#{t 'go_back_to'}#{t 'home_page'}", :controller => 'main', :action => 'index' diff --git a/config/locales/en.yml b/config/locales/en.yml --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -35,6 +35,7 @@ participation: 'Want to participate?' please: 'Please' register: 'register' + forget_password: 'Forget password?' main: start_soon: "The contest at your site will start soon. Please wait." @@ -55,6 +56,9 @@ registration: title: "New user registration" + + description: "Please enter your information below. Please make sure your e-mail is correct, because you will have to confirm the registration through an e-mail we send to that e-mail address." + successful_title: "Registration successful" login_guide: "Only a-z, A-Z, 0-9 and _. Can be at most 20 characters long" @@ -98,6 +102,23 @@ email_exists: "A user with this E-mail exists." invalid: "Your activation code is invalid. Please check again." + password_retrieval: + header: "Password retrieval" + instructions: "Please enter the e-mail address that you used to register." + button_label: "Request new password" + no_email: "No user with that e-mail address." + email_body: "Hello {{full_name}}, + +You have requested for new password for {{contest_name}}. We have generated it for you: + +user name: {{login}} +password: {{password}} + +If you didn't ask for new password or you're not the person who registered, +please ignore this e-mail and inform {{admin_email}} of this mistake. + +Thanks!" + 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 @@ -35,6 +35,7 @@ participation: 'ต้องการเข้าร่วม?' please: 'กรุณา' register: 'ลงทะเบียน' + forget_password: 'ลืมรหัสผ่าน?' main: start_soon: "การแข่งขันกำลังจะเริ่ม กรุณารอก่อน" @@ -53,7 +54,6 @@ intro: "คุณสามารถทดลองการทำงานของโปรแกรมที่เขียนกับข้อมูลชุดทดสอบของคุณเองในสภาพแวดล้อมจริงของการตรวจโปรแกรมได้ โดยเลือกโปรแกรมส่งแล้วที่ด้านล่างพร้อมทั้งส่งแฟ้มข้อมูลชุดทดสอบที่ต้องการให้ทำงานด้วย" disabled_at_end_announcement: "หมายเหตุ: ระบบทดสอบโปรแกรมจะหยุดทำงานในช่วงเวลา 30 นาทีสุดท้ายของการแข่งขัน" - registration: title: "ลงทะเบียนผู้ใช้ใหม่" description: "ในการลงทะเบียน ให้ผู้สนใจเข้าร่วมการแข่งขันกรอกข้อมูลด้านล่าง จากนั้นระบบจะส่ง e-mail ไปยัง e-mail ที่ระบุเพื่อให้ยืนยันตัวตนและเปิดใช้บัญชีผู้ใช้
ในกรณีที่ผู้เข้าแข่งขันเป็นนักเรียน รบกวนช่วยให้ข้อมูลเกี่ยวกับโรงเรียนและจังหวัดด้วย" @@ -101,6 +101,23 @@ email_exists: "มีผู้ใช้ที่ใช้ e-mail นี้แล้ว" invalid: "รหัสสำหรับยืนยันผิดพลาด กรุณาตรวจสอบอีกครั้ง" + password_retrieval: + header: "การขอรหัสผ่านใหม่" + instructions: "กรุณากรอก e-mail ที่ลงทะเบียน" + button_label: "ขอรหัสผ่านใหม่" + no_email: "ไม่มีบัญชีผู้ใช้ที่ใช้ e-mail ดังกล่าว" + email_body: "สวัสดีครับ {{full_name}}, + +คุณได้ร้องขอรหัสผ่านใหม่ สำหรับการแข่งขัน {{contest_name}} ซึ่งเราได้สร้างให้คุณแล้ว ดังนี้ + +บัญชีเข้าใช้ของคุณคือ: {{login}} +รหัสผ่านคือ: {{password}} + +ถ้าคุณไม่ได้ขอรหัสผ่านใหม่ หรือไม่ใช่คนที่ลงทะเบียน กรุณาละทิ้ง e-mail ฉบับนี้ +และแจ้งความผิดพลาดนี้กับ {{admin_email}} + +ขอบคุณมาก!" + help: how_to_submit: "วิธีการส่งโปรแกรม" must_specify_language: "คุณต้องระบุภาษาโปรแกรมที่ใช้ที่ตอนต้นของรหัสโปรแกรม (source code) นอกจากนี้คุณอาจจะระบุโจทย์ที่ต้องการส่งได้ด้วย"