diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -7,7 +7,7 @@ SINGLE_USER_MODE_CONF_KEY = 'system.single_user_mode' MULTIPLE_IP_LOGIN_CONF_KEY = 'right.multiple_ip_login' - ALLOW_WHITELIST_IP_ONLY_CONF_KEY = 'right.allow_whitelist_ip_only' + WHITELIST_IGNORE_CONF_KEY = 'right.whitelist_ignore' WHITELIST_IP_CONF_KEY = 'right.whitelist_ip' #report and redirect for unauthorized activities @@ -81,9 +81,9 @@ end # check if user ip is allowed - unless @current_user.admin? || !GraderConfiguration[ALLOW_WHITELIST_IP_ONLY_CONF_KEY] + unless @current_user.admin? || GraderConfiguration[WHITELIST_IGNORE_CONF_KEY] unless is_request_ip_allowed? - unauthorized_redirect 'Your IP is not allowed' + unauthorized_redirect 'Your IP is not allowed to login at this time.' return false end end @@ -108,10 +108,8 @@ #this assume that we have already authenticate normally unless GraderConfiguration[MULTIPLE_IP_LOGIN_CONF_KEY] user = User.find(session[:user_id]) - puts "User admin #{user.admin?}" if (!user.admin? && user.last_ip && user.last_ip != request.remote_ip) flash[:notice] = "You cannot use the system from #{request.remote_ip}. Your last ip is #{user.last_ip}" - puts "hahaha" redirect_to :controller => 'main', :action => 'login' return false end @@ -152,14 +150,16 @@ end def is_request_ip_allowed? - if GraderConfiguration[ALLOW_WHITELIST_IP_ONLY_CONF_KEY] + unless GraderConfiguration[WHITELIST_IGNORE_CONF_KEY] user_ip = IPAddr.new(request.remote_ip) - GraderConfiguration[WHITELIST_IP_LIST_CONF_KEY].delete(' ').split(',').each do |ips| + + GraderConfiguration[WHITELIST_IP_CONF_KEY].delete(' ').split(',').each do |ips| allow_ips = IPAddr.new(ips) - unless allow_ips.includes(user_ip) - return false + if allow_ips.include?(user_ip) + return true end end + return false end return true end 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 @@ -21,6 +21,7 @@ saved_notice = flash[:notice] reset_session flash.now[:notice] = saved_notice + @remote_ip = request.remote_ip # EXPERIMENT: # Hide login if in single user mode and the url does not diff --git a/app/controllers/report_controller.rb b/app/controllers/report_controller.rb --- a/app/controllers/report_controller.rb +++ b/app/controllers/report_controller.rb @@ -465,6 +465,7 @@ protected def calculate_max_score(problems, users,since_id,until_id, get_last_score = false) + #scorearray[i] = user #i's user stat where i is the index (not id) scorearray = Array.new users.each do |u| ustat = Array.new diff --git a/app/controllers/submissions_controller.rb b/app/controllers/submissions_controller.rb --- a/app/controllers/submissions_controller.rb +++ b/app/controllers/submissions_controller.rb @@ -75,9 +75,6 @@ def get_latest_submission_status @problem = Problem.find(params[:pid]) @submission = Submission.find_last_by_user_and_problem(params[:uid],params[:pid]) - puts User.find(params[:uid]).login - puts Problem.find(params[:pid]).name - puts 'nil' unless @submission respond_to do |format| format.js end diff --git a/app/controllers/user_admin_controller.rb b/app/controllers/user_admin_controller.rb --- a/app/controllers/user_admin_controller.rb +++ b/app/controllers/user_admin_controller.rb @@ -61,6 +61,7 @@ note = [] error_note = [] + error_msg = nil ok_user = [] lines.split("\n").each do |line| @@ -76,7 +77,7 @@ password = items[2].chomp(" ") else password = random_password - add_random_password=true; + added_random_password=true; end if items.length>= 4 and items[3].chomp(" ").length > 0; @@ -96,11 +97,11 @@ user.remark = remark else user = User.new({:login => login, - :full_name => full_name, - :password => password, - :password_confirmation => password, - :alias => user_alias, - :remark => remark}) + :full_name => full_name, + :password => password, + :password_confirmation => password, + :alias => user_alias, + :remark => remark}) end user.activated = true @@ -112,7 +113,8 @@ end ok_user << user else - error_note << "#{login}" + error_note << "'#{login}'" + error_msg = user.errors.full_messages.to_sentence unless error_msg end end @@ -127,11 +129,13 @@ end # show flash - flash[:success] = 'User(s) ' + note.join(', ') + - ' were successfully created. ' + - '( (+) - created with random passwords.)' + if note.size > 0 + flash[:success] = 'User(s) ' + note.join(', ') + + ' were successfully created. ' + + '( (+) - created with random passwords.)' + end if error_note.size > 0 - flash[:error] = "Following user(s) failed to be created: " + error_note.join(', ') + flash[:error] = "Following user(s) failed to be created: " + error_note.join(', ') + ". The error of the first failed one are: " + error_msg; end redirect_to :action => 'index' end diff --git a/app/views/groups/show.html.haml b/app/views/groups/show.html.haml --- a/app/views/groups/show.html.haml +++ b/app/views/groups/show.html.haml @@ -21,11 +21,13 @@ %li If you want to add several users to a group, it may be easier to just re-import those users in = link_to 'New list of users', new_list_user_admin_index_path - page + page. You can also use + = link_to 'Bulk Manage User', bulk_manage_user_admin_index_path + page. =form_tag add_user_group_path(@group), class: 'form-inline' do .form-group =label_tag :user_id, "User" - =select_tag :user_id, options_from_collection_for_select(User.all,'id','full_name'), class: 'select2' + =select_tag :user_id, options_from_collection_for_select(User.all,'id','full_name'), class: 'select2', style: 'width: 10em'; =submit_tag "Add",class: 'btn btn-primary' @@ -52,12 +54,12 @@ %ul %li If you want to add several problem to a group, it may be easier to bulk manage them in the - = link_to 'Bulk Manage', manage_problems_path + = link_to 'Bulk Manage Problems', manage_problems_path page =form_tag add_problem_group_path(@group), class: 'form-inline' do .form-group =label_tag :problem_id, "Problem" - =select_tag :problem_id, options_from_collection_for_select(Problem.all,'id','full_name'), class: 'select2' + =select_tag :problem_id, options_from_collection_for_select(Problem.all,'id','full_name'), class: 'select2', style: 'width: 10em'; =submit_tag "Add",class: 'btn btn-primary' diff --git a/app/views/main/login.html.haml b/app/views/main/login.html.haml --- a/app/views/main/login.html.haml +++ b/app/views/main/login.html.haml @@ -9,3 +9,4 @@ = render :partial => 'announcement', :collection => @announcements .col-md-4{style: "padding-left: 20px;"} = render :partial => 'login_box' + = "current ip is #{@remote_ip}" diff --git a/app/views/report/_score_table.html.haml b/app/views/report/_score_table.html.haml --- a/app/views/report/_score_table.html.haml +++ b/app/views/report/_score_table.html.haml @@ -12,9 +12,9 @@ %th.text-right Total %th.text-right Passed %tbody - - sum = Array.new(@scorearray[0].count,0) - - nonzero = Array.new(@scorearray[0].count,0) - - full = Array.new(@scorearray[0].count,0) + - sum = Array.new(@problems.count+1,0) + - nonzero = Array.new(@problems.count+1,0) + - full = Array.new(@problems.count+1,0) - @scorearray.each do |sc| %tr - total,num_passed = 0,0 diff --git a/app/views/report/current_score.html.haml b/app/views/report/current_score.html.haml --- a/app/views/report/current_score.html.haml +++ b/app/views/report/current_score.html.haml @@ -2,7 +2,7 @@ %h1 Current Score = form_tag current_score_report_path, method: 'get' do Show only users from this group - = select_tag "group_id", options_from_collection_for_select( Group.all, 'id','name',params[:group_name]), id: 'group_name',class: 'select2' + = select_tag "group_id", options_from_collection_for_select( Group.all, 'id','name',params[:group_id]), id: 'group_name',class: 'select2', style: 'width: 20em'; = submit_tag 'Apply',class: 'btn btn-default' %br diff --git a/config/routes.rb b/config/routes.rb --- a/config/routes.rb +++ b/config/routes.rb @@ -147,7 +147,6 @@ get 'current_score(/:group_id)', action: 'current_score', as: 'current_score' get 'max_score' post 'show_max_score' - get 'problem_hof(/:id)', action: 'problem_hof', as: 'problem_hof' get 'stuck' get 'cheat_report' post 'cheat_report' diff --git a/db/schema.rb b/db/schema.rb --- a/db/schema.rb +++ b/db/schema.rb @@ -12,7 +12,7 @@ ActiveRecord::Schema.define(version: 2018_06_12_102327) do - create_table "announcements", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + create_table "announcements", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.string "author" t.text "body" t.boolean "published" @@ -24,7 +24,7 @@ t.string "notes" end - create_table "contests", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + create_table "contests", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.string "title" t.boolean "enabled" t.datetime "created_at" @@ -32,30 +32,30 @@ t.string "name" end - create_table "contests_problems", id: false, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + create_table "contests_problems", id: false, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.integer "contest_id" t.integer "problem_id" end - create_table "contests_users", id: false, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + create_table "contests_users", id: false, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.integer "contest_id" t.integer "user_id" end - create_table "countries", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + create_table "countries", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.string "name" t.datetime "created_at" t.datetime "updated_at" end - create_table "descriptions", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + create_table "descriptions", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.text "body" t.boolean "markdowned" t.datetime "created_at" t.datetime "updated_at" end - create_table "grader_configurations", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + create_table "grader_configurations", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.string "key" t.string "value_type" t.string "value" @@ -64,7 +64,7 @@ t.text "description" end - create_table "grader_processes", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + create_table "grader_processes", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.string "host" t.integer "pid" t.string "mode" @@ -74,21 +74,21 @@ t.integer "task_id" t.string "task_type" t.boolean "terminated" - t.index ["host", "pid"], name: "index_grader_processes_on_ip_and_pid" + t.index ["host", "pid"], name: "index_grader_processes_on_host_and_pid" end - create_table "groups", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci", force: :cascade do |t| + create_table "groups", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.string "name" t.string "description" end - create_table "groups_problems", id: false, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci", force: :cascade do |t| + create_table "groups_problems", id: false, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.integer "problem_id", null: false t.integer "group_id", null: false t.index ["group_id", "problem_id"], name: "index_groups_problems_on_group_id_and_problem_id" end - create_table "groups_users", id: false, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci", force: :cascade do |t| + create_table "groups_users", id: false, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.integer "group_id", null: false t.integer "user_id", null: false t.index ["user_id", "group_id"], name: "index_groups_users_on_user_id_and_group_id" @@ -97,13 +97,13 @@ create_table "heart_beats", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.integer "user_id" t.string "ip_address" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at" + t.datetime "updated_at" t.string "status" t.index ["updated_at"], name: "index_heart_beats_on_updated_at" end - create_table "languages", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + create_table "languages", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.string "name", limit: 10 t.string "pretty_name" t.string "ext", limit: 10 @@ -113,11 +113,11 @@ create_table "logins", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.integer "user_id" t.string "ip_address" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at" + t.datetime "updated_at" end - create_table "messages", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + create_table "messages", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.integer "sender_id" t.integer "receiver_id" t.integer "replying_message_id" @@ -127,7 +127,7 @@ t.datetime "updated_at" end - create_table "problems", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + create_table "problems", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.string "name", limit: 30 t.string "full_name" t.integer "full_score" @@ -141,7 +141,7 @@ t.boolean "view_testcase" end - create_table "problems_tags", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci", force: :cascade do |t| + create_table "problems_tags", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.integer "problem_id" t.integer "tag_id" t.index ["problem_id", "tag_id"], name: "index_problems_tags_on_problem_id_and_tag_id", unique: true @@ -149,29 +149,29 @@ t.index ["tag_id"], name: "index_problems_tags_on_tag_id" end - create_table "rights", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + create_table "rights", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.string "name" t.string "controller" t.string "action" end - create_table "rights_roles", id: false, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + create_table "rights_roles", id: false, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.integer "right_id" t.integer "role_id" t.index ["role_id"], name: "index_rights_roles_on_role_id" end - create_table "roles", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + create_table "roles", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.string "name" end - create_table "roles_users", id: false, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + create_table "roles_users", id: false, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.integer "role_id" t.integer "user_id" t.index ["user_id"], name: "index_roles_users_on_user_id" end - create_table "sessions", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + create_table "sessions", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.string "session_id" t.text "data" t.datetime "updated_at" @@ -179,7 +179,7 @@ t.index ["updated_at"], name: "index_sessions_on_updated_at" end - create_table "sites", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + create_table "sites", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.string "name" t.boolean "started" t.datetime "start_time" @@ -192,11 +192,11 @@ create_table "submission_view_logs", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.integer "user_id" t.integer "submission_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at" + t.datetime "updated_at" end - create_table "submissions", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + create_table "submissions", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.integer "user_id" t.integer "problem_id" t.integer "language_id" @@ -218,7 +218,7 @@ t.index ["user_id", "problem_id"], name: "index_submissions_on_user_id_and_problem_id" end - create_table "tags", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci", force: :cascade do |t| + create_table "tags", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.string "name", null: false t.text "description" t.boolean "public" @@ -226,7 +226,7 @@ t.datetime "updated_at", null: false end - create_table "tasks", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + create_table "tasks", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.integer "submission_id" t.datetime "created_at" t.integer "status" @@ -234,7 +234,7 @@ t.index ["submission_id"], name: "index_tasks_on_submission_id" end - create_table "test_pairs", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + create_table "test_pairs", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.integer "problem_id" t.text "input", limit: 16777215 t.text "solution", limit: 16777215 @@ -242,7 +242,7 @@ t.datetime "updated_at" end - create_table "test_requests", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + create_table "test_requests", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.integer "user_id" t.integer "problem_id" t.integer "submission_id" @@ -263,7 +263,7 @@ t.index ["user_id", "problem_id"], name: "index_test_requests_on_user_id_and_problem_id" end - create_table "testcases", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci", force: :cascade do |t| + create_table "testcases", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.integer "problem_id" t.integer "num" t.integer "group" @@ -275,14 +275,15 @@ t.index ["problem_id"], name: "index_testcases_on_problem_id" end - create_table "user_contest_stats", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + create_table "user_contest_stats", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.integer "user_id" t.datetime "started_at" t.datetime "created_at" t.datetime "updated_at" + t.boolean "forced_logout" end - create_table "users", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + create_table "users", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.string "login", limit: 50 t.string "full_name" t.string "hashed_password" diff --git a/db/seeds.rb b/db/seeds.rb --- a/db/seeds.rb +++ b/db/seeds.rb @@ -100,7 +100,7 @@ :key => 'right.view_testcase', :value_type => 'boolean', :default_value => 'false', - :description => 'When true, any user can view/download test data' + :description => 'If true, any user can view/download test data' }, { @@ -182,17 +182,17 @@ { - :key => 'right.whitelist_ip_only', + :key => 'right.whitelist_ignore', :value_type => 'boolean', - :default_value => 'false', - :description => "If true, non-admin user will be able to use the system only when their ip is in the 'whitelist_ip'." + :default_value => 'true', + :description => "If true, no IP check against whitelist_ip is perform. However, when false, non-admin user must have their ip in 'whitelist_ip' to be able to login." }, { :key => 'right.whitelist_ip', :value_type => 'string', :default_value => '0.0.0.0/0', - :description => "list of whitelist ip, given in comma separated CIDR notation. For example '161.200.92.0/23, 161.200.80.1/32'" + :description => "list of whitelist ip, given in comma separated CIDR notation. For example '192.168.90.0/23, 192.168.1.23/32'" }, ] @@ -274,7 +274,7 @@ end def seed_more_languages - Language.delete_all + #Language.delete_all Language.find_or_create_by( name: 'c', pretty_name: 'C', ext: 'c', common_ext: 'c' ) Language.find_or_create_by( name: 'cpp', pretty_name: 'C++', ext: 'cpp', common_ext: 'cpp,cc' ) Language.find_or_create_by( name: 'pas', pretty_name: 'Pascal', ext: 'pas', common_ext: 'pas' ) diff --git a/test/system/users_test.rb b/test/system/users_test.rb --- a/test/system/users_test.rb +++ b/test/system/users_test.rb @@ -50,7 +50,7 @@ click_on 'New list of users', match: :first find(:css, 'textarea').fill_in with:"abc1,Boaty McBoatface,abcdef,alias1,remark1,\nabc2,Boaty2 McSecond,acbdef123,aias2,remark2" - click_on 'create users' + click_on 'Create following users' assert_text('remark1') assert_text('remark2')