# HG changeset patch # User jittat # Date 2008-05-07 11:44:42 # Node ID dfa0387efb1e209e121b4f58d4c7a7eb1d176a11 # Parent 00c0c912cae94e06cf526d59da2f57cf2103e1ac [web] added mode + access control, when sites started/finished git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@250 6386c4cd-e34a-4fa8-8920-d93eb39b512e diff --git a/app/controllers/main_controller.rb b/app/controllers/main_controller.rb --- a/app/controllers/main_controller.rb +++ b/app/controllers/main_controller.rb @@ -1,6 +1,9 @@ class MainController < ApplicationController + SYSTEM_MODE_CONF_KEY = 'system.mode' + before_filter :authenticate, :except => [:index, :login] + before_filter :check_viewability # # COMMENT OUT: filter in each action instead @@ -43,7 +46,8 @@ end @submission.submitted_at = Time.new.gmtime - if user.site!=nil and user.site.finished? + if Configuration[SYSTEM_MODE_CONF_KEY]=='contest' and + user.site!=nil and user.site.finished? @submission.errors.add_to_base "The contest is over." prepare_list_information render :action => 'list' and return @@ -125,5 +129,13 @@ :order => "created_at DESC") end + def check_viewability + user = User.find(session[:user_id]) + if (!Configuration.show_tasks_to?(user)) and + ((action_name=='submission') or (action_name=='submit')) + redirect_to :action => 'list' and return + end + end + end diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -1,7 +1,6 @@ class TasksController < ApplicationController - before_filter :authenticate - + before_filter :authenticate, :check_viewability def index redirect_to :action => 'list' @@ -12,4 +11,14 @@ @user = User.find(session[:user_id]) end + protected + + def check_viewability + user = User.find(session[:user_id]) + if user==nil or !Configuration.show_tasks_to?(user) + redirect_to :controller => 'main', :action => 'list' + return false + end + end + end diff --git a/app/controllers/test_controller.rb b/app/controllers/test_controller.rb --- a/app/controllers/test_controller.rb +++ b/app/controllers/test_controller.rb @@ -1,6 +1,8 @@ class TestController < ApplicationController - before_filter :authenticate + SYSTEM_MODE_CONF_KEY = 'system.mode' + + before_filter :authenticate, :check_viewability # # COMMENT OUT: filter in each action instead @@ -24,7 +26,8 @@ render :action => 'index' and return end - if @user.site!=nil and @user.site.finished? + if Configuration[SYSTEM_MODE_CONF_KEY]=='contest' and + @user.site!=nil and @user.site.finished? @submitted_test_request.errors.add_to_base('Contest is over.') prepare_index_information render :action => 'index' and return @@ -92,4 +95,14 @@ @test_requests = @user.test_requests end + def check_viewability + user = User.find(session[:user_id]) + if !Configuration.show_tasks_to?(user) + redirect_to :controller => 'main', :action => 'list' + end + if (!Configuration.show_submitbox_to?(user)) and (action_name=='submit') + redirect_to :controller => 'test', :action => 'index' + end + end + end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,6 +1,8 @@ # Methods added to this helper will be available to all templates in the application. module ApplicationHelper + SYSTEM_MODE_CONF_KEY = 'system.mode' + def user_header menu_items = '' user = User.find(session[:user_id]) @@ -21,9 +23,12 @@ # main page append_to menu_items, '[Main]', 'main', 'list' append_to menu_items, '[Messages]', 'messages', 'list' - append_to menu_items, '[Tasks]', 'tasks', 'list' - append_to menu_items, '[Submissions]', 'main', 'submission' - append_to menu_items, '[Test]', 'test', 'index' + + if (user!=nil) and (Configuration.show_tasks_to?(user)) + append_to menu_items, '[Tasks]', 'tasks', 'list' + append_to menu_items, '[Submissions]', 'main', 'submission' + append_to menu_items, '[Test]', 'test', 'index' + end append_to menu_items, '[Help]', 'main', 'help' #append_to menu_items, '[Settings]', 'users', 'index' append_to menu_items, '[Log out]', 'main', 'login' @@ -48,19 +53,44 @@ st + time.strftime("%X") end + def read_textfile(fname,max_size=2048) + begin + File.open(fname).read(max_size) + rescue + nil + end + end def user_title_bar(user) - if user.site!=nil and user.site.finished? - contest_over_string = < THE CONTEST IS OVER CONTEST_OVER end + + # + # if the contest is in the anaysis mode + if Configuration[SYSTEM_MODE_CONF_KEY]=='analysis' + header = < +ANALYSIS MODE + +ANALYSISMODE + end + + # + # build real title bar < -#{contest_over_string} +#{header}
#{user.full_name}
@@ -73,12 +103,4 @@ TITLEBAR end - def read_textfile(fname,max_size=2048) - begin - File.open(fname).read(max_size) - rescue - nil - end - end - end diff --git a/app/models/configuration.rb b/app/models/configuration.rb --- a/app/models/configuration.rb +++ b/app/models/configuration.rb @@ -1,5 +1,10 @@ +# +# This class also contains various login of the system. +# class Configuration < ActiveRecord::Base + SYSTEM_MODE_CONF_KEY = 'system.mode' + @@configurations = nil def self.get(key) @@ -21,6 +26,28 @@ @@configurations = nil end + # + # View decision + # + def self.show_submitbox_to?(user) + mode = get(SYSTEM_MODE_CONF_KEY) + return false if mode=='analysis' + if (mode=='contest') + return false if (user.site!=nil) and + ((user.site.started==false) or (user.site.finished?)) + end + return true + end + + def self.show_tasks_to?(user) + mode = get(SYSTEM_MODE_CONF_KEY) + if (mode=='contest') + return false if (user.site!=nil) and (user.site.started==false) + end + return true + end + + protected def self.read_config @@configurations = {} diff --git a/app/views/main/list.html.haml b/app/views/main/list.html.haml --- a/app/views/main/list.html.haml +++ b/app/views/main/list.html.haml @@ -6,20 +6,22 @@ Announcements = render :partial => 'announcement', :collection => @announcements -.submitbox - = error_messages_for 'submission' - = render :partial => 'submission_box' +- if Configuration.show_submitbox_to?(@user) + .submitbox + = error_messages_for 'submission' + = render :partial => 'submission_box' %hr/ -%table.info - %tr.info-head - %th - %th Tasks - %th # of sub(s) - %th Results - = render :partial => 'problem', :collection => @problems +- if Configuration.show_tasks_to?(@user) + %table.info + %tr.info-head + %th + %th Tasks + %th # of sub(s) + %th Results + = render :partial => 'problem', :collection => @problems %hr/ diff --git a/app/views/test/index.html.erb b/app/views/test/index.html.erb --- a/app/views/test/index.html.erb +++ b/app/views/test/index.html.erb @@ -30,61 +30,63 @@ } -
-<%= error_messages_for 'submitted_test_request' %> -<% form_for :test_request, nil, - :url => { :action => 'submit'}, - :html => { :multipart => true } do |f| %> - - - - - - - - - - - - - - - - - - - - - - -
Task: - <%= select(:test_request, - :problem_id, - @problems.collect {|p| [p.name, p.id]}, {}, - { :onclick => "updateSubmissionList();" }) %> -
Submission: - <%= select(:test_request, - :submission_number, - ((1..@submissions[0].number).collect {|n| [n,n]}).reverse) %> -
Input data: - <%= f.file_field :input_file %> - - (combined size should not exceed 2MB) -
- Additional file*: - - <%= f.file_field :additional_file %> - - - * This option works only for task max. - You can use this to submit questions.txt.
- The file shall be copied to the execution directory before your program runs. -
-
- <%= submit_tag 'submit' %> -
+<% if Configuration.show_submitbox_to?(@user) %> +
+ <%= error_messages_for 'submitted_test_request' %> + <% form_for :test_request, nil, + :url => { :action => 'submit'}, + :html => { :multipart => true } do |f| %> + + + + + + + + + + + + + + + + + + + + + + +
Task: + <%= select(:test_request, + :problem_id, + @problems.collect {|p| [p.name, p.id]}, {}, + { :onclick => "updateSubmissionList();" }) %> +
Submission: + <%= select(:test_request, + :submission_number, + ((1..@submissions[0].number).collect {|n| [n,n]}).reverse) %> +
Input data: + <%= f.file_field :input_file %> + + (combined size should not exceed 2MB) +
+ Additional file*: + + <%= f.file_field :additional_file %> + + + * This option works only for task max. + You can use this to submit questions.txt.
+ The file shall be copied to the execution directory before your program runs. +
+
+ <%= submit_tag 'submit' %> +
+ <% end %> +
<% end %> -

Previous requests

diff --git a/db/migrate/036_add_mode_to_configurations.rb b/db/migrate/036_add_mode_to_configurations.rb new file mode 100644 --- /dev/null +++ b/db/migrate/036_add_mode_to_configurations.rb @@ -0,0 +1,17 @@ +class AddModeToConfigurations < ActiveRecord::Migration + def self.up + + # Configuration['system.mode']: + # * 'standard' mode + # * 'contest' mode (check site start time/stop time) + # * 'analysis' mode (show results, no new submissions) + + Configuration.create(:key => 'system.mode', + :value_type => 'string', + :value => 'standard') + end + + def self.down + Configuration.find_by_key('system.mode').destroy + end +end diff --git a/db/schema.rb b/db/schema.rb --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 35) do +ActiveRecord::Schema.define(:version => 36) do create_table "announcements", :force => true do |t| t.string "author"