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 @@ -7,7 +7,7 @@ before_filter :admin_authorization # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html) - verify :method => :post, :only => [ :destroy, + verify :method => :post, :only => [ :create, :create_from_list, :update, :manage_contest, @@ -16,11 +16,6 @@ :redirect_to => { :action => :list } def index - list - render :action => 'list' - end - - def list @user_count = User.count if params[:page] == 'all' @users = User.all @@ -34,7 +29,7 @@ end def active - sessions = ActiveRecord::SessionStore::Session.find(:all, :conditions => ["updated_at >= ?", 60.minutes.ago]) + sessions = ActiveRecord::SessionStore::Session.where("updated_at >= ?", 60.minutes.ago) @users = [] sessions.each do |session| if session.data[:user_id] @@ -52,16 +47,23 @@ end def create - @user = User.new(params[:user]) + @user = User.new(user_params) @user.activated = true if @user.save flash[:notice] = 'User was successfully created.' - redirect_to :action => 'list' + redirect_to :action => 'index' else render :action => 'new' end end + def clear_last_ip + @user = User.find(params[:id]) + @user.last_ip = nil + @user.save + redirect_to action: 'index', page: params[:page] + end + def create_from_list lines = params[:user_list] @@ -72,27 +74,39 @@ if items.length>=2 login = items[0] full_name = items[1] + remark ='' + user_alias = '' added_random_password = false - if items.length>=3 + if items.length >= 3 and items[2].chomp(" ").length > 0; password = items[2].chomp(" ") - user_alias = (items.length>=4) ? items[3] : login else password = random_password - user_alias = (items.length>=4) ? items[3] : login - added_random_password = true + add_random_password=true; + end + + if items.length>= 4 and items[3].chomp(" ").length > 0; + user_alias = items[3].chomp(" ") + else + user_alias = login + end + + if items.length>=5 + remark = items[4].strip; end user = User.find_by_login(login) if (user) user.full_name = full_name user.password = password + user.remark = remark else user = User.new({:login => login, :full_name => full_name, :password => password, :password_confirmation => password, - :alias => user_alias}) + :alias => user_alias, + :remark => remark}) end user.activated = true user.save @@ -104,10 +118,10 @@ end end end - flash[:notice] = 'User(s) ' + note.join(', ') + + flash[:success] = 'User(s) ' + note.join(', ') + ' were successfully created. ' + '( (+) - created with random passwords.)' - redirect_to :action => 'list' + redirect_to :action => 'index' end def edit @@ -116,7 +130,7 @@ def update @user = User.find(params[:id]) - if @user.update_attributes(params[:user]) + if @user.update_attributes(user_params) flash[:notice] = 'User was successfully updated.' redirect_to :action => 'show', :id => @user else @@ -126,23 +140,23 @@ def destroy User.find(params[:id]).destroy - redirect_to :action => 'list' + redirect_to :action => 'index' end def user_stat if params[:commit] == 'download csv' @problems = Problem.all else - @problems = Problem.find_available_problems + @problems = Problem.available_problems end - @users = User.find(:all, :include => [:contests, :contest_stat]) + @users = User.includes(:contests, :contest_stat).where(enabled: true) @scorearray = Array.new @users.each do |u| ustat = Array.new ustat[0] = u @problems.each do |p| sub = Submission.find_last_by_user_and_problem(u.id,p.id) - if (sub!=nil) and (sub.points!=nil) + if (sub!=nil) and (sub.points!=nil) and p and p.full_score ustat << [(sub.points.to_f*100/p.full_score).round, (sub.points>=p.full_score)] else ustat << [0,false] @@ -150,15 +164,21 @@ end @scorearray << ustat end + if params[:commit] == 'download csv' then + csv = gen_csv_from_scorearray(@scorearray,@problems) + send_data csv, filename: 'last_score.csv' + else + render template: 'user_admin/user_stat' + end end def user_stat_max if params[:commit] == 'download csv' @problems = Problem.all else - @problems = Problem.find_available_problems + @problems = Problem.available_problems end - @users = User.find(:all, :include => [:contests, :contest_stat]) + @users = User.includes(:contests).includes(:contest_stat).all @scorearray = Array.new #set up range from param since_id = params.fetch(:since_id, 0).to_i @@ -187,13 +207,13 @@ def import if params[:file]=='' flash[:notice] = 'Error importing no file' - redirect_to :action => 'list' and return + redirect_to :action => 'index' and return end import_from_file(params[:file]) end def random_all_passwords - users = User.find(:all) + users = User.all @prefix = params[:prefix] || '' @non_admin_users = User.find_non_admin_with_prefix(@prefix) @changed = false @@ -207,7 +227,7 @@ @changed = true end end - + # contest management def contests @@ -240,7 +260,7 @@ if user and contest user.contests << contest end - redirect_to :action => 'list' + redirect_to :action => 'index' end def remove_from_contest @@ -249,7 +269,7 @@ if user and contest user.contests.delete(contest) end - redirect_to :action => 'list' + redirect_to :action => 'index' end def contest_management @@ -316,7 +336,7 @@ # admin management def admin - @admins = User.find(:all).find_all {|user| user.admin? } + @admins = User.all.find_all {|user| user.admin? } end def grant_admin @@ -387,6 +407,39 @@ redirect_to :action => 'mass_mailing' end + #bulk manage + def bulk_manage + + begin + @users = User.where('login REGEXP ?',params[:regex]) if params[:regex] + @users.count if @users #i don't know why I have to call count, but if I won't exception is not raised + rescue Exception + flash[:error] = 'Regular Expression is malformed' + @users = nil + end + + if params[:commit] + @action = {} + @action[:set_enable] = params[:enabled] + @action[:enabled] = params[:enable] == "1" + @action[:gen_password] = params[:gen_password] + end + + if params[:commit] == "Perform" + if @action[:set_enable] + @users.update_all(enabled: @action[:enabled]) + end + if @action[:gen_password] + @users.each do |u| + password = random_password + u.password = password + u.password_confirmation = password + u.save + end + end + end + end + protected def random_password(length=5) @@ -513,7 +566,7 @@ row << sc[i].login row << sc[i].full_name row << sc[i].activated - row << (sc[i].try(:contest_stat).try(:started_at)!=nil ? 'yes' : 'no') + row << (sc[i].try(:contest_stat).try(:started_at).nil? ? 'no' : 'yes') row << sc[i].contests.collect {|c| c.name}.join(', ') else row << sc[i][0] @@ -527,4 +580,9 @@ end end end + + private + def user_params + params.require(:user).permit(:login,:password,:password_confirmation,:email, :alias, :full_name,:remark) + end end