Description:
confusing merge
Commit status:
[Not Reviewed]
References:
merge default
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r483:7cf62e1e74ba - - 1 file changed: 0 inserted, 1 deleted

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