Description:
fixed login box showing in announcement box after user logged out in other tabs
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r249:11f906d2e75a - - 1 file changed: 10 inserted, 0 deleted
@@ -1,330 +1,340 | |||
|
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 :update_user_start_time, :except => [:index, :login] |
|
7 | 7 | |
|
8 | + # to prevent log in box to be shown when user logged out of the | |
|
9 | + # system only in some tab | |
|
10 | + prepend_before_filter :reject_announcement_refresh_when_logged_out, :only => [:announcements] | |
|
11 | + | |
|
8 | 12 | # COMMENTED OUT: filter in each action instead |
|
9 | 13 | # before_filter :verify_time_limit, :only => [:submit] |
|
10 | 14 | |
|
11 | 15 | verify :method => :post, :only => [:submit], |
|
12 | 16 | :redirect_to => { :action => :index } |
|
13 | 17 | |
|
14 | 18 | # COMMENT OUT: only need when having high load |
|
15 | 19 | # caches_action :index, :login |
|
16 | 20 | |
|
17 | 21 | # NOTE: This method is not actually needed, 'config/routes.rb' has |
|
18 | 22 | # assigned action login as a default action. |
|
19 | 23 | def index |
|
20 | 24 | redirect_to :action => 'login' |
|
21 | 25 | end |
|
22 | 26 | |
|
23 | 27 | def login |
|
24 | 28 | saved_notice = flash[:notice] |
|
25 | 29 | reset_session |
|
26 | 30 | flash.now[:notice] = saved_notice |
|
27 | 31 | |
|
28 | 32 | # EXPERIMENT: |
|
29 | 33 | # Hide login if in single user mode and the url does not |
|
30 | 34 | # explicitly specify /login |
|
31 | 35 | # |
|
32 | 36 | # logger.info "PATH: #{request.path}" |
|
33 | 37 | # if Configuration['system.single_user_mode'] and |
|
34 | 38 | # request.path!='/main/login' |
|
35 | 39 | # @hidelogin = true |
|
36 | 40 | # end |
|
37 | 41 | |
|
38 | 42 | @announcements = Announcement.find_for_frontpage |
|
39 | 43 | render :action => 'login', :layout => 'empty' |
|
40 | 44 | end |
|
41 | 45 | |
|
42 | 46 | def list |
|
43 | 47 | prepare_list_information |
|
44 | 48 | end |
|
45 | 49 | |
|
46 | 50 | def help |
|
47 | 51 | @user = User.find(session[:user_id]) |
|
48 | 52 | end |
|
49 | 53 | |
|
50 | 54 | def submit |
|
51 | 55 | user = User.find(session[:user_id]) |
|
52 | 56 | |
|
53 | 57 | @submission = Submission.new(params[:submission]) |
|
54 | 58 | @submission.user = user |
|
55 | 59 | @submission.language_id = 0 |
|
56 | 60 | if (params['file']) and (params['file']!='') |
|
57 | 61 | @submission.source = params['file'].read |
|
58 | 62 | @submission.source_filename = params['file'].original_filename |
|
59 | 63 | end |
|
60 | 64 | @submission.submitted_at = Time.new.gmtime |
|
61 | 65 | |
|
62 | 66 | if Configuration.time_limit_mode? and user.contest_finished? |
|
63 | 67 | @submission.errors.add_to_base "The contest is over." |
|
64 | 68 | prepare_list_information |
|
65 | 69 | render :action => 'list' and return |
|
66 | 70 | end |
|
67 | 71 | |
|
68 | 72 | if @submission.valid? |
|
69 | 73 | if @submission.save == false |
|
70 | 74 | flash[:notice] = 'Error saving your submission' |
|
71 | 75 | elsif Task.create(:submission_id => @submission.id, |
|
72 | 76 | :status => Task::STATUS_INQUEUE) == false |
|
73 | 77 | flash[:notice] = 'Error adding your submission to task queue' |
|
74 | 78 | end |
|
75 | 79 | else |
|
76 | 80 | prepare_list_information |
|
77 | 81 | render :action => 'list' and return |
|
78 | 82 | end |
|
79 | 83 | redirect_to :action => 'list' |
|
80 | 84 | end |
|
81 | 85 | |
|
82 | 86 | def source |
|
83 | 87 | submission = Submission.find(params[:id]) |
|
84 | 88 | if submission.user_id == session[:user_id] |
|
85 | 89 | send_data(submission.source, |
|
86 | 90 | {:filename => submission.download_filename, |
|
87 | 91 | :type => 'text/plain'}) |
|
88 | 92 | else |
|
89 | 93 | flash[:notice] = 'Error viewing source' |
|
90 | 94 | redirect_to :action => 'list' |
|
91 | 95 | end |
|
92 | 96 | end |
|
93 | 97 | |
|
94 | 98 | def compiler_msg |
|
95 | 99 | @submission = Submission.find(params[:id]) |
|
96 | 100 | if @submission.user_id == session[:user_id] |
|
97 | 101 | render :action => 'compiler_msg', :layout => 'empty' |
|
98 | 102 | else |
|
99 | 103 | flash[:notice] = 'Error viewing source' |
|
100 | 104 | redirect_to :action => 'list' |
|
101 | 105 | end |
|
102 | 106 | end |
|
103 | 107 | |
|
104 | 108 | def submission |
|
105 | 109 | @user = User.find(session[:user_id]) |
|
106 | 110 | @problems = Problem.find_available_problems |
|
107 | 111 | if params[:id]==nil |
|
108 | 112 | @problem = nil |
|
109 | 113 | @submissions = nil |
|
110 | 114 | else |
|
111 | 115 | @problem = Problem.find_by_name(params[:id]) |
|
112 | 116 | if not @problem.available |
|
113 | 117 | redirect_to :action => 'list' |
|
114 | 118 | flash[:notice] = 'Error: submissions for that problem are not viewable.' |
|
115 | 119 | return |
|
116 | 120 | end |
|
117 | 121 | @submissions = Submission.find_all_by_user_problem(@user.id, @problem.id) |
|
118 | 122 | end |
|
119 | 123 | end |
|
120 | 124 | |
|
121 | 125 | def result |
|
122 | 126 | if !Configuration.show_grading_result |
|
123 | 127 | redirect_to :action => 'list' and return |
|
124 | 128 | end |
|
125 | 129 | @user = User.find(session[:user_id]) |
|
126 | 130 | @submission = Submission.find(params[:id]) |
|
127 | 131 | if @submission.user!=@user |
|
128 | 132 | flash[:notice] = 'You are not allowed to view result of other users.' |
|
129 | 133 | redirect_to :action => 'list' and return |
|
130 | 134 | end |
|
131 | 135 | prepare_grading_result(@submission) |
|
132 | 136 | end |
|
133 | 137 | |
|
134 | 138 | def load_output |
|
135 | 139 | if !Configuration.show_grading_result or params[:num]==nil |
|
136 | 140 | redirect_to :action => 'list' and return |
|
137 | 141 | end |
|
138 | 142 | @user = User.find(session[:user_id]) |
|
139 | 143 | @submission = Submission.find(params[:id]) |
|
140 | 144 | if @submission.user!=@user |
|
141 | 145 | flash[:notice] = 'You are not allowed to view result of other users.' |
|
142 | 146 | redirect_to :action => 'list' and return |
|
143 | 147 | end |
|
144 | 148 | case_num = params[:num].to_i |
|
145 | 149 | out_filename = output_filename(@user.login, |
|
146 | 150 | @submission.problem.name, |
|
147 | 151 | @submission.id, |
|
148 | 152 | case_num) |
|
149 | 153 | if !FileTest.exists?(out_filename) |
|
150 | 154 | flash[:notice] = 'Output not found.' |
|
151 | 155 | redirect_to :action => 'list' and return |
|
152 | 156 | end |
|
153 | 157 | |
|
154 | 158 | response.headers['Content-Type'] = "application/force-download" |
|
155 | 159 | response.headers['Content-Disposition'] = "attachment; filename=\"output-#{case_num}.txt\"" |
|
156 | 160 | response.headers["X-Sendfile"] = out_filename |
|
157 | 161 | response.headers['Content-length'] = File.size(out_filename) |
|
158 | 162 | render :nothing => true |
|
159 | 163 | end |
|
160 | 164 | |
|
161 | 165 | def error |
|
162 | 166 | @user = User.find(session[:user_id]) |
|
163 | 167 | end |
|
164 | 168 | |
|
165 | 169 | # announcement refreshing and hiding methods |
|
166 | 170 | |
|
167 | 171 | def announcements |
|
168 | 172 | if params.has_key? 'recent' |
|
169 | 173 | prepare_announcements(params[:recent]) |
|
170 | 174 | else |
|
171 | 175 | prepare_announcements |
|
172 | 176 | end |
|
173 | 177 | render(:partial => 'announcement', |
|
174 | 178 | :collection => @announcements, |
|
175 | 179 | :locals => {:announcement_effect => true}) |
|
176 | 180 | end |
|
177 | 181 | |
|
178 | 182 | protected |
|
179 | 183 | |
|
180 | 184 | def prepare_announcements(recent=nil) |
|
181 | 185 | if Configuration.show_tasks_to?(@user) |
|
182 | 186 | @announcements = Announcement.find_published(true) |
|
183 | 187 | else |
|
184 | 188 | @announcements = Announcement.find_published |
|
185 | 189 | end |
|
186 | 190 | if recent!=nil |
|
187 | 191 | recent_id = recent.to_i |
|
188 | 192 | @announcements = @announcements.find_all { |a| a.id > recent_id } |
|
189 | 193 | end |
|
190 | 194 | end |
|
191 | 195 | |
|
192 | 196 | def prepare_list_information |
|
193 | 197 | @problems = Problem.find_available_problems |
|
194 | 198 | @prob_submissions = Array.new |
|
195 | 199 | @user = User.find(session[:user_id]) |
|
196 | 200 | @problems.each do |p| |
|
197 | 201 | sub = Submission.find_last_by_user_and_problem(@user.id,p.id) |
|
198 | 202 | if sub!=nil |
|
199 | 203 | @prob_submissions << { :count => sub.number, :submission => sub } |
|
200 | 204 | else |
|
201 | 205 | @prob_submissions << { :count => 0, :submission => nil } |
|
202 | 206 | end |
|
203 | 207 | end |
|
204 | 208 | prepare_announcements |
|
205 | 209 | end |
|
206 | 210 | |
|
207 | 211 | def check_viewability |
|
208 | 212 | @user = User.find(session[:user_id]) |
|
209 | 213 | if (!Configuration.show_tasks_to?(@user)) and |
|
210 | 214 | ((action_name=='submission') or (action_name=='submit')) |
|
211 | 215 | redirect_to :action => 'list' and return |
|
212 | 216 | end |
|
213 | 217 | end |
|
214 | 218 | |
|
215 | 219 | def prepare_grading_result(submission) |
|
216 | 220 | if Configuration.task_grading_info.has_key? submission.problem.name |
|
217 | 221 | grading_info = Configuration.task_grading_info[submission.problem.name] |
|
218 | 222 | else |
|
219 | 223 | # guess task info from problem.full_score |
|
220 | 224 | cases = submission.problem.full_score / 10 |
|
221 | 225 | grading_info = { |
|
222 | 226 | 'testruns' => cases, |
|
223 | 227 | 'testcases' => cases |
|
224 | 228 | } |
|
225 | 229 | end |
|
226 | 230 | @test_runs = [] |
|
227 | 231 | if grading_info['testruns'].is_a? Integer |
|
228 | 232 | trun_count = grading_info['testruns'] |
|
229 | 233 | trun_count.times do |i| |
|
230 | 234 | @test_runs << [ read_grading_result(@user.login, |
|
231 | 235 | submission.problem.name, |
|
232 | 236 | submission.id, |
|
233 | 237 | i+1) ] |
|
234 | 238 | end |
|
235 | 239 | else |
|
236 | 240 | grading_info['testruns'].keys.sort.each do |num| |
|
237 | 241 | run = [] |
|
238 | 242 | testrun = grading_info['testruns'][num] |
|
239 | 243 | testrun.each do |c| |
|
240 | 244 | run << read_grading_result(@user.login, |
|
241 | 245 | submission.problem.name, |
|
242 | 246 | submission.id, |
|
243 | 247 | c) |
|
244 | 248 | end |
|
245 | 249 | @test_runs << run |
|
246 | 250 | end |
|
247 | 251 | end |
|
248 | 252 | end |
|
249 | 253 | |
|
250 | 254 | def grading_result_dir(user_name, problem_name, submission_id, case_num) |
|
251 | 255 | return "#{GRADING_RESULT_DIR}/#{user_name}/#{problem_name}/#{submission_id}/test-result/#{case_num}" |
|
252 | 256 | end |
|
253 | 257 | |
|
254 | 258 | def output_filename(user_name, problem_name, submission_id, case_num) |
|
255 | 259 | dir = grading_result_dir(user_name,problem_name, submission_id, case_num) |
|
256 | 260 | return "#{dir}/output.txt" |
|
257 | 261 | end |
|
258 | 262 | |
|
259 | 263 | def read_grading_result(user_name, problem_name, submission_id, case_num) |
|
260 | 264 | dir = grading_result_dir(user_name,problem_name, submission_id, case_num) |
|
261 | 265 | result_file_name = "#{dir}/result" |
|
262 | 266 | if !FileTest.exists?(result_file_name) |
|
263 | 267 | return {:num => case_num, :msg => 'program did not run'} |
|
264 | 268 | else |
|
265 | 269 | results = File.open(result_file_name).readlines |
|
266 | 270 | run_stat = extract_running_stat(results) |
|
267 | 271 | output_filename = "#{dir}/output.txt" |
|
268 | 272 | if FileTest.exists?(output_filename) |
|
269 | 273 | output_file = true |
|
270 | 274 | output_size = File.size(output_filename) |
|
271 | 275 | else |
|
272 | 276 | output_file = false |
|
273 | 277 | output_size = 0 |
|
274 | 278 | end |
|
275 | 279 | |
|
276 | 280 | return { |
|
277 | 281 | :num => case_num, |
|
278 | 282 | :msg => results[0], |
|
279 | 283 | :run_stat => run_stat, |
|
280 | 284 | :output => output_file, |
|
281 | 285 | :output_size => output_size |
|
282 | 286 | } |
|
283 | 287 | end |
|
284 | 288 | end |
|
285 | 289 | |
|
286 | 290 | # copied from grader/script/lib/test_request_helper.rb |
|
287 | 291 | def extract_running_stat(results) |
|
288 | 292 | running_stat_line = results[-1] |
|
289 | 293 | |
|
290 | 294 | # extract exit status line |
|
291 | 295 | run_stat = "" |
|
292 | 296 | if !(/[Cc]orrect/.match(results[0])) |
|
293 | 297 | run_stat = results[0].chomp |
|
294 | 298 | else |
|
295 | 299 | run_stat = 'Program exited normally' |
|
296 | 300 | end |
|
297 | 301 | |
|
298 | 302 | logger.info "Stat line: #{running_stat_line}" |
|
299 | 303 | |
|
300 | 304 | # extract running time |
|
301 | 305 | if res = /r(.*)u(.*)s/.match(running_stat_line) |
|
302 | 306 | seconds = (res[1].to_f + res[2].to_f) |
|
303 | 307 | time_stat = "Time used: #{seconds} sec." |
|
304 | 308 | else |
|
305 | 309 | seconds = nil |
|
306 | 310 | time_stat = "Time used: n/a sec." |
|
307 | 311 | end |
|
308 | 312 | |
|
309 | 313 | # extract memory usage |
|
310 | 314 | if res = /s(.*)m/.match(running_stat_line) |
|
311 | 315 | memory_used = res[1].to_i |
|
312 | 316 | else |
|
313 | 317 | memory_used = -1 |
|
314 | 318 | end |
|
315 | 319 | |
|
316 | 320 | return { |
|
317 | 321 | :msg => "#{run_stat}\n#{time_stat}", |
|
318 | 322 | :running_time => seconds, |
|
319 | 323 | :exit_status => run_stat, |
|
320 | 324 | :memory_usage => memory_used |
|
321 | 325 | } |
|
322 | 326 | end |
|
323 | 327 | |
|
324 | 328 | def update_user_start_time |
|
325 | 329 | user = User.find(session[:user_id]) |
|
326 | 330 | UserContestStat.update_user_start_time(user) |
|
327 | 331 | end |
|
328 | 332 | |
|
333 | + def reject_announcement_refresh_when_logged_out | |
|
334 | + if not session[:user_id] | |
|
335 | + render :text => 'Access forbidden', :status => 403 | |
|
336 | + end | |
|
337 | + end | |
|
338 | + | |
|
329 | 339 | end |
|
330 | 340 |
You need to be logged in to leave comments.
Login now