Description:
remove junk from ealier merge
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r484:a0c51d75908c - - 1 file changed: 0 inserted, 28 deleted

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