Description:
fixed form_tag bug in views
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r340:f512a445945b - - 2 files changed: 3 inserted, 3 deleted

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