Description:
modify user list creation into user list update
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r469:b28cd479ede6 - - 1 file changed: 11 inserted, 5 deleted

@@ -1,283 +1,289
1 require 'csv'
1 require 'csv'
2
2
3 class UserAdminController < ApplicationController
3 class UserAdminController < ApplicationController
4
4
5
5
6 include MailHelperMethods
6 include MailHelperMethods
7
7
8 before_filter :admin_authorization
8 before_filter :admin_authorization
9
9
10 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
10 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
11 verify :method => :post, :only => [ :destroy,
11 verify :method => :post, :only => [ :destroy,
12 :create, :create_from_list,
12 :create, :create_from_list,
13 :update,
13 :update,
14 :manage_contest,
14 :manage_contest,
15 :bulk_mail
15 :bulk_mail
16 ],
16 ],
17 :redirect_to => { :action => :list }
17 :redirect_to => { :action => :list }
18
18
19 def index
19 def index
20 list
20 list
21 render :action => 'list'
21 render :action => 'list'
22 end
22 end
23
23
24 def list
24 def list
25 @user_count = User.count
25 @user_count = User.count
26 if params[:page] == 'all'
26 if params[:page] == 'all'
27 @users = User.all
27 @users = User.all
28 @paginated = false
28 @paginated = false
29 else
29 else
30 @users = User.paginate :page => params[:page]
30 @users = User.paginate :page => params[:page]
31 @paginated = true
31 @paginated = true
32 end
32 end
33 @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at']
33 @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at']
34 @contests = Contest.enabled
34 @contests = Contest.enabled
35 end
35 end
36
36
37 def active
37 def active
38 sessions = ActiveRecord::SessionStore::Session.find(:all, :conditions => ["updated_at >= ?", 60.minutes.ago])
38 sessions = ActiveRecord::SessionStore::Session.find(:all, :conditions => ["updated_at >= ?", 60.minutes.ago])
39 @users = []
39 @users = []
40 sessions.each do |session|
40 sessions.each do |session|
41 if session.data[:user_id]
41 if session.data[:user_id]
42 @users << User.find(session.data[:user_id])
42 @users << User.find(session.data[:user_id])
43 end
43 end
44 end
44 end
45 end
45 end
46
46
47 def show
47 def show
48 @user = User.find(params[:id])
48 @user = User.find(params[:id])
49 end
49 end
50
50
51 def new
51 def new
52 @user = User.new
52 @user = User.new
53 end
53 end
54
54
55 def create
55 def create
56 @user = User.new(params[:user])
56 @user = User.new(params[:user])
57 @user.activated = true
57 @user.activated = true
58 if @user.save
58 if @user.save
59 flash[:notice] = 'User was successfully created.'
59 flash[:notice] = 'User was successfully created.'
60 redirect_to :action => 'list'
60 redirect_to :action => 'list'
61 else
61 else
62 render :action => 'new'
62 render :action => 'new'
63 end
63 end
64 end
64 end
65
65
66 def create_from_list
66 def create_from_list
67 lines = params[:user_list]
67 lines = params[:user_list]
68
68
69 note = []
69 note = []
70
70
71 lines.split("\n").each do |line|
71 lines.split("\n").each do |line|
72 items = line.chomp.split(',')
72 items = line.chomp.split(',')
73 if items.length>=2
73 if items.length>=2
74 login = items[0]
74 login = items[0]
75 full_name = items[1]
75 full_name = items[1]
76
76
77 added_random_password = false
77 added_random_password = false
78 if items.length>=3
78 if items.length>=3
79 password = items[2].chomp(" ")
79 password = items[2].chomp(" ")
80 user_alias = (items.length>=4) ? items[3] : login
80 user_alias = (items.length>=4) ? items[3] : login
81 else
81 else
82 password = random_password
82 password = random_password
83 user_alias = (items.length>=4) ? items[3] : login
83 user_alias = (items.length>=4) ? items[3] : login
84 added_random_password = true
84 added_random_password = true
85 end
85 end
86
86
87 - user = User.new({:login => login,
87 + user = User.find_by_login(login)
88 - :full_name => full_name,
88 + if (user)
89 - :password => password,
89 + user.full_name = full_name
90 - :password_confirmation => password,
90 + user.password = password
91 - :alias => user_alias})
91 + else
92 + user = User.new({:login => login,
93 + :full_name => full_name,
94 + :password => password,
95 + :password_confirmation => password,
96 + :alias => user_alias})
97 + end
92 user.activated = true
98 user.activated = true
93 user.save
99 user.save
94
100
95 if added_random_password
101 if added_random_password
96 note << "'#{login}' (+)"
102 note << "'#{login}' (+)"
97 else
103 else
98 note << login
104 note << login
99 end
105 end
100 end
106 end
101 end
107 end
102 flash[:notice] = 'User(s) ' + note.join(', ') +
108 flash[:notice] = 'User(s) ' + note.join(', ') +
103 ' were successfully created. ' +
109 ' were successfully created. ' +
104 '( (+) - created with random passwords.)'
110 '( (+) - created with random passwords.)'
105 redirect_to :action => 'list'
111 redirect_to :action => 'list'
106 end
112 end
107
113
108 def edit
114 def edit
109 @user = User.find(params[:id])
115 @user = User.find(params[:id])
110 end
116 end
111
117
112 def update
118 def update
113 @user = User.find(params[:id])
119 @user = User.find(params[:id])
114 if @user.update_attributes(params[:user])
120 if @user.update_attributes(params[:user])
115 flash[:notice] = 'User was successfully updated.'
121 flash[:notice] = 'User was successfully updated.'
116 redirect_to :action => 'show', :id => @user
122 redirect_to :action => 'show', :id => @user
117 else
123 else
118 render :action => 'edit'
124 render :action => 'edit'
119 end
125 end
120 end
126 end
121
127
122 def destroy
128 def destroy
123 User.find(params[:id]).destroy
129 User.find(params[:id]).destroy
124 redirect_to :action => 'list'
130 redirect_to :action => 'list'
125 end
131 end
126
132
127 def user_stat
133 def user_stat
128 if params[:commit] == 'download csv'
134 if params[:commit] == 'download csv'
129 @problems = Problem.all
135 @problems = Problem.all
130 else
136 else
131 @problems = Problem.find_available_problems
137 @problems = Problem.find_available_problems
132 end
138 end
133 @users = User.find(:all, :include => [:contests, :contest_stat])
139 @users = User.find(:all, :include => [:contests, :contest_stat])
134 @scorearray = Array.new
140 @scorearray = Array.new
135 @users.each do |u|
141 @users.each do |u|
136 ustat = Array.new
142 ustat = Array.new
137 ustat[0] = u
143 ustat[0] = u
138 @problems.each do |p|
144 @problems.each do |p|
139 sub = Submission.find_last_by_user_and_problem(u.id,p.id)
145 sub = Submission.find_last_by_user_and_problem(u.id,p.id)
140 if (sub!=nil) and (sub.points!=nil)
146 if (sub!=nil) and (sub.points!=nil)
141 ustat << [(sub.points.to_f*100/p.full_score).round, (sub.points>=p.full_score)]
147 ustat << [(sub.points.to_f*100/p.full_score).round, (sub.points>=p.full_score)]
142 else
148 else
143 ustat << [0,false]
149 ustat << [0,false]
144 end
150 end
145 end
151 end
146 @scorearray << ustat
152 @scorearray << ustat
147 end
153 end
148
154
149 if params[:commit] == 'download csv' then
155 if params[:commit] == 'download csv' then
150 csv = gen_csv_from_scorearray(@scorearray,@problems)
156 csv = gen_csv_from_scorearray(@scorearray,@problems)
151 send_data csv, filename: 'last_score.csv'
157 send_data csv, filename: 'last_score.csv'
152 else
158 else
153 render template: 'user_admin/user_stat'
159 render template: 'user_admin/user_stat'
154 end
160 end
155 end
161 end
156
162
157 def user_stat_max
163 def user_stat_max
158 if params[:commit] == 'download csv'
164 if params[:commit] == 'download csv'
159 @problems = Problem.all
165 @problems = Problem.all
160 else
166 else
161 @problems = Problem.find_available_problems
167 @problems = Problem.find_available_problems
162 end
168 end
163 @users = User.find(:all, :include => [:contests, :contest_stat])
169 @users = User.find(:all, :include => [:contests, :contest_stat])
164 @scorearray = Array.new
170 @scorearray = Array.new
165 #set up range from param
171 #set up range from param
166 since_id = params.fetch(:since_id, 0).to_i
172 since_id = params.fetch(:since_id, 0).to_i
167 until_id = params.fetch(:until_id, 0).to_i
173 until_id = params.fetch(:until_id, 0).to_i
168 @users.each do |u|
174 @users.each do |u|
169 ustat = Array.new
175 ustat = Array.new
170 ustat[0] = u
176 ustat[0] = u
171 @problems.each do |p|
177 @problems.each do |p|
172 max_points = 0
178 max_points = 0
173 Submission.find_in_range_by_user_and_problem(u.id,p.id,since_id,until_id).each do |sub|
179 Submission.find_in_range_by_user_and_problem(u.id,p.id,since_id,until_id).each do |sub|
174 max_points = sub.points if sub and sub.points and (sub.points > max_points)
180 max_points = sub.points if sub and sub.points and (sub.points > max_points)
175 end
181 end
176 ustat << [(max_points.to_f*100/p.full_score).round, (max_points>=p.full_score)]
182 ustat << [(max_points.to_f*100/p.full_score).round, (max_points>=p.full_score)]
177 end
183 end
178 @scorearray << ustat
184 @scorearray << ustat
179 end
185 end
180
186
181 if params[:commit] == 'download csv' then
187 if params[:commit] == 'download csv' then
182 csv = gen_csv_from_scorearray(@scorearray,@problems)
188 csv = gen_csv_from_scorearray(@scorearray,@problems)
183 send_data csv, filename: 'max_score.csv'
189 send_data csv, filename: 'max_score.csv'
184 else
190 else
185 render template: 'user_admin/user_stat'
191 render template: 'user_admin/user_stat'
186 end
192 end
187 end
193 end
188
194
189 def import
195 def import
190 if params[:file]==''
196 if params[:file]==''
191 flash[:notice] = 'Error importing no file'
197 flash[:notice] = 'Error importing no file'
192 redirect_to :action => 'list' and return
198 redirect_to :action => 'list' and return
193 end
199 end
194 import_from_file(params[:file])
200 import_from_file(params[:file])
195 end
201 end
196
202
197 def random_all_passwords
203 def random_all_passwords
198 users = User.find(:all)
204 users = User.find(:all)
199 @prefix = params[:prefix] || ''
205 @prefix = params[:prefix] || ''
200 @non_admin_users = User.find_non_admin_with_prefix(@prefix)
206 @non_admin_users = User.find_non_admin_with_prefix(@prefix)
201 @changed = false
207 @changed = false
202 if request.request_method == 'POST'
208 if request.request_method == 'POST'
203 @non_admin_users.each do |user|
209 @non_admin_users.each do |user|
204 password = random_password
210 password = random_password
205 user.password = password
211 user.password = password
206 user.password_confirmation = password
212 user.password_confirmation = password
207 user.save
213 user.save
208 end
214 end
209 @changed = true
215 @changed = true
210 end
216 end
211 end
217 end
212
218
213 # contest management
219 # contest management
214
220
215 def contests
221 def contests
216 @contest, @users = find_contest_and_user_from_contest_id(params[:id])
222 @contest, @users = find_contest_and_user_from_contest_id(params[:id])
217 @contests = Contest.enabled
223 @contests = Contest.enabled
218 end
224 end
219
225
220 def assign_from_list
226 def assign_from_list
221 contest_id = params[:users_contest_id]
227 contest_id = params[:users_contest_id]
222 org_contest, users = find_contest_and_user_from_contest_id(contest_id)
228 org_contest, users = find_contest_and_user_from_contest_id(contest_id)
223 contest = Contest.find(params[:new_contest][:id])
229 contest = Contest.find(params[:new_contest][:id])
224 if !contest
230 if !contest
225 flash[:notice] = 'Error: no contest'
231 flash[:notice] = 'Error: no contest'
226 redirect_to :action => 'contests', :id =>contest_id
232 redirect_to :action => 'contests', :id =>contest_id
227 end
233 end
228
234
229 note = []
235 note = []
230 users.each do |u|
236 users.each do |u|
231 u.contests = [contest]
237 u.contests = [contest]
232 note << u.login
238 note << u.login
233 end
239 end
234 flash[:notice] = 'User(s) ' + note.join(', ') +
240 flash[:notice] = 'User(s) ' + note.join(', ') +
235 " were successfully reassigned to #{contest.title}."
241 " were successfully reassigned to #{contest.title}."
236 redirect_to :action => 'contests', :id =>contest.id
242 redirect_to :action => 'contests', :id =>contest.id
237 end
243 end
238
244
239 def add_to_contest
245 def add_to_contest
240 user = User.find(params[:id])
246 user = User.find(params[:id])
241 contest = Contest.find(params[:contest_id])
247 contest = Contest.find(params[:contest_id])
242 if user and contest
248 if user and contest
243 user.contests << contest
249 user.contests << contest
244 end
250 end
245 redirect_to :action => 'list'
251 redirect_to :action => 'list'
246 end
252 end
247
253
248 def remove_from_contest
254 def remove_from_contest
249 user = User.find(params[:id])
255 user = User.find(params[:id])
250 contest = Contest.find(params[:contest_id])
256 contest = Contest.find(params[:contest_id])
251 if user and contest
257 if user and contest
252 user.contests.delete(contest)
258 user.contests.delete(contest)
253 end
259 end
254 redirect_to :action => 'list'
260 redirect_to :action => 'list'
255 end
261 end
256
262
257 def contest_management
263 def contest_management
258 end
264 end
259
265
260 def manage_contest
266 def manage_contest
261 contest = Contest.find(params[:contest][:id])
267 contest = Contest.find(params[:contest][:id])
262 if !contest
268 if !contest
263 flash[:notice] = 'You did not choose the contest.'
269 flash[:notice] = 'You did not choose the contest.'
264 redirect_to :action => 'contest_management' and return
270 redirect_to :action => 'contest_management' and return
265 end
271 end
266
272
267 operation = params[:operation]
273 operation = params[:operation]
268
274
269 if not ['add','remove','assign'].include? operation
275 if not ['add','remove','assign'].include? operation
270 flash[:notice] = 'You did not choose the operation to perform.'
276 flash[:notice] = 'You did not choose the operation to perform.'
271 redirect_to :action => 'contest_management' and return
277 redirect_to :action => 'contest_management' and return
272 end
278 end
273
279
274 lines = params[:login_list]
280 lines = params[:login_list]
275 if !lines or lines.blank?
281 if !lines or lines.blank?
276 flash[:notice] = 'You entered an empty list.'
282 flash[:notice] = 'You entered an empty list.'
277 redirect_to :action => 'contest_management' and return
283 redirect_to :action => 'contest_management' and return
278 end
284 end
279
285
280 note = []
286 note = []
281 users = []
287 users = []
282 lines.split("\n").each do |line|
288 lines.split("\n").each do |line|
283 user = User.find_by_login(line.chomp)
289 user = User.find_by_login(line.chomp)
You need to be logged in to leave comments. Login now