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,456 +1,456 | |||||
|
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_action :admin_authorization |
|
7 | before_action :admin_authorization |
|
8 |
|
8 | ||
|
9 | def index |
|
9 | def index |
|
10 | @user_count = User.count |
|
10 | @user_count = User.count |
|
11 | @users = User.all |
|
11 | @users = User.all |
|
12 | @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at'] |
|
12 | @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at'] |
|
13 | @contests = Contest.enabled |
|
13 | @contests = Contest.enabled |
|
14 | end |
|
14 | end |
|
15 |
|
15 | ||
|
16 | def active |
|
16 | def active |
|
17 | sessions = ActiveRecord::SessionStore::Session.where("updated_at >= ?", 60.minutes.ago) |
|
17 | sessions = ActiveRecord::SessionStore::Session.where("updated_at >= ?", 60.minutes.ago) |
|
18 | @users = [] |
|
18 | @users = [] |
|
19 | sessions.each do |session| |
|
19 | sessions.each do |session| |
|
20 | if session.data[:user_id] |
|
20 | if session.data[:user_id] |
|
21 | @users << User.find(session.data[:user_id]) |
|
21 | @users << User.find(session.data[:user_id]) |
|
22 | end |
|
22 | end |
|
23 | end |
|
23 | end |
|
24 | end |
|
24 | end |
|
25 |
|
25 | ||
|
26 | def show |
|
26 | def show |
|
27 | @user = User.find(params[:id]) |
|
27 | @user = User.find(params[:id]) |
|
28 | end |
|
28 | end |
|
29 |
|
29 | ||
|
30 | def new |
|
30 | def new |
|
31 | @user = User.new |
|
31 | @user = User.new |
|
32 | end |
|
32 | end |
|
33 |
|
33 | ||
|
34 | def create |
|
34 | def create |
|
35 | @user = User.new(user_params) |
|
35 | @user = User.new(user_params) |
|
36 | @user.activated = true |
|
36 | @user.activated = true |
|
37 | if @user.save |
|
37 | if @user.save |
|
38 | flash[:notice] = 'User was successfully created.' |
|
38 | flash[:notice] = 'User was successfully created.' |
|
39 | redirect_to :action => 'index' |
|
39 | redirect_to :action => 'index' |
|
40 | else |
|
40 | else |
|
41 | render :action => 'new' |
|
41 | render :action => 'new' |
|
42 | end |
|
42 | end |
|
43 | end |
|
43 | end |
|
44 |
|
44 | ||
|
45 | def clear_last_ip |
|
45 | def clear_last_ip |
|
46 | @user = User.find(params[:id]) |
|
46 | @user = User.find(params[:id]) |
|
47 | @user.last_ip = nil |
|
47 | @user.last_ip = nil |
|
48 | @user.save |
|
48 | @user.save |
|
49 | redirect_to action: 'index', page: params[:page] |
|
49 | redirect_to action: 'index', page: params[:page] |
|
50 | end |
|
50 | end |
|
51 |
|
51 | ||
|
52 | def create_from_list |
|
52 | def create_from_list |
|
53 | lines = params[:user_list] |
|
53 | lines = params[:user_list] |
|
54 |
|
54 | ||
|
55 |
|
55 | ||
|
56 | res = User.create_from_list(lines) |
|
56 | res = User.create_from_list(lines) |
|
57 | error_logins = res[:error_logins] |
|
57 | error_logins = res[:error_logins] |
|
58 | error_msg = res[:first_error] |
|
58 | error_msg = res[:first_error] |
|
59 | ok_user = res[:created_users] |
|
59 | ok_user = res[:created_users] |
|
60 |
|
60 | ||
|
61 |
|
61 | ||
|
62 | #add to group |
|
62 | #add to group |
|
63 | if params[:add_to_group] |
|
63 | if params[:add_to_group] |
|
64 | group = Group.find_by(id: params[:group_id])&.add_users_skip_existing(ok_user) |
|
64 | group = Group.find_by(id: params[:group_id])&.add_users_skip_existing(ok_user) |
|
65 | end |
|
65 | end |
|
66 |
|
66 | ||
|
67 | # show flash |
|
67 | # show flash |
|
68 | if ok_user.count > 0 |
|
68 | if ok_user.count > 0 |
|
69 | flash[:success] = "#{ok_user.count} user(s) was created or updated successfully" |
|
69 | flash[:success] = "#{ok_user.count} user(s) was created or updated successfully" |
|
70 | end |
|
70 | end |
|
71 | if error_logins.size > 0 |
|
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 | end |
|
73 | end |
|
74 | redirect_to :action => 'index' |
|
74 | redirect_to :action => 'index' |
|
75 | end |
|
75 | end |
|
76 |
|
76 | ||
|
77 | def edit |
|
77 | def edit |
|
78 | @user = User.find(params[:id]) |
|
78 | @user = User.find(params[:id]) |
|
79 | end |
|
79 | end |
|
80 |
|
80 | ||
|
81 | def update |
|
81 | def update |
|
82 | @user = User.find(params[:id]) |
|
82 | @user = User.find(params[:id]) |
|
83 | if @user.update_attributes(user_params) |
|
83 | if @user.update_attributes(user_params) |
|
84 | flash[:notice] = 'User was successfully updated.' |
|
84 | flash[:notice] = 'User was successfully updated.' |
|
85 | redirect_to :action => 'show', :id => @user |
|
85 | redirect_to :action => 'show', :id => @user |
|
86 | else |
|
86 | else |
|
87 | render :action => 'edit' |
|
87 | render :action => 'edit' |
|
88 | end |
|
88 | end |
|
89 | end |
|
89 | end |
|
90 |
|
90 | ||
|
91 | def destroy |
|
91 | def destroy |
|
92 | User.find(params[:id]).destroy |
|
92 | User.find(params[:id]).destroy |
|
93 | redirect_to :action => 'index' |
|
93 | redirect_to :action => 'index' |
|
94 | end |
|
94 | end |
|
95 |
|
95 | ||
|
96 | def user_stat |
|
96 | def user_stat |
|
97 | if params[:commit] == 'download csv' |
|
97 | if params[:commit] == 'download csv' |
|
98 | @problems = Problem.all |
|
98 | @problems = Problem.all |
|
99 | else |
|
99 | else |
|
100 | @problems = Problem.available_problems |
|
100 | @problems = Problem.available_problems |
|
101 | end |
|
101 | end |
|
102 | @users = User.includes(:contests, :contest_stat).where(enabled: true) |
|
102 | @users = User.includes(:contests, :contest_stat).where(enabled: true) |
|
103 | @scorearray = Array.new |
|
103 | @scorearray = Array.new |
|
104 | @users.each do |u| |
|
104 | @users.each do |u| |
|
105 | ustat = Array.new |
|
105 | ustat = Array.new |
|
106 | ustat[0] = u |
|
106 | ustat[0] = u |
|
107 | @problems.each do |p| |
|
107 | @problems.each do |p| |
|
108 | sub = Submission.find_last_by_user_and_problem(u.id,p.id) |
|
108 | sub = Submission.find_last_by_user_and_problem(u.id,p.id) |
|
109 | if (sub!=nil) and (sub.points!=nil) and p and p.full_score |
|
109 | if (sub!=nil) and (sub.points!=nil) and p and p.full_score |
|
110 | ustat << [(sub.points.to_f*100/p.full_score).round, (sub.points>=p.full_score)] |
|
110 | ustat << [(sub.points.to_f*100/p.full_score).round, (sub.points>=p.full_score)] |
|
111 | else |
|
111 | else |
|
112 | ustat << [0,false] |
|
112 | ustat << [0,false] |
|
113 | end |
|
113 | end |
|
114 | end |
|
114 | end |
|
115 | @scorearray << ustat |
|
115 | @scorearray << ustat |
|
116 | end |
|
116 | end |
|
117 | if params[:commit] == 'download csv' then |
|
117 | if params[:commit] == 'download csv' then |
|
118 | csv = gen_csv_from_scorearray(@scorearray,@problems) |
|
118 | csv = gen_csv_from_scorearray(@scorearray,@problems) |
|
119 | send_data csv, filename: 'last_score.csv' |
|
119 | send_data csv, filename: 'last_score.csv' |
|
120 | else |
|
120 | else |
|
121 | render template: 'user_admin/user_stat' |
|
121 | render template: 'user_admin/user_stat' |
|
122 | end |
|
122 | end |
|
123 | end |
|
123 | end |
|
124 |
|
124 | ||
|
125 | def user_stat_max |
|
125 | def user_stat_max |
|
126 | if params[:commit] == 'download csv' |
|
126 | if params[:commit] == 'download csv' |
|
127 | @problems = Problem.all |
|
127 | @problems = Problem.all |
|
128 | else |
|
128 | else |
|
129 | @problems = Problem.available_problems |
|
129 | @problems = Problem.available_problems |
|
130 | end |
|
130 | end |
|
131 | @users = User.includes(:contests).includes(:contest_stat).all |
|
131 | @users = User.includes(:contests).includes(:contest_stat).all |
|
132 | @scorearray = Array.new |
|
132 | @scorearray = Array.new |
|
133 | #set up range from param |
|
133 | #set up range from param |
|
134 | since_id = params.fetch(:since_id, 0).to_i |
|
134 | since_id = params.fetch(:since_id, 0).to_i |
|
135 | until_id = params.fetch(:until_id, 0).to_i |
|
135 | until_id = params.fetch(:until_id, 0).to_i |
|
136 | @users.each do |u| |
|
136 | @users.each do |u| |
|
137 | ustat = Array.new |
|
137 | ustat = Array.new |
|
138 | ustat[0] = u |
|
138 | ustat[0] = u |
|
139 | @problems.each do |p| |
|
139 | @problems.each do |p| |
|
140 | max_points = 0 |
|
140 | max_points = 0 |
|
141 | Submission.find_in_range_by_user_and_problem(u.id,p.id,since_id,until_id).each do |sub| |
|
141 | Submission.find_in_range_by_user_and_problem(u.id,p.id,since_id,until_id).each do |sub| |
|
142 | max_points = sub.points if sub and sub.points and (sub.points > max_points) |
|
142 | max_points = sub.points if sub and sub.points and (sub.points > max_points) |
|
143 | end |
|
143 | end |
|
144 | ustat << [(max_points.to_f*100/p.full_score).round, (max_points>=p.full_score)] |
|
144 | ustat << [(max_points.to_f*100/p.full_score).round, (max_points>=p.full_score)] |
|
145 | end |
|
145 | end |
|
146 | @scorearray << ustat |
|
146 | @scorearray << ustat |
|
147 | end |
|
147 | end |
|
148 |
|
148 | ||
|
149 | if params[:commit] == 'download csv' then |
|
149 | if params[:commit] == 'download csv' then |
|
150 | csv = gen_csv_from_scorearray(@scorearray,@problems) |
|
150 | csv = gen_csv_from_scorearray(@scorearray,@problems) |
|
151 | send_data csv, filename: 'max_score.csv' |
|
151 | send_data csv, filename: 'max_score.csv' |
|
152 | else |
|
152 | else |
|
153 | render template: 'user_admin/user_stat' |
|
153 | render template: 'user_admin/user_stat' |
|
154 | end |
|
154 | end |
|
155 | end |
|
155 | end |
|
156 |
|
156 | ||
|
157 | def import |
|
157 | def import |
|
158 | if params[:file]=='' |
|
158 | if params[:file]=='' |
|
159 | flash[:notice] = 'Error importing no file' |
|
159 | flash[:notice] = 'Error importing no file' |
|
160 | redirect_to :action => 'index' and return |
|
160 | redirect_to :action => 'index' and return |
|
161 | end |
|
161 | end |
|
162 | import_from_file(params[:file]) |
|
162 | import_from_file(params[:file]) |
|
163 | end |
|
163 | end |
|
164 |
|
164 | ||
|
165 | def random_all_passwords |
|
165 | def random_all_passwords |
|
166 | users = User.all |
|
166 | users = User.all |
|
167 | @prefix = params[:prefix] || '' |
|
167 | @prefix = params[:prefix] || '' |
|
168 | @non_admin_users = User.find_non_admin_with_prefix(@prefix) |
|
168 | @non_admin_users = User.find_non_admin_with_prefix(@prefix) |
|
169 | @changed = false |
|
169 | @changed = false |
|
170 | if params[:commit] == 'Go ahead' |
|
170 | if params[:commit] == 'Go ahead' |
|
171 | @non_admin_users.each do |user| |
|
171 | @non_admin_users.each do |user| |
|
172 | password = random_password |
|
172 | password = random_password |
|
173 | user.password = password |
|
173 | user.password = password |
|
174 | user.password_confirmation = password |
|
174 | user.password_confirmation = password |
|
175 | user.save |
|
175 | user.save |
|
176 | end |
|
176 | end |
|
177 | @changed = true |
|
177 | @changed = true |
|
178 | end |
|
178 | end |
|
179 | end |
|
179 | end |
|
180 |
|
180 | ||
|
181 | # contest management |
|
181 | # contest management |
|
182 |
|
182 | ||
|
183 | def contests |
|
183 | def contests |
|
184 | @contest, @users = find_contest_and_user_from_contest_id(params[:id]) |
|
184 | @contest, @users = find_contest_and_user_from_contest_id(params[:id]) |
|
185 | @contests = Contest.enabled |
|
185 | @contests = Contest.enabled |
|
186 | end |
|
186 | end |
|
187 |
|
187 | ||
|
188 | def assign_from_list |
|
188 | def assign_from_list |
|
189 | contest_id = params[:users_contest_id] |
|
189 | contest_id = params[:users_contest_id] |
|
190 | org_contest, users = find_contest_and_user_from_contest_id(contest_id) |
|
190 | org_contest, users = find_contest_and_user_from_contest_id(contest_id) |
|
191 | contest = Contest.find(params[:new_contest][:id]) |
|
191 | contest = Contest.find(params[:new_contest][:id]) |
|
192 | if !contest |
|
192 | if !contest |
|
193 | flash[:notice] = 'Error: no contest' |
|
193 | flash[:notice] = 'Error: no contest' |
|
194 | redirect_to :action => 'contests', :id =>contest_id |
|
194 | redirect_to :action => 'contests', :id =>contest_id |
|
195 | end |
|
195 | end |
|
196 |
|
196 | ||
|
197 | note = [] |
|
197 | note = [] |
|
198 | users.each do |u| |
|
198 | users.each do |u| |
|
199 | u.contests = [contest] |
|
199 | u.contests = [contest] |
|
200 | note << u.login |
|
200 | note << u.login |
|
201 | end |
|
201 | end |
|
202 | flash[:notice] = 'User(s) ' + note.join(', ') + |
|
202 | flash[:notice] = 'User(s) ' + note.join(', ') + |
|
203 | " were successfully reassigned to #{contest.title}." |
|
203 | " were successfully reassigned to #{contest.title}." |
|
204 | redirect_to :action => 'contests', :id =>contest.id |
|
204 | redirect_to :action => 'contests', :id =>contest.id |
|
205 | end |
|
205 | end |
|
206 |
|
206 | ||
|
207 | def add_to_contest |
|
207 | def add_to_contest |
|
208 | user = User.find(params[:id]) |
|
208 | user = User.find(params[:id]) |
|
209 | contest = Contest.find(params[:contest_id]) |
|
209 | contest = Contest.find(params[:contest_id]) |
|
210 | if user and contest |
|
210 | if user and contest |
|
211 | user.contests << contest |
|
211 | user.contests << contest |
|
212 | end |
|
212 | end |
|
213 | redirect_to :action => 'index' |
|
213 | redirect_to :action => 'index' |
|
214 | end |
|
214 | end |
|
215 |
|
215 | ||
|
216 | def remove_from_contest |
|
216 | def remove_from_contest |
|
217 | user = User.find(params[:id]) |
|
217 | user = User.find(params[:id]) |
|
218 | contest = Contest.find(params[:contest_id]) |
|
218 | contest = Contest.find(params[:contest_id]) |
|
219 | if user and contest |
|
219 | if user and contest |
|
220 | user.contests.delete(contest) |
|
220 | user.contests.delete(contest) |
|
221 | end |
|
221 | end |
|
222 | redirect_to :action => 'index' |
|
222 | redirect_to :action => 'index' |
|
223 | end |
|
223 | end |
|
224 |
|
224 | ||
|
225 | def contest_management |
|
225 | def contest_management |
|
226 | end |
|
226 | end |
|
227 |
|
227 | ||
|
228 | def manage_contest |
|
228 | def manage_contest |
|
229 | contest = Contest.find(params[:contest][:id]) |
|
229 | contest = Contest.find(params[:contest][:id]) |
|
230 | if !contest |
|
230 | if !contest |
|
231 | flash[:notice] = 'You did not choose the contest.' |
|
231 | flash[:notice] = 'You did not choose the contest.' |
|
232 | redirect_to :action => 'contest_management' and return |
|
232 | redirect_to :action => 'contest_management' and return |
|
233 | end |
|
233 | end |
|
234 |
|
234 | ||
|
235 | operation = params[:operation] |
|
235 | operation = params[:operation] |
|
236 |
|
236 | ||
|
237 | if not ['add','remove','assign'].include? operation |
|
237 | if not ['add','remove','assign'].include? operation |
|
238 | flash[:notice] = 'You did not choose the operation to perform.' |
|
238 | flash[:notice] = 'You did not choose the operation to perform.' |
|
239 | redirect_to :action => 'contest_management' and return |
|
239 | redirect_to :action => 'contest_management' and return |
|
240 | end |
|
240 | end |
|
241 |
|
241 | ||
|
242 | lines = params[:login_list] |
|
242 | lines = params[:login_list] |
|
243 | if !lines or lines.blank? |
|
243 | if !lines or lines.blank? |
|
244 | flash[:notice] = 'You entered an empty list.' |
|
244 | flash[:notice] = 'You entered an empty list.' |
|
245 | redirect_to :action => 'contest_management' and return |
|
245 | redirect_to :action => 'contest_management' and return |
|
246 | end |
|
246 | end |
|
247 |
|
247 | ||
|
248 | note = [] |
|
248 | note = [] |
|
249 | users = [] |
|
249 | users = [] |
|
250 | lines.split("\n").each do |line| |
|
250 | lines.split("\n").each do |line| |
|
251 | user = User.find_by_login(line.chomp) |
|
251 | user = User.find_by_login(line.chomp) |
|
252 | if user |
|
252 | if user |
|
253 | if operation=='add' |
|
253 | if operation=='add' |
|
254 | if ! user.contests.include? contest |
|
254 | if ! user.contests.include? contest |
|
255 | user.contests << contest |
|
255 | user.contests << contest |
|
256 | end |
|
256 | end |
|
257 | elsif operation=='remove' |
|
257 | elsif operation=='remove' |
|
258 | user.contests.delete(contest) |
|
258 | user.contests.delete(contest) |
|
259 | else |
|
259 | else |
|
260 | user.contests = [contest] |
|
260 | user.contests = [contest] |
|
261 | end |
|
261 | end |
|
262 |
|
262 | ||
|
263 | if params[:reset_timer] |
|
263 | if params[:reset_timer] |
|
264 | user.contest_stat.forced_logout = true |
|
264 | user.contest_stat.forced_logout = true |
|
265 | user.contest_stat.reset_timer_and_save |
|
265 | user.contest_stat.reset_timer_and_save |
|
266 | end |
|
266 | end |
|
267 |
|
267 | ||
|
268 | if params[:notification_emails] |
|
268 | if params[:notification_emails] |
|
269 | send_contest_update_notification_email(user, contest) |
|
269 | send_contest_update_notification_email(user, contest) |
|
270 | end |
|
270 | end |
|
271 |
|
271 | ||
|
272 | note << user.login |
|
272 | note << user.login |
|
273 | users << user |
|
273 | users << user |
|
274 | end |
|
274 | end |
|
275 | end |
|
275 | end |
|
276 |
|
276 | ||
|
277 | if params[:reset_timer] |
|
277 | if params[:reset_timer] |
|
278 | logout_users(users) |
|
278 | logout_users(users) |
|
279 | end |
|
279 | end |
|
280 |
|
280 | ||
|
281 | flash[:notice] = 'User(s) ' + note.join(', ') + |
|
281 | flash[:notice] = 'User(s) ' + note.join(', ') + |
|
282 | ' were successfully modified. ' |
|
282 | ' were successfully modified. ' |
|
283 | redirect_to :action => 'contest_management' |
|
283 | redirect_to :action => 'contest_management' |
|
284 | end |
|
284 | end |
|
285 |
|
285 | ||
|
286 | # admin management |
|
286 | # admin management |
|
287 |
|
287 | ||
|
288 | def admin |
|
288 | def admin |
|
289 | @admins = Role.where(name: 'admin').take.users |
|
289 | @admins = Role.where(name: 'admin').take.users |
|
290 | @tas = Role.where(name: 'ta').take.users |
|
290 | @tas = Role.where(name: 'ta').take.users |
|
291 | end |
|
291 | end |
|
292 |
|
292 | ||
|
293 | def modify_role |
|
293 | def modify_role |
|
294 | user = User.find_by_login(params[:login]) |
|
294 | user = User.find_by_login(params[:login]) |
|
295 | role = Role.find_by_name(params[:role]) |
|
295 | role = Role.find_by_name(params[:role]) |
|
296 | unless user && role |
|
296 | unless user && role |
|
297 | flash[:error] = 'Unknown user or role' |
|
297 | flash[:error] = 'Unknown user or role' |
|
298 | redirect_to admin_user_admin_index_path |
|
298 | redirect_to admin_user_admin_index_path |
|
299 | return |
|
299 | return |
|
300 | end |
|
300 | end |
|
301 | if params[:commit] == 'Grant' |
|
301 | if params[:commit] == 'Grant' |
|
302 | #grant role |
|
302 | #grant role |
|
303 | user.roles << role |
|
303 | user.roles << role |
|
304 | flash[:notice] = "User '#{user.login}' has been granted the role '#{role.name}'" |
|
304 | flash[:notice] = "User '#{user.login}' has been granted the role '#{role.name}'" |
|
305 | else |
|
305 | else |
|
306 | #revoke role |
|
306 | #revoke role |
|
307 | if user.login == 'root' && role.name == 'admin' |
|
307 | if user.login == 'root' && role.name == 'admin' |
|
308 | flash[:error] = 'You cannot revoke admisnistrator permission from root.' |
|
308 | flash[:error] = 'You cannot revoke admisnistrator permission from root.' |
|
309 | redirect_to admin_user_admin_index_path |
|
309 | redirect_to admin_user_admin_index_path |
|
310 | return |
|
310 | return |
|
311 | end |
|
311 | end |
|
312 | user.roles.delete(role) |
|
312 | user.roles.delete(role) |
|
313 | flash[:notice] = "The role '#{role.name}' has been revoked from User '#{user.login}'" |
|
313 | flash[:notice] = "The role '#{role.name}' has been revoked from User '#{user.login}'" |
|
314 | end |
|
314 | end |
|
315 | redirect_to admin_user_admin_index_path |
|
315 | redirect_to admin_user_admin_index_path |
|
316 | end |
|
316 | end |
|
317 |
|
317 | ||
|
318 | # mass mailing |
|
318 | # mass mailing |
|
319 |
|
319 | ||
|
320 | def mass_mailing |
|
320 | def mass_mailing |
|
321 | end |
|
321 | end |
|
322 |
|
322 | ||
|
323 | def bulk_mail |
|
323 | def bulk_mail |
|
324 | lines = params[:login_list] |
|
324 | lines = params[:login_list] |
|
325 | if !lines or lines.blank? |
|
325 | if !lines or lines.blank? |
|
326 | flash[:notice] = 'You entered an empty list.' |
|
326 | flash[:notice] = 'You entered an empty list.' |
|
327 | redirect_to :action => 'mass_mailing' and return |
|
327 | redirect_to :action => 'mass_mailing' and return |
|
328 | end |
|
328 | end |
|
329 |
|
329 | ||
|
330 | mail_subject = params[:subject] |
|
330 | mail_subject = params[:subject] |
|
331 | if !mail_subject or mail_subject.blank? |
|
331 | if !mail_subject or mail_subject.blank? |
|
332 | flash[:notice] = 'You entered an empty mail subject.' |
|
332 | flash[:notice] = 'You entered an empty mail subject.' |
|
333 | redirect_to :action => 'mass_mailing' and return |
|
333 | redirect_to :action => 'mass_mailing' and return |
|
334 | end |
|
334 | end |
|
335 |
|
335 | ||
|
336 | mail_body = params[:email_body] |
|
336 | mail_body = params[:email_body] |
|
337 | if !mail_body or mail_body.blank? |
|
337 | if !mail_body or mail_body.blank? |
|
338 | flash[:notice] = 'You entered an empty mail body.' |
|
338 | flash[:notice] = 'You entered an empty mail body.' |
|
339 | redirect_to :action => 'mass_mailing' and return |
|
339 | redirect_to :action => 'mass_mailing' and return |
|
340 | end |
|
340 | end |
|
341 |
|
341 | ||
|
342 | note = [] |
|
342 | note = [] |
|
343 | users = [] |
|
343 | users = [] |
|
344 | lines.split("\n").each do |line| |
|
344 | lines.split("\n").each do |line| |
|
345 | user = User.find_by_login(line.chomp) |
|
345 | user = User.find_by_login(line.chomp) |
|
346 | if user |
|
346 | if user |
|
347 | send_mail(user.email, mail_subject, mail_body) |
|
347 | send_mail(user.email, mail_subject, mail_body) |
|
348 | note << user.login |
|
348 | note << user.login |
|
349 | end |
|
349 | end |
|
350 | end |
|
350 | end |
|
351 |
|
351 | ||
|
352 | flash[:notice] = 'User(s) ' + note.join(', ') + |
|
352 | flash[:notice] = 'User(s) ' + note.join(', ') + |
|
353 | ' were successfully modified. ' |
|
353 | ' were successfully modified. ' |
|
354 | redirect_to :action => 'mass_mailing' |
|
354 | redirect_to :action => 'mass_mailing' |
|
355 | end |
|
355 | end |
|
356 |
|
356 | ||
|
357 | #bulk manage |
|
357 | #bulk manage |
|
358 | def bulk_manage |
|
358 | def bulk_manage |
|
359 |
|
359 | ||
|
360 | begin |
|
360 | begin |
|
361 | if params[:filter_group] |
|
361 | if params[:filter_group] |
|
362 | @users = Group.find_by(id: params[:filter_group_id]).users |
|
362 | @users = Group.find_by(id: params[:filter_group_id]).users |
|
363 | else |
|
363 | else |
|
364 | @users = User.all |
|
364 | @users = User.all |
|
365 | end |
|
365 | end |
|
366 | @users = @users.where('(login REGEXP ?) OR (remark REGEXP ?)',params[:regex],params[:regex]) unless params[:regex].blank? |
|
366 | @users = @users.where('(login REGEXP ?) OR (remark REGEXP ?)',params[:regex],params[:regex]) unless params[:regex].blank? |
|
367 | @users.count if @users #test the sql |
|
367 | @users.count if @users #test the sql |
|
368 | rescue Exception |
|
368 | rescue Exception |
|
369 | flash[:error] = 'Regular Expression is malformed' |
|
369 | flash[:error] = 'Regular Expression is malformed' |
|
370 | @users = nil |
|
370 | @users = nil |
|
371 | end |
|
371 | end |
|
372 |
|
372 | ||
|
373 | if params[:commit] |
|
373 | if params[:commit] |
|
374 | @action = {} |
|
374 | @action = {} |
|
375 | @action[:set_enable] = params[:enabled] |
|
375 | @action[:set_enable] = params[:enabled] |
|
376 | @action[:enabled] = params[:enable] == "1" |
|
376 | @action[:enabled] = params[:enable] == "1" |
|
377 | @action[:gen_password] = params[:gen_password] |
|
377 | @action[:gen_password] = params[:gen_password] |
|
378 | @action[:add_group] = params[:add_group] |
|
378 | @action[:add_group] = params[:add_group] |
|
379 | @action[:group_name] = params[:group_name] |
|
379 | @action[:group_name] = params[:group_name] |
|
380 | end |
|
380 | end |
|
381 |
|
381 | ||
|
382 | if params[:commit] == "Perform" |
|
382 | if params[:commit] == "Perform" |
|
383 | if @action[:set_enable] |
|
383 | if @action[:set_enable] |
|
384 | @users.update_all(enabled: @action[:enabled]) |
|
384 | @users.update_all(enabled: @action[:enabled]) |
|
385 | end |
|
385 | end |
|
386 | if @action[:gen_password] |
|
386 | if @action[:gen_password] |
|
387 | @users.each do |u| |
|
387 | @users.each do |u| |
|
388 | password = random_password |
|
388 | password = random_password |
|
389 | u.password = password |
|
389 | u.password = password |
|
390 | u.password_confirmation = password |
|
390 | u.password_confirmation = password |
|
391 | u.save |
|
391 | u.save |
|
392 | end |
|
392 | end |
|
393 | end |
|
393 | end |
|
394 | if @action[:add_group] and @action[:group_name] |
|
394 | if @action[:add_group] and @action[:group_name] |
|
395 | @group = Group.find(@action[:group_name]) |
|
395 | @group = Group.find(@action[:group_name]) |
|
396 | ok = [] |
|
396 | ok = [] |
|
397 | failed = [] |
|
397 | failed = [] |
|
398 | @users.each do |user| |
|
398 | @users.each do |user| |
|
399 | begin |
|
399 | begin |
|
400 | @group.users << user |
|
400 | @group.users << user |
|
401 | ok << user.login |
|
401 | ok << user.login |
|
402 | rescue => e |
|
402 | rescue => e |
|
403 | failed << user.login |
|
403 | failed << user.login |
|
404 | end |
|
404 | end |
|
405 | end |
|
405 | end |
|
406 | flash[:success] = "The following users are added to the 'group #{@group.name}': " + ok.join(', ') if ok.count > 0 |
|
406 | flash[:success] = "The following users are added to the 'group #{@group.name}': " + ok.join(', ') if ok.count > 0 |
|
407 | flash[:alert] = "The following users are already in the 'group #{@group.name}': " + failed.join(', ') if failed.count > 0 |
|
407 | flash[:alert] = "The following users are already in the 'group #{@group.name}': " + failed.join(', ') if failed.count > 0 |
|
408 | end |
|
408 | end |
|
409 | end |
|
409 | end |
|
410 | end |
|
410 | end |
|
411 |
|
411 | ||
|
412 | protected |
|
412 | protected |
|
413 |
|
413 | ||
|
414 | def random_password(length=5) |
|
414 | def random_password(length=5) |
|
415 | chars = 'abcdefghijkmnopqrstuvwxyz23456789' |
|
415 | chars = 'abcdefghijkmnopqrstuvwxyz23456789' |
|
416 | newpass = "" |
|
416 | newpass = "" |
|
417 | length.times { newpass << chars[rand(chars.size-1)] } |
|
417 | length.times { newpass << chars[rand(chars.size-1)] } |
|
418 | return newpass |
|
418 | return newpass |
|
419 | end |
|
419 | end |
|
420 |
|
420 | ||
|
421 | def import_from_file(f) |
|
421 | def import_from_file(f) |
|
422 | data_hash = YAML.load(f) |
|
422 | data_hash = YAML.load(f) |
|
423 | @import_log = "" |
|
423 | @import_log = "" |
|
424 |
|
424 | ||
|
425 | country_data = data_hash[:countries] |
|
425 | country_data = data_hash[:countries] |
|
426 | site_data = data_hash[:sites] |
|
426 | site_data = data_hash[:sites] |
|
427 | user_data = data_hash[:users] |
|
427 | user_data = data_hash[:users] |
|
428 |
|
428 | ||
|
429 | # import country |
|
429 | # import country |
|
430 | countries = {} |
|
430 | countries = {} |
|
431 | country_data.each_pair do |id,country| |
|
431 | country_data.each_pair do |id,country| |
|
432 | c = Country.find_by_name(country[:name]) |
|
432 | c = Country.find_by_name(country[:name]) |
|
433 | if c!=nil |
|
433 | if c!=nil |
|
434 | countries[id] = c |
|
434 | countries[id] = c |
|
435 | @import_log << "Found #{country[:name]}\n" |
|
435 | @import_log << "Found #{country[:name]}\n" |
|
436 | else |
|
436 | else |
|
437 | countries[id] = Country.new(:name => country[:name]) |
|
437 | countries[id] = Country.new(:name => country[:name]) |
|
438 | countries[id].save |
|
438 | countries[id].save |
|
439 | @import_log << "Created #{country[:name]}\n" |
|
439 | @import_log << "Created #{country[:name]}\n" |
|
440 | end |
|
440 | end |
|
441 | end |
|
441 | end |
|
442 |
|
442 | ||
|
443 | # import sites |
|
443 | # import sites |
|
444 | sites = {} |
|
444 | sites = {} |
|
445 | site_data.each_pair do |id,site| |
|
445 | site_data.each_pair do |id,site| |
|
446 | s = Site.find_by_name(site[:name]) |
|
446 | s = Site.find_by_name(site[:name]) |
|
447 | if s!=nil |
|
447 | if s!=nil |
|
448 | @import_log << "Found #{site[:name]}\n" |
|
448 | @import_log << "Found #{site[:name]}\n" |
|
449 | else |
|
449 | else |
|
450 | s = Site.new(:name => site[:name]) |
|
450 | s = Site.new(:name => site[:name]) |
|
451 | @import_log << "Created #{site[:name]}\n" |
|
451 | @import_log << "Created #{site[:name]}\n" |
|
452 | end |
|
452 | end |
|
453 | s.password = site[:password] |
|
453 | s.password = site[:password] |
|
454 | s.country = countries[site[:country_id]] |
|
454 | s.country = countries[site[:country_id]] |
|
455 | s.save |
|
455 | s.save |
|
456 | sites[id] = s |
|
456 | sites[id] = s |
You need to be logged in to leave comments.
Login now