Description:
more test on user_admin
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r752:3a8a3897c926 - - 6 files changed: 71 inserted, 59 deleted
@@ -0,0 +1,50 | |||
|
1 | + require "application_system_test_case" | |
|
2 | + | |
|
3 | + class UsersTest < ApplicationSystemTestCase | |
|
4 | + # test "visiting the index" do | |
|
5 | + # visit users_url | |
|
6 | + # | |
|
7 | + # assert_selector "h1", text: "User" | |
|
8 | + # end | |
|
9 | + | |
|
10 | + test "add new user and edit" do | |
|
11 | + login('admin','admin') | |
|
12 | + within 'header' do | |
|
13 | + click_on 'Manage' | |
|
14 | + click_on 'Users', match: :first | |
|
15 | + end | |
|
16 | + | |
|
17 | + assert_text "Users" | |
|
18 | + assert_text "New user" | |
|
19 | + | |
|
20 | + click_on "New user", match: :first | |
|
21 | + fill_in 'Login', with: 'test1' | |
|
22 | + fill_in 'Full name', with: 'test1 McTestface' | |
|
23 | + fill_in 'e-mail', with: 'a@a.com' | |
|
24 | + fill_in 'Password', with: 'abcdef' | |
|
25 | + fill_in 'Password confirmation', with: 'abcdef' | |
|
26 | + | |
|
27 | + click_on 'Create' | |
|
28 | + | |
|
29 | + assert_text 'User was successfully created' | |
|
30 | + assert_text 'a@a.com' | |
|
31 | + assert_text 'test1 McTestface' | |
|
32 | + | |
|
33 | + within('tr', text: 'McTestface') do | |
|
34 | + click_on 'Edit' | |
|
35 | + end | |
|
36 | + | |
|
37 | + fill_in 'Alias', with: 'hahaha' | |
|
38 | + fill_in 'Remark', with: 'section 2' | |
|
39 | + click_on 'Update User' | |
|
40 | + | |
|
41 | + assert_text 'section 2' | |
|
42 | + end | |
|
43 | + | |
|
44 | + def login(username,password) | |
|
45 | + visit root_path | |
|
46 | + fill_in "Login", with: username | |
|
47 | + fill_in "Password", with: password | |
|
48 | + click_on "Login" | |
|
49 | + end | |
|
50 | + end |
@@ -1,598 +1,597 | |||
|
1 | 1 | require 'csv' |
|
2 | 2 | |
|
3 | 3 | class UserAdminController < ApplicationController |
|
4 | 4 | |
|
5 | 5 | include MailHelperMethods |
|
6 | 6 | |
|
7 | 7 | before_action :admin_authorization |
|
8 | 8 | |
|
9 | 9 | def index |
|
10 | 10 | @user_count = User.count |
|
11 | 11 | if params[:page] == 'all' |
|
12 | 12 | @users = User.all |
|
13 | 13 | @paginated = false |
|
14 | 14 | else |
|
15 | 15 | @users = User.paginate :page => params[:page] |
|
16 | 16 | @paginated = true |
|
17 | 17 | end |
|
18 | 18 | @users = User.all |
|
19 | 19 | @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at'] |
|
20 | 20 | @contests = Contest.enabled |
|
21 | 21 | end |
|
22 | 22 | |
|
23 | 23 | def active |
|
24 | 24 | sessions = ActiveRecord::SessionStore::Session.where("updated_at >= ?", 60.minutes.ago) |
|
25 | 25 | @users = [] |
|
26 | 26 | sessions.each do |session| |
|
27 | 27 | if session.data[:user_id] |
|
28 | 28 | @users << User.find(session.data[:user_id]) |
|
29 | 29 | end |
|
30 | 30 | end |
|
31 | 31 | end |
|
32 | 32 | |
|
33 | 33 | def show |
|
34 | 34 | @user = User.find(params[:id]) |
|
35 | 35 | end |
|
36 | 36 | |
|
37 | 37 | def new |
|
38 | 38 | @user = User.new |
|
39 | 39 | end |
|
40 | 40 | |
|
41 | 41 | def create |
|
42 | 42 | @user = User.new(user_params) |
|
43 | 43 | @user.activated = true |
|
44 | 44 | if @user.save |
|
45 | 45 | flash[:notice] = 'User was successfully created.' |
|
46 | 46 | redirect_to :action => 'index' |
|
47 | 47 | else |
|
48 | 48 | render :action => 'new' |
|
49 | 49 | end |
|
50 | 50 | end |
|
51 | 51 | |
|
52 | 52 | def clear_last_ip |
|
53 | 53 | @user = User.find(params[:id]) |
|
54 | 54 | @user.last_ip = nil |
|
55 | 55 | @user.save |
|
56 | 56 | redirect_to action: 'index', page: params[:page] |
|
57 | 57 | end |
|
58 | 58 | |
|
59 | 59 | def create_from_list |
|
60 | 60 | lines = params[:user_list] |
|
61 | 61 | |
|
62 | 62 | note = [] |
|
63 | 63 | |
|
64 | 64 | lines.split("\n").each do |line| |
|
65 | 65 | items = line.chomp.split(',') |
|
66 | 66 | if items.length>=2 |
|
67 | 67 | login = items[0] |
|
68 | 68 | full_name = items[1] |
|
69 | 69 | remark ='' |
|
70 | 70 | user_alias = '' |
|
71 | 71 | |
|
72 | 72 | added_random_password = false |
|
73 | 73 | if items.length >= 3 and items[2].chomp(" ").length > 0; |
|
74 | 74 | password = items[2].chomp(" ") |
|
75 | 75 | else |
|
76 | 76 | password = random_password |
|
77 | 77 | add_random_password=true; |
|
78 | 78 | end |
|
79 | 79 | |
|
80 | 80 | if items.length>= 4 and items[3].chomp(" ").length > 0; |
|
81 | 81 | user_alias = items[3].chomp(" ") |
|
82 | 82 | else |
|
83 | 83 | user_alias = login |
|
84 | 84 | end |
|
85 | 85 | |
|
86 | 86 | if items.length>=5 |
|
87 | 87 | remark = items[4].strip; |
|
88 | 88 | end |
|
89 | 89 | |
|
90 | 90 | user = User.find_by_login(login) |
|
91 | 91 | if (user) |
|
92 | 92 | user.full_name = full_name |
|
93 | 93 | user.password = password |
|
94 | 94 | user.remark = remark |
|
95 | 95 | else |
|
96 | 96 | user = User.new({:login => login, |
|
97 | 97 | :full_name => full_name, |
|
98 | 98 | :password => password, |
|
99 | 99 | :password_confirmation => password, |
|
100 | 100 | :alias => user_alias, |
|
101 | 101 | :remark => remark}) |
|
102 | 102 | end |
|
103 | 103 | user.activated = true |
|
104 | 104 | user.save |
|
105 | 105 | |
|
106 | 106 | if added_random_password |
|
107 | 107 | note << "'#{login}' (+)" |
|
108 | 108 | else |
|
109 | 109 | note << login |
|
110 | 110 | end |
|
111 | 111 | end |
|
112 | 112 | end |
|
113 | 113 | flash[:success] = 'User(s) ' + note.join(', ') + |
|
114 | 114 | ' were successfully created. ' + |
|
115 | 115 | '( (+) - created with random passwords.)' |
|
116 | 116 | redirect_to :action => 'index' |
|
117 | 117 | end |
|
118 | 118 | |
|
119 | 119 | def edit |
|
120 | 120 | @user = User.find(params[:id]) |
|
121 | 121 | end |
|
122 | 122 | |
|
123 | 123 | def update |
|
124 | 124 | @user = User.find(params[:id]) |
|
125 | 125 | if @user.update_attributes(user_params) |
|
126 | 126 | flash[:notice] = 'User was successfully updated.' |
|
127 | 127 | redirect_to :action => 'show', :id => @user |
|
128 | 128 | else |
|
129 | 129 | render :action => 'edit' |
|
130 | 130 | end |
|
131 | 131 | end |
|
132 | 132 | |
|
133 | 133 | def destroy |
|
134 | 134 | User.find(params[:id]).destroy |
|
135 | 135 | redirect_to :action => 'index' |
|
136 | 136 | end |
|
137 | 137 | |
|
138 | 138 | def user_stat |
|
139 | 139 | if params[:commit] == 'download csv' |
|
140 | 140 | @problems = Problem.all |
|
141 | 141 | else |
|
142 | 142 | @problems = Problem.available_problems |
|
143 | 143 | end |
|
144 | 144 | @users = User.includes(:contests, :contest_stat).where(enabled: true) |
|
145 | 145 | @scorearray = Array.new |
|
146 | 146 | @users.each do |u| |
|
147 | 147 | ustat = Array.new |
|
148 | 148 | ustat[0] = u |
|
149 | 149 | @problems.each do |p| |
|
150 | 150 | sub = Submission.find_last_by_user_and_problem(u.id,p.id) |
|
151 | 151 | if (sub!=nil) and (sub.points!=nil) and p and p.full_score |
|
152 | 152 | ustat << [(sub.points.to_f*100/p.full_score).round, (sub.points>=p.full_score)] |
|
153 | 153 | else |
|
154 | 154 | ustat << [0,false] |
|
155 | 155 | end |
|
156 | 156 | end |
|
157 | 157 | @scorearray << ustat |
|
158 | 158 | end |
|
159 | 159 | if params[:commit] == 'download csv' then |
|
160 | 160 | csv = gen_csv_from_scorearray(@scorearray,@problems) |
|
161 | 161 | send_data csv, filename: 'last_score.csv' |
|
162 | 162 | else |
|
163 | 163 | render template: 'user_admin/user_stat' |
|
164 | 164 | end |
|
165 | 165 | end |
|
166 | 166 | |
|
167 | 167 | def user_stat_max |
|
168 | 168 | if params[:commit] == 'download csv' |
|
169 | 169 | @problems = Problem.all |
|
170 | 170 | else |
|
171 | 171 | @problems = Problem.available_problems |
|
172 | 172 | end |
|
173 | 173 | @users = User.includes(:contests).includes(:contest_stat).all |
|
174 | 174 | @scorearray = Array.new |
|
175 | 175 | #set up range from param |
|
176 | 176 | since_id = params.fetch(:since_id, 0).to_i |
|
177 | 177 | until_id = params.fetch(:until_id, 0).to_i |
|
178 | 178 | @users.each do |u| |
|
179 | 179 | ustat = Array.new |
|
180 | 180 | ustat[0] = u |
|
181 | 181 | @problems.each do |p| |
|
182 | 182 | max_points = 0 |
|
183 | 183 | Submission.find_in_range_by_user_and_problem(u.id,p.id,since_id,until_id).each do |sub| |
|
184 | 184 | max_points = sub.points if sub and sub.points and (sub.points > max_points) |
|
185 | 185 | end |
|
186 | 186 | ustat << [(max_points.to_f*100/p.full_score).round, (max_points>=p.full_score)] |
|
187 | 187 | end |
|
188 | 188 | @scorearray << ustat |
|
189 | 189 | end |
|
190 | 190 | |
|
191 | 191 | if params[:commit] == 'download csv' then |
|
192 | 192 | csv = gen_csv_from_scorearray(@scorearray,@problems) |
|
193 | 193 | send_data csv, filename: 'max_score.csv' |
|
194 | 194 | else |
|
195 | 195 | render template: 'user_admin/user_stat' |
|
196 | 196 | end |
|
197 | 197 | end |
|
198 | 198 | |
|
199 | 199 | def import |
|
200 | 200 | if params[:file]=='' |
|
201 | 201 | flash[:notice] = 'Error importing no file' |
|
202 | 202 | redirect_to :action => 'index' and return |
|
203 | 203 | end |
|
204 | 204 | import_from_file(params[:file]) |
|
205 | 205 | end |
|
206 | 206 | |
|
207 | 207 | def random_all_passwords |
|
208 | 208 | users = User.all |
|
209 | 209 | @prefix = params[:prefix] || '' |
|
210 | 210 | @non_admin_users = User.find_non_admin_with_prefix(@prefix) |
|
211 | 211 | @changed = false |
|
212 | - if request.request_method == 'POST' | |
|
212 | + if params[:commit] == 'Go ahead' | |
|
213 | 213 | @non_admin_users.each do |user| |
|
214 | 214 | password = random_password |
|
215 | 215 | user.password = password |
|
216 | 216 | user.password_confirmation = password |
|
217 | 217 | user.save |
|
218 | 218 | end |
|
219 | 219 | @changed = true |
|
220 | 220 | end |
|
221 | 221 | end |
|
222 | 222 | |
|
223 | - | |
|
224 | 223 | # contest management |
|
225 | 224 | |
|
226 | 225 | def contests |
|
227 | 226 | @contest, @users = find_contest_and_user_from_contest_id(params[:id]) |
|
228 | 227 | @contests = Contest.enabled |
|
229 | 228 | end |
|
230 | 229 | |
|
231 | 230 | def assign_from_list |
|
232 | 231 | contest_id = params[:users_contest_id] |
|
233 | 232 | org_contest, users = find_contest_and_user_from_contest_id(contest_id) |
|
234 | 233 | contest = Contest.find(params[:new_contest][:id]) |
|
235 | 234 | if !contest |
|
236 | 235 | flash[:notice] = 'Error: no contest' |
|
237 | 236 | redirect_to :action => 'contests', :id =>contest_id |
|
238 | 237 | end |
|
239 | 238 | |
|
240 | 239 | note = [] |
|
241 | 240 | users.each do |u| |
|
242 | 241 | u.contests = [contest] |
|
243 | 242 | note << u.login |
|
244 | 243 | end |
|
245 | 244 | flash[:notice] = 'User(s) ' + note.join(', ') + |
|
246 | 245 | " were successfully reassigned to #{contest.title}." |
|
247 | 246 | redirect_to :action => 'contests', :id =>contest.id |
|
248 | 247 | end |
|
249 | 248 | |
|
250 | 249 | def add_to_contest |
|
251 | 250 | user = User.find(params[:id]) |
|
252 | 251 | contest = Contest.find(params[:contest_id]) |
|
253 | 252 | if user and contest |
|
254 | 253 | user.contests << contest |
|
255 | 254 | end |
|
256 | 255 | redirect_to :action => 'index' |
|
257 | 256 | end |
|
258 | 257 | |
|
259 | 258 | def remove_from_contest |
|
260 | 259 | user = User.find(params[:id]) |
|
261 | 260 | contest = Contest.find(params[:contest_id]) |
|
262 | 261 | if user and contest |
|
263 | 262 | user.contests.delete(contest) |
|
264 | 263 | end |
|
265 | 264 | redirect_to :action => 'index' |
|
266 | 265 | end |
|
267 | 266 | |
|
268 | 267 | def contest_management |
|
269 | 268 | end |
|
270 | 269 | |
|
271 | 270 | def manage_contest |
|
272 | 271 | contest = Contest.find(params[:contest][:id]) |
|
273 | 272 | if !contest |
|
274 | 273 | flash[:notice] = 'You did not choose the contest.' |
|
275 | 274 | redirect_to :action => 'contest_management' and return |
|
276 | 275 | end |
|
277 | 276 | |
|
278 | 277 | operation = params[:operation] |
|
279 | 278 | |
|
280 | 279 | if not ['add','remove','assign'].include? operation |
|
281 | 280 | flash[:notice] = 'You did not choose the operation to perform.' |
|
282 | 281 | redirect_to :action => 'contest_management' and return |
|
283 | 282 | end |
|
284 | 283 | |
|
285 | 284 | lines = params[:login_list] |
|
286 | 285 | if !lines or lines.blank? |
|
287 | 286 | flash[:notice] = 'You entered an empty list.' |
|
288 | 287 | redirect_to :action => 'contest_management' and return |
|
289 | 288 | end |
|
290 | 289 | |
|
291 | 290 | note = [] |
|
292 | 291 | users = [] |
|
293 | 292 | lines.split("\n").each do |line| |
|
294 | 293 | user = User.find_by_login(line.chomp) |
|
295 | 294 | if user |
|
296 | 295 | if operation=='add' |
|
297 | 296 | if ! user.contests.include? contest |
|
298 | 297 | user.contests << contest |
|
299 | 298 | end |
|
300 | 299 | elsif operation=='remove' |
|
301 | 300 | user.contests.delete(contest) |
|
302 | 301 | else |
|
303 | 302 | user.contests = [contest] |
|
304 | 303 | end |
|
305 | 304 | |
|
306 | 305 | if params[:reset_timer] |
|
307 | 306 | user.contest_stat.forced_logout = true |
|
308 | 307 | user.contest_stat.reset_timer_and_save |
|
309 | 308 | end |
|
310 | 309 | |
|
311 | 310 | if params[:notification_emails] |
|
312 | 311 | send_contest_update_notification_email(user, contest) |
|
313 | 312 | end |
|
314 | 313 | |
|
315 | 314 | note << user.login |
|
316 | 315 | users << user |
|
317 | 316 | end |
|
318 | 317 | end |
|
319 | 318 | |
|
320 | 319 | if params[:reset_timer] |
|
321 | 320 | logout_users(users) |
|
322 | 321 | end |
|
323 | 322 | |
|
324 | 323 | flash[:notice] = 'User(s) ' + note.join(', ') + |
|
325 | 324 | ' were successfully modified. ' |
|
326 | 325 | redirect_to :action => 'contest_management' |
|
327 | 326 | end |
|
328 | 327 | |
|
329 | 328 | # admin management |
|
330 | 329 | |
|
331 | 330 | def admin |
|
332 | 331 | @admins = User.all.find_all {|user| user.admin? } |
|
333 | 332 | end |
|
334 | 333 | |
|
335 | 334 | def grant_admin |
|
336 | 335 | login = params[:login] |
|
337 | 336 | user = User.find_by_login(login) |
|
338 | 337 | if user!=nil |
|
339 | 338 | admin_role = Role.find_by_name('admin') |
|
340 | 339 | user.roles << admin_role |
|
341 | 340 | else |
|
342 | 341 | flash[:notice] = 'Unknown user' |
|
343 | 342 | end |
|
344 | 343 | flash[:notice] = 'User added as admins' |
|
345 | 344 | redirect_to :action => 'admin' |
|
346 | 345 | end |
|
347 | 346 | |
|
348 | 347 | def revoke_admin |
|
349 | 348 | user = User.find(params[:id]) |
|
350 | 349 | if user==nil |
|
351 | 350 | flash[:notice] = 'Unknown user' |
|
352 | 351 | redirect_to :action => 'admin' and return |
|
353 | 352 | elsif user.login == 'root' |
|
354 | 353 | flash[:notice] = 'You cannot revoke admisnistrator permission from root.' |
|
355 | 354 | redirect_to :action => 'admin' and return |
|
356 | 355 | end |
|
357 | 356 | |
|
358 | 357 | admin_role = Role.find_by_name('admin') |
|
359 | 358 | user.roles.delete(admin_role) |
|
360 | 359 | flash[:notice] = 'User permission revoked' |
|
361 | 360 | redirect_to :action => 'admin' |
|
362 | 361 | end |
|
363 | 362 | |
|
364 | 363 | # mass mailing |
|
365 | 364 | |
|
366 | 365 | def mass_mailing |
|
367 | 366 | end |
|
368 | 367 | |
|
369 | 368 | def bulk_mail |
|
370 | 369 | lines = params[:login_list] |
|
371 | 370 | if !lines or lines.blank? |
|
372 | 371 | flash[:notice] = 'You entered an empty list.' |
|
373 | 372 | redirect_to :action => 'mass_mailing' and return |
|
374 | 373 | end |
|
375 | 374 | |
|
376 | 375 | mail_subject = params[:subject] |
|
377 | 376 | if !mail_subject or mail_subject.blank? |
|
378 | 377 | flash[:notice] = 'You entered an empty mail subject.' |
|
379 | 378 | redirect_to :action => 'mass_mailing' and return |
|
380 | 379 | end |
|
381 | 380 | |
|
382 | 381 | mail_body = params[:email_body] |
|
383 | 382 | if !mail_body or mail_body.blank? |
|
384 | 383 | flash[:notice] = 'You entered an empty mail body.' |
|
385 | 384 | redirect_to :action => 'mass_mailing' and return |
|
386 | 385 | end |
|
387 | 386 | |
|
388 | 387 | note = [] |
|
389 | 388 | users = [] |
|
390 | 389 | lines.split("\n").each do |line| |
|
391 | 390 | user = User.find_by_login(line.chomp) |
|
392 | 391 | if user |
|
393 | 392 | send_mail(user.email, mail_subject, mail_body) |
|
394 | 393 | note << user.login |
|
395 | 394 | end |
|
396 | 395 | end |
|
397 | 396 | |
|
398 | 397 | flash[:notice] = 'User(s) ' + note.join(', ') + |
|
399 | 398 | ' were successfully modified. ' |
|
400 | 399 | redirect_to :action => 'mass_mailing' |
|
401 | 400 | end |
|
402 | 401 | |
|
403 | 402 | #bulk manage |
|
404 | 403 | def bulk_manage |
|
405 | 404 | |
|
406 | 405 | begin |
|
407 | 406 | @users = User.where('(login REGEXP ?) OR (remark REGEXP ?)',params[:regex],params[:regex]) if params[:regex] |
|
408 | 407 | @users.count if @users #i don't know why I have to call count, but if I won't exception is not raised |
|
409 | 408 | rescue Exception |
|
410 | 409 | flash[:error] = 'Regular Expression is malformed' |
|
411 | 410 | @users = nil |
|
412 | 411 | end |
|
413 | 412 | |
|
414 | 413 | if params[:commit] |
|
415 | 414 | @action = {} |
|
416 | 415 | @action[:set_enable] = params[:enabled] |
|
417 | 416 | @action[:enabled] = params[:enable] == "1" |
|
418 | 417 | @action[:gen_password] = params[:gen_password] |
|
419 | 418 | @action[:add_group] = params[:add_group] |
|
420 | 419 | @action[:group_name] = params[:group_name] |
|
421 | 420 | end |
|
422 | 421 | |
|
423 | 422 | if params[:commit] == "Perform" |
|
424 | 423 | if @action[:set_enable] |
|
425 | 424 | @users.update_all(enabled: @action[:enabled]) |
|
426 | 425 | end |
|
427 | 426 | if @action[:gen_password] |
|
428 | 427 | @users.each do |u| |
|
429 | 428 | password = random_password |
|
430 | 429 | u.password = password |
|
431 | 430 | u.password_confirmation = password |
|
432 | 431 | u.save |
|
433 | 432 | end |
|
434 | 433 | end |
|
435 | 434 | if @action[:add_group] and @action[:group_name] |
|
436 | 435 | @group = Group.find(@action[:group_name]) |
|
437 | 436 | ok = [] |
|
438 | 437 | failed = [] |
|
439 | 438 | @users.each do |user| |
|
440 | 439 | begin |
|
441 | 440 | @group.users << user |
|
442 | 441 | ok << user.login |
|
443 | 442 | rescue => e |
|
444 | 443 | failed << user.login |
|
445 | 444 | end |
|
446 | 445 | end |
|
447 | 446 | flash[:success] = "The following users are added to the 'group #{@group.name}': " + ok.join(', ') if ok.count > 0 |
|
448 | 447 | flash[:alert] = "The following users are already in the 'group #{@group.name}': " + failed.join(', ') if failed.count > 0 |
|
449 | 448 | end |
|
450 | 449 | end |
|
451 | 450 | end |
|
452 | 451 | |
|
453 | 452 | protected |
|
454 | 453 | |
|
455 | 454 | def random_password(length=5) |
|
456 | 455 | chars = 'abcdefghijkmnopqrstuvwxyz23456789' |
|
457 | 456 | newpass = "" |
|
458 | 457 | length.times { newpass << chars[rand(chars.size-1)] } |
|
459 | 458 | return newpass |
|
460 | 459 | end |
|
461 | 460 | |
|
462 | 461 | def import_from_file(f) |
|
463 | 462 | data_hash = YAML.load(f) |
|
464 | 463 | @import_log = "" |
|
465 | 464 | |
|
466 | 465 | country_data = data_hash[:countries] |
|
467 | 466 | site_data = data_hash[:sites] |
|
468 | 467 | user_data = data_hash[:users] |
|
469 | 468 | |
|
470 | 469 | # import country |
|
471 | 470 | countries = {} |
|
472 | 471 | country_data.each_pair do |id,country| |
|
473 | 472 | c = Country.find_by_name(country[:name]) |
|
474 | 473 | if c!=nil |
|
475 | 474 | countries[id] = c |
|
476 | 475 | @import_log << "Found #{country[:name]}\n" |
|
477 | 476 | else |
|
478 | 477 | countries[id] = Country.new(:name => country[:name]) |
|
479 | 478 | countries[id].save |
|
480 | 479 | @import_log << "Created #{country[:name]}\n" |
|
481 | 480 | end |
|
482 | 481 | end |
|
483 | 482 | |
|
484 | 483 | # import sites |
|
485 | 484 | sites = {} |
|
486 | 485 | site_data.each_pair do |id,site| |
|
487 | 486 | s = Site.find_by_name(site[:name]) |
|
488 | 487 | if s!=nil |
|
489 | 488 | @import_log << "Found #{site[:name]}\n" |
|
490 | 489 | else |
|
491 | 490 | s = Site.new(:name => site[:name]) |
|
492 | 491 | @import_log << "Created #{site[:name]}\n" |
|
493 | 492 | end |
|
494 | 493 | s.password = site[:password] |
|
495 | 494 | s.country = countries[site[:country_id]] |
|
496 | 495 | s.save |
|
497 | 496 | sites[id] = s |
|
498 | 497 | end |
|
499 | 498 | |
|
500 | 499 | # import users |
|
501 | 500 | user_data.each_pair do |id,user| |
|
502 | 501 | u = User.find_by_login(user[:login]) |
|
503 | 502 | if u!=nil |
|
504 | 503 | @import_log << "Found #{user[:login]}\n" |
|
505 | 504 | else |
|
506 | 505 | u = User.new(:login => user[:login]) |
|
507 | 506 | @import_log << "Created #{user[:login]}\n" |
|
508 | 507 | end |
|
509 | 508 | u.full_name = user[:name] |
|
510 | 509 | u.password = user[:password] |
|
511 | 510 | u.country = countries[user[:country_id]] |
|
512 | 511 | u.site = sites[user[:site_id]] |
|
513 | 512 | u.activated = true |
|
514 | 513 | u.email = "empty-#{u.login}@none.com" |
|
515 | 514 | if not u.save |
|
516 | 515 | @import_log << "Errors\n" |
|
517 | 516 | u.errors.each { |attr,msg| @import_log << "#{attr} - #{msg}\n" } |
|
518 | 517 | end |
|
519 | 518 | end |
|
520 | 519 | |
|
521 | 520 | end |
|
522 | 521 | |
|
523 | 522 | def logout_users(users) |
|
524 | 523 | users.each do |user| |
|
525 | 524 | contest_stat = user.contest_stat(true) |
|
526 | 525 | if contest_stat and !contest_stat.forced_logout |
|
527 | 526 | contest_stat.forced_logout = true |
|
528 | 527 | contest_stat.save |
|
529 | 528 | end |
|
530 | 529 | end |
|
531 | 530 | end |
|
532 | 531 | |
|
533 | 532 | def send_contest_update_notification_email(user, contest) |
|
534 | 533 | contest_title_name = GraderConfiguration['contest.name'] |
|
535 | 534 | contest_name = contest.name |
|
536 | 535 | mail_subject = t('contest.notification.email_subject', { |
|
537 | 536 | :contest_title_name => contest_title_name, |
|
538 | 537 | :contest_name => contest_name }) |
|
539 | 538 | mail_body = t('contest.notification.email_body', { |
|
540 | 539 | :full_name => user.full_name, |
|
541 | 540 | :contest_title_name => contest_title_name, |
|
542 | 541 | :contest_name => contest.name, |
|
543 | 542 | }) |
|
544 | 543 | |
|
545 | 544 | logger.info mail_body |
|
546 | 545 | send_mail(user.email, mail_subject, mail_body) |
|
547 | 546 | end |
|
548 | 547 | |
|
549 | 548 | def find_contest_and_user_from_contest_id(id) |
|
550 | 549 | if id!='none' |
|
551 | 550 | @contest = Contest.find(id) |
|
552 | 551 | else |
|
553 | 552 | @contest = nil |
|
554 | 553 | end |
|
555 | 554 | if @contest |
|
556 | 555 | @users = @contest.users |
|
557 | 556 | else |
|
558 | 557 | @users = User.find_users_with_no_contest |
|
559 | 558 | end |
|
560 | 559 | return [@contest, @users] |
|
561 | 560 | end |
|
562 | 561 | |
|
563 | 562 | def gen_csv_from_scorearray(scorearray,problem) |
|
564 | 563 | CSV.generate do |csv| |
|
565 | 564 | #add header |
|
566 | 565 | header = ['User','Name', 'Activated?', 'Logged in', 'Contest'] |
|
567 | 566 | problem.each { |p| header << p.name } |
|
568 | 567 | header += ['Total','Passed'] |
|
569 | 568 | csv << header |
|
570 | 569 | #add data |
|
571 | 570 | scorearray.each do |sc| |
|
572 | 571 | total = num_passed = 0 |
|
573 | 572 | row = Array.new |
|
574 | 573 | sc.each_index do |i| |
|
575 | 574 | if i == 0 |
|
576 | 575 | row << sc[i].login |
|
577 | 576 | row << sc[i].full_name |
|
578 | 577 | row << sc[i].activated |
|
579 | 578 | row << (sc[i].try(:contest_stat).try(:started_at).nil? ? 'no' : 'yes') |
|
580 | 579 | row << sc[i].contests.collect {|c| c.name}.join(', ') |
|
581 | 580 | else |
|
582 | 581 | row << sc[i][0] |
|
583 | 582 | total += sc[i][0] |
|
584 | 583 | num_passed += 1 if sc[i][1] |
|
585 | 584 | end |
|
586 | 585 | end |
|
587 | 586 | row << total |
|
588 | 587 | row << num_passed |
|
589 | 588 | csv << row |
|
590 | 589 | end |
|
591 | 590 | end |
|
592 | 591 | end |
|
593 | 592 | |
|
594 | 593 | private |
|
595 | 594 | def user_params |
|
596 | 595 | params.require(:user).permit(:login,:password,:password_confirmation,:email, :alias, :full_name,:remark) |
|
597 | 596 | end |
|
598 | 597 | end |
@@ -1,38 +1,14 | |||
|
1 | - = error_messages_for 'user' | |
|
2 | - / [form:user] | |
|
3 | - .form-group | |
|
4 | - %label.col-md-2.control-label{for: :login} Login | |
|
5 | - .col-md-4 | |
|
6 | - = text_field 'user', 'login', class: 'form-control' | |
|
7 | - .col-md-6 | |
|
8 | - .form-group | |
|
9 | - %label.col-md-2.control-label{for: :full_name} Full name | |
|
10 | - .col-md-4 | |
|
11 | - = text_field 'user', 'full_name', class: 'form-control' | |
|
12 | - .col-md-6 | |
|
13 | - .form-group | |
|
14 | - %label.col-md-2.control-label{for: :password} Password | |
|
15 | - .col-md-4 | |
|
16 | - = password_field 'user', 'password', class: 'form-control' | |
|
17 | - .col-md-6 | |
|
18 | - .form-group | |
|
19 | - %label.col-md-2.control-label{for: :password_confirmation} Password (confirm) | |
|
20 | - .col-md-4 | |
|
21 | - = password_field 'user', 'password_confirmation', class: 'form-control' | |
|
22 | - .col-md-6 | |
|
23 | - .form-group | |
|
24 | - %label.col-md-2.control-label{for: :email} E-mail | |
|
25 | - .col-md-4 | |
|
26 | - = email_field 'user', 'email', class: 'form-control' | |
|
27 | - .col-md-6 | |
|
28 | - .form-group | |
|
29 | - %label.col-md-2.control-label{for: :alias} Alias | |
|
30 | - .col-md-4 | |
|
31 | - = text_field 'user', 'alias', class: 'form-control' | |
|
32 | - .col-md-6 | |
|
33 | - .form-group | |
|
34 | - %label.col-md-2.control-label{for: :remark} Remark | |
|
35 | - .col-md-4 | |
|
36 | - = text_field 'user', 'remark', class: 'form-control' | |
|
37 | - .col-md-6 | |
|
38 | - / [eoform:user] | |
|
1 | + = simple_form_for(@user) do |f| | |
|
2 | + = f.error_notification | |
|
3 | + .row | |
|
4 | + .col-md-6.col-md-offset-2 | |
|
5 | + = f.input :login, label: 'Login' | |
|
6 | + = f.input :full_name, label: 'Full name' | |
|
7 | + = f.input :password | |
|
8 | + = f.input :password_confirmation | |
|
9 | + = f.input :email | |
|
10 | + = f.input :alias | |
|
11 | + = f.input :remark | |
|
12 | + = f.button :submit, class: 'btn btn-success' | |
|
13 | + = link_to 'Cancel', :back, class: 'btn btn-default' | |
|
14 | + |
@@ -1,13 +1,4 | |||
|
1 | 1 | %h1 Editing user |
|
2 | + = simple_form_for @user, url: user_admin_path(@user) do |f| | |
|
3 | + = render partial: 'form', local: f | |
|
2 | 4 | |
|
3 | - = form_tag( {:action => 'update', :id => @user}, {class: 'form-horizontal'}) do | |
|
4 | - = error_messages_for 'user' | |
|
5 | - = render partial: "form" | |
|
6 | - .form-group | |
|
7 | - .col-md-offset-2.col-md-4 | |
|
8 | - = submit_tag "Edit", class: 'btn btn-primary' | |
|
9 | - | |
|
10 | - | |
|
11 | - = link_to 'Show', :action => 'show', :id => @user | |
|
12 | - | | |
|
13 | - = link_to 'Back', :action => 'index' |
@@ -1,7 +1,3 | |||
|
1 | 1 | %h1 New user |
|
2 | - = form_tag( {action: 'create'}, { class: 'form-horizontal'}) do | |
|
3 |
- = render |
|
|
4 | - .form-group | |
|
5 | - .col-md-offset-2.col-md-10 | |
|
6 | - = submit_tag "Create", class: 'btn btn-primary' | |
|
7 | - = link_to 'Back', :action => 'index' | |
|
2 | + = simple_form_for @user, url: user_admin_index_path do |f| | |
|
3 | + = render partial: 'form', local: f |
@@ -1,171 +1,171 | |||
|
1 | 1 | Rails.application.routes.draw do |
|
2 | 2 | resources :tags |
|
3 | 3 | get "sources/direct_edit" |
|
4 | 4 | |
|
5 | 5 | root :to => 'main#login' |
|
6 | 6 | |
|
7 | 7 | #logins |
|
8 | 8 | match 'login/login', to: 'login#login', via: [:get,:post] |
|
9 | 9 | |
|
10 | 10 | |
|
11 | 11 | resources :contests |
|
12 | 12 | |
|
13 | 13 | resources :sites |
|
14 | 14 | |
|
15 | 15 | resources :test |
|
16 | 16 | |
|
17 | 17 | resources :messages do |
|
18 | 18 | collection do |
|
19 | 19 | get 'console' |
|
20 | 20 | end |
|
21 | 21 | end |
|
22 | 22 | |
|
23 | 23 | resources :announcements do |
|
24 | 24 | member do |
|
25 | 25 | get 'toggle','toggle_front' |
|
26 | 26 | end |
|
27 | 27 | end |
|
28 | 28 | |
|
29 | 29 | resources :problems do |
|
30 | 30 | member do |
|
31 | 31 | get 'toggle' |
|
32 | 32 | get 'toggle_test' |
|
33 | 33 | get 'toggle_view_testcase' |
|
34 | 34 | get 'stat' |
|
35 | 35 | end |
|
36 | 36 | collection do |
|
37 | 37 | get 'turn_all_off' |
|
38 | 38 | get 'turn_all_on' |
|
39 | 39 | get 'import' |
|
40 | 40 | get 'manage' |
|
41 | 41 | get 'quick_create' |
|
42 | 42 | post 'do_manage' |
|
43 | 43 | end |
|
44 | 44 | end |
|
45 | 45 | |
|
46 | 46 | resources :groups do |
|
47 | 47 | member do |
|
48 | 48 | post 'add_user', to: 'groups#add_user', as: 'add_user' |
|
49 | 49 | delete 'remove_user/:user_id', to: 'groups#remove_user', as: 'remove_user' |
|
50 | 50 | delete 'remove_all_user', to: 'groups#remove_all_user', as: 'remove_all_user' |
|
51 | 51 | post 'add_problem', to: 'groups#add_problem', as: 'add_problem' |
|
52 | 52 | delete 'remove_problem/:problem_id', to: 'groups#remove_problem', as: 'remove_problem' |
|
53 | 53 | delete 'remove_all_problem', to: 'groups#remove_all_problem', as: 'remove_all_problem' |
|
54 | 54 | end |
|
55 | 55 | collection do |
|
56 | 56 | |
|
57 | 57 | end |
|
58 | 58 | end |
|
59 | 59 | |
|
60 | 60 | resources :testcases, only: [] do |
|
61 | 61 | member do |
|
62 | 62 | get 'download_input' |
|
63 | 63 | get 'download_sol' |
|
64 | 64 | end |
|
65 | 65 | collection do |
|
66 | 66 | get 'show_problem/:problem_id(/:test_num)' => 'testcases#show_problem', as: 'show_problem' |
|
67 | 67 | end |
|
68 | 68 | end |
|
69 | 69 | |
|
70 | 70 | resources :grader_configuration, controller: 'configurations' |
|
71 | 71 | |
|
72 | 72 | resources :users do |
|
73 | 73 | member do |
|
74 | 74 | get 'toggle_activate', 'toggle_enable' |
|
75 | 75 | get 'stat' |
|
76 | 76 | end |
|
77 | 77 | end |
|
78 | 78 | |
|
79 | 79 | resources :submissions do |
|
80 | 80 | member do |
|
81 | 81 | get 'download' |
|
82 | 82 | get 'compiler_msg' |
|
83 | 83 | get 'rejudge' |
|
84 | 84 | end |
|
85 | 85 | collection do |
|
86 | 86 | get 'prob/:problem_id', to: 'submissions#index', as: 'problem' |
|
87 | 87 | get 'direct_edit_problem/:problem_id(/:user_id)', to: 'submissions#direct_edit_problem', as: 'direct_edit_problem' |
|
88 | 88 | get 'get_latest_submission_status/:uid/:pid', to: 'submissions#get_latest_submission_status', as: 'get_latest_submission_status' |
|
89 | 89 | end |
|
90 | 90 | end |
|
91 | 91 | |
|
92 | 92 | |
|
93 | 93 | #user admin |
|
94 | 94 | resources :user_admin do |
|
95 | 95 | collection do |
|
96 | 96 | match 'bulk_manage', via: [:get, :post] |
|
97 | 97 | get 'bulk_mail' |
|
98 | 98 | get 'user_stat' |
|
99 | 99 | get 'import' |
|
100 | 100 | get 'new_list' |
|
101 | 101 | get 'admin' |
|
102 | - get 'random_all_passwords' | |
|
103 | 102 | get 'active' |
|
104 | 103 | get 'mass_mailing' |
|
104 | + post 'grant_admin' | |
|
105 | 105 | match 'create_from_list', via: [:get, :post] |
|
106 | - post 'grant_admin' | |
|
106 | + match 'random_all_passwords', via: [:get, :post] | |
|
107 | 107 | end |
|
108 | 108 | member do |
|
109 | 109 | get 'clear_last_ip' |
|
110 | 110 | end |
|
111 | 111 | end |
|
112 | 112 | |
|
113 | 113 | resources :contest_management, only: [:index] do |
|
114 | 114 | collection do |
|
115 | 115 | get 'user_stat' |
|
116 | 116 | get 'clear_stat' |
|
117 | 117 | get 'clear_all_stat' |
|
118 | 118 | end |
|
119 | 119 | end |
|
120 | 120 | |
|
121 | 121 | #get 'user_admin', to: 'user_admin#index' |
|
122 | 122 | #get 'user_admin/bulk_manage', to: 'user_admin#bulk_manage', as: 'bulk_manage_user_admin' |
|
123 | 123 | #post 'user_admin', to: 'user_admin#create' |
|
124 | 124 | #delete 'user_admin/:id', to: 'user_admin#destroy', as: 'user_admin_destroy' |
|
125 | 125 | |
|
126 | 126 | #singular resource |
|
127 | 127 | #---- BEWARE ---- singular resource maps to plural controller by default, we can override by provide controller name directly |
|
128 | 128 | #report |
|
129 | 129 | resource :report, only: [], controller: 'report' do |
|
130 | 130 | get 'login' |
|
131 | 131 | get 'multiple_login' |
|
132 | 132 | get 'problem_hof/:id', action: 'problem_hof' |
|
133 | 133 | get 'current_score' |
|
134 | 134 | get 'max_score' |
|
135 | 135 | post 'show_max_score' |
|
136 | 136 | end |
|
137 | 137 | #get 'report/current_score', to: 'report#current_score', as: 'report_current_score' |
|
138 | 138 | #get 'report/problem_hof(/:id)', to: 'report#problem_hof', as: 'report_problem_hof' |
|
139 | 139 | #get "report/login" |
|
140 | 140 | #get 'report/max_score', to: 'report#max_score', as: 'report_max_score' |
|
141 | 141 | #post 'report/show_max_score', to: 'report#show_max_score', as: 'report_show_max_score' |
|
142 | 142 | |
|
143 | 143 | resource :main, only: [], controller: 'main' do |
|
144 | 144 | get 'list' |
|
145 | 145 | get 'submission(/:id)', action: 'submission', as: 'main_submission' |
|
146 | 146 | post 'submit' |
|
147 | 147 | get 'announcements' |
|
148 | 148 | get 'help' |
|
149 | 149 | end |
|
150 | 150 | #main |
|
151 | 151 | #get "main/list" |
|
152 | 152 | #get 'main/submission(/:id)', to: 'main#submission', as: 'main_submission' |
|
153 | 153 | #post 'main/submit', to: 'main#submit' |
|
154 | 154 | #get 'main/announcements', to: 'main#announcements' |
|
155 | 155 | |
|
156 | 156 | |
|
157 | 157 | # |
|
158 | 158 | get 'tasks/view/:file.:ext' => 'tasks#view' |
|
159 | 159 | get 'tasks/download/:id/:file.:ext' => 'tasks#download' |
|
160 | 160 | get 'heartbeat/:id/edit' => 'heartbeat#edit' |
|
161 | 161 | |
|
162 | 162 | #grader |
|
163 | 163 | get 'graders/list', to: 'graders#list', as: 'grader_list' |
|
164 | 164 | |
|
165 | 165 | |
|
166 | 166 | # See how all your routes lay out with "rake routes" |
|
167 | 167 | |
|
168 | 168 | # This is a legacy wild controller route that's not recommended for RESTful applications. |
|
169 | 169 | # Note: This route will make all actions in every controller accessible via GET requests. |
|
170 | 170 | # match ':controller(/:action(/:id))(.:format)', via: [:get, :post] |
|
171 | 171 | end |
You need to be logged in to leave comments.
Login now