Show More
Commit Description:
merge
Commit Description:
merge
References:
File last commit:
Show/Diff file:
Action:
app/controllers/application_controller.rb | 239 lines | 7.5 KiB | text/x-ruby | RubyLexer |
add ip whitelisting
r755 require 'ipaddr'
jittat
MERGED 308:HEAD from http://theory.cpe.ku.ac.th/grader/web/branches/ytopc08-2/, removed some registration info...
r162 class ApplicationController < ActionController::Base
Jittat Fakcharoenphol
boots into rails 3
r318 protect_from_forgery
jittat
MERGED 308:HEAD from http://theory.cpe.ku.ac.th/grader/web/branches/ytopc08-2/, removed some registration info...
r162
change depricated before_filter to before_action
r745 before_action :current_user
use jquery by default
r554
jittat
MERGED 308:HEAD from http://theory.cpe.ku.ac.th/grader/web/branches/ytopc08-2/, removed some registration info...
r162 SINGLE_USER_MODE_CONF_KEY = 'system.single_user_mode'
add option to disable login from multiple ip
r525 MULTIPLE_IP_LOGIN_CONF_KEY = 'right.multiple_ip_login'
fix whitelisting bugs...
r784 WHITELIST_IGNORE_CONF_KEY = 'right.whitelist_ignore'
add ip whitelisting
r755 WHITELIST_IP_CONF_KEY = 'right.whitelist_ip'
jittat
MERGED 308:HEAD from http://theory.cpe.ku.ac.th/grader/web/branches/ytopc08-2/, removed some registration info...
r162
- fix authorization for viewing submission, only admin can view all problems all the time, normal user depends on right.view_submission and problem.available?...
r593 #report and redirect for unauthorized activities
more test and clean up authorization
r756 def unauthorized_redirect(notice = 'You are not authorized to view the page you requested')
flash[:notice] = notice
redirect_to login_main_path
- fix authorization for viewing submission, only admin can view all problems all the time, normal user depends on right.view_submission and problem.available?...
r593 end
use jquery by default
r554 # Returns the current logged-in user (if any).
def current_user
wip: bootstrap toggle switch...
r556 return nil unless session[:user_id]
use jquery by default
r554 @current_user ||= User.find(session[:user_id])
end
jittat
MERGED 308:HEAD from http://theory.cpe.ku.ac.th/grader/web/branches/ytopc08-2/, removed some registration info...
r162 def admin_authorization
more test and clean up authorization
r756 return false unless check_valid_login
change find(:xxx) to correct syntax for rails 4
r619 user = User.includes(:roles).find(session[:user_id])
prepare for better hall of fame
r425 unless user.admin?
- fix authorization for viewing submission, only admin can view all problems all the time, normal user depends on right.view_submission and problem.available?...
r593 unauthorized_redirect
prepare for better hall of fame
r425 return false
end
return true
jittat
MERGED 308:HEAD from http://theory.cpe.ku.ac.th/grader/web/branches/ytopc08-2/, removed some registration info...
r162 end
def authorization_by_roles(allowed_roles)
more test and clean up authorization
r756 return false unless check_valid_login
jittat
MERGED 308:HEAD from http://theory.cpe.ku.ac.th/grader/web/branches/ytopc08-2/, removed some registration info...
r162 user = User.find(session[:user_id])
unless user.roles.detect { |role| allowed_roles.member?(role.name) }
- fix authorization for viewing submission, only admin can view all problems all the time, normal user depends on right.view_submission and problem.available?...
r593 unauthorized_redirect
jittat
MERGED 308:HEAD from http://theory.cpe.ku.ac.th/grader/web/branches/ytopc08-2/, removed some registration info...
r162 return false
end
end
add show testcase feature
r625 def testcase_authorization
#admin always has privileged
if @current_user.admin?
return true
end
- add view testcase toggle for each problem...
r632 unauthorized_redirect unless GraderConfiguration["right.view_testcase"]
add show testcase feature
r625 end
add ip whitelisting
r755
jittat
MERGED 308:HEAD from http://theory.cpe.ku.ac.th/grader/web/branches/ytopc08-2/, removed some registration info...
r162 protected
add ip whitelisting
r755 #redirect to root (and also force logout)
#if the user is not logged_in or the system is in "ADMIN ONLY" mode
more test and clean up authorization
r756 def check_valid_login
#check if logged in
jittat
MERGED 308:HEAD from http://theory.cpe.ku.ac.th/grader/web/branches/ytopc08-2/, removed some registration info...
r162 unless session[:user_id]
add options to allow hall of fame viewing by any user...
r424 if GraderConfiguration[SINGLE_USER_MODE_CONF_KEY]
more test and clean up authorization
r756 unauthorized_redirect('You need to login but you cannot log in at this time')
else
unauthorized_redirect('You need to login')
add options to allow hall of fame viewing by any user...
r424 end
jittat
MERGED 308:HEAD from http://theory.cpe.ku.ac.th/grader/web/branches/ytopc08-2/, removed some registration info...
r162 return false
end
# check if run in single user mode
Jittat Fakcharoenphol
renamed model Configuration to GraderConfiguration, renamed rhtml views to erb, fixed other small errors
r320 if GraderConfiguration[SINGLE_USER_MODE_CONF_KEY]
fix user.admin? bug
r757 if @current_user==nil || (!@current_user.admin?)
more test and clean up authorization
r756 unauthorized_redirect('You cannot log in at this time')
jittat
MERGED 308:HEAD from http://theory.cpe.ku.ac.th/grader/web/branches/ytopc08-2/, removed some registration info...
r162 return false
end
end
add enabled option for user
r670 # check if the user is enabled
add ip whitelisting
r755 unless @current_user.enabled? || @current_user.admin?
more test and clean up authorization
r756 unauthorized_redirect 'Your account is disabled'
add enabled option for user
r670 return false
end
add ip whitelisting
r755 # check if user ip is allowed
fix whitelisting bugs...
r784 unless @current_user.admin? || GraderConfiguration[WHITELIST_IGNORE_CONF_KEY]
add ip whitelisting
r755 unless is_request_ip_allowed?
fix whitelisting bugs...
r784 unauthorized_redirect 'Your IP is not allowed to login at this time.'
more test and clean up authorization
r756 return false
add ip whitelisting
r755 end
end
Jittat Fakcharoenphol
renamed model Configuration to GraderConfiguration, renamed rhtml views to erb, fixed other small errors
r320 if GraderConfiguration.multicontests?
add enabled option for user
r670 return true if @current_user.admin?
Jittat Fakcharoenphol
a cleaner, testable way to log out user after contest changed
r295 begin
add enabled option for user
r670 if @current_user.contest_stat(true).forced_logout
Jittat Fakcharoenphol
a cleaner, testable way to log out user after contest changed
r295 flash[:notice] = 'You have been automatically logged out.'
redirect_to :controller => 'main', :action => 'index'
end
rescue
end
end
jittat
MERGED 308:HEAD from http://theory.cpe.ku.ac.th/grader/web/branches/ytopc08-2/, removed some registration info...
r162 return true
end
add ip whitelisting
r755 #redirect to root (and also force logout)
#if the user use different ip from the previous connection
# only applicable when MULTIPLE_IP_LOGIN options is false only
add option to disable login from multiple ip
r525 def authenticate_by_ip_address
#this assume that we have already authenticate normally
unless GraderConfiguration[MULTIPLE_IP_LOGIN_CONF_KEY]
user = User.find(session[:user_id])
fix user.admin? bug
r757 if (!user.admin? && user.last_ip && user.last_ip != request.remote_ip)
add option to disable login from multiple ip
r525 flash[:notice] = "You cannot use the system from #{request.remote_ip}. Your last ip is #{user.last_ip}"
redirect_to :controller => 'main', :action => 'login'
return false
end
unless user.last_ip
user.last_ip = request.remote_ip
user.save
end
end
return true
end
jittat
MERGED 308:HEAD from http://theory.cpe.ku.ac.th/grader/web/branches/ytopc08-2/, removed some registration info...
r162 def authorization
more test and clean up authorization
r756 return false unless check_valid_login
jittat
MERGED 308:HEAD from http://theory.cpe.ku.ac.th/grader/web/branches/ytopc08-2/, removed some registration info...
r162 user = User.find(session[:user_id])
unless user.roles.detect { |role|
- add view testcase toggle for each problem...
r632 role.rights.detect{ |right|
right.controller == self.class.controller_name and
add ip whitelisting
r755 (right.action == 'all' || right.action == action_name)
- add view testcase toggle for each problem...
r632 }
jittat
MERGED 308:HEAD from http://theory.cpe.ku.ac.th/grader/web/branches/ytopc08-2/, removed some registration info...
r162 }
flash[:notice] = 'You are not authorized to view the page you requested'
#request.env['HTTP_REFERER'] ? (redirect_to :back) : (redirect_to :controller => 'login')
redirect_to :controller => 'main', :action => 'login'
return false
end
end
def verify_time_limit
return true if session[:user_id]==nil
user = User.find(session[:user_id], :include => :site)
add ip whitelisting
r755 return true if user==nil || user.site == nil
Jittat Fakcharoenphol
added individual contest mode
r217 if user.contest_finished?
flash[:notice] = 'Error: the contest you are participating is over.'
jittat
MERGED 308:HEAD from http://theory.cpe.ku.ac.th/grader/web/branches/ytopc08-2/, removed some registration info...
r162 redirect_to :back
return false
end
return true
end
add ip whitelisting
r755 def is_request_ip_allowed?
fix whitelisting bugs...
r784 unless GraderConfiguration[WHITELIST_IGNORE_CONF_KEY]
add ip whitelisting
r755 user_ip = IPAddr.new(request.remote_ip)
problem group enabled
r796 allowed = GraderConfiguration[WHITELIST_IP_CONF_KEY] || ''
fix whitelisting bugs...
r784
problem group enabled
r796 allowed.delete(' ').split(',').each do |ips|
add ip whitelisting
r755 allow_ips = IPAddr.new(ips)
fix whitelisting bugs...
r784 if allow_ips.include?(user_ip)
return true
add ip whitelisting
r755 end
end
fix whitelisting bugs...
r784 return false
add ip whitelisting
r755 end
return true
end
submission stat
r788 #function for datatable ajax query
#return record,total_count,filter_count
def process_query_record(record,
submission report
r790 total_count: nil,
select: '',
global_search: [],
no_search: false,
force_order: '',
date_filter: '', date_param_since: 'date_since',date_param_until: 'date_until',
hard_limit: nil)
submission stat
r788 arel_table = record.model.arel_table
if !no_search && params['search']
global_value = record.model.sanitize_sql(params['search']['value'].strip.downcase)
if !global_value.blank?
global_value.split.each do |value|
global_where = global_search.map{|f| "LOWER(#{f}) like '%#{value}%'"}.join(' OR ')
record = record.where(global_where)
end
end
params['columns'].each do |i, col|
if !col['search']['value'].blank?
record = record.where(arel_table[col['name']].lower.matches("%#{col['search']['value'].strip.downcase}%"))
end
end
end
if !date_filter.blank?
submission report
r790 param_since = params[date_param_since]
param_until = params[date_param_until]
date_since = Time.zone.parse( param_since ) || Time.new(1,1,1) rescue Time.new(1,1,1)
date_until = Time.zone.parse( param_until ) || Time.zone.now() rescue Time.zone.now()
date_range = date_since..(date_until.end_of_day)
submission stat
r788 record = record.where(date_filter.to_sym => date_range)
end
if force_order.blank?
if params['order']
params['order'].each do |i, o|
colName = params['columns'][o['column']]['name']
colName = "#{record.model.table_name}.#{colName}" if colName.upcase == 'ID'
record = record.order("#{colName} #{o['dir'].casecmp('desc') != 0 ? 'ASC' : 'DESC'}") unless colName.blank?
end
end
else
record = record.order(force_order)
end
filterCount = record.count(record.model.primary_key)
# if .group() is used, filterCount might be like {id_1: count_1, id_2: count_2, ...}
# so we should count the result again..
if filterCount.is_a? Hash
filterCount = filterCount.count
end
submission report
r790
record = record.offset(params['start'] || 0)
record = record.limit(hard_limit)
if (params['length'])
limit = params['length'].to_i
limit == hard_limit if (hard_limit && hard_limit < limit)
record = record.limit(limit)
end
submission stat
r788 if (!select.blank?)
record = record.select(select)
end
return record, total_count || record.model.count, filterCount
end
jittat
MERGED 308:HEAD from http://theory.cpe.ku.ac.th/grader/web/branches/ytopc08-2/, removed some registration info...
r162 end