Description:
fix access right control bugs
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r683:d4ac431eeeb6 - - 2 files changed: 9 inserted, 7 deleted
@@ -1,113 +1,115 | |||||
|
1 | class SubmissionsController < ApplicationController |
|
1 | class SubmissionsController < ApplicationController |
|
2 | before_action :authenticate |
|
2 | before_action :authenticate |
|
3 |
- before_action :submission_authorization, only: [:show, |
|
3 | + before_action :submission_authorization, only: [:show, :download, :edit] |
|
4 | before_action :admin_authorization, only: [:rejudge] |
|
4 | before_action :admin_authorization, only: [:rejudge] |
|
5 |
|
5 | ||
|
6 | # GET /submissions |
|
6 | # GET /submissions |
|
7 | # GET /submissions.json |
|
7 | # GET /submissions.json |
|
8 | # Show problem selection and user's submission of that problem |
|
8 | # Show problem selection and user's submission of that problem |
|
9 | def index |
|
9 | def index |
|
10 | @user = @current_user |
|
10 | @user = @current_user |
|
11 | @problems = @user.available_problems |
|
11 | @problems = @user.available_problems |
|
12 |
|
12 | ||
|
13 | if params[:problem_id]==nil |
|
13 | if params[:problem_id]==nil |
|
14 | @problem = nil |
|
14 | @problem = nil |
|
15 | @submissions = nil |
|
15 | @submissions = nil |
|
16 | else |
|
16 | else |
|
17 | @problem = Problem.find_by_id(params[:problem_id]) |
|
17 | @problem = Problem.find_by_id(params[:problem_id]) |
|
18 | if (@problem == nil) or (not @problem.available) |
|
18 | if (@problem == nil) or (not @problem.available) |
|
19 | redirect_to main_list_path |
|
19 | redirect_to main_list_path |
|
20 | flash[:notice] = 'Error: submissions for that problem are not viewable.' |
|
20 | flash[:notice] = 'Error: submissions for that problem are not viewable.' |
|
21 | return |
|
21 | return |
|
22 | end |
|
22 | end |
|
23 | @submissions = Submission.find_all_by_user_problem(@user.id, @problem.id).order(id: :desc) |
|
23 | @submissions = Submission.find_all_by_user_problem(@user.id, @problem.id).order(id: :desc) |
|
24 | end |
|
24 | end |
|
25 | end |
|
25 | end |
|
26 |
|
26 | ||
|
27 | # GET /submissions/1 |
|
27 | # GET /submissions/1 |
|
28 | # GET /submissions/1.json |
|
28 | # GET /submissions/1.json |
|
29 | def show |
|
29 | def show |
|
30 | @submission = Submission.find(params[:id]) |
|
30 | @submission = Submission.find(params[:id]) |
|
31 |
|
31 | ||
|
32 | #log the viewing |
|
32 | #log the viewing |
|
33 | user = User.find(session[:user_id]) |
|
33 | user = User.find(session[:user_id]) |
|
34 | SubmissionViewLog.create(user_id: session[:user_id],submission_id: @submission.id) unless user.admin? |
|
34 | SubmissionViewLog.create(user_id: session[:user_id],submission_id: @submission.id) unless user.admin? |
|
35 |
|
35 | ||
|
36 | @task = @submission.task |
|
36 | @task = @submission.task |
|
37 | end |
|
37 | end |
|
38 |
|
38 | ||
|
39 | def download |
|
39 | def download |
|
40 | @submission = Submission.find(params[:id]) |
|
40 | @submission = Submission.find(params[:id]) |
|
41 | send_data(@submission.source, {:filename => @submission.download_filename, :type => 'text/plain'}) |
|
41 | send_data(@submission.source, {:filename => @submission.download_filename, :type => 'text/plain'}) |
|
42 | end |
|
42 | end |
|
43 |
|
43 | ||
|
44 | def compiler_msg |
|
44 | def compiler_msg |
|
45 | @submission = Submission.find(params[:id]) |
|
45 | @submission = Submission.find(params[:id]) |
|
46 | respond_to do |format| |
|
46 | respond_to do |format| |
|
47 | format.js |
|
47 | format.js |
|
48 | end |
|
48 | end |
|
49 | end |
|
49 | end |
|
50 |
|
50 | ||
|
51 | #on-site new submission on specific problem |
|
51 | #on-site new submission on specific problem |
|
52 | def direct_edit_problem |
|
52 | def direct_edit_problem |
|
53 | @problem = Problem.find(params[:problem_id]) |
|
53 | @problem = Problem.find(params[:problem_id]) |
|
|
54 | + unless @current_user.can_view_problem?(@problem) | ||
|
|
55 | + unauthorized_redirect | ||
|
|
56 | + return | ||
|
|
57 | + end | ||
|
54 | @source = '' |
|
58 | @source = '' |
|
55 |
- if (params[: |
|
59 | + if (params[:view_latest]) |
|
56 | - u = User.find(params[:user_id]) |
|
60 | + sub = Submission.find_last_by_user_and_problem(@current_user.id,@problem.id) |
|
57 | - @submission = Submission.find_last_by_user_and_problem(u.id,@problem.id) |
|
||
|
58 | @source = @submission.source.to_s if @submission and @submission.source |
|
61 | @source = @submission.source.to_s if @submission and @submission.source |
|
59 | end |
|
62 | end |
|
60 | render 'edit' |
|
63 | render 'edit' |
|
61 | end |
|
64 | end |
|
62 |
|
65 | ||
|
63 | # GET /submissions/1/edit |
|
66 | # GET /submissions/1/edit |
|
64 | def edit |
|
67 | def edit |
|
65 | @submission = Submission.find(params[:id]) |
|
68 | @submission = Submission.find(params[:id]) |
|
66 | @source = @submission.source.to_s |
|
69 | @source = @submission.source.to_s |
|
67 | @problem = @submission.problem |
|
70 | @problem = @submission.problem |
|
68 | @lang_id = @submission.language.id |
|
71 | @lang_id = @submission.language.id |
|
69 | end |
|
72 | end |
|
70 |
|
73 | ||
|
71 |
|
74 | ||
|
72 | def get_latest_submission_status |
|
75 | def get_latest_submission_status |
|
73 | @problem = Problem.find(params[:pid]) |
|
76 | @problem = Problem.find(params[:pid]) |
|
74 | @submission = Submission.find_last_by_user_and_problem(params[:uid],params[:pid]) |
|
77 | @submission = Submission.find_last_by_user_and_problem(params[:uid],params[:pid]) |
|
75 | puts User.find(params[:uid]).login |
|
78 | puts User.find(params[:uid]).login |
|
76 | puts Problem.find(params[:pid]).name |
|
79 | puts Problem.find(params[:pid]).name |
|
77 | puts 'nil' unless @submission |
|
80 | puts 'nil' unless @submission |
|
78 | respond_to do |format| |
|
81 | respond_to do |format| |
|
79 | format.js |
|
82 | format.js |
|
80 | end |
|
83 | end |
|
81 | end |
|
84 | end |
|
82 |
|
85 | ||
|
83 | # GET /submissions/:id/rejudge |
|
86 | # GET /submissions/:id/rejudge |
|
84 | def rejudge |
|
87 | def rejudge |
|
85 | @submission = Submission.find(params[:id]) |
|
88 | @submission = Submission.find(params[:id]) |
|
86 | @task = @submission.task |
|
89 | @task = @submission.task |
|
87 | @task.status_inqueue! if @task |
|
90 | @task.status_inqueue! if @task |
|
88 | respond_to do |format| |
|
91 | respond_to do |format| |
|
89 | format.js |
|
92 | format.js |
|
90 | end |
|
93 | end |
|
91 | end |
|
94 | end |
|
92 |
|
95 | ||
|
93 | protected |
|
96 | protected |
|
94 |
|
97 | ||
|
95 | def submission_authorization |
|
98 | def submission_authorization |
|
96 | #admin always has privileged |
|
99 | #admin always has privileged |
|
97 | if @current_user.admin? |
|
100 | if @current_user.admin? |
|
98 | return true |
|
101 | return true |
|
99 | end |
|
102 | end |
|
100 |
|
103 | ||
|
101 | sub = Submission.find(params[:id]) |
|
104 | sub = Submission.find(params[:id]) |
|
102 | - if sub.problem.available? |
|
105 | + if @current_user.available_problems.include? sub.problem |
|
103 | - puts "sub = #{sub.user.id}, current = #{@current_user.id}" |
|
||
|
104 | return true if GraderConfiguration["right.user_view_submission"] or sub.user == @current_user |
|
106 | return true if GraderConfiguration["right.user_view_submission"] or sub.user == @current_user |
|
105 | end |
|
107 | end |
|
106 |
|
108 | ||
|
107 | #default to NO |
|
109 | #default to NO |
|
108 | unauthorized_redirect |
|
110 | unauthorized_redirect |
|
109 | return false |
|
111 | return false |
|
110 | end |
|
112 | end |
|
111 |
|
113 | ||
|
112 |
|
114 | ||
|
113 | end |
|
115 | end |
@@ -1,166 +1,166 | |||||
|
1 | class Submission < ActiveRecord::Base |
|
1 | class Submission < ActiveRecord::Base |
|
2 |
|
2 | ||
|
3 | belongs_to :language |
|
3 | belongs_to :language |
|
4 | belongs_to :problem |
|
4 | belongs_to :problem |
|
5 | belongs_to :user |
|
5 | belongs_to :user |
|
6 |
|
6 | ||
|
7 | before_validation :assign_problem |
|
7 | before_validation :assign_problem |
|
8 | before_validation :assign_language |
|
8 | before_validation :assign_language |
|
9 |
|
9 | ||
|
10 | validates_presence_of :source |
|
10 | validates_presence_of :source |
|
11 | validates_length_of :source, :maximum => 100_000, :allow_blank => true, :message => 'too long' |
|
11 | validates_length_of :source, :maximum => 100_000, :allow_blank => true, :message => 'too long' |
|
12 | validates_length_of :source, :minimum => 1, :allow_blank => true, :message => 'too short' |
|
12 | validates_length_of :source, :minimum => 1, :allow_blank => true, :message => 'too short' |
|
13 | validate :must_have_valid_problem |
|
13 | validate :must_have_valid_problem |
|
14 | validate :must_specify_language |
|
14 | validate :must_specify_language |
|
15 |
|
15 | ||
|
16 | has_one :task |
|
16 | has_one :task |
|
17 |
|
17 | ||
|
18 | before_save :assign_latest_number_if_new_recond |
|
18 | before_save :assign_latest_number_if_new_recond |
|
19 |
|
19 | ||
|
20 | def self.find_last_by_user_and_problem(user_id, problem_id) |
|
20 | def self.find_last_by_user_and_problem(user_id, problem_id) |
|
21 | where("user_id = ? AND problem_id = ?",user_id,problem_id).last |
|
21 | where("user_id = ? AND problem_id = ?",user_id,problem_id).last |
|
22 | end |
|
22 | end |
|
23 |
|
23 | ||
|
24 | def self.find_all_last_by_problem(problem_id) |
|
24 | def self.find_all_last_by_problem(problem_id) |
|
25 | # need to put in SQL command, maybe there's a better way |
|
25 | # need to put in SQL command, maybe there's a better way |
|
26 | Submission.includes(:user).find_by_sql("SELECT * FROM submissions " + |
|
26 | Submission.includes(:user).find_by_sql("SELECT * FROM submissions " + |
|
27 | "WHERE id = " + |
|
27 | "WHERE id = " + |
|
28 | "(SELECT MAX(id) FROM submissions AS subs " + |
|
28 | "(SELECT MAX(id) FROM submissions AS subs " + |
|
29 | "WHERE subs.user_id = submissions.user_id AND " + |
|
29 | "WHERE subs.user_id = submissions.user_id AND " + |
|
30 | "problem_id = " + problem_id.to_s + " " + |
|
30 | "problem_id = " + problem_id.to_s + " " + |
|
31 | "GROUP BY user_id) " + |
|
31 | "GROUP BY user_id) " + |
|
32 | "ORDER BY user_id") |
|
32 | "ORDER BY user_id") |
|
33 | end |
|
33 | end |
|
34 |
|
34 | ||
|
35 | def self.find_in_range_by_user_and_problem(user_id, problem_id,since_id,until_id) |
|
35 | def self.find_in_range_by_user_and_problem(user_id, problem_id,since_id,until_id) |
|
36 | records = Submission.where(problem_id: problem_id,user_id: user_id) |
|
36 | records = Submission.where(problem_id: problem_id,user_id: user_id) |
|
37 | records = records.where('id >= ?',since_id) if since_id > 0 |
|
37 | records = records.where('id >= ?',since_id) if since_id > 0 |
|
38 | records = records.where('id <= ?',until_id) if until_id > 0 |
|
38 | records = records.where('id <= ?',until_id) if until_id > 0 |
|
39 | records.all |
|
39 | records.all |
|
40 | end |
|
40 | end |
|
41 |
|
41 | ||
|
42 | def self.find_last_for_all_available_problems(user_id) |
|
42 | def self.find_last_for_all_available_problems(user_id) |
|
43 | submissions = Array.new |
|
43 | submissions = Array.new |
|
44 | problems = Problem.available_problems |
|
44 | problems = Problem.available_problems |
|
45 | problems.each do |problem| |
|
45 | problems.each do |problem| |
|
46 | sub = Submission.find_last_by_user_and_problem(user_id, problem.id) |
|
46 | sub = Submission.find_last_by_user_and_problem(user_id, problem.id) |
|
47 | submissions << sub if sub!=nil |
|
47 | submissions << sub if sub!=nil |
|
48 | end |
|
48 | end |
|
49 | submissions |
|
49 | submissions |
|
50 | end |
|
50 | end |
|
51 |
|
51 | ||
|
52 | def self.find_by_user_problem_number(user_id, problem_id, number) |
|
52 | def self.find_by_user_problem_number(user_id, problem_id, number) |
|
53 | where("user_id = ? AND problem_id = ? AND number = ?",user_id,problem_id,number).first |
|
53 | where("user_id = ? AND problem_id = ? AND number = ?",user_id,problem_id,number).first |
|
54 | end |
|
54 | end |
|
55 |
|
55 | ||
|
56 | def self.find_all_by_user_problem(user_id, problem_id) |
|
56 | def self.find_all_by_user_problem(user_id, problem_id) |
|
57 | where("user_id = ? AND problem_id = ?",user_id,problem_id) |
|
57 | where("user_id = ? AND problem_id = ?",user_id,problem_id) |
|
58 | end |
|
58 | end |
|
59 |
|
59 | ||
|
60 | def download_filename |
|
60 | def download_filename |
|
61 | if self.problem.output_only |
|
61 | if self.problem.output_only |
|
62 | return self.source_filename |
|
62 | return self.source_filename |
|
63 | else |
|
63 | else |
|
64 | timestamp = self.submitted_at.localtime.strftime("%H%M%S") |
|
64 | timestamp = self.submitted_at.localtime.strftime("%H%M%S") |
|
65 | return "#{self.problem.name}-#{timestamp}.#{self.language.ext}" |
|
65 | return "#{self.problem.name}-#{timestamp}.#{self.language.ext}" |
|
66 | end |
|
66 | end |
|
67 | end |
|
67 | end |
|
68 |
|
68 | ||
|
69 | protected |
|
69 | protected |
|
70 |
|
70 | ||
|
71 | def self.find_option_in_source(option, source) |
|
71 | def self.find_option_in_source(option, source) |
|
72 | if source==nil |
|
72 | if source==nil |
|
73 | return nil |
|
73 | return nil |
|
74 | end |
|
74 | end |
|
75 | i = 0 |
|
75 | i = 0 |
|
76 | source.each_line do |s| |
|
76 | source.each_line do |s| |
|
77 | if s =~ option |
|
77 | if s =~ option |
|
78 | words = s.split |
|
78 | words = s.split |
|
79 | return words[1] |
|
79 | return words[1] |
|
80 | end |
|
80 | end |
|
81 | i = i + 1 |
|
81 | i = i + 1 |
|
82 | if i==10 |
|
82 | if i==10 |
|
83 | return nil |
|
83 | return nil |
|
84 | end |
|
84 | end |
|
85 | end |
|
85 | end |
|
86 | return nil |
|
86 | return nil |
|
87 | end |
|
87 | end |
|
88 |
|
88 | ||
|
89 | def self.find_language_in_source(source, source_filename="") |
|
89 | def self.find_language_in_source(source, source_filename="") |
|
90 | langopt = find_option_in_source(/^LANG:/,source) |
|
90 | langopt = find_option_in_source(/^LANG:/,source) |
|
91 | if langopt |
|
91 | if langopt |
|
92 | return (Language.find_by_name(langopt) || |
|
92 | return (Language.find_by_name(langopt) || |
|
93 | Language.find_by_pretty_name(langopt)) |
|
93 | Language.find_by_pretty_name(langopt)) |
|
94 | else |
|
94 | else |
|
95 | if source_filename |
|
95 | if source_filename |
|
96 | return Language.find_by_extension(source_filename.split('.').last) |
|
96 | return Language.find_by_extension(source_filename.split('.').last) |
|
97 | else |
|
97 | else |
|
98 | return nil |
|
98 | return nil |
|
99 | end |
|
99 | end |
|
100 | end |
|
100 | end |
|
101 | end |
|
101 | end |
|
102 |
|
102 | ||
|
103 | def self.find_problem_in_source(source, source_filename="") |
|
103 | def self.find_problem_in_source(source, source_filename="") |
|
104 | prob_opt = find_option_in_source(/^TASK:/,source) |
|
104 | prob_opt = find_option_in_source(/^TASK:/,source) |
|
105 | if problem = Problem.find_by_name(prob_opt) |
|
105 | if problem = Problem.find_by_name(prob_opt) |
|
106 | return problem |
|
106 | return problem |
|
107 | else |
|
107 | else |
|
108 | if source_filename |
|
108 | if source_filename |
|
109 | return Problem.find_by_name(source_filename.split('.').first) |
|
109 | return Problem.find_by_name(source_filename.split('.').first) |
|
110 | else |
|
110 | else |
|
111 | return nil |
|
111 | return nil |
|
112 | end |
|
112 | end |
|
113 | end |
|
113 | end |
|
114 | end |
|
114 | end |
|
115 |
|
115 | ||
|
116 | def assign_problem |
|
116 | def assign_problem |
|
117 | if self.problem_id!=-1 |
|
117 | if self.problem_id!=-1 |
|
118 | begin |
|
118 | begin |
|
119 | self.problem = Problem.find(self.problem_id) |
|
119 | self.problem = Problem.find(self.problem_id) |
|
120 | rescue ActiveRecord::RecordNotFound |
|
120 | rescue ActiveRecord::RecordNotFound |
|
121 | self.problem = nil |
|
121 | self.problem = nil |
|
122 | end |
|
122 | end |
|
123 | else |
|
123 | else |
|
124 | self.problem = Submission.find_problem_in_source(self.source, |
|
124 | self.problem = Submission.find_problem_in_source(self.source, |
|
125 | self.source_filename) |
|
125 | self.source_filename) |
|
126 | end |
|
126 | end |
|
127 | end |
|
127 | end |
|
128 |
|
128 | ||
|
129 | def assign_language |
|
129 | def assign_language |
|
130 | self.language = Submission.find_language_in_source(self.source, |
|
130 | self.language = Submission.find_language_in_source(self.source, |
|
131 | self.source_filename) |
|
131 | self.source_filename) |
|
132 | end |
|
132 | end |
|
133 |
|
133 | ||
|
134 | # validation codes |
|
134 | # validation codes |
|
135 | def must_specify_language |
|
135 | def must_specify_language |
|
136 | return if self.source==nil |
|
136 | return if self.source==nil |
|
137 |
|
137 | ||
|
138 | # for output_only tasks |
|
138 | # for output_only tasks |
|
139 | return if self.problem!=nil and self.problem.output_only |
|
139 | return if self.problem!=nil and self.problem.output_only |
|
140 |
|
140 | ||
|
141 | if self.language==nil |
|
141 | if self.language==nil |
|
142 | errors.add('source',"Cannot detect language. Did you submit a correct source file?") unless self.language!=nil |
|
142 | errors.add('source',"Cannot detect language. Did you submit a correct source file?") unless self.language!=nil |
|
143 | end |
|
143 | end |
|
144 | end |
|
144 | end |
|
145 |
|
145 | ||
|
146 | def must_have_valid_problem |
|
146 | def must_have_valid_problem |
|
147 | return if self.source==nil |
|
147 | return if self.source==nil |
|
148 | if self.problem==nil |
|
148 | if self.problem==nil |
|
149 | errors.add('problem',"must be specified.") |
|
149 | errors.add('problem',"must be specified.") |
|
150 | else |
|
150 | else |
|
151 | #admin always have right |
|
151 | #admin always have right |
|
152 | return if self.user.admin? |
|
152 | return if self.user.admin? |
|
153 |
|
153 | ||
|
154 | #check if user has the right to submit the problem |
|
154 | #check if user has the right to submit the problem |
|
155 | - errors.add('problem',"must be valid.") if (!self.user.available_problem.include?(self.problem)) and (self.new_record?) |
|
155 | + errors.add('problem',"must be valid.") if (!self.user.available_problems.include?(self.problem)) and (self.new_record?) |
|
156 | end |
|
156 | end |
|
157 | end |
|
157 | end |
|
158 |
|
158 | ||
|
159 | # callbacks |
|
159 | # callbacks |
|
160 | def assign_latest_number_if_new_recond |
|
160 | def assign_latest_number_if_new_recond |
|
161 | return if !self.new_record? |
|
161 | return if !self.new_record? |
|
162 | latest = Submission.find_last_by_user_and_problem(self.user_id, self.problem_id) |
|
162 | latest = Submission.find_last_by_user_and_problem(self.user_id, self.problem_id) |
|
163 | self.number = (latest==nil) ? 1 : latest.number + 1; |
|
163 | self.number = (latest==nil) ? 1 : latest.number + 1; |
|
164 | end |
|
164 | end |
|
165 |
|
165 | ||
|
166 | end |
|
166 | end |
You need to be logged in to leave comments.
Login now