Description:
fix user update
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r639:2826fdf23cce - - 2 files changed: 4 inserted, 4 deleted
@@ -1,555 +1,555 | |||||
|
1 | require 'csv' |
|
1 | require 'csv' |
|
2 |
|
2 | ||
|
3 | class UserAdminController < ApplicationController |
|
3 | class UserAdminController < ApplicationController |
|
4 |
|
4 | ||
|
5 | include MailHelperMethods |
|
5 | include MailHelperMethods |
|
6 |
|
6 | ||
|
7 | before_filter :admin_authorization |
|
7 | before_filter :admin_authorization |
|
8 |
|
8 | ||
|
9 | # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html) |
|
9 | # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html) |
|
10 | verify :method => :post, :only => [ |
|
10 | verify :method => :post, :only => [ |
|
11 | :create, :create_from_list, |
|
11 | :create, :create_from_list, |
|
12 | :update, |
|
12 | :update, |
|
13 | :manage_contest, |
|
13 | :manage_contest, |
|
14 | :bulk_mail |
|
14 | :bulk_mail |
|
15 | ], |
|
15 | ], |
|
16 | :redirect_to => { :action => :list } |
|
16 | :redirect_to => { :action => :list } |
|
17 |
|
17 | ||
|
18 | def index |
|
18 | def index |
|
19 | @user_count = User.count |
|
19 | @user_count = User.count |
|
20 | if params[:page] == 'all' |
|
20 | if params[:page] == 'all' |
|
21 | @users = User.all |
|
21 | @users = User.all |
|
22 | @paginated = false |
|
22 | @paginated = false |
|
23 | else |
|
23 | else |
|
24 | @users = User.paginate :page => params[:page] |
|
24 | @users = User.paginate :page => params[:page] |
|
25 | @paginated = true |
|
25 | @paginated = true |
|
26 | end |
|
26 | end |
|
27 | @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at'] |
|
27 | @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at'] |
|
28 | @contests = Contest.enabled |
|
28 | @contests = Contest.enabled |
|
29 | end |
|
29 | end |
|
30 |
|
30 | ||
|
31 | def active |
|
31 | def active |
|
32 | sessions = ActiveRecord::SessionStore::Session.where("updated_at >= ?", 60.minutes.ago) |
|
32 | sessions = ActiveRecord::SessionStore::Session.where("updated_at >= ?", 60.minutes.ago) |
|
33 | @users = [] |
|
33 | @users = [] |
|
34 | sessions.each do |session| |
|
34 | sessions.each do |session| |
|
35 | if session.data[:user_id] |
|
35 | if session.data[:user_id] |
|
36 | @users << User.find(session.data[:user_id]) |
|
36 | @users << User.find(session.data[:user_id]) |
|
37 | end |
|
37 | end |
|
38 | end |
|
38 | end |
|
39 | end |
|
39 | end |
|
40 |
|
40 | ||
|
41 | def show |
|
41 | def show |
|
42 | @user = User.find(params[:id]) |
|
42 | @user = User.find(params[:id]) |
|
43 | end |
|
43 | end |
|
44 |
|
44 | ||
|
45 | def new |
|
45 | def new |
|
46 | @user = User.new |
|
46 | @user = User.new |
|
47 | end |
|
47 | end |
|
48 |
|
48 | ||
|
49 | def create |
|
49 | def create |
|
50 | @user = User.new(user_params) |
|
50 | @user = User.new(user_params) |
|
51 | @user.activated = true |
|
51 | @user.activated = true |
|
52 | if @user.save |
|
52 | if @user.save |
|
53 | flash[:notice] = 'User was successfully created.' |
|
53 | flash[:notice] = 'User was successfully created.' |
|
54 | redirect_to :action => 'index' |
|
54 | redirect_to :action => 'index' |
|
55 | else |
|
55 | else |
|
56 | render :action => 'new' |
|
56 | render :action => 'new' |
|
57 | end |
|
57 | end |
|
58 | end |
|
58 | end |
|
59 |
|
59 | ||
|
60 | def clear_last_ip |
|
60 | def clear_last_ip |
|
61 | @user = User.find(params[:id]) |
|
61 | @user = User.find(params[:id]) |
|
62 | @user.last_ip = nil |
|
62 | @user.last_ip = nil |
|
63 | @user.save |
|
63 | @user.save |
|
64 | redirect_to action: 'index', page: params[:page] |
|
64 | redirect_to action: 'index', page: params[:page] |
|
65 | end |
|
65 | end |
|
66 |
|
66 | ||
|
67 | def create_from_list |
|
67 | def create_from_list |
|
68 | lines = params[:user_list] |
|
68 | lines = params[:user_list] |
|
69 |
|
69 | ||
|
70 | note = [] |
|
70 | note = [] |
|
71 |
|
71 | ||
|
72 | lines.split("\n").each do |line| |
|
72 | lines.split("\n").each do |line| |
|
73 | items = line.chomp.split(',') |
|
73 | items = line.chomp.split(',') |
|
74 | if items.length>=2 |
|
74 | if items.length>=2 |
|
75 | login = items[0] |
|
75 | login = items[0] |
|
76 | full_name = items[1] |
|
76 | full_name = items[1] |
|
77 | remark ='' |
|
77 | remark ='' |
|
78 | user_alias = '' |
|
78 | user_alias = '' |
|
79 |
|
79 | ||
|
80 | added_random_password = false |
|
80 | added_random_password = false |
|
81 | if items.length >= 3 and items[2].chomp(" ").length > 0; |
|
81 | if items.length >= 3 and items[2].chomp(" ").length > 0; |
|
82 | password = items[2].chomp(" ") |
|
82 | password = items[2].chomp(" ") |
|
83 | else |
|
83 | else |
|
84 | password = random_password |
|
84 | password = random_password |
|
85 | add_random_password=true; |
|
85 | add_random_password=true; |
|
86 | end |
|
86 | end |
|
87 |
|
87 | ||
|
88 | if items.length>= 4 and items[3].chomp(" ").length > 0; |
|
88 | if items.length>= 4 and items[3].chomp(" ").length > 0; |
|
89 | user_alias = items[3].chomp(" ") |
|
89 | user_alias = items[3].chomp(" ") |
|
90 | else |
|
90 | else |
|
91 | user_alias = login |
|
91 | user_alias = login |
|
92 | end |
|
92 | end |
|
93 |
|
93 | ||
|
94 | if items.length>=5 |
|
94 | if items.length>=5 |
|
95 | remark = items[4].strip; |
|
95 | remark = items[4].strip; |
|
96 | end |
|
96 | end |
|
97 |
|
97 | ||
|
98 | user = User.find_by_login(login) |
|
98 | user = User.find_by_login(login) |
|
99 | if (user) |
|
99 | if (user) |
|
100 | user.full_name = full_name |
|
100 | user.full_name = full_name |
|
101 | user.password = password |
|
101 | user.password = password |
|
102 | user.remark = remark |
|
102 | user.remark = remark |
|
103 | else |
|
103 | else |
|
104 | user = User.new({:login => login, |
|
104 | user = User.new({:login => login, |
|
105 | :full_name => full_name, |
|
105 | :full_name => full_name, |
|
106 | :password => password, |
|
106 | :password => password, |
|
107 | :password_confirmation => password, |
|
107 | :password_confirmation => password, |
|
108 | :alias => user_alias, |
|
108 | :alias => user_alias, |
|
109 | :remark => remark}) |
|
109 | :remark => remark}) |
|
110 | end |
|
110 | end |
|
111 | user.activated = true |
|
111 | user.activated = true |
|
112 | user.save |
|
112 | user.save |
|
113 |
|
113 | ||
|
114 | if added_random_password |
|
114 | if added_random_password |
|
115 | note << "'#{login}' (+)" |
|
115 | note << "'#{login}' (+)" |
|
116 | else |
|
116 | else |
|
117 | note << login |
|
117 | note << login |
|
118 | end |
|
118 | end |
|
119 | end |
|
119 | end |
|
120 | end |
|
120 | end |
|
121 | flash[:success] = 'User(s) ' + note.join(', ') + |
|
121 | flash[:success] = 'User(s) ' + note.join(', ') + |
|
122 | ' were successfully created. ' + |
|
122 | ' were successfully created. ' + |
|
123 | '( (+) - created with random passwords.)' |
|
123 | '( (+) - created with random passwords.)' |
|
124 | redirect_to :action => 'index' |
|
124 | redirect_to :action => 'index' |
|
125 | end |
|
125 | end |
|
126 |
|
126 | ||
|
127 | def edit |
|
127 | def edit |
|
128 | @user = User.find(params[:id]) |
|
128 | @user = User.find(params[:id]) |
|
129 | end |
|
129 | end |
|
130 |
|
130 | ||
|
131 | def update |
|
131 | def update |
|
132 | @user = User.find(params[:id]) |
|
132 | @user = User.find(params[:id]) |
|
133 | if @user.update_attributes(user_params) |
|
133 | if @user.update_attributes(user_params) |
|
134 | flash[:notice] = 'User was successfully updated.' |
|
134 | flash[:notice] = 'User was successfully updated.' |
|
135 | redirect_to :action => 'show', :id => @user |
|
135 | redirect_to :action => 'show', :id => @user |
|
136 | else |
|
136 | else |
|
137 | render :action => 'edit' |
|
137 | render :action => 'edit' |
|
138 | end |
|
138 | end |
|
139 | end |
|
139 | end |
|
140 |
|
140 | ||
|
141 | def destroy |
|
141 | def destroy |
|
142 | User.find(params[:id]).destroy |
|
142 | User.find(params[:id]).destroy |
|
143 | redirect_to :action => 'index' |
|
143 | redirect_to :action => 'index' |
|
144 | end |
|
144 | end |
|
145 |
|
145 | ||
|
146 | def user_stat |
|
146 | def user_stat |
|
147 | if params[:commit] == 'download csv' |
|
147 | if params[:commit] == 'download csv' |
|
148 | @problems = Problem.all |
|
148 | @problems = Problem.all |
|
149 | else |
|
149 | else |
|
150 | @problems = Problem.available_problems |
|
150 | @problems = Problem.available_problems |
|
151 | end |
|
151 | end |
|
152 | @users = User.includes(:contests, :contest_stat).where(enabled: true) |
|
152 | @users = User.includes(:contests, :contest_stat).where(enabled: true) |
|
153 | @scorearray = Array.new |
|
153 | @scorearray = Array.new |
|
154 | @users.each do |u| |
|
154 | @users.each do |u| |
|
155 | ustat = Array.new |
|
155 | ustat = Array.new |
|
156 | ustat[0] = u |
|
156 | ustat[0] = u |
|
157 | @problems.each do |p| |
|
157 | @problems.each do |p| |
|
158 | sub = Submission.find_last_by_user_and_problem(u.id,p.id) |
|
158 | sub = Submission.find_last_by_user_and_problem(u.id,p.id) |
|
159 | if (sub!=nil) and (sub.points!=nil) and p and p.full_score |
|
159 | if (sub!=nil) and (sub.points!=nil) and p and p.full_score |
|
160 | ustat << [(sub.points.to_f*100/p.full_score).round, (sub.points>=p.full_score)] |
|
160 | ustat << [(sub.points.to_f*100/p.full_score).round, (sub.points>=p.full_score)] |
|
161 | else |
|
161 | else |
|
162 | ustat << [0,false] |
|
162 | ustat << [0,false] |
|
163 | end |
|
163 | end |
|
164 | end |
|
164 | end |
|
165 | @scorearray << ustat |
|
165 | @scorearray << ustat |
|
166 | end |
|
166 | end |
|
167 | if params[:commit] == 'download csv' then |
|
167 | if params[:commit] == 'download csv' then |
|
168 | csv = gen_csv_from_scorearray(@scorearray,@problems) |
|
168 | csv = gen_csv_from_scorearray(@scorearray,@problems) |
|
169 | send_data csv, filename: 'last_score.csv' |
|
169 | send_data csv, filename: 'last_score.csv' |
|
170 | else |
|
170 | else |
|
171 | render template: 'user_admin/user_stat' |
|
171 | render template: 'user_admin/user_stat' |
|
172 | end |
|
172 | end |
|
173 | end |
|
173 | end |
|
174 |
|
174 | ||
|
175 | def user_stat_max |
|
175 | def user_stat_max |
|
176 | if params[:commit] == 'download csv' |
|
176 | if params[:commit] == 'download csv' |
|
177 | @problems = Problem.all |
|
177 | @problems = Problem.all |
|
178 | else |
|
178 | else |
|
179 | @problems = Problem.available_problems |
|
179 | @problems = Problem.available_problems |
|
180 | end |
|
180 | end |
|
181 | @users = User.includes(:contests).includes(:contest_stat).all |
|
181 | @users = User.includes(:contests).includes(:contest_stat).all |
|
182 | @scorearray = Array.new |
|
182 | @scorearray = Array.new |
|
183 | #set up range from param |
|
183 | #set up range from param |
|
184 | since_id = params.fetch(:since_id, 0).to_i |
|
184 | since_id = params.fetch(:since_id, 0).to_i |
|
185 | until_id = params.fetch(:until_id, 0).to_i |
|
185 | until_id = params.fetch(:until_id, 0).to_i |
|
186 | @users.each do |u| |
|
186 | @users.each do |u| |
|
187 | ustat = Array.new |
|
187 | ustat = Array.new |
|
188 | ustat[0] = u |
|
188 | ustat[0] = u |
|
189 | @problems.each do |p| |
|
189 | @problems.each do |p| |
|
190 | max_points = 0 |
|
190 | max_points = 0 |
|
191 | Submission.find_in_range_by_user_and_problem(u.id,p.id,since_id,until_id).each do |sub| |
|
191 | Submission.find_in_range_by_user_and_problem(u.id,p.id,since_id,until_id).each do |sub| |
|
192 | max_points = sub.points if sub and sub.points and (sub.points > max_points) |
|
192 | max_points = sub.points if sub and sub.points and (sub.points > max_points) |
|
193 | end |
|
193 | end |
|
194 | ustat << [(max_points.to_f*100/p.full_score).round, (max_points>=p.full_score)] |
|
194 | ustat << [(max_points.to_f*100/p.full_score).round, (max_points>=p.full_score)] |
|
195 | end |
|
195 | end |
|
196 | @scorearray << ustat |
|
196 | @scorearray << ustat |
|
197 | end |
|
197 | end |
|
198 |
|
198 | ||
|
199 | if params[:commit] == 'download csv' then |
|
199 | if params[:commit] == 'download csv' then |
|
200 | csv = gen_csv_from_scorearray(@scorearray,@problems) |
|
200 | csv = gen_csv_from_scorearray(@scorearray,@problems) |
|
201 | send_data csv, filename: 'max_score.csv' |
|
201 | send_data csv, filename: 'max_score.csv' |
|
202 | else |
|
202 | else |
|
203 | render template: 'user_admin/user_stat' |
|
203 | render template: 'user_admin/user_stat' |
|
204 | end |
|
204 | end |
|
205 | end |
|
205 | end |
|
206 |
|
206 | ||
|
207 | def import |
|
207 | def import |
|
208 | if params[:file]=='' |
|
208 | if params[:file]=='' |
|
209 | flash[:notice] = 'Error importing no file' |
|
209 | flash[:notice] = 'Error importing no file' |
|
210 | redirect_to :action => 'index' and return |
|
210 | redirect_to :action => 'index' and return |
|
211 | end |
|
211 | end |
|
212 | import_from_file(params[:file]) |
|
212 | import_from_file(params[:file]) |
|
213 | end |
|
213 | end |
|
214 |
|
214 | ||
|
215 | def random_all_passwords |
|
215 | def random_all_passwords |
|
216 | users = User.all |
|
216 | users = User.all |
|
217 | @prefix = params[:prefix] || '' |
|
217 | @prefix = params[:prefix] || '' |
|
218 | @non_admin_users = User.find_non_admin_with_prefix(@prefix) |
|
218 | @non_admin_users = User.find_non_admin_with_prefix(@prefix) |
|
219 | @changed = false |
|
219 | @changed = false |
|
220 | if request.request_method == 'POST' |
|
220 | if request.request_method == 'POST' |
|
221 | @non_admin_users.each do |user| |
|
221 | @non_admin_users.each do |user| |
|
222 | password = random_password |
|
222 | password = random_password |
|
223 | user.password = password |
|
223 | user.password = password |
|
224 | user.password_confirmation = password |
|
224 | user.password_confirmation = password |
|
225 | user.save |
|
225 | user.save |
|
226 | end |
|
226 | end |
|
227 | @changed = true |
|
227 | @changed = true |
|
228 | end |
|
228 | end |
|
229 | end |
|
229 | end |
|
230 |
|
230 | ||
|
231 | # contest management |
|
231 | # contest management |
|
232 |
|
232 | ||
|
233 | def contests |
|
233 | def contests |
|
234 | @contest, @users = find_contest_and_user_from_contest_id(params[:id]) |
|
234 | @contest, @users = find_contest_and_user_from_contest_id(params[:id]) |
|
235 | @contests = Contest.enabled |
|
235 | @contests = Contest.enabled |
|
236 | end |
|
236 | end |
|
237 |
|
237 | ||
|
238 | def assign_from_list |
|
238 | def assign_from_list |
|
239 | contest_id = params[:users_contest_id] |
|
239 | contest_id = params[:users_contest_id] |
|
240 | org_contest, users = find_contest_and_user_from_contest_id(contest_id) |
|
240 | org_contest, users = find_contest_and_user_from_contest_id(contest_id) |
|
241 | contest = Contest.find(params[:new_contest][:id]) |
|
241 | contest = Contest.find(params[:new_contest][:id]) |
|
242 | if !contest |
|
242 | if !contest |
|
243 | flash[:notice] = 'Error: no contest' |
|
243 | flash[:notice] = 'Error: no contest' |
|
244 | redirect_to :action => 'contests', :id =>contest_id |
|
244 | redirect_to :action => 'contests', :id =>contest_id |
|
245 | end |
|
245 | end |
|
246 |
|
246 | ||
|
247 | note = [] |
|
247 | note = [] |
|
248 | users.each do |u| |
|
248 | users.each do |u| |
|
249 | u.contests = [contest] |
|
249 | u.contests = [contest] |
|
250 | note << u.login |
|
250 | note << u.login |
|
251 | end |
|
251 | end |
|
252 | flash[:notice] = 'User(s) ' + note.join(', ') + |
|
252 | flash[:notice] = 'User(s) ' + note.join(', ') + |
|
253 | " were successfully reassigned to #{contest.title}." |
|
253 | " were successfully reassigned to #{contest.title}." |
|
254 | redirect_to :action => 'contests', :id =>contest.id |
|
254 | redirect_to :action => 'contests', :id =>contest.id |
|
255 | end |
|
255 | end |
|
256 |
|
256 | ||
|
257 | def add_to_contest |
|
257 | def add_to_contest |
|
258 | user = User.find(params[:id]) |
|
258 | user = User.find(params[:id]) |
|
259 | contest = Contest.find(params[:contest_id]) |
|
259 | contest = Contest.find(params[:contest_id]) |
|
260 | if user and contest |
|
260 | if user and contest |
|
261 | user.contests << contest |
|
261 | user.contests << contest |
|
262 | end |
|
262 | end |
|
263 | redirect_to :action => 'index' |
|
263 | redirect_to :action => 'index' |
|
264 | end |
|
264 | end |
|
265 |
|
265 | ||
|
266 | def remove_from_contest |
|
266 | def remove_from_contest |
|
267 | user = User.find(params[:id]) |
|
267 | user = User.find(params[:id]) |
|
268 | contest = Contest.find(params[:contest_id]) |
|
268 | contest = Contest.find(params[:contest_id]) |
|
269 | if user and contest |
|
269 | if user and contest |
|
270 | user.contests.delete(contest) |
|
270 | user.contests.delete(contest) |
|
271 | end |
|
271 | end |
|
272 | redirect_to :action => 'index' |
|
272 | redirect_to :action => 'index' |
|
273 | end |
|
273 | end |
|
274 |
|
274 | ||
|
275 | def contest_management |
|
275 | def contest_management |
|
276 | end |
|
276 | end |
|
277 |
|
277 | ||
|
278 | def manage_contest |
|
278 | def manage_contest |
|
279 | contest = Contest.find(params[:contest][:id]) |
|
279 | contest = Contest.find(params[:contest][:id]) |
|
280 | if !contest |
|
280 | if !contest |
|
281 | flash[:notice] = 'You did not choose the contest.' |
|
281 | flash[:notice] = 'You did not choose the contest.' |
|
282 | redirect_to :action => 'contest_management' and return |
|
282 | redirect_to :action => 'contest_management' and return |
|
283 | end |
|
283 | end |
|
284 |
|
284 | ||
|
285 | operation = params[:operation] |
|
285 | operation = params[:operation] |
|
286 |
|
286 | ||
|
287 | if not ['add','remove','assign'].include? operation |
|
287 | if not ['add','remove','assign'].include? operation |
|
288 | flash[:notice] = 'You did not choose the operation to perform.' |
|
288 | flash[:notice] = 'You did not choose the operation to perform.' |
|
289 | redirect_to :action => 'contest_management' and return |
|
289 | redirect_to :action => 'contest_management' and return |
|
290 | end |
|
290 | end |
|
291 |
|
291 | ||
|
292 | lines = params[:login_list] |
|
292 | lines = params[:login_list] |
|
293 | if !lines or lines.blank? |
|
293 | if !lines or lines.blank? |
|
294 | flash[:notice] = 'You entered an empty list.' |
|
294 | flash[:notice] = 'You entered an empty list.' |
|
295 | redirect_to :action => 'contest_management' and return |
|
295 | redirect_to :action => 'contest_management' and return |
|
296 | end |
|
296 | end |
|
297 |
|
297 | ||
|
298 | note = [] |
|
298 | note = [] |
|
299 | users = [] |
|
299 | users = [] |
|
300 | lines.split("\n").each do |line| |
|
300 | lines.split("\n").each do |line| |
|
301 | user = User.find_by_login(line.chomp) |
|
301 | user = User.find_by_login(line.chomp) |
|
302 | if user |
|
302 | if user |
|
303 | if operation=='add' |
|
303 | if operation=='add' |
|
304 | if ! user.contests.include? contest |
|
304 | if ! user.contests.include? contest |
|
305 | user.contests << contest |
|
305 | user.contests << contest |
|
306 | end |
|
306 | end |
|
307 | elsif operation=='remove' |
|
307 | elsif operation=='remove' |
|
308 | user.contests.delete(contest) |
|
308 | user.contests.delete(contest) |
|
309 | else |
|
309 | else |
|
310 | user.contests = [contest] |
|
310 | user.contests = [contest] |
|
311 | end |
|
311 | end |
|
312 |
|
312 | ||
|
313 | if params[:reset_timer] |
|
313 | if params[:reset_timer] |
|
314 | user.contest_stat.forced_logout = true |
|
314 | user.contest_stat.forced_logout = true |
|
315 | user.contest_stat.reset_timer_and_save |
|
315 | user.contest_stat.reset_timer_and_save |
|
316 | end |
|
316 | end |
|
317 |
|
317 | ||
|
318 | if params[:notification_emails] |
|
318 | if params[:notification_emails] |
|
319 | send_contest_update_notification_email(user, contest) |
|
319 | send_contest_update_notification_email(user, contest) |
|
320 | end |
|
320 | end |
|
321 |
|
321 | ||
|
322 | note << user.login |
|
322 | note << user.login |
|
323 | users << user |
|
323 | users << user |
|
324 | end |
|
324 | end |
|
325 | end |
|
325 | end |
|
326 |
|
326 | ||
|
327 | if params[:reset_timer] |
|
327 | if params[:reset_timer] |
|
328 | logout_users(users) |
|
328 | logout_users(users) |
|
329 | end |
|
329 | end |
|
330 |
|
330 | ||
|
331 | flash[:notice] = 'User(s) ' + note.join(', ') + |
|
331 | flash[:notice] = 'User(s) ' + note.join(', ') + |
|
332 | ' were successfully modified. ' |
|
332 | ' were successfully modified. ' |
|
333 | redirect_to :action => 'contest_management' |
|
333 | redirect_to :action => 'contest_management' |
|
334 | end |
|
334 | end |
|
335 |
|
335 | ||
|
336 | # admin management |
|
336 | # admin management |
|
337 |
|
337 | ||
|
338 | def admin |
|
338 | def admin |
|
339 | @admins = User.all.find_all {|user| user.admin? } |
|
339 | @admins = User.all.find_all {|user| user.admin? } |
|
340 | end |
|
340 | end |
|
341 |
|
341 | ||
|
342 | def grant_admin |
|
342 | def grant_admin |
|
343 | login = params[:login] |
|
343 | login = params[:login] |
|
344 | user = User.find_by_login(login) |
|
344 | user = User.find_by_login(login) |
|
345 | if user!=nil |
|
345 | if user!=nil |
|
346 | admin_role = Role.find_by_name('admin') |
|
346 | admin_role = Role.find_by_name('admin') |
|
347 | user.roles << admin_role |
|
347 | user.roles << admin_role |
|
348 | else |
|
348 | else |
|
349 | flash[:notice] = 'Unknown user' |
|
349 | flash[:notice] = 'Unknown user' |
|
350 | end |
|
350 | end |
|
351 | flash[:notice] = 'User added as admins' |
|
351 | flash[:notice] = 'User added as admins' |
|
352 | redirect_to :action => 'admin' |
|
352 | redirect_to :action => 'admin' |
|
353 | end |
|
353 | end |
|
354 |
|
354 | ||
|
355 | def revoke_admin |
|
355 | def revoke_admin |
|
356 | user = User.find(params[:id]) |
|
356 | user = User.find(params[:id]) |
|
357 | if user==nil |
|
357 | if user==nil |
|
358 | flash[:notice] = 'Unknown user' |
|
358 | flash[:notice] = 'Unknown user' |
|
359 | redirect_to :action => 'admin' and return |
|
359 | redirect_to :action => 'admin' and return |
|
360 | elsif user.login == 'root' |
|
360 | elsif user.login == 'root' |
|
361 | flash[:notice] = 'You cannot revoke admisnistrator permission from root.' |
|
361 | flash[:notice] = 'You cannot revoke admisnistrator permission from root.' |
|
362 | redirect_to :action => 'admin' and return |
|
362 | redirect_to :action => 'admin' and return |
|
363 | end |
|
363 | end |
|
364 |
|
364 | ||
|
365 | admin_role = Role.find_by_name('admin') |
|
365 | admin_role = Role.find_by_name('admin') |
|
366 | user.roles.delete(admin_role) |
|
366 | user.roles.delete(admin_role) |
|
367 | flash[:notice] = 'User permission revoked' |
|
367 | flash[:notice] = 'User permission revoked' |
|
368 | redirect_to :action => 'admin' |
|
368 | redirect_to :action => 'admin' |
|
369 | end |
|
369 | end |
|
370 |
|
370 | ||
|
371 | # mass mailing |
|
371 | # mass mailing |
|
372 |
|
372 | ||
|
373 | def mass_mailing |
|
373 | def mass_mailing |
|
374 | end |
|
374 | end |
|
375 |
|
375 | ||
|
376 | def bulk_mail |
|
376 | def bulk_mail |
|
377 | lines = params[:login_list] |
|
377 | lines = params[:login_list] |
|
378 | if !lines or lines.blank? |
|
378 | if !lines or lines.blank? |
|
379 | flash[:notice] = 'You entered an empty list.' |
|
379 | flash[:notice] = 'You entered an empty list.' |
|
380 | redirect_to :action => 'mass_mailing' and return |
|
380 | redirect_to :action => 'mass_mailing' and return |
|
381 | end |
|
381 | end |
|
382 |
|
382 | ||
|
383 | mail_subject = params[:subject] |
|
383 | mail_subject = params[:subject] |
|
384 | if !mail_subject or mail_subject.blank? |
|
384 | if !mail_subject or mail_subject.blank? |
|
385 | flash[:notice] = 'You entered an empty mail subject.' |
|
385 | flash[:notice] = 'You entered an empty mail subject.' |
|
386 | redirect_to :action => 'mass_mailing' and return |
|
386 | redirect_to :action => 'mass_mailing' and return |
|
387 | end |
|
387 | end |
|
388 |
|
388 | ||
|
389 | mail_body = params[:email_body] |
|
389 | mail_body = params[:email_body] |
|
390 | if !mail_body or mail_body.blank? |
|
390 | if !mail_body or mail_body.blank? |
|
391 | flash[:notice] = 'You entered an empty mail body.' |
|
391 | flash[:notice] = 'You entered an empty mail body.' |
|
392 | redirect_to :action => 'mass_mailing' and return |
|
392 | redirect_to :action => 'mass_mailing' and return |
|
393 | end |
|
393 | end |
|
394 |
|
394 | ||
|
395 | note = [] |
|
395 | note = [] |
|
396 | users = [] |
|
396 | users = [] |
|
397 | lines.split("\n").each do |line| |
|
397 | lines.split("\n").each do |line| |
|
398 | user = User.find_by_login(line.chomp) |
|
398 | user = User.find_by_login(line.chomp) |
|
399 | if user |
|
399 | if user |
|
400 | send_mail(user.email, mail_subject, mail_body) |
|
400 | send_mail(user.email, mail_subject, mail_body) |
|
401 | note << user.login |
|
401 | note << user.login |
|
402 | end |
|
402 | end |
|
403 | end |
|
403 | end |
|
404 |
|
404 | ||
|
405 | flash[:notice] = 'User(s) ' + note.join(', ') + |
|
405 | flash[:notice] = 'User(s) ' + note.join(', ') + |
|
406 | ' were successfully modified. ' |
|
406 | ' were successfully modified. ' |
|
407 | redirect_to :action => 'mass_mailing' |
|
407 | redirect_to :action => 'mass_mailing' |
|
408 | end |
|
408 | end |
|
409 |
|
409 | ||
|
410 | protected |
|
410 | protected |
|
411 |
|
411 | ||
|
412 | def random_password(length=5) |
|
412 | def random_password(length=5) |
|
413 | chars = 'abcdefghijkmnopqrstuvwxyz23456789' |
|
413 | chars = 'abcdefghijkmnopqrstuvwxyz23456789' |
|
414 | newpass = "" |
|
414 | newpass = "" |
|
415 | length.times { newpass << chars[rand(chars.size-1)] } |
|
415 | length.times { newpass << chars[rand(chars.size-1)] } |
|
416 | return newpass |
|
416 | return newpass |
|
417 | end |
|
417 | end |
|
418 |
|
418 | ||
|
419 | def import_from_file(f) |
|
419 | def import_from_file(f) |
|
420 | data_hash = YAML.load(f) |
|
420 | data_hash = YAML.load(f) |
|
421 | @import_log = "" |
|
421 | @import_log = "" |
|
422 |
|
422 | ||
|
423 | country_data = data_hash[:countries] |
|
423 | country_data = data_hash[:countries] |
|
424 | site_data = data_hash[:sites] |
|
424 | site_data = data_hash[:sites] |
|
425 | user_data = data_hash[:users] |
|
425 | user_data = data_hash[:users] |
|
426 |
|
426 | ||
|
427 | # import country |
|
427 | # import country |
|
428 | countries = {} |
|
428 | countries = {} |
|
429 | country_data.each_pair do |id,country| |
|
429 | country_data.each_pair do |id,country| |
|
430 | c = Country.find_by_name(country[:name]) |
|
430 | c = Country.find_by_name(country[:name]) |
|
431 | if c!=nil |
|
431 | if c!=nil |
|
432 | countries[id] = c |
|
432 | countries[id] = c |
|
433 | @import_log << "Found #{country[:name]}\n" |
|
433 | @import_log << "Found #{country[:name]}\n" |
|
434 | else |
|
434 | else |
|
435 | countries[id] = Country.new(:name => country[:name]) |
|
435 | countries[id] = Country.new(:name => country[:name]) |
|
436 | countries[id].save |
|
436 | countries[id].save |
|
437 | @import_log << "Created #{country[:name]}\n" |
|
437 | @import_log << "Created #{country[:name]}\n" |
|
438 | end |
|
438 | end |
|
439 | end |
|
439 | end |
|
440 |
|
440 | ||
|
441 | # import sites |
|
441 | # import sites |
|
442 | sites = {} |
|
442 | sites = {} |
|
443 | site_data.each_pair do |id,site| |
|
443 | site_data.each_pair do |id,site| |
|
444 | s = Site.find_by_name(site[:name]) |
|
444 | s = Site.find_by_name(site[:name]) |
|
445 | if s!=nil |
|
445 | if s!=nil |
|
446 | @import_log << "Found #{site[:name]}\n" |
|
446 | @import_log << "Found #{site[:name]}\n" |
|
447 | else |
|
447 | else |
|
448 | s = Site.new(:name => site[:name]) |
|
448 | s = Site.new(:name => site[:name]) |
|
449 | @import_log << "Created #{site[:name]}\n" |
|
449 | @import_log << "Created #{site[:name]}\n" |
|
450 | end |
|
450 | end |
|
451 | s.password = site[:password] |
|
451 | s.password = site[:password] |
|
452 | s.country = countries[site[:country_id]] |
|
452 | s.country = countries[site[:country_id]] |
|
453 | s.save |
|
453 | s.save |
|
454 | sites[id] = s |
|
454 | sites[id] = s |
|
455 | end |
|
455 | end |
|
456 |
|
456 | ||
|
457 | # import users |
|
457 | # import users |
|
458 | user_data.each_pair do |id,user| |
|
458 | user_data.each_pair do |id,user| |
|
459 | u = User.find_by_login(user[:login]) |
|
459 | u = User.find_by_login(user[:login]) |
|
460 | if u!=nil |
|
460 | if u!=nil |
|
461 | @import_log << "Found #{user[:login]}\n" |
|
461 | @import_log << "Found #{user[:login]}\n" |
|
462 | else |
|
462 | else |
|
463 | u = User.new(:login => user[:login]) |
|
463 | u = User.new(:login => user[:login]) |
|
464 | @import_log << "Created #{user[:login]}\n" |
|
464 | @import_log << "Created #{user[:login]}\n" |
|
465 | end |
|
465 | end |
|
466 | u.full_name = user[:name] |
|
466 | u.full_name = user[:name] |
|
467 | u.password = user[:password] |
|
467 | u.password = user[:password] |
|
468 | u.country = countries[user[:country_id]] |
|
468 | u.country = countries[user[:country_id]] |
|
469 | u.site = sites[user[:site_id]] |
|
469 | u.site = sites[user[:site_id]] |
|
470 | u.activated = true |
|
470 | u.activated = true |
|
471 | u.email = "empty-#{u.login}@none.com" |
|
471 | u.email = "empty-#{u.login}@none.com" |
|
472 | if not u.save |
|
472 | if not u.save |
|
473 | @import_log << "Errors\n" |
|
473 | @import_log << "Errors\n" |
|
474 | u.errors.each { |attr,msg| @import_log << "#{attr} - #{msg}\n" } |
|
474 | u.errors.each { |attr,msg| @import_log << "#{attr} - #{msg}\n" } |
|
475 | end |
|
475 | end |
|
476 | end |
|
476 | end |
|
477 |
|
477 | ||
|
478 | end |
|
478 | end |
|
479 |
|
479 | ||
|
480 | def logout_users(users) |
|
480 | def logout_users(users) |
|
481 | users.each do |user| |
|
481 | users.each do |user| |
|
482 | contest_stat = user.contest_stat(true) |
|
482 | contest_stat = user.contest_stat(true) |
|
483 | if contest_stat and !contest_stat.forced_logout |
|
483 | if contest_stat and !contest_stat.forced_logout |
|
484 | contest_stat.forced_logout = true |
|
484 | contest_stat.forced_logout = true |
|
485 | contest_stat.save |
|
485 | contest_stat.save |
|
486 | end |
|
486 | end |
|
487 | end |
|
487 | end |
|
488 | end |
|
488 | end |
|
489 |
|
489 | ||
|
490 | def send_contest_update_notification_email(user, contest) |
|
490 | def send_contest_update_notification_email(user, contest) |
|
491 | contest_title_name = GraderConfiguration['contest.name'] |
|
491 | contest_title_name = GraderConfiguration['contest.name'] |
|
492 | contest_name = contest.name |
|
492 | contest_name = contest.name |
|
493 | mail_subject = t('contest.notification.email_subject', { |
|
493 | mail_subject = t('contest.notification.email_subject', { |
|
494 | :contest_title_name => contest_title_name, |
|
494 | :contest_title_name => contest_title_name, |
|
495 | :contest_name => contest_name }) |
|
495 | :contest_name => contest_name }) |
|
496 | mail_body = t('contest.notification.email_body', { |
|
496 | mail_body = t('contest.notification.email_body', { |
|
497 | :full_name => user.full_name, |
|
497 | :full_name => user.full_name, |
|
498 | :contest_title_name => contest_title_name, |
|
498 | :contest_title_name => contest_title_name, |
|
499 | :contest_name => contest.name, |
|
499 | :contest_name => contest.name, |
|
500 | }) |
|
500 | }) |
|
501 |
|
501 | ||
|
502 | logger.info mail_body |
|
502 | logger.info mail_body |
|
503 | send_mail(user.email, mail_subject, mail_body) |
|
503 | send_mail(user.email, mail_subject, mail_body) |
|
504 | end |
|
504 | end |
|
505 |
|
505 | ||
|
506 | def find_contest_and_user_from_contest_id(id) |
|
506 | def find_contest_and_user_from_contest_id(id) |
|
507 | if id!='none' |
|
507 | if id!='none' |
|
508 | @contest = Contest.find(id) |
|
508 | @contest = Contest.find(id) |
|
509 | else |
|
509 | else |
|
510 | @contest = nil |
|
510 | @contest = nil |
|
511 | end |
|
511 | end |
|
512 | if @contest |
|
512 | if @contest |
|
513 | @users = @contest.users |
|
513 | @users = @contest.users |
|
514 | else |
|
514 | else |
|
515 | @users = User.find_users_with_no_contest |
|
515 | @users = User.find_users_with_no_contest |
|
516 | end |
|
516 | end |
|
517 | return [@contest, @users] |
|
517 | return [@contest, @users] |
|
518 | end |
|
518 | end |
|
519 |
|
519 | ||
|
520 | def gen_csv_from_scorearray(scorearray,problem) |
|
520 | def gen_csv_from_scorearray(scorearray,problem) |
|
521 | CSV.generate do |csv| |
|
521 | CSV.generate do |csv| |
|
522 | #add header |
|
522 | #add header |
|
523 | header = ['User','Name', 'Activated?', 'Logged in', 'Contest'] |
|
523 | header = ['User','Name', 'Activated?', 'Logged in', 'Contest'] |
|
524 | problem.each { |p| header << p.name } |
|
524 | problem.each { |p| header << p.name } |
|
525 | header += ['Total','Passed'] |
|
525 | header += ['Total','Passed'] |
|
526 | csv << header |
|
526 | csv << header |
|
527 | #add data |
|
527 | #add data |
|
528 | scorearray.each do |sc| |
|
528 | scorearray.each do |sc| |
|
529 | total = num_passed = 0 |
|
529 | total = num_passed = 0 |
|
530 | row = Array.new |
|
530 | row = Array.new |
|
531 | sc.each_index do |i| |
|
531 | sc.each_index do |i| |
|
532 | if i == 0 |
|
532 | if i == 0 |
|
533 | row << sc[i].login |
|
533 | row << sc[i].login |
|
534 | row << sc[i].full_name |
|
534 | row << sc[i].full_name |
|
535 | row << sc[i].activated |
|
535 | row << sc[i].activated |
|
536 | row << (sc[i].try(:contest_stat).try(:started_at).nil? ? 'no' : 'yes') |
|
536 | row << (sc[i].try(:contest_stat).try(:started_at).nil? ? 'no' : 'yes') |
|
537 | row << sc[i].contests.collect {|c| c.name}.join(', ') |
|
537 | row << sc[i].contests.collect {|c| c.name}.join(', ') |
|
538 | else |
|
538 | else |
|
539 | row << sc[i][0] |
|
539 | row << sc[i][0] |
|
540 | total += sc[i][0] |
|
540 | total += sc[i][0] |
|
541 | num_passed += 1 if sc[i][1] |
|
541 | num_passed += 1 if sc[i][1] |
|
542 | end |
|
542 | end |
|
543 | end |
|
543 | end |
|
544 | row << total |
|
544 | row << total |
|
545 | row << num_passed |
|
545 | row << num_passed |
|
546 | csv << row |
|
546 | csv << row |
|
547 | end |
|
547 | end |
|
548 | end |
|
548 | end |
|
549 | end |
|
549 | end |
|
550 |
|
550 | ||
|
551 | private |
|
551 | private |
|
552 | def user_params |
|
552 | def user_params |
|
553 | - params.require(:user).permit(:login,:full_name,:hashed_password,:salt,:alias,:email,:site_id,:country_id,:activated,:enabled,:remark,:last_ip,:section) |
|
553 | + params.require(:user).permit(:login,:password,:password_confirmation,:email, :alias, :full_name,:remark) |
|
554 | end |
|
554 | end |
|
555 | end |
|
555 | end |
@@ -1,50 +1,50 | |||||
|
1 | require 'test_helper' |
|
1 | require 'test_helper' |
|
2 |
|
2 | ||
|
3 | class AnnouncementsControllerTest < ActionController::TestCase |
|
3 | class AnnouncementsControllerTest < ActionController::TestCase |
|
4 | setup do |
|
4 | setup do |
|
5 | @announcement = announcements(:one) |
|
5 | @announcement = announcements(:one) |
|
6 |
- @request.session[:user_id] = user |
|
6 | + @request.session[:user_id] = user(:admin).id |
|
7 | end |
|
7 | end |
|
8 |
|
8 | ||
|
9 | test "should get index" do |
|
9 | test "should get index" do |
|
10 | get :index |
|
10 | get :index |
|
11 | assert_response :success |
|
11 | assert_response :success |
|
12 | assert_not_nil assigns(:announcements) |
|
12 | assert_not_nil assigns(:announcements) |
|
13 | end |
|
13 | end |
|
14 |
|
14 | ||
|
15 | test "should get new" do |
|
15 | test "should get new" do |
|
16 | get :new |
|
16 | get :new |
|
17 | assert_response :success |
|
17 | assert_response :success |
|
18 | end |
|
18 | end |
|
19 |
|
19 | ||
|
20 | test "should create announcement" do |
|
20 | test "should create announcement" do |
|
21 | assert_difference('Announcement.count') do |
|
21 | assert_difference('Announcement.count') do |
|
22 |
- post :create, announcement: { |
|
22 | + post :create, announcement: { } |
|
23 | end |
|
23 | end |
|
24 |
|
24 | ||
|
25 | assert_redirected_to announcement_path(assigns(:announcement)) |
|
25 | assert_redirected_to announcement_path(assigns(:announcement)) |
|
26 | end |
|
26 | end |
|
27 |
|
27 | ||
|
28 | test "should show announcement" do |
|
28 | test "should show announcement" do |
|
29 | get :show, id: @announcement |
|
29 | get :show, id: @announcement |
|
30 | assert_response :success |
|
30 | assert_response :success |
|
31 | end |
|
31 | end |
|
32 |
|
32 | ||
|
33 | test "should get edit" do |
|
33 | test "should get edit" do |
|
34 | get :edit, id: @announcement |
|
34 | get :edit, id: @announcement |
|
35 | assert_response :success |
|
35 | assert_response :success |
|
36 | end |
|
36 | end |
|
37 |
|
37 | ||
|
38 | test "should update announcement" do |
|
38 | test "should update announcement" do |
|
39 |
- patch :update, id: @announcement, announcement: { |
|
39 | + patch :update, id: @announcement, announcement: { } |
|
40 | assert_redirected_to announcement_path(assigns(:announcement)) |
|
40 | assert_redirected_to announcement_path(assigns(:announcement)) |
|
41 | end |
|
41 | end |
|
42 |
|
42 | ||
|
43 | test "should destroy announcement" do |
|
43 | test "should destroy announcement" do |
|
44 | assert_difference('Announcement.count', -1) do |
|
44 | assert_difference('Announcement.count', -1) do |
|
45 | delete :destroy, id: @announcement |
|
45 | delete :destroy, id: @announcement |
|
46 | end |
|
46 | end |
|
47 |
|
47 | ||
|
48 | assert_redirected_to announcements_path |
|
48 | assert_redirected_to announcements_path |
|
49 | end |
|
49 | end |
|
50 | end |
|
50 | end |
You need to be logged in to leave comments.
Login now