Description:
fix utf8 for java
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r462:c960ef68f0b1 - - 2 files changed: 3 inserted, 1 deleted
@@ -1,258 +1,259 | |||
|
1 | 1 | class MainController < ApplicationController |
|
2 | 2 | |
|
3 | 3 | before_filter :authenticate, :except => [:index, :login] |
|
4 | 4 | before_filter :check_viewability, :except => [:index, :login] |
|
5 | 5 | |
|
6 | 6 | append_before_filter :confirm_and_update_start_time, |
|
7 | 7 | :except => [:index, |
|
8 | 8 | :login, |
|
9 | 9 | :confirm_contest_start] |
|
10 | 10 | |
|
11 | 11 | # to prevent log in box to be shown when user logged out of the |
|
12 | 12 | # system only in some tab |
|
13 | 13 | prepend_before_filter :reject_announcement_refresh_when_logged_out, |
|
14 | 14 | :only => [:announcements] |
|
15 | 15 | |
|
16 | 16 | # COMMENTED OUT: filter in each action instead |
|
17 | 17 | # before_filter :verify_time_limit, :only => [:submit] |
|
18 | 18 | |
|
19 | 19 | verify :method => :post, :only => [:submit], |
|
20 | 20 | :redirect_to => { :action => :index } |
|
21 | 21 | |
|
22 | 22 | # COMMENT OUT: only need when having high load |
|
23 | 23 | # caches_action :index, :login |
|
24 | 24 | |
|
25 | 25 | # NOTE: This method is not actually needed, 'config/routes.rb' has |
|
26 | 26 | # assigned action login as a default action. |
|
27 | 27 | def index |
|
28 | 28 | redirect_to :action => 'login' |
|
29 | 29 | end |
|
30 | 30 | |
|
31 | 31 | def login |
|
32 | 32 | saved_notice = flash[:notice] |
|
33 | 33 | reset_session |
|
34 | 34 | flash.now[:notice] = saved_notice |
|
35 | 35 | |
|
36 | 36 | # EXPERIMENT: |
|
37 | 37 | # Hide login if in single user mode and the url does not |
|
38 | 38 | # explicitly specify /login |
|
39 | 39 | # |
|
40 | 40 | # logger.info "PATH: #{request.path}" |
|
41 | 41 | # if GraderConfiguration['system.single_user_mode'] and |
|
42 | 42 | # request.path!='/main/login' |
|
43 | 43 | # @hidelogin = true |
|
44 | 44 | # end |
|
45 | 45 | |
|
46 | 46 | @announcements = Announcement.find_for_frontpage |
|
47 | 47 | render :action => 'login', :layout => 'empty' |
|
48 | 48 | end |
|
49 | 49 | |
|
50 | 50 | def list |
|
51 | 51 | prepare_list_information |
|
52 | 52 | end |
|
53 | 53 | |
|
54 | 54 | def help |
|
55 | 55 | @user = User.find(session[:user_id]) |
|
56 | 56 | end |
|
57 | 57 | |
|
58 | 58 | def submit |
|
59 | 59 | user = User.find(session[:user_id]) |
|
60 | 60 | |
|
61 | 61 | @submission = Submission.new |
|
62 | 62 | @submission.problem_id = params[:submission][:problem_id] |
|
63 | 63 | @submission.user = user |
|
64 | 64 | @submission.language_id = 0 |
|
65 | 65 | if (params['file']) and (params['file']!='') |
|
66 | - @submission.source = params['file'].read | |
|
66 | + @submission.source = File.open(params['file'].path,'r:UTF-8',&:read) | |
|
67 | + @submission.source.encode!('UTF-8','UTF-8',invalid: :replace, replace: '') | |
|
67 | 68 | @submission.source_filename = params['file'].original_filename |
|
68 | 69 | end |
|
69 | 70 | @submission.submitted_at = Time.new.gmtime |
|
70 | 71 | @submission.ip_address = request.remote_ip |
|
71 | 72 | |
|
72 | 73 | if GraderConfiguration.time_limit_mode? and user.contest_finished? |
|
73 | 74 | @submission.errors.add_to_base "The contest is over." |
|
74 | 75 | prepare_list_information |
|
75 | 76 | render :action => 'list' and return |
|
76 | 77 | end |
|
77 | 78 | |
|
78 | 79 | if @submission.valid? |
|
79 | 80 | if @submission.save == false |
|
80 | 81 | flash[:notice] = 'Error saving your submission' |
|
81 | 82 | elsif Task.create(:submission_id => @submission.id, |
|
82 | 83 | :status => Task::STATUS_INQUEUE) == false |
|
83 | 84 | flash[:notice] = 'Error adding your submission to task queue' |
|
84 | 85 | end |
|
85 | 86 | else |
|
86 | 87 | prepare_list_information |
|
87 | 88 | render :action => 'list' and return |
|
88 | 89 | end |
|
89 | 90 | redirect_to :action => 'list' |
|
90 | 91 | end |
|
91 | 92 | |
|
92 | 93 | def source |
|
93 | 94 | submission = Submission.find(params[:id]) |
|
94 | 95 | if ((submission.user_id == session[:user_id]) and |
|
95 | 96 | (submission.problem != nil) and |
|
96 | 97 | (submission.problem.available)) |
|
97 | 98 | send_data(submission.source, |
|
98 | 99 | {:filename => submission.download_filename, |
|
99 | 100 | :type => 'text/plain'}) |
|
100 | 101 | else |
|
101 | 102 | flash[:notice] = 'Error viewing source' |
|
102 | 103 | redirect_to :action => 'list' |
|
103 | 104 | end |
|
104 | 105 | end |
|
105 | 106 | |
|
106 | 107 | def compiler_msg |
|
107 | 108 | @submission = Submission.find(params[:id]) |
|
108 | 109 | if @submission.user_id == session[:user_id] |
|
109 | 110 | render :action => 'compiler_msg', :layout => 'empty' |
|
110 | 111 | else |
|
111 | 112 | flash[:notice] = 'Error viewing source' |
|
112 | 113 | redirect_to :action => 'list' |
|
113 | 114 | end |
|
114 | 115 | end |
|
115 | 116 | |
|
116 | 117 | def submission |
|
117 | 118 | @user = User.find(session[:user_id]) |
|
118 | 119 | @problems = @user.available_problems |
|
119 | 120 | if params[:id]==nil |
|
120 | 121 | @problem = nil |
|
121 | 122 | @submissions = nil |
|
122 | 123 | else |
|
123 | 124 | @problem = Problem.find_by_name(params[:id]) |
|
124 | 125 | if not @problem.available |
|
125 | 126 | redirect_to :action => 'list' |
|
126 | 127 | flash[:notice] = 'Error: submissions for that problem are not viewable.' |
|
127 | 128 | return |
|
128 | 129 | end |
|
129 | 130 | @submissions = Submission.find_all_by_user_problem(@user.id, @problem.id) |
|
130 | 131 | end |
|
131 | 132 | end |
|
132 | 133 | |
|
133 | 134 | def result |
|
134 | 135 | if !GraderConfiguration.show_grading_result |
|
135 | 136 | redirect_to :action => 'list' and return |
|
136 | 137 | end |
|
137 | 138 | @user = User.find(session[:user_id]) |
|
138 | 139 | @submission = Submission.find(params[:id]) |
|
139 | 140 | if @submission.user!=@user |
|
140 | 141 | flash[:notice] = 'You are not allowed to view result of other users.' |
|
141 | 142 | redirect_to :action => 'list' and return |
|
142 | 143 | end |
|
143 | 144 | prepare_grading_result(@submission) |
|
144 | 145 | end |
|
145 | 146 | |
|
146 | 147 | def load_output |
|
147 | 148 | if !GraderConfiguration.show_grading_result or params[:num]==nil |
|
148 | 149 | redirect_to :action => 'list' and return |
|
149 | 150 | end |
|
150 | 151 | @user = User.find(session[:user_id]) |
|
151 | 152 | @submission = Submission.find(params[:id]) |
|
152 | 153 | if @submission.user!=@user |
|
153 | 154 | flash[:notice] = 'You are not allowed to view result of other users.' |
|
154 | 155 | redirect_to :action => 'list' and return |
|
155 | 156 | end |
|
156 | 157 | case_num = params[:num].to_i |
|
157 | 158 | out_filename = output_filename(@user.login, |
|
158 | 159 | @submission.problem.name, |
|
159 | 160 | @submission.id, |
|
160 | 161 | case_num) |
|
161 | 162 | if !FileTest.exists?(out_filename) |
|
162 | 163 | flash[:notice] = 'Output not found.' |
|
163 | 164 | redirect_to :action => 'list' and return |
|
164 | 165 | end |
|
165 | 166 | |
|
166 | 167 | if defined?(USE_APACHE_XSENDFILE) and USE_APACHE_XSENDFILE |
|
167 | 168 | response.headers['Content-Type'] = "application/force-download" |
|
168 | 169 | response.headers['Content-Disposition'] = "attachment; filename=\"output-#{case_num}.txt\"" |
|
169 | 170 | response.headers["X-Sendfile"] = out_filename |
|
170 | 171 | response.headers['Content-length'] = File.size(out_filename) |
|
171 | 172 | render :nothing => true |
|
172 | 173 | else |
|
173 | 174 | send_file out_filename, :stream => false, :filename => "output-#{case_num}.txt", :type => "text/plain" |
|
174 | 175 | end |
|
175 | 176 | end |
|
176 | 177 | |
|
177 | 178 | def error |
|
178 | 179 | @user = User.find(session[:user_id]) |
|
179 | 180 | end |
|
180 | 181 | |
|
181 | 182 | # announcement refreshing and hiding methods |
|
182 | 183 | |
|
183 | 184 | def announcements |
|
184 | 185 | if params.has_key? 'recent' |
|
185 | 186 | prepare_announcements(params[:recent]) |
|
186 | 187 | else |
|
187 | 188 | prepare_announcements |
|
188 | 189 | end |
|
189 | 190 | render(:partial => 'announcement', |
|
190 | 191 | :collection => @announcements, |
|
191 | 192 | :locals => {:announcement_effect => true}) |
|
192 | 193 | end |
|
193 | 194 | |
|
194 | 195 | def confirm_contest_start |
|
195 | 196 | user = User.find(session[:user_id]) |
|
196 | 197 | if request.method == :post |
|
197 | 198 | user.update_start_time |
|
198 | 199 | redirect_to :action => 'list' |
|
199 | 200 | else |
|
200 | 201 | @contests = user.contests |
|
201 | 202 | @user = user |
|
202 | 203 | end |
|
203 | 204 | end |
|
204 | 205 | |
|
205 | 206 | protected |
|
206 | 207 | |
|
207 | 208 | def prepare_announcements(recent=nil) |
|
208 | 209 | if GraderConfiguration.show_tasks_to?(@user) |
|
209 | 210 | @announcements = Announcement.find_published(true) |
|
210 | 211 | else |
|
211 | 212 | @announcements = Announcement.find_published |
|
212 | 213 | end |
|
213 | 214 | if recent!=nil |
|
214 | 215 | recent_id = recent.to_i |
|
215 | 216 | @announcements = @announcements.find_all { |a| a.id > recent_id } |
|
216 | 217 | end |
|
217 | 218 | end |
|
218 | 219 | |
|
219 | 220 | def prepare_list_information |
|
220 | 221 | @user = User.find(session[:user_id]) |
|
221 | 222 | if not GraderConfiguration.multicontests? |
|
222 | 223 | @problems = @user.available_problems |
|
223 | 224 | else |
|
224 | 225 | @contest_problems = @user.available_problems_group_by_contests |
|
225 | 226 | @problems = @user.available_problems |
|
226 | 227 | end |
|
227 | 228 | @prob_submissions = {} |
|
228 | 229 | @problems.each do |p| |
|
229 | 230 | sub = Submission.find_last_by_user_and_problem(@user.id,p.id) |
|
230 | 231 | if sub!=nil |
|
231 | 232 | @prob_submissions[p.id] = { :count => sub.number, :submission => sub } |
|
232 | 233 | else |
|
233 | 234 | @prob_submissions[p.id] = { :count => 0, :submission => nil } |
|
234 | 235 | end |
|
235 | 236 | end |
|
236 | 237 | prepare_announcements |
|
237 | 238 | end |
|
238 | 239 | |
|
239 | 240 | def check_viewability |
|
240 | 241 | @user = User.find(session[:user_id]) |
|
241 | 242 | if (!GraderConfiguration.show_tasks_to?(@user)) and |
|
242 | 243 | ((action_name=='submission') or (action_name=='submit')) |
|
243 | 244 | redirect_to :action => 'list' and return |
|
244 | 245 | end |
|
245 | 246 | end |
|
246 | 247 | |
|
247 | 248 | def prepare_grading_result(submission) |
|
248 | 249 | if GraderConfiguration.task_grading_info.has_key? submission.problem.name |
|
249 | 250 | grading_info = GraderConfiguration.task_grading_info[submission.problem.name] |
|
250 | 251 | else |
|
251 | 252 | # guess task info from problem.full_score |
|
252 | 253 | cases = submission.problem.full_score / 10 |
|
253 | 254 | grading_info = { |
|
254 | 255 | 'testruns' => cases, |
|
255 | 256 | 'testcases' => cases |
|
256 | 257 | } |
|
257 | 258 | end |
|
258 | 259 | @test_runs = [] |
@@ -1,194 +1,195 | |||
|
1 | 1 | require 'net/smtp' |
|
2 | 2 | |
|
3 | 3 | class UsersController < ApplicationController |
|
4 | 4 | |
|
5 | 5 | include MailHelperMethods |
|
6 | 6 | |
|
7 | 7 | before_filter :authenticate, :except => [:new, |
|
8 | 8 | :register, |
|
9 | 9 | :confirm, |
|
10 | 10 | :forget, |
|
11 | 11 | :retrieve_password] |
|
12 | 12 | |
|
13 | 13 | before_filter :verify_online_registration, :only => [:new, |
|
14 | 14 | :register, |
|
15 | 15 | :forget, |
|
16 | 16 | :retrieve_password] |
|
17 | 17 | before_filter :authenticate, :profile_authorization, only: [:profile] |
|
18 | 18 | |
|
19 | 19 | verify :method => :post, :only => [:chg_passwd], |
|
20 | 20 | :redirect_to => { :action => :index } |
|
21 | 21 | |
|
22 | 22 | #in_place_edit_for :user, :alias_for_editing |
|
23 | 23 | #in_place_edit_for :user, :email_for_editing |
|
24 | 24 | |
|
25 | 25 | def index |
|
26 | 26 | if !GraderConfiguration['system.user_setting_enabled'] |
|
27 | 27 | redirect_to :controller => 'main', :action => 'list' |
|
28 | 28 | else |
|
29 | 29 | @user = User.find(session[:user_id]) |
|
30 | 30 | end |
|
31 | 31 | end |
|
32 | 32 | |
|
33 | 33 | def chg_passwd |
|
34 | 34 | user = User.find(session[:user_id]) |
|
35 | 35 | user.password = params[:passwd] |
|
36 | 36 | user.password_confirmation = params[:passwd_verify] |
|
37 | 37 | if user.save |
|
38 | 38 | flash[:notice] = 'password changed' |
|
39 | 39 | else |
|
40 | 40 | flash[:notice] = 'Error: password changing failed' |
|
41 | 41 | end |
|
42 | 42 | redirect_to :action => 'index' |
|
43 | 43 | end |
|
44 | 44 | |
|
45 | 45 | def new |
|
46 | 46 | @user = User.new |
|
47 | 47 | render :action => 'new', :layout => 'empty' |
|
48 | 48 | end |
|
49 | 49 | |
|
50 | 50 | def register |
|
51 | 51 | if(params[:cancel]) |
|
52 | 52 | redirect_to :controller => 'main', :action => 'login' |
|
53 | 53 | return |
|
54 | 54 | end |
|
55 | 55 | @user = User.new(params[:user]) |
|
56 | 56 | @user.password_confirmation = @user.password = User.random_password |
|
57 | 57 | @user.activated = false |
|
58 | 58 | if (@user.valid?) and (@user.save) |
|
59 | 59 | if send_confirmation_email(@user) |
|
60 | 60 | render :action => 'new_splash', :layout => 'empty' |
|
61 | 61 | else |
|
62 | 62 | @admin_email = GraderConfiguration['system.admin_email'] |
|
63 | 63 | render :action => 'email_error', :layout => 'empty' |
|
64 | 64 | end |
|
65 | 65 | else |
|
66 | 66 | @user.errors.add_to_base("Email cannot be blank") if @user.email=='' |
|
67 | 67 | render :action => 'new', :layout => 'empty' |
|
68 | 68 | end |
|
69 | 69 | end |
|
70 | 70 | |
|
71 | 71 | def confirm |
|
72 | 72 | login = params[:login] |
|
73 | 73 | key = params[:activation] |
|
74 | 74 | @user = User.find_by_login(login) |
|
75 | 75 | if (@user) and (@user.verify_activation_key(key)) |
|
76 | 76 | if @user.valid? # check uniquenss of email |
|
77 | 77 | @user.activated = true |
|
78 | 78 | @user.save |
|
79 | 79 | @result = :successful |
|
80 | 80 | else |
|
81 | 81 | @result = :email_used |
|
82 | 82 | end |
|
83 | 83 | else |
|
84 | 84 | @result = :failed |
|
85 | 85 | end |
|
86 | 86 | render :action => 'confirm', :layout => 'empty' |
|
87 | 87 | end |
|
88 | 88 | |
|
89 | 89 | def forget |
|
90 | 90 | render :action => 'forget', :layout => 'empty' |
|
91 | 91 | end |
|
92 | 92 | |
|
93 | 93 | def retrieve_password |
|
94 | 94 | email = params[:email] |
|
95 | 95 | user = User.find_by_email(email) |
|
96 | 96 | if user |
|
97 | 97 | last_updated_time = user.updated_at || user.created_at || (Time.now.gmtime - 1.hour) |
|
98 | 98 | if last_updated_time > Time.now.gmtime - 5.minutes |
|
99 | 99 | flash[:notice] = 'The account has recently created or new password has recently been requested. Please wait for 5 minutes' |
|
100 | 100 | else |
|
101 | 101 | user.password = user.password_confirmation = User.random_password |
|
102 | 102 | user.save |
|
103 | 103 | send_new_password_email(user) |
|
104 | 104 | flash[:notice] = 'New password has been mailed to you.' |
|
105 | 105 | end |
|
106 | 106 | else |
|
107 | 107 | flash[:notice] = I18n.t 'registration.password_retrieval.no_email' |
|
108 | 108 | end |
|
109 | 109 | redirect_to :action => 'forget' |
|
110 | 110 | end |
|
111 | 111 | |
|
112 | 112 | def profile |
|
113 | 113 | @user = User.find(params[:id]) |
|
114 | 114 | @submission = Submission.includes(:problem).where(user_id: params[:id]) |
|
115 | 115 | |
|
116 | 116 | range = 120 |
|
117 | 117 | @histogram = { data: Array.new(range,0), summary: {} } |
|
118 | 118 | @summary = {count: 0, solve: 0, attempt: 0} |
|
119 | 119 | problem = Hash.new(0) |
|
120 | 120 | |
|
121 | 121 | @submission.find_each do |sub| |
|
122 | 122 | #histogram |
|
123 | 123 | d = (DateTime.now.in_time_zone - sub.submitted_at) / 24 / 60 / 60 |
|
124 | 124 | @histogram[:data][d.to_i] += 1 if d < range |
|
125 | 125 | |
|
126 | 126 | @summary[:count] += 1 |
|
127 | + next unless sub.problem | |
|
127 | 128 | problem[sub.problem] = [problem[sub.problem], (sub.points >= sub.problem.full_score) ? 1 : 0].max |
|
128 | 129 | end |
|
129 | 130 | |
|
130 | 131 | @histogram[:summary][:max] = [@histogram[:data].max,1].max |
|
131 | 132 | @summary[:attempt] = problem.count |
|
132 | 133 | problem.each_value { |v| @summary[:solve] += 1 if v == 1 } |
|
133 | 134 | end |
|
134 | 135 | |
|
135 | 136 | protected |
|
136 | 137 | |
|
137 | 138 | def verify_online_registration |
|
138 | 139 | if !GraderConfiguration['system.online_registration'] |
|
139 | 140 | redirect_to :controller => 'main', :action => 'login' |
|
140 | 141 | end |
|
141 | 142 | end |
|
142 | 143 | |
|
143 | 144 | def send_confirmation_email(user) |
|
144 | 145 | contest_name = GraderConfiguration['contest.name'] |
|
145 | 146 | activation_url = url_for(:action => 'confirm', |
|
146 | 147 | :login => user.login, |
|
147 | 148 | :activation => user.activation_key) |
|
148 | 149 | home_url = url_for(:controller => 'main', :action => 'index') |
|
149 | 150 | mail_subject = "[#{contest_name}] Confirmation" |
|
150 | 151 | mail_body = t('registration.email_body', { |
|
151 | 152 | :full_name => user.full_name, |
|
152 | 153 | :contest_name => contest_name, |
|
153 | 154 | :login => user.login, |
|
154 | 155 | :password => user.password, |
|
155 | 156 | :activation_url => activation_url, |
|
156 | 157 | :admin_email => admin_email |
|
157 | 158 | }) |
|
158 | 159 | |
|
159 | 160 | logger.info mail_body |
|
160 | 161 | |
|
161 | 162 | send_mail(user.email, mail_subject, mail_body) |
|
162 | 163 | end |
|
163 | 164 | |
|
164 | 165 | def send_new_password_email(user) |
|
165 | 166 | contest_name = GraderConfiguration['contest.name'] |
|
166 | 167 | mail_subject = "[#{contest_name}] Password recovery" |
|
167 | 168 | mail_body = t('registration.password_retrieval.email_body', { |
|
168 | 169 | :full_name => user.full_name, |
|
169 | 170 | :contest_name => contest_name, |
|
170 | 171 | :login => user.login, |
|
171 | 172 | :password => user.password, |
|
172 | 173 | :admin_email => admin_email |
|
173 | 174 | }) |
|
174 | 175 | |
|
175 | 176 | logger.info mail_body |
|
176 | 177 | |
|
177 | 178 | send_mail(user.email, mail_subject, mail_body) |
|
178 | 179 | end |
|
179 | 180 | |
|
180 | 181 | # allow viewing of regular user profile only when options allow so |
|
181 | 182 | # only admins can view admins profile |
|
182 | 183 | def profile_authorization |
|
183 | 184 | #if view admins' profile, allow only admin |
|
184 | 185 | return false unless(params[:id]) |
|
185 | 186 | user = User.find(params[:id]) |
|
186 | 187 | return false unless user |
|
187 | 188 | return admin_authorization if user.admin? |
|
188 | 189 | return true if GraderConfiguration["right.user_view_submission"] |
|
189 | 190 | |
|
190 | 191 | #finally, we allow only admin |
|
191 | 192 | admin_authorization |
|
192 | 193 | end |
|
193 | 194 | |
|
194 | 195 | end |
You need to be logged in to leave comments.
Login now