Description:
+ new list of user with remark
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r633:49e05926711f - - 1 file changed: 17 inserted, 5 deleted
@@ -1,289 +1,301 | |||||
|
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(params[:user]) |
|
50 | @user = User.new(params[:user]) |
|
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 ='' | ||
|
|
78 | + user_alias = '' | ||
|
77 |
|
79 | ||
|
78 | added_random_password = false |
|
80 | added_random_password = false |
|
79 | - if items.length>=3 |
|
81 | + if items.length >= 3 and items[2].chomp(" ").length > 0; |
|
80 | password = items[2].chomp(" ") |
|
82 | password = items[2].chomp(" ") |
|
81 | - user_alias = (items.length>=4) ? items[3] : login |
|
||
|
82 | else |
|
83 | else |
|
83 | password = random_password |
|
84 | password = random_password |
|
84 | - user_alias = (items.length>=4) ? items[3] : login |
|
85 | + add_random_password=true; |
|
85 | - added_random_password = true |
|
86 | + end |
|
|
87 | + | ||
|
|
88 | + if items.length>= 4 and items[3].chomp(" ").length > 0; | ||
|
|
89 | + user_alias = items[3].chomp(" ") | ||
|
|
90 | + else | ||
|
|
91 | + user_alias = login | ||
|
|
92 | + end | ||
|
|
93 | + | ||
|
|
94 | + if items.length>=5 | ||
|
|
95 | + remark = items[4].strip; | ||
|
86 | end |
|
96 | end |
|
87 |
|
97 | ||
|
88 | user = User.find_by_login(login) |
|
98 | user = User.find_by_login(login) |
|
89 | if (user) |
|
99 | if (user) |
|
90 | user.full_name = full_name |
|
100 | user.full_name = full_name |
|
91 | user.password = password |
|
101 | user.password = password |
|
|
102 | + user.remark = remark | ||
|
92 | else |
|
103 | else |
|
93 | user = User.new({:login => login, |
|
104 | user = User.new({:login => login, |
|
94 | :full_name => full_name, |
|
105 | :full_name => full_name, |
|
95 | :password => password, |
|
106 | :password => password, |
|
96 | :password_confirmation => password, |
|
107 | :password_confirmation => password, |
|
97 |
- :alias => user_alias |
|
108 | + :alias => user_alias, |
|
|
109 | + :remark => remark}) | ||
|
98 | end |
|
110 | end |
|
99 | user.activated = true |
|
111 | user.activated = true |
|
100 | user.save |
|
112 | user.save |
|
101 |
|
113 | ||
|
102 | if added_random_password |
|
114 | if added_random_password |
|
103 | note << "'#{login}' (+)" |
|
115 | note << "'#{login}' (+)" |
|
104 | else |
|
116 | else |
|
105 | note << login |
|
117 | note << login |
|
106 | end |
|
118 | end |
|
107 | end |
|
119 | end |
|
108 | end |
|
120 | end |
|
109 | flash[:notice] = 'User(s) ' + note.join(', ') + |
|
121 | flash[:notice] = 'User(s) ' + note.join(', ') + |
|
110 | ' were successfully created. ' + |
|
122 | ' were successfully created. ' + |
|
111 | '( (+) - created with random passwords.)' |
|
123 | '( (+) - created with random passwords.)' |
|
112 | redirect_to :action => 'index' |
|
124 | redirect_to :action => 'index' |
|
113 | end |
|
125 | end |
|
114 |
|
126 | ||
|
115 | def edit |
|
127 | def edit |
|
116 | @user = User.find(params[:id]) |
|
128 | @user = User.find(params[:id]) |
|
117 | end |
|
129 | end |
|
118 |
|
130 | ||
|
119 | def update |
|
131 | def update |
|
120 | @user = User.find(params[:id]) |
|
132 | @user = User.find(params[:id]) |
|
121 | if @user.update_attributes(user_params) |
|
133 | if @user.update_attributes(user_params) |
|
122 | flash[:notice] = 'User was successfully updated.' |
|
134 | flash[:notice] = 'User was successfully updated.' |
|
123 | redirect_to :action => 'show', :id => @user |
|
135 | redirect_to :action => 'show', :id => @user |
|
124 | else |
|
136 | else |
|
125 | render :action => 'edit' |
|
137 | render :action => 'edit' |
|
126 | end |
|
138 | end |
|
127 | end |
|
139 | end |
|
128 |
|
140 | ||
|
129 | def destroy |
|
141 | def destroy |
|
130 | User.find(params[:id]).destroy |
|
142 | User.find(params[:id]).destroy |
|
131 | redirect_to :action => 'index' |
|
143 | redirect_to :action => 'index' |
|
132 | end |
|
144 | end |
|
133 |
|
145 | ||
|
134 | def user_stat |
|
146 | def user_stat |
|
135 | if params[:commit] == 'download csv' |
|
147 | if params[:commit] == 'download csv' |
|
136 | @problems = Problem.all |
|
148 | @problems = Problem.all |
|
137 | else |
|
149 | else |
|
138 | @problems = Problem.available_problems |
|
150 | @problems = Problem.available_problems |
|
139 | end |
|
151 | end |
|
140 | @users = User.includes(:contests, :contest_stat).where(enabled: true) |
|
152 | @users = User.includes(:contests, :contest_stat).where(enabled: true) |
|
141 | @scorearray = Array.new |
|
153 | @scorearray = Array.new |
|
142 | @users.each do |u| |
|
154 | @users.each do |u| |
|
143 | ustat = Array.new |
|
155 | ustat = Array.new |
|
144 | ustat[0] = u |
|
156 | ustat[0] = u |
|
145 | @problems.each do |p| |
|
157 | @problems.each do |p| |
|
146 | 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) |
|
147 | 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 |
|
148 | 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)] |
|
149 | else |
|
161 | else |
|
150 | ustat << [0,false] |
|
162 | ustat << [0,false] |
|
151 | end |
|
163 | end |
|
152 | end |
|
164 | end |
|
153 | @scorearray << ustat |
|
165 | @scorearray << ustat |
|
154 | end |
|
166 | end |
|
155 | if params[:commit] == 'download csv' then |
|
167 | if params[:commit] == 'download csv' then |
|
156 | csv = gen_csv_from_scorearray(@scorearray,@problems) |
|
168 | csv = gen_csv_from_scorearray(@scorearray,@problems) |
|
157 | send_data csv, filename: 'last_score.csv' |
|
169 | send_data csv, filename: 'last_score.csv' |
|
158 | else |
|
170 | else |
|
159 | render template: 'user_admin/user_stat' |
|
171 | render template: 'user_admin/user_stat' |
|
160 | end |
|
172 | end |
|
161 | end |
|
173 | end |
|
162 |
|
174 | ||
|
163 | def user_stat_max |
|
175 | def user_stat_max |
|
164 | if params[:commit] == 'download csv' |
|
176 | if params[:commit] == 'download csv' |
|
165 | @problems = Problem.all |
|
177 | @problems = Problem.all |
|
166 | else |
|
178 | else |
|
167 | @problems = Problem.available_problems |
|
179 | @problems = Problem.available_problems |
|
168 | end |
|
180 | end |
|
169 | @users = User.includes(:contests).includes(:contest_stat).all |
|
181 | @users = User.includes(:contests).includes(:contest_stat).all |
|
170 | @scorearray = Array.new |
|
182 | @scorearray = Array.new |
|
171 | #set up range from param |
|
183 | #set up range from param |
|
172 | since_id = params.fetch(:since_id, 0).to_i |
|
184 | since_id = params.fetch(:since_id, 0).to_i |
|
173 | until_id = params.fetch(:until_id, 0).to_i |
|
185 | until_id = params.fetch(:until_id, 0).to_i |
|
174 | @users.each do |u| |
|
186 | @users.each do |u| |
|
175 | ustat = Array.new |
|
187 | ustat = Array.new |
|
176 | ustat[0] = u |
|
188 | ustat[0] = u |
|
177 | @problems.each do |p| |
|
189 | @problems.each do |p| |
|
178 | max_points = 0 |
|
190 | max_points = 0 |
|
179 | 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| |
|
180 | 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) |
|
181 | end |
|
193 | end |
|
182 | 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)] |
|
183 | end |
|
195 | end |
|
184 | @scorearray << ustat |
|
196 | @scorearray << ustat |
|
185 | end |
|
197 | end |
|
186 |
|
198 | ||
|
187 | if params[:commit] == 'download csv' then |
|
199 | if params[:commit] == 'download csv' then |
|
188 | csv = gen_csv_from_scorearray(@scorearray,@problems) |
|
200 | csv = gen_csv_from_scorearray(@scorearray,@problems) |
|
189 | send_data csv, filename: 'max_score.csv' |
|
201 | send_data csv, filename: 'max_score.csv' |
|
190 | else |
|
202 | else |
|
191 | render template: 'user_admin/user_stat' |
|
203 | render template: 'user_admin/user_stat' |
|
192 | end |
|
204 | end |
|
193 | end |
|
205 | end |
|
194 |
|
206 | ||
|
195 | def import |
|
207 | def import |
|
196 | if params[:file]=='' |
|
208 | if params[:file]=='' |
|
197 | flash[:notice] = 'Error importing no file' |
|
209 | flash[:notice] = 'Error importing no file' |
|
198 | redirect_to :action => 'index' and return |
|
210 | redirect_to :action => 'index' and return |
|
199 | end |
|
211 | end |
|
200 | import_from_file(params[:file]) |
|
212 | import_from_file(params[:file]) |
|
201 | end |
|
213 | end |
|
202 |
|
214 | ||
|
203 | def random_all_passwords |
|
215 | def random_all_passwords |
|
204 | users = User.all |
|
216 | users = User.all |
|
205 | @prefix = params[:prefix] || '' |
|
217 | @prefix = params[:prefix] || '' |
|
206 | @non_admin_users = User.find_non_admin_with_prefix(@prefix) |
|
218 | @non_admin_users = User.find_non_admin_with_prefix(@prefix) |
|
207 | @changed = false |
|
219 | @changed = false |
|
208 | if request.request_method == 'POST' |
|
220 | if request.request_method == 'POST' |
|
209 | @non_admin_users.each do |user| |
|
221 | @non_admin_users.each do |user| |
|
210 | password = random_password |
|
222 | password = random_password |
|
211 | user.password = password |
|
223 | user.password = password |
|
212 | user.password_confirmation = password |
|
224 | user.password_confirmation = password |
|
213 | user.save |
|
225 | user.save |
|
214 | end |
|
226 | end |
|
215 | @changed = true |
|
227 | @changed = true |
|
216 | end |
|
228 | end |
|
217 | end |
|
229 | end |
|
218 |
|
230 | ||
|
219 | # contest management |
|
231 | # contest management |
|
220 |
|
232 | ||
|
221 | def contests |
|
233 | def contests |
|
222 | @contest, @users = find_contest_and_user_from_contest_id(params[:id]) |
|
234 | @contest, @users = find_contest_and_user_from_contest_id(params[:id]) |
|
223 | @contests = Contest.enabled |
|
235 | @contests = Contest.enabled |
|
224 | end |
|
236 | end |
|
225 |
|
237 | ||
|
226 | def assign_from_list |
|
238 | def assign_from_list |
|
227 | contest_id = params[:users_contest_id] |
|
239 | contest_id = params[:users_contest_id] |
|
228 | 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) |
|
229 | contest = Contest.find(params[:new_contest][:id]) |
|
241 | contest = Contest.find(params[:new_contest][:id]) |
|
230 | if !contest |
|
242 | if !contest |
|
231 | flash[:notice] = 'Error: no contest' |
|
243 | flash[:notice] = 'Error: no contest' |
|
232 | redirect_to :action => 'contests', :id =>contest_id |
|
244 | redirect_to :action => 'contests', :id =>contest_id |
|
233 | end |
|
245 | end |
|
234 |
|
246 | ||
|
235 | note = [] |
|
247 | note = [] |
|
236 | users.each do |u| |
|
248 | users.each do |u| |
|
237 | u.contests = [contest] |
|
249 | u.contests = [contest] |
|
238 | note << u.login |
|
250 | note << u.login |
|
239 | end |
|
251 | end |
|
240 | flash[:notice] = 'User(s) ' + note.join(', ') + |
|
252 | flash[:notice] = 'User(s) ' + note.join(', ') + |
|
241 | " were successfully reassigned to #{contest.title}." |
|
253 | " were successfully reassigned to #{contest.title}." |
|
242 | redirect_to :action => 'contests', :id =>contest.id |
|
254 | redirect_to :action => 'contests', :id =>contest.id |
|
243 | end |
|
255 | end |
|
244 |
|
256 | ||
|
245 | def add_to_contest |
|
257 | def add_to_contest |
|
246 | user = User.find(params[:id]) |
|
258 | user = User.find(params[:id]) |
|
247 | contest = Contest.find(params[:contest_id]) |
|
259 | contest = Contest.find(params[:contest_id]) |
|
248 | if user and contest |
|
260 | if user and contest |
|
249 | user.contests << contest |
|
261 | user.contests << contest |
|
250 | end |
|
262 | end |
|
251 | redirect_to :action => 'index' |
|
263 | redirect_to :action => 'index' |
|
252 | end |
|
264 | end |
|
253 |
|
265 | ||
|
254 | def remove_from_contest |
|
266 | def remove_from_contest |
|
255 | user = User.find(params[:id]) |
|
267 | user = User.find(params[:id]) |
|
256 | contest = Contest.find(params[:contest_id]) |
|
268 | contest = Contest.find(params[:contest_id]) |
|
257 | if user and contest |
|
269 | if user and contest |
|
258 | user.contests.delete(contest) |
|
270 | user.contests.delete(contest) |
|
259 | end |
|
271 | end |
|
260 | redirect_to :action => 'index' |
|
272 | redirect_to :action => 'index' |
|
261 | end |
|
273 | end |
|
262 |
|
274 | ||
|
263 | def contest_management |
|
275 | def contest_management |
|
264 | end |
|
276 | end |
|
265 |
|
277 | ||
|
266 | def manage_contest |
|
278 | def manage_contest |
|
267 | contest = Contest.find(params[:contest][:id]) |
|
279 | contest = Contest.find(params[:contest][:id]) |
|
268 | if !contest |
|
280 | if !contest |
|
269 | flash[:notice] = 'You did not choose the contest.' |
|
281 | flash[:notice] = 'You did not choose the contest.' |
|
270 | redirect_to :action => 'contest_management' and return |
|
282 | redirect_to :action => 'contest_management' and return |
|
271 | end |
|
283 | end |
|
272 |
|
284 | ||
|
273 | operation = params[:operation] |
|
285 | operation = params[:operation] |
|
274 |
|
286 | ||
|
275 | if not ['add','remove','assign'].include? operation |
|
287 | if not ['add','remove','assign'].include? operation |
|
276 | flash[:notice] = 'You did not choose the operation to perform.' |
|
288 | flash[:notice] = 'You did not choose the operation to perform.' |
|
277 | redirect_to :action => 'contest_management' and return |
|
289 | redirect_to :action => 'contest_management' and return |
|
278 | end |
|
290 | end |
|
279 |
|
291 | ||
|
280 | lines = params[:login_list] |
|
292 | lines = params[:login_list] |
|
281 | if !lines or lines.blank? |
|
293 | if !lines or lines.blank? |
|
282 | flash[:notice] = 'You entered an empty list.' |
|
294 | flash[:notice] = 'You entered an empty list.' |
|
283 | redirect_to :action => 'contest_management' and return |
|
295 | redirect_to :action => 'contest_management' and return |
|
284 | end |
|
296 | end |
|
285 |
|
297 | ||
|
286 | note = [] |
|
298 | note = [] |
|
287 | users = [] |
|
299 | users = [] |
|
288 | lines.split("\n").each do |line| |
|
300 | lines.split("\n").each do |line| |
|
289 | user = User.find_by_login(line.chomp) |
|
301 | user = User.find_by_login(line.chomp) |
You need to be logged in to leave comments.
Login now