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