Description:
added password recovery through e-mails git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@400 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r189:5c9646c12fab - - 5 files changed: 120 inserted, 4 deleted

@@ -0,0 +1,18
1 + .contest-title
2 + %h1
3 + = "#{Configuration['contest.name']}: #{t 'registration.password_retrieval.header'}"
4 +
5 + - if flash[:notice]
6 + %hr/
7 + %b= flash[:notice]
8 + %hr/
9 +
10 + %br/
11 +
12 + - form_tag :action => 'retrieve_password' do
13 + =t 'registration.password_retrieval.instructions'
14 + = text_field 'email', nil, :size => 20
15 + %br/
16 + = submit_tag(t 'registration.password_retrieval.button_label')
17 +
18 + = link_to "#{t 'go_back_to'}#{t 'home_page'}", :controller => 'main', :action => 'index'
@@ -1,124 +1,184
1 1 require 'tmail'
2 2 require 'net/smtp'
3 3
4 4 class UsersController < ApplicationController
5 5
6 - before_filter :authenticate, :except => [:new, :register, :confirm]
6 + before_filter :authenticate, :except => [:new,
7 + :register,
8 + :confirm,
9 + :forget,
10 + :retrieve_password]
7 11
8 - before_filter :verify_online_registration, :only => [:new, :register]
12 + before_filter :verify_online_registration, :only => [:new,
13 + :register,
14 + :forget,
15 + :retrieve_password]
9 16
10 17 verify :method => :post, :only => [:chg_passwd],
11 18 :redirect_to => { :action => :index }
12 19
13 20 #in_place_edit_for :user, :alias_for_editing
14 21 #in_place_edit_for :user, :email_for_editing
15 22
16 23 def index
17 24 if !Configuration['system.user_setting_enabled']
18 25 redirect_to :controller => 'main', :action => 'list'
19 26 else
20 27 @user = User.find(session[:user_id])
21 28 end
22 29 end
23 30
24 31 def chg_passwd
25 32 user = User.find(session[:user_id])
26 33 user.password = params[:passwd]
27 34 user.password_confirmation = params[:passwd_verify]
28 35 if user.save
29 36 flash[:notice] = 'password changed'
30 37 else
31 38 flash[:notice] = 'Error: password changing failed'
32 39 end
33 40 redirect_to :action => 'index'
34 41 end
35 42
36 43 def new
37 44 @user = User.new
38 45 render :action => 'new', :layout => 'empty'
39 46 end
40 47
41 48 def register
42 49 if(params[:cancel])
43 50 redirect_to :controller => 'main', :action => 'login'
44 51 return
45 52 end
46 53 @user = User.new(params[:user])
47 54 @user.password_confirmation = @user.password = User.random_password
48 55 @user.activated = false
49 56 if (@user.valid?) and (@user.save)
50 57 if send_confirmation_email(@user)
51 58 render :action => 'new_splash', :layout => 'empty'
52 59 else
53 60 @admin_email = Configuration['system.admin_email']
54 61 render :action => 'email_error', :layout => 'empty'
55 62 end
56 63 else
57 64 @user.errors.add_to_base("Email cannot be blank") if @user.email==''
58 65 render :action => 'new', :layout => 'empty'
59 66 end
60 67 end
61 68
62 69 def confirm
63 70 login = params[:login]
64 71 key = params[:activation]
65 72 @user = User.find_by_login(login)
66 73 if (@user) and (@user.verify_activation_key(key))
67 74 if @user.valid? # check uniquenss of email
68 75 @user.activated = true
69 76 @user.save
70 77 @result = :successful
71 78 else
72 79 @result = :email_used
73 80 end
74 81 else
75 82 @result = :failed
76 83 end
77 84 render :action => 'confirm', :layout => 'empty'
78 85 end
79 86
87 + def forget
88 + render :action => 'forget', :layout => 'empty'
89 + end
90 +
91 + def retrieve_password
92 + email = params[:email]
93 + user = User.find_by_email(email)
94 + if user
95 + last_updated_time = user.updated_at || user.created_at || (Time.now.gmtime - 1.hour)
96 + if last_updated_time > Time.now.gmtime - 5.minutes
97 + flash[:notice] = 'The account has recently created or new password has recently been requested. Please wait for 5 minutes'
98 + else
99 + user.password = user.password_confirmation = User.random_password
100 + send_new_password_email(user)
101 + flash[:notice] = 'New password has been mailed to you.'
102 + end
103 + else
104 + flash[:notice] = I18n.t 'registration.password_retrieval.no_email'
105 + end
106 + redirect_to :action => 'forget'
107 + end
108 +
80 109 protected
81 110
82 111 def verify_online_registration
83 112 if !Configuration['system.online_registration']
84 113 redirect_to :controller => 'main', :action => 'login'
85 114 end
86 115 end
87 116
88 117 def send_confirmation_email(user)
89 118 contest_name = Configuration['contest.name']
90 119 admin_email = Configuration['system.admin_email']
91 120 activation_url = url_for(:action => 'confirm',
92 121 :login => user.login,
93 122 :activation => user.activation_key)
94 123 home_url = url_for(:controller => 'main', :action => 'index')
95 124 mail = TMail::Mail.new
96 125 mail.to = user.email
97 126 mail.from = Configuration['system.online_registration.from']
98 127 mail.subject = "[#{contest_name}] Confirmation"
99 128 mail.body = t('registration.email_body', {
100 129 :full_name => user.full_name,
101 130 :contest_name => contest_name,
102 131 :login => user.login,
103 132 :password => user.password,
104 133 :activation_url => activation_url,
105 134 :admin_email => admin_email
106 135 })
107 136
108 137 logger.info mail.body
109 138
110 139 smtp_server = Configuration['system.online_registration.smtp']
111 140
112 141 begin
113 142 Net::SMTP.start(smtp_server) do |smtp|
114 143 smtp.send_message(mail.to_s, mail.from, mail.to)
115 144 end
116 145 result = true
117 146 rescue
118 147 result = false
119 148 end
120 149
121 150 return result
122 151 end
123 152
153 + def send_new_password_email(user)
154 + contest_name = Configuration['contest.name']
155 + admin_email = Configuration['system.admin_email']
156 + mail = TMail::Mail.new
157 + mail.to = user.email
158 + mail.from = Configuration['system.online_registration.from']
159 + mail.subject = "[#{contest_name}] Confirmation"
160 + mail.body = t('registration.password_retrieval.email_body', {
161 + :full_name => user.full_name,
162 + :contest_name => contest_name,
163 + :login => user.login,
164 + :password => user.password,
165 + :admin_email => admin_email
166 + })
167 +
168 + logger.info mail.body
169 +
170 + smtp_server = Configuration['system.online_registration.smtp']
171 +
172 + begin
173 + Net::SMTP.start(smtp_server) do |smtp|
174 + smtp.send_message(mail.to_s, mail.from, mail.to)
124 175 end
176 + result = true
177 + rescue
178 + result = false
179 + end
180 +
181 + return result
182 + end
183 +
184 + end
@@ -1,42 +1,42
1 1 %h1= Configuration['ui.front.title']
2 2
3 3 - if @announcements.length!=0
4 4 .announcementbox
5 5 %span{:class => 'title'}
6 6 Announcements
7 7 = render :partial => 'announcement', :collection => @announcements
8 8
9 9 %b= Configuration['ui.front.welcome_message']
10 10 %br/
11 11
12 12 - if !@hidelogin
13 13 =t 'login.message'
14 14 %br/
15 15 %br/
16 16
17 17 - if flash[:notice]
18 18 %hr/
19 19 %b= flash[:notice]
20 20 %hr/
21 21
22 22 %div{ :style => "border: solid 1px gray; padding: 2px; background: #f0f0f0;"}
23 23 - form_tag :controller => 'login', :action => 'login' do
24 24 %table
25 25 %tr
26 26 %td{:align => "right"}
27 27 ="#{t 'login_label'}:"
28 28 %td= text_field_tag 'login'
29 29 %tr
30 30 %td{:align => "right"}
31 31 ="#{t 'password_label'}:"
32 32 %td= password_field_tag
33 33 = submit_tag t('login.login_submit')
34 34 %br/
35 35
36 36 - if Configuration['system.online_registration']
37 37 =t 'login.participation'
38 38 %b
39 39 = "#{t 'login.please'} "
40 40 = link_to "#{t 'login.register'}", :controller => :users, :action => :new
41 41 %br/
42 -
42 + = link_to "#{t 'login.forget_password'}", :controller => :users, :action => :forget
@@ -1,118 +1,139
1 1 # Sample localization file for English. Add more files in this directory for other locales.
2 2 # See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3 3
4 4 en:
5 5 cancel: 'Cancel'
6 6
7 7 login_label: 'Login'
8 8 full_name_label: 'Full name'
9 9 email_label: 'E-mail'
10 10 password_label: 'Password'
11 11
12 12 go_ahead_to: "Go ahead to"
13 13 go_back_to: "Go back to"
14 14 login_page: "login page"
15 15 home_page: "home page"
16 16
17 17 menu:
18 18 main: 'Main'
19 19 messages: 'Messages'
20 20 tasks: 'Tasks'
21 21 submissions: 'Submissions'
22 22 test: 'Test Interface'
23 23 help: 'Help'
24 24 settings: 'Settings'
25 25 log_out: 'Log out'
26 26
27 27 title_bar:
28 28 current_time: "Current time is"
29 29 remaining_time: "Time left: "
30 30 contest_not_started: "The contest has not started."
31 31
32 32 login:
33 33 message: 'Please login to see the problem list'
34 34 login_submit: 'Login'
35 35 participation: 'Want to participate?'
36 36 please: 'Please'
37 37 register: 'register'
38 + forget_password: 'Forget password?'
38 39
39 40 main:
40 41 start_soon: "The contest at your site will start soon. Please wait."
41 42 specified_in_header: "Specified in header"
42 43
43 44 problem_desc: "desc"
44 45 submitted_at: "Submitted at"
45 46 graded_at: "Graded at"
46 47 score: "score: "
47 48 cmp_msg: "compiler msg"
48 49 src_link: "src"
49 50 submissions_link: "submissions"
50 51
51 52 test:
52 53 title: "Test Interface"
53 54 intro: "You can test your submission with your own test data on the grading environment using this test interface."
54 55 disabled_at_end_announcement: "<b>Note:</b> Test interface will be disabled in the last 30 minutes of the contest time on your site."
55 56
56 57 registration:
57 58 title: "New user registration"
59 +
60 + 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."
61 +
58 62 successful_title: "Registration successful"
59 63
60 64 login_guide: "Only a-z, A-Z, 0-9 and _. Can be at most 20 characters long"
61 65 email_guide: "Please make sure that your e-mail is correct.<br/>You'll need to verify your account by email."
62 66 register: "Register"
63 67
64 68 email_body: "Hello {{full_name}},
65 69
66 70 You have registered for {{contest_name}}
67 71
68 72 Your login is: {{login}}
69 73
70 74 Your password is: {{password}}
71 75
72 76 Please follow the link:
73 77
74 78 {{activation_url}}
75 79
76 80 to activate your user account.
77 81
78 82 If you did not register, please ignore this e-mail
79 83 and report this event to {{admin_email}}.
80 84
81 85 Thanks!"
82 86
83 87 email_sent: "We have sent a confimation message to your e-mail. (Please also check the Junk mail box."
84 88 email_verify_at: "Please check at {{email}} and confirm."
85 89
86 90 activation_sucessful_title: "User activated"
87 91 account_activated: "Your account has been activated."
88 92
89 93 activation_failed_title: "Activation failed"
90 94
91 95 errors:
92 96 header: "Errors occured during registration"
93 97 email:
94 98 title: "Errors in sending registration confirmation"
95 99 expl: "<h2>Your user account has been created, but the system cannot send you the confirmation e-mail.</h2>
96 100 Maybe there's a problem in the configuration. Please report the admin at {{email}}.<br/>Thank you!"
97 101 activation:
98 102 email_exists: "A user with this E-mail exists."
99 103 invalid: "Your activation code is invalid. Please check again."
100 104
105 + password_retrieval:
106 + header: "Password retrieval"
107 + instructions: "Please enter the e-mail address that you used to register."
108 + button_label: "Request new password"
109 + no_email: "No user with that e-mail address."
110 + email_body: "Hello {{full_name}},
111 +
112 + You have requested for new password for {{contest_name}}. We have generated it for you:
113 +
114 + user name: {{login}}
115 + password: {{password}}
116 +
117 + If you didn't ask for new password or you're not the person who registered,
118 + please ignore this e-mail and inform {{admin_email}} of this mistake.
119 +
120 + Thanks!"
121 +
101 122 help:
102 123 how_to_submit: "How to submit"
103 124 must_specify_language: "You <b>must</b> specify the language you are using in your program header. You can optionally specify the task you are submitting to."
104 125 list_available_language: "The possible language options are <tt>C</tt>, <tt>C++</tt>, and <tt>Pascal</tt>. The follow are examples."
105 126 accept_only_language_specified: "The server <b>will not</b> accept your submission, if you do not specify the language."
106 127 specifying_task: "Optionally, you can also specify the task with <tt>TASK:</tt> <i>taskname</i>. On the first page, the taskname for each task is shown in parentheses."
107 128 example_cpp: "For example, suppose you are using <tt>C++</tt> to write task <b>mobiles</b>, you put the following on top of your source code."
108 129 example_pas: "If you are using <tt>Pascal</tt> to write the same task, you'll use"
109 130 ask_questions_at_messages: "If you have any problems, you can ask at [<a href=\"{{url}}\">{{message_link_name}}</a>]."
110 131
111 132 activerecord:
112 133 attributes:
113 134 user:
114 135 login: "login"
115 136 full_name: "full name"
116 137 email: "e-mail"
117 138 province: "province"
118 139
@@ -1,121 +1,138
1 1 # Sample localization file for English. Add more files in this directory for other locales.
2 2 # See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3 3
4 4 th:
5 5 cancel: 'ยกเลิก'
6 6
7 7 login_label: 'ชื่อเข้าใช้ระบบ (login)'
8 8 full_name_label: 'ชื่อเต็ม'
9 9 email_label: 'E-mail'
10 10 password_label: 'รหัสผ่าน'
11 11
12 12 go_ahead_to: "ไปยัง"
13 13 go_back_to: "กลับไปยัง"
14 14 login_page: "หน้าเข้าใช้ระบบ"
15 15 home_page: "หน้าแรก"
16 16
17 17 menu:
18 18 main: 'หน้าหลัก'
19 19 messages: 'ข้อความ'
20 20 tasks: 'โจทย์'
21 21 submissions: 'โปรแกรมที่ส่ง'
22 22 test: 'ทดสอบโปรแกรม'
23 23 help: 'ความช่วยเหลือ'
24 24 settings: 'เปลี่ยนรหัสผ่าน'
25 25 log_out: 'ออกจากระบบ'
26 26
27 27 title_bar:
28 28 current_time: "เวลาปัจจุบันคือ"
29 29 remaining_time: "เหลือเวลาอีก"
30 30 contest_not_started: "ยังไม่เริ่มแข่งขัน"
31 31
32 32 login:
33 33 message: 'กรุณา login เพื่อเข้าสู่ระบบ'
34 34 login_submit: 'เข้าใช้ระบบ'
35 35 participation: 'ต้องการเข้าร่วม?'
36 36 please: 'กรุณา'
37 37 register: 'ลงทะเบียน'
38 + forget_password: 'ลืมรหัสผ่าน?'
38 39
39 40 main:
40 41 start_soon: "การแข่งขันกำลังจะเริ่ม กรุณารอก่อน"
41 42 specified_in_header: "ระบุที่หัวโปรแกรมแล้ว"
42 43
43 44 problem_desc: "อ่าน"
44 45 submitted_at: "ส่งเมื่อเวลา"
45 46 graded_at: "ตรวจเมื่อเวลา"
46 47 score: "คะแนน: "
47 48 cmp_msg: "ผลคอมไพล์"
48 49 src_link: "ต้นฉบับ"
49 50 submissions_link: "การส่งครั้งอื่น ๆ"
50 51
51 52 test:
52 53 title: "ทดสอบโปรแกรมบนสภาพแวดล้อมของเครื่องตรวจ"
53 54 intro: "คุณสามารถทดลองการทำงานของโปรแกรมที่เขียนกับข้อมูลชุดทดสอบของคุณเองในสภาพแวดล้อมจริงของการตรวจโปรแกรมได้ โดยเลือกโปรแกรมส่งแล้วที่ด้านล่างพร้อมทั้งส่งแฟ้มข้อมูลชุดทดสอบที่ต้องการให้ทำงานด้วย"
54 55 disabled_at_end_announcement: "<b>หมายเหตุ:</b> ระบบทดสอบโปรแกรมจะหยุดทำงานในช่วงเวลา 30 นาทีสุดท้ายของการแข่งขัน"
55 56
56 -
57 57 registration:
58 58 title: "ลงทะเบียนผู้ใช้ใหม่"
59 59 description: "ในการลงทะเบียน ให้ผู้สนใจเข้าร่วมการแข่งขันกรอกข้อมูลด้านล่าง จากนั้นระบบจะส่ง e-mail ไปยัง e-mail ที่ระบุเพื่อให้ยืนยันตัวตนและเปิดใช้บัญชีผู้ใช้<br/>ในกรณีที่ผู้เข้าแข่งขันเป็นนักเรียน รบกวนช่วยให้ข้อมูลเกี่ยวกับโรงเรียนและจังหวัดด้วย"
60 60
61 61 successful_title: "การลงทะเบียนเสร็จเรียบร้อย"
62 62
63 63 login_guide: "ใช้ได้เฉพาะ a-z, A-Z, 0-9 และ _ ความยาวไม่เกิน 20 ตัวอักษร"
64 64 email_guide: "กรุณาตรวจสอบ e-mail ที่ใส่ให้ถูกต้อง<br/>คุณจะต้องยืนยันการลงทะเบียนผ่านทางข้อมูลที่จะส่งให้ทาง e-mail"
65 65 register: "ลงทะเบียน"
66 66
67 67 email_body: "สวัสดีครับ {{full_name}},
68 68
69 69 คุณได้ลงทะเบียนเข้าร่วมการแข่งขัน {{contest_name}}
70 70
71 71 บัญชีเข้าใช้ของคุณคือ: {{login}}
72 72
73 73 รหัสผ่านคือ: {{password}}
74 74
75 75 กรุณาเข้าลิงก์ต่อไปนี้:
76 76
77 77 {{activation_url}}
78 78
79 79 เพื่อเปิดใช้งานบัญชีของคุณ
80 80
81 81 ถ้าคุณไม่ใช้คนที่ลงทะเบียน กรุณาละทิ้ง e-mail ฉบับนี้
82 82 และแจ้งความผิดพลาดนี้กับ {{admin_email}}
83 83
84 84 ขอบคุณมาก!"
85 85
86 86 email_sent: "เราได้ส่งข้อมูลสำหรับยืนยันไปให้คุณแล้ว (โปรดอย่าลืมตรวจดูในส่วน Junk mail ด้วย)"
87 87 email_verify_at: "กรุณาตรวจสอบที่ {{email}} พร้อมทั้งยืนยัน"
88 88
89 89 activation_sucessful_title: "บัณชีผู้ใช้ได้รับการยืนยันแล้ว"
90 90 account_activated: "บัญชีผู้ใช้ของคุณพร้อมใช้งานแล้ว"
91 91
92 92 activation_failed_title: "การยืนยันล้มเหลว"
93 93
94 94 errors:
95 95 header: 'การลงทะเบียนมีข้อผิดพลาด'
96 96 email:
97 97 title: "เกิดปัญหาระหว่างการส่ง e-mail เพื่อยืนยันการสมัคร"
98 98 expl: "<h2>บัญชีผู้ใช้ของคุณถูกสร้างขึ้นแล้ว แต่ระบบไม่สามารถส่ง e-mail เพื่อยืนยันการสมัครได้</h2>
99 99 อาจเกิดปัญหาในการตั้งค่าเริ่มต้นของระบบ กรุณาช่วยติดต่อผู้ดูแลระบบด้วยที่ {{email}}<br/>ขอขอบคุณจากทีมงาน"
100 100 activation:
101 101 email_exists: "มีผู้ใช้ที่ใช้ e-mail นี้แล้ว"
102 102 invalid: "รหัสสำหรับยืนยันผิดพลาด กรุณาตรวจสอบอีกครั้ง"
103 103
104 + password_retrieval:
105 + header: "การขอรหัสผ่านใหม่"
106 + instructions: "กรุณากรอก e-mail ที่ลงทะเบียน"
107 + button_label: "ขอรหัสผ่านใหม่"
108 + no_email: "ไม่มีบัญชีผู้ใช้ที่ใช้ e-mail ดังกล่าว"
109 + email_body: "สวัสดีครับ {{full_name}},
110 +
111 + คุณได้ร้องขอรหัสผ่านใหม่ สำหรับการแข่งขัน {{contest_name}} ซึ่งเราได้สร้างให้คุณแล้ว ดังนี้
112 +
113 + บัญชีเข้าใช้ของคุณคือ: {{login}}
114 + รหัสผ่านคือ: {{password}}
115 +
116 + ถ้าคุณไม่ได้ขอรหัสผ่านใหม่ หรือไม่ใช่คนที่ลงทะเบียน กรุณาละทิ้ง e-mail ฉบับนี้
117 + และแจ้งความผิดพลาดนี้กับ {{admin_email}}
118 +
119 + ขอบคุณมาก!"
120 +
104 121 help:
105 122 how_to_submit: "วิธีการส่งโปรแกรม"
106 123 must_specify_language: "คุณ<b>ต้อง</b>ระบุภาษาโปรแกรมที่ใช้ที่ตอนต้นของรหัสโปรแกรม (source code) นอกจากนี้คุณอาจจะระบุโจทย์ที่ต้องการส่งได้ด้วย"
107 124 list_available_language: "ภาษาโปรแกรมที่สามารถระบุได้คือ <tt>C</tt>, <tt>C++</tt>, และ <tt>Pascal</tt> ด้านล่างแสดงตัวอย่างของการระบุสำหรับภาษาต่าง ๆ"
108 125 accept_only_language_specified: "ระบบจะ<b>ไม่รับ</b>โปรแกรมที่ส่งถ้าคุณไม่ได้ระบุภาษาที่ใช้"
109 126 specifying_task: "นอกจากนี้ คุณยังสามารถระบุชื่อของโจทย์ที่ต้องการส่งเพิ่มเติมได้ ในการระบุให้ใส่ <tt>TASK:</tt> <i>taskname</i> คุณสามารถตรวจสอบชื่อของโจทย์ได้ โดยจะแสดงในวงเล็บหลังชื่อภาษาไทยของโจทย์"
110 127 example_cpp: "ยกตัวอย่างเช่น ถ้าคุณใช้ภาษา <tt>C++</tt> สำหรับเขียนโจทย์ <tt>mobiles</tt> ตอนต้นโปรแกรมคุณจะใส่ดังนี้"
111 128 example_pas: "ถ้าคุณใช้ภาษา <tt>Pascal</tt> เพื่อเขียนโจทย์ข้อเดียวกัน คุณจะระบุ"
112 129 ask_questions_at_messages: "ถ้ามีปัญหาในการใช้งานสามารถสอบถามได้ที่หน้า<a href=\"{{url}}\">{{message_link_name}}</a>"
113 130
114 131 activerecord:
115 132 attributes:
116 133 user:
117 134 login: "ชื่อเข้าใช้ระบบ"
118 135 full_name: "ชื่อเต็ม"
119 136 email: "e-mail"
120 137 province: "จังหวัด"
121 138
You need to be logged in to leave comments. Login now