Description:
add ip to front page, also fix seeds.rb
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r783:f1343ca4b141 - - 3 files changed: 3 inserted, 1 deleted

@@ -1,119 +1,120
1 class MainController < ApplicationController
1 class MainController < ApplicationController
2
2
3 before_action :check_valid_login, :except => [:login]
3 before_action :check_valid_login, :except => [:login]
4 before_action :check_viewability, :except => [:index, :login]
4 before_action :check_viewability, :except => [:index, :login]
5
5
6 append_before_action :confirm_and_update_start_time,
6 append_before_action :confirm_and_update_start_time,
7 :except => [:index,
7 :except => [:index,
8 :login,
8 :login,
9 :confirm_contest_start]
9 :confirm_contest_start]
10
10
11 # to prevent log in box to be shown when user logged out of the
11 # to prevent log in box to be shown when user logged out of the
12 # system only in some tab
12 # system only in some tab
13 prepend_before_action :reject_announcement_refresh_when_logged_out,
13 prepend_before_action :reject_announcement_refresh_when_logged_out,
14 :only => [:announcements]
14 :only => [:announcements]
15
15
16 before_action :authenticate_by_ip_address, :only => [:list]
16 before_action :authenticate_by_ip_address, :only => [:list]
17
17
18 #reset login, clear session
18 #reset login, clear session
19 #front page
19 #front page
20 def login
20 def login
21 saved_notice = flash[:notice]
21 saved_notice = flash[:notice]
22 reset_session
22 reset_session
23 flash.now[:notice] = saved_notice
23 flash.now[:notice] = saved_notice
24 + @remote_ip = request.remote_ip
24
25
25 # EXPERIMENT:
26 # EXPERIMENT:
26 # Hide login if in single user mode and the url does not
27 # Hide login if in single user mode and the url does not
27 # explicitly specify /login
28 # explicitly specify /login
28 #
29 #
29 # logger.info "PATH: #{request.path}"
30 # logger.info "PATH: #{request.path}"
30 # if GraderConfiguration['system.single_user_mode'] and
31 # if GraderConfiguration['system.single_user_mode'] and
31 # request.path!='/main/login'
32 # request.path!='/main/login'
32 # @hidelogin = true
33 # @hidelogin = true
33 # end
34 # end
34
35
35 @announcements = Announcement.frontpage
36 @announcements = Announcement.frontpage
36 render :action => 'login', :layout => 'empty'
37 render :action => 'login', :layout => 'empty'
37 end
38 end
38
39
39 def logout
40 def logout
40 reset_session
41 reset_session
41 redirect_to root_path
42 redirect_to root_path
42 end
43 end
43
44
44 def list
45 def list
45 prepare_list_information
46 prepare_list_information
46 end
47 end
47
48
48 def help
49 def help
49 @user = User.find(session[:user_id])
50 @user = User.find(session[:user_id])
50 end
51 end
51
52
52 def submit
53 def submit
53 user = User.find(session[:user_id])
54 user = User.find(session[:user_id])
54
55
55 @submission = Submission.new
56 @submission = Submission.new
56 @submission.problem_id = params[:submission][:problem_id]
57 @submission.problem_id = params[:submission][:problem_id]
57 @submission.user = user
58 @submission.user = user
58 @submission.language_id = 0
59 @submission.language_id = 0
59 if (params['file']) and (params['file']!='')
60 if (params['file']) and (params['file']!='')
60 @submission.source = File.open(params['file'].path,'r:UTF-8',&:read)
61 @submission.source = File.open(params['file'].path,'r:UTF-8',&:read)
61 @submission.source.encode!('UTF-8','UTF-8',invalid: :replace, replace: '')
62 @submission.source.encode!('UTF-8','UTF-8',invalid: :replace, replace: '')
62 @submission.source_filename = params['file'].original_filename
63 @submission.source_filename = params['file'].original_filename
63 end
64 end
64
65
65 if (params[:editor_text])
66 if (params[:editor_text])
66 language = Language.find_by_id(params[:language_id])
67 language = Language.find_by_id(params[:language_id])
67 @submission.source = params[:editor_text]
68 @submission.source = params[:editor_text]
68 @submission.source_filename = "live_edit.#{language.ext}"
69 @submission.source_filename = "live_edit.#{language.ext}"
69 @submission.language = language
70 @submission.language = language
70 end
71 end
71
72
72 @submission.submitted_at = Time.new.gmtime
73 @submission.submitted_at = Time.new.gmtime
73 @submission.ip_address = request.remote_ip
74 @submission.ip_address = request.remote_ip
74
75
75 if GraderConfiguration.time_limit_mode? and user.contest_finished?
76 if GraderConfiguration.time_limit_mode? and user.contest_finished?
76 @submission.errors.add(:base,"The contest is over.")
77 @submission.errors.add(:base,"The contest is over.")
77 prepare_list_information
78 prepare_list_information
78 render :action => 'list' and return
79 render :action => 'list' and return
79 end
80 end
80
81
81 if @submission.valid?(@current_user)
82 if @submission.valid?(@current_user)
82 if @submission.save == false
83 if @submission.save == false
83 flash[:notice] = 'Error saving your submission'
84 flash[:notice] = 'Error saving your submission'
84 elsif Task.create(:submission_id => @submission.id,
85 elsif Task.create(:submission_id => @submission.id,
85 :status => Task::STATUS_INQUEUE) == false
86 :status => Task::STATUS_INQUEUE) == false
86 flash[:notice] = 'Error adding your submission to task queue'
87 flash[:notice] = 'Error adding your submission to task queue'
87 end
88 end
88 else
89 else
89 prepare_list_information
90 prepare_list_information
90 render :action => 'list' and return
91 render :action => 'list' and return
91 end
92 end
92 redirect_to edit_submission_path(@submission)
93 redirect_to edit_submission_path(@submission)
93 end
94 end
94
95
95 def source
96 def source
96 submission = Submission.find(params[:id])
97 submission = Submission.find(params[:id])
97 if ((submission.user_id == session[:user_id]) and
98 if ((submission.user_id == session[:user_id]) and
98 (submission.problem != nil) and
99 (submission.problem != nil) and
99 (submission.problem.available))
100 (submission.problem.available))
100 send_data(submission.source,
101 send_data(submission.source,
101 {:filename => submission.download_filename,
102 {:filename => submission.download_filename,
102 :type => 'text/plain'})
103 :type => 'text/plain'})
103 else
104 else
104 flash[:notice] = 'Error viewing source'
105 flash[:notice] = 'Error viewing source'
105 redirect_to :action => 'list'
106 redirect_to :action => 'list'
106 end
107 end
107 end
108 end
108
109
109 def compiler_msg
110 def compiler_msg
110 @submission = Submission.find(params[:id])
111 @submission = Submission.find(params[:id])
111 if @submission.user_id == session[:user_id]
112 if @submission.user_id == session[:user_id]
112 render :action => 'compiler_msg', :layout => 'empty'
113 render :action => 'compiler_msg', :layout => 'empty'
113 else
114 else
114 flash[:notice] = 'Error viewing source'
115 flash[:notice] = 'Error viewing source'
115 redirect_to :action => 'list'
116 redirect_to :action => 'list'
116 end
117 end
117 end
118 end
118
119
119 def result
120 def result
@@ -1,11 +1,12
1 %h1= GraderConfiguration['ui.front.title']
1 %h1= GraderConfiguration['ui.front.title']
2
2
3 .row
3 .row
4 .col-md-6
4 .col-md-6
5 - if @announcements.length!=0
5 - if @announcements.length!=0
6 .announcementbox{:style => 'margin-top: 0px'}
6 .announcementbox{:style => 'margin-top: 0px'}
7 %span{:class => 'title'}
7 %span{:class => 'title'}
8 Announcements
8 Announcements
9 = render :partial => 'announcement', :collection => @announcements
9 = render :partial => 'announcement', :collection => @announcements
10 .col-md-4{style: "padding-left: 20px;"}
10 .col-md-4{style: "padding-left: 20px;"}
11 = render :partial => 'login_box'
11 = render :partial => 'login_box'
12 + = "current ip is #{@remote_ip}"
@@ -181,108 +181,108
181 },
181 },
182
182
183
183
184 {
184 {
185 :key => 'right.whitelist_ip_only',
185 :key => 'right.whitelist_ip_only',
186 :value_type => 'boolean',
186 :value_type => 'boolean',
187 :default_value => 'false',
187 :default_value => 'false',
188 :description => "If true, non-admin user will be able to use the system only when their ip is in the 'whitelist_ip'."
188 :description => "If true, non-admin user will be able to use the system only when their ip is in the 'whitelist_ip'."
189 },
189 },
190
190
191 {
191 {
192 :key => 'right.whitelist_ip',
192 :key => 'right.whitelist_ip',
193 :value_type => 'string',
193 :value_type => 'string',
194 :default_value => '0.0.0.0/0',
194 :default_value => '0.0.0.0/0',
195 :description => "list of whitelist ip, given in comma separated CIDR notation. For example '161.200.92.0/23, 161.200.80.1/32'"
195 :description => "list of whitelist ip, given in comma separated CIDR notation. For example '161.200.92.0/23, 161.200.80.1/32'"
196 },
196 },
197
197
198 ]
198 ]
199
199
200
200
201 def create_configuration_key(key,
201 def create_configuration_key(key,
202 value_type,
202 value_type,
203 default_value,
203 default_value,
204 description='')
204 description='')
205 conf = (GraderConfiguration.find_by_key(key) ||
205 conf = (GraderConfiguration.find_by_key(key) ||
206 GraderConfiguration.new(:key => key,
206 GraderConfiguration.new(:key => key,
207 :value_type => value_type,
207 :value_type => value_type,
208 :value => default_value))
208 :value => default_value))
209 conf.description = description
209 conf.description = description
210 conf.save
210 conf.save
211 end
211 end
212
212
213 def seed_config
213 def seed_config
214 CONFIGURATIONS.each do |conf|
214 CONFIGURATIONS.each do |conf|
215 if conf.has_key? :description
215 if conf.has_key? :description
216 desc = conf[:description]
216 desc = conf[:description]
217 else
217 else
218 desc = ''
218 desc = ''
219 end
219 end
220 create_configuration_key(conf[:key],
220 create_configuration_key(conf[:key],
221 conf[:value_type],
221 conf[:value_type],
222 conf[:default_value],
222 conf[:default_value],
223 desc)
223 desc)
224 end
224 end
225 end
225 end
226
226
227 def seed_roles
227 def seed_roles
228 return if Role.find_by_name('admin')
228 return if Role.find_by_name('admin')
229
229
230 role = Role.create(:name => 'admin')
230 role = Role.create(:name => 'admin')
231 user_admin_right = Right.create(:name => 'user_admin',
231 user_admin_right = Right.create(:name => 'user_admin',
232 :controller => 'user_admin',
232 :controller => 'user_admin',
233 :action => 'all')
233 :action => 'all')
234 problem_admin_right = Right.create(:name=> 'problem_admin',
234 problem_admin_right = Right.create(:name=> 'problem_admin',
235 :controller => 'problems',
235 :controller => 'problems',
236 :action => 'all')
236 :action => 'all')
237
237
238 graders_right = Right.create(:name => 'graders_admin',
238 graders_right = Right.create(:name => 'graders_admin',
239 :controller => 'graders',
239 :controller => 'graders',
240 :action => 'all')
240 :action => 'all')
241
241
242 role.rights << user_admin_right;
242 role.rights << user_admin_right;
243 role.rights << problem_admin_right;
243 role.rights << problem_admin_right;
244 role.rights << graders_right;
244 role.rights << graders_right;
245 role.save
245 role.save
246 end
246 end
247
247
248 def seed_root
248 def seed_root
249 return if User.find_by_login('root')
249 return if User.find_by_login('root')
250
250
251 root = User.new(:login => 'root',
251 root = User.new(:login => 'root',
252 :full_name => 'Administrator',
252 :full_name => 'Administrator',
253 :alias => 'root')
253 :alias => 'root')
254 root.password = 'ioionrails';
254 root.password = 'ioionrails';
255
255
256 class << root
256 class << root
257 public :encrypt_new_password
257 public :encrypt_new_password
258 def valid?(context=nil)
258 def valid?(context=nil)
259 true
259 true
260 end
260 end
261 end
261 end
262
262
263 root.encrypt_new_password
263 root.encrypt_new_password
264
264
265 root.roles << Role.find_by_name('admin')
265 root.roles << Role.find_by_name('admin')
266
266
267 root.activated = true
267 root.activated = true
268 root.save
268 root.save
269 end
269 end
270
270
271 def seed_users_and_roles
271 def seed_users_and_roles
272 seed_roles
272 seed_roles
273 seed_root
273 seed_root
274 end
274 end
275
275
276 def seed_more_languages
276 def seed_more_languages
277 - Language.delete_all
277 + #Language.delete_all
278 Language.find_or_create_by( name: 'c', pretty_name: 'C', ext: 'c', common_ext: 'c' )
278 Language.find_or_create_by( name: 'c', pretty_name: 'C', ext: 'c', common_ext: 'c' )
279 Language.find_or_create_by( name: 'cpp', pretty_name: 'C++', ext: 'cpp', common_ext: 'cpp,cc' )
279 Language.find_or_create_by( name: 'cpp', pretty_name: 'C++', ext: 'cpp', common_ext: 'cpp,cc' )
280 Language.find_or_create_by( name: 'pas', pretty_name: 'Pascal', ext: 'pas', common_ext: 'pas' )
280 Language.find_or_create_by( name: 'pas', pretty_name: 'Pascal', ext: 'pas', common_ext: 'pas' )
281 Language.find_or_create_by( name: 'ruby', pretty_name: 'Ruby', ext: 'rb', common_ext: 'rb' )
281 Language.find_or_create_by( name: 'ruby', pretty_name: 'Ruby', ext: 'rb', common_ext: 'rb' )
282 Language.find_or_create_by( name: 'python', pretty_name: 'Python', ext: 'py', common_ext: 'py' )
282 Language.find_or_create_by( name: 'python', pretty_name: 'Python', ext: 'py', common_ext: 'py' )
283 Language.find_or_create_by( name: 'java', pretty_name: 'Java', ext: 'java', common_ext: 'java' )
283 Language.find_or_create_by( name: 'java', pretty_name: 'Java', ext: 'java', common_ext: 'java' )
284 end
284 end
285
285
286 seed_config
286 seed_config
287 seed_users_and_roles
287 seed_users_and_roles
288 seed_more_languages
288 seed_more_languages
You need to be logged in to leave comments. Login now