Description:
fix bugs when user creation error
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r812:e2597379728c - - 1 file changed: 1 inserted, 1 deleted
@@ -1,264 +1,264 | |||
|
1 | 1 | require 'csv' |
|
2 | 2 | |
|
3 | 3 | class UserAdminController < ApplicationController |
|
4 | 4 | |
|
5 | 5 | include MailHelperMethods |
|
6 | 6 | |
|
7 | 7 | before_action :admin_authorization |
|
8 | 8 | |
|
9 | 9 | def index |
|
10 | 10 | @user_count = User.count |
|
11 | 11 | @users = User.all |
|
12 | 12 | @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at'] |
|
13 | 13 | @contests = Contest.enabled |
|
14 | 14 | end |
|
15 | 15 | |
|
16 | 16 | def active |
|
17 | 17 | sessions = ActiveRecord::SessionStore::Session.where("updated_at >= ?", 60.minutes.ago) |
|
18 | 18 | @users = [] |
|
19 | 19 | sessions.each do |session| |
|
20 | 20 | if session.data[:user_id] |
|
21 | 21 | @users << User.find(session.data[:user_id]) |
|
22 | 22 | end |
|
23 | 23 | end |
|
24 | 24 | end |
|
25 | 25 | |
|
26 | 26 | def show |
|
27 | 27 | @user = User.find(params[:id]) |
|
28 | 28 | end |
|
29 | 29 | |
|
30 | 30 | def new |
|
31 | 31 | @user = User.new |
|
32 | 32 | end |
|
33 | 33 | |
|
34 | 34 | def create |
|
35 | 35 | @user = User.new(user_params) |
|
36 | 36 | @user.activated = true |
|
37 | 37 | if @user.save |
|
38 | 38 | flash[:notice] = 'User was successfully created.' |
|
39 | 39 | redirect_to :action => 'index' |
|
40 | 40 | else |
|
41 | 41 | render :action => 'new' |
|
42 | 42 | end |
|
43 | 43 | end |
|
44 | 44 | |
|
45 | 45 | def clear_last_ip |
|
46 | 46 | @user = User.find(params[:id]) |
|
47 | 47 | @user.last_ip = nil |
|
48 | 48 | @user.save |
|
49 | 49 | redirect_to action: 'index', page: params[:page] |
|
50 | 50 | end |
|
51 | 51 | |
|
52 | 52 | def create_from_list |
|
53 | 53 | lines = params[:user_list] |
|
54 | 54 | |
|
55 | 55 | |
|
56 | 56 | res = User.create_from_list(lines) |
|
57 | 57 | error_logins = res[:error_logins] |
|
58 | 58 | error_msg = res[:first_error] |
|
59 | 59 | ok_user = res[:created_users] |
|
60 | 60 | |
|
61 | 61 | |
|
62 | 62 | #add to group |
|
63 | 63 | if params[:add_to_group] |
|
64 | 64 | group = Group.find_by(id: params[:group_id])&.add_users_skip_existing(ok_user) |
|
65 | 65 | end |
|
66 | 66 | |
|
67 | 67 | # show flash |
|
68 | 68 | if ok_user.count > 0 |
|
69 | 69 | flash[:success] = "#{ok_user.count} user(s) was created or updated successfully" |
|
70 | 70 | end |
|
71 | 71 | if error_logins.size > 0 |
|
72 |
- flash[:error] = "Following user(s) failed to be created: " + error_ |
|
|
72 | + flash[:error] = "Following user(s) failed to be created: " + error_logins.join(', ') + ". The error of the first failed one are: " + error_msg; | |
|
73 | 73 | end |
|
74 | 74 | redirect_to :action => 'index' |
|
75 | 75 | end |
|
76 | 76 | |
|
77 | 77 | def edit |
|
78 | 78 | @user = User.find(params[:id]) |
|
79 | 79 | end |
|
80 | 80 | |
|
81 | 81 | def update |
|
82 | 82 | @user = User.find(params[:id]) |
|
83 | 83 | if @user.update_attributes(user_params) |
|
84 | 84 | flash[:notice] = 'User was successfully updated.' |
|
85 | 85 | redirect_to :action => 'show', :id => @user |
|
86 | 86 | else |
|
87 | 87 | render :action => 'edit' |
|
88 | 88 | end |
|
89 | 89 | end |
|
90 | 90 | |
|
91 | 91 | def destroy |
|
92 | 92 | User.find(params[:id]).destroy |
|
93 | 93 | redirect_to :action => 'index' |
|
94 | 94 | end |
|
95 | 95 | |
|
96 | 96 | def user_stat |
|
97 | 97 | if params[:commit] == 'download csv' |
|
98 | 98 | @problems = Problem.all |
|
99 | 99 | else |
|
100 | 100 | @problems = Problem.available_problems |
|
101 | 101 | end |
|
102 | 102 | @users = User.includes(:contests, :contest_stat).where(enabled: true) |
|
103 | 103 | @scorearray = Array.new |
|
104 | 104 | @users.each do |u| |
|
105 | 105 | ustat = Array.new |
|
106 | 106 | ustat[0] = u |
|
107 | 107 | @problems.each do |p| |
|
108 | 108 | sub = Submission.find_last_by_user_and_problem(u.id,p.id) |
|
109 | 109 | if (sub!=nil) and (sub.points!=nil) and p and p.full_score |
|
110 | 110 | ustat << [(sub.points.to_f*100/p.full_score).round, (sub.points>=p.full_score)] |
|
111 | 111 | else |
|
112 | 112 | ustat << [0,false] |
|
113 | 113 | end |
|
114 | 114 | end |
|
115 | 115 | @scorearray << ustat |
|
116 | 116 | end |
|
117 | 117 | if params[:commit] == 'download csv' then |
|
118 | 118 | csv = gen_csv_from_scorearray(@scorearray,@problems) |
|
119 | 119 | send_data csv, filename: 'last_score.csv' |
|
120 | 120 | else |
|
121 | 121 | render template: 'user_admin/user_stat' |
|
122 | 122 | end |
|
123 | 123 | end |
|
124 | 124 | |
|
125 | 125 | def user_stat_max |
|
126 | 126 | if params[:commit] == 'download csv' |
|
127 | 127 | @problems = Problem.all |
|
128 | 128 | else |
|
129 | 129 | @problems = Problem.available_problems |
|
130 | 130 | end |
|
131 | 131 | @users = User.includes(:contests).includes(:contest_stat).all |
|
132 | 132 | @scorearray = Array.new |
|
133 | 133 | #set up range from param |
|
134 | 134 | since_id = params.fetch(:since_id, 0).to_i |
|
135 | 135 | until_id = params.fetch(:until_id, 0).to_i |
|
136 | 136 | @users.each do |u| |
|
137 | 137 | ustat = Array.new |
|
138 | 138 | ustat[0] = u |
|
139 | 139 | @problems.each do |p| |
|
140 | 140 | max_points = 0 |
|
141 | 141 | Submission.find_in_range_by_user_and_problem(u.id,p.id,since_id,until_id).each do |sub| |
|
142 | 142 | max_points = sub.points if sub and sub.points and (sub.points > max_points) |
|
143 | 143 | end |
|
144 | 144 | ustat << [(max_points.to_f*100/p.full_score).round, (max_points>=p.full_score)] |
|
145 | 145 | end |
|
146 | 146 | @scorearray << ustat |
|
147 | 147 | end |
|
148 | 148 | |
|
149 | 149 | if params[:commit] == 'download csv' then |
|
150 | 150 | csv = gen_csv_from_scorearray(@scorearray,@problems) |
|
151 | 151 | send_data csv, filename: 'max_score.csv' |
|
152 | 152 | else |
|
153 | 153 | render template: 'user_admin/user_stat' |
|
154 | 154 | end |
|
155 | 155 | end |
|
156 | 156 | |
|
157 | 157 | def import |
|
158 | 158 | if params[:file]=='' |
|
159 | 159 | flash[:notice] = 'Error importing no file' |
|
160 | 160 | redirect_to :action => 'index' and return |
|
161 | 161 | end |
|
162 | 162 | import_from_file(params[:file]) |
|
163 | 163 | end |
|
164 | 164 | |
|
165 | 165 | def random_all_passwords |
|
166 | 166 | users = User.all |
|
167 | 167 | @prefix = params[:prefix] || '' |
|
168 | 168 | @non_admin_users = User.find_non_admin_with_prefix(@prefix) |
|
169 | 169 | @changed = false |
|
170 | 170 | if params[:commit] == 'Go ahead' |
|
171 | 171 | @non_admin_users.each do |user| |
|
172 | 172 | password = random_password |
|
173 | 173 | user.password = password |
|
174 | 174 | user.password_confirmation = password |
|
175 | 175 | user.save |
|
176 | 176 | end |
|
177 | 177 | @changed = true |
|
178 | 178 | end |
|
179 | 179 | end |
|
180 | 180 | |
|
181 | 181 | # contest management |
|
182 | 182 | |
|
183 | 183 | def contests |
|
184 | 184 | @contest, @users = find_contest_and_user_from_contest_id(params[:id]) |
|
185 | 185 | @contests = Contest.enabled |
|
186 | 186 | end |
|
187 | 187 | |
|
188 | 188 | def assign_from_list |
|
189 | 189 | contest_id = params[:users_contest_id] |
|
190 | 190 | org_contest, users = find_contest_and_user_from_contest_id(contest_id) |
|
191 | 191 | contest = Contest.find(params[:new_contest][:id]) |
|
192 | 192 | if !contest |
|
193 | 193 | flash[:notice] = 'Error: no contest' |
|
194 | 194 | redirect_to :action => 'contests', :id =>contest_id |
|
195 | 195 | end |
|
196 | 196 | |
|
197 | 197 | note = [] |
|
198 | 198 | users.each do |u| |
|
199 | 199 | u.contests = [contest] |
|
200 | 200 | note << u.login |
|
201 | 201 | end |
|
202 | 202 | flash[:notice] = 'User(s) ' + note.join(', ') + |
|
203 | 203 | " were successfully reassigned to #{contest.title}." |
|
204 | 204 | redirect_to :action => 'contests', :id =>contest.id |
|
205 | 205 | end |
|
206 | 206 | |
|
207 | 207 | def add_to_contest |
|
208 | 208 | user = User.find(params[:id]) |
|
209 | 209 | contest = Contest.find(params[:contest_id]) |
|
210 | 210 | if user and contest |
|
211 | 211 | user.contests << contest |
|
212 | 212 | end |
|
213 | 213 | redirect_to :action => 'index' |
|
214 | 214 | end |
|
215 | 215 | |
|
216 | 216 | def remove_from_contest |
|
217 | 217 | user = User.find(params[:id]) |
|
218 | 218 | contest = Contest.find(params[:contest_id]) |
|
219 | 219 | if user and contest |
|
220 | 220 | user.contests.delete(contest) |
|
221 | 221 | end |
|
222 | 222 | redirect_to :action => 'index' |
|
223 | 223 | end |
|
224 | 224 | |
|
225 | 225 | def contest_management |
|
226 | 226 | end |
|
227 | 227 | |
|
228 | 228 | def manage_contest |
|
229 | 229 | contest = Contest.find(params[:contest][:id]) |
|
230 | 230 | if !contest |
|
231 | 231 | flash[:notice] = 'You did not choose the contest.' |
|
232 | 232 | redirect_to :action => 'contest_management' and return |
|
233 | 233 | end |
|
234 | 234 | |
|
235 | 235 | operation = params[:operation] |
|
236 | 236 | |
|
237 | 237 | if not ['add','remove','assign'].include? operation |
|
238 | 238 | flash[:notice] = 'You did not choose the operation to perform.' |
|
239 | 239 | redirect_to :action => 'contest_management' and return |
|
240 | 240 | end |
|
241 | 241 | |
|
242 | 242 | lines = params[:login_list] |
|
243 | 243 | if !lines or lines.blank? |
|
244 | 244 | flash[:notice] = 'You entered an empty list.' |
|
245 | 245 | redirect_to :action => 'contest_management' and return |
|
246 | 246 | end |
|
247 | 247 | |
|
248 | 248 | note = [] |
|
249 | 249 | users = [] |
|
250 | 250 | lines.split("\n").each do |line| |
|
251 | 251 | user = User.find_by_login(line.chomp) |
|
252 | 252 | if user |
|
253 | 253 | if operation=='add' |
|
254 | 254 | if ! user.contests.include? contest |
|
255 | 255 | user.contests << contest |
|
256 | 256 | end |
|
257 | 257 | elsif operation=='remove' |
|
258 | 258 | user.contests.delete(contest) |
|
259 | 259 | else |
|
260 | 260 | user.contests = [contest] |
|
261 | 261 | end |
|
262 | 262 | |
|
263 | 263 | if params[:reset_timer] |
|
264 | 264 | user.contest_stat.forced_logout = true |
You need to be logged in to leave comments.
Login now